2021-08-25 15:42:55 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2021-11-17 15:39:07 +00:00
|
|
|
import {
|
2021-11-22 23:40:24 +00:00
|
|
|
AlignmentControl,
|
|
|
|
BlockControls,
|
2021-11-17 15:39:07 +00:00
|
|
|
InspectorControls,
|
|
|
|
useBlockProps,
|
|
|
|
getColorClassName,
|
|
|
|
} from '@wordpress/block-editor';
|
2021-08-25 15:42:55 +00:00
|
|
|
import type { ReactElement } from 'react';
|
2021-10-29 02:35:17 +00:00
|
|
|
import { formatPrice } from '@woocommerce/price-format';
|
2021-10-21 09:07:20 +00:00
|
|
|
import { CartCheckoutCompatibilityNotice } from '@woocommerce/editor-components/compatibility-notices';
|
2021-11-30 09:42:07 +00:00
|
|
|
import { PanelBody, ExternalLink, ToggleControl } from '@wordpress/components';
|
|
|
|
import { addQueryArgs } from '@wordpress/url';
|
|
|
|
import { ADMIN_URL, getSetting } from '@woocommerce/settings';
|
2021-11-17 15:39:07 +00:00
|
|
|
import { __ } from '@wordpress/i18n';
|
2021-11-22 23:40:24 +00:00
|
|
|
import { positionCenter, positionRight, positionLeft } from '@wordpress/icons';
|
2021-11-17 15:39:07 +00:00
|
|
|
import classnames from 'classnames';
|
2021-11-30 09:42:07 +00:00
|
|
|
import { isString } from '@woocommerce/types';
|
2021-12-07 14:29:56 +00:00
|
|
|
import Noninteractive from '@woocommerce/base-components/noninteractive';
|
2021-08-25 15:42:55 +00:00
|
|
|
|
2021-10-29 02:35:17 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import QuantityBadge from './quantity-badge';
|
|
|
|
|
2021-11-17 15:39:07 +00:00
|
|
|
interface Attributes {
|
2021-11-22 23:40:24 +00:00
|
|
|
align: string;
|
2021-11-17 15:39:07 +00:00
|
|
|
isInitiallyOpen?: boolean;
|
|
|
|
transparentButton: boolean;
|
|
|
|
backgroundColor?: string;
|
|
|
|
textColor?: string;
|
|
|
|
style?: Record< string, Record< string, string > >;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface Props {
|
|
|
|
attributes: Attributes;
|
|
|
|
setAttributes: ( attributes: Record< string, unknown > ) => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
const MiniCartBlock = ( {
|
|
|
|
attributes,
|
|
|
|
setAttributes,
|
|
|
|
}: Props ): ReactElement => {
|
2021-11-22 23:40:24 +00:00
|
|
|
const {
|
|
|
|
transparentButton,
|
|
|
|
backgroundColor,
|
|
|
|
textColor,
|
|
|
|
style,
|
|
|
|
align,
|
|
|
|
} = attributes;
|
2021-08-25 15:42:55 +00:00
|
|
|
const blockProps = useBlockProps( {
|
2021-11-22 23:40:24 +00:00
|
|
|
className: classnames( `wc-block-mini-cart align-${ align }`, {
|
2021-11-17 15:39:07 +00:00
|
|
|
'is-transparent': transparentButton,
|
|
|
|
} ),
|
|
|
|
} );
|
|
|
|
|
2021-11-30 09:42:07 +00:00
|
|
|
const themeSlug = getSetting( 'themeSlug', '' );
|
|
|
|
|
2021-12-10 10:23:16 +00:00
|
|
|
const isBlockTheme = getSetting( 'isBlockTheme', false );
|
2021-11-30 09:42:07 +00:00
|
|
|
|
2021-11-17 15:39:07 +00:00
|
|
|
/**
|
|
|
|
* @todo Replace `getColorClassName` and manual style manipulation with
|
|
|
|
* `useColorProps` once the hook is no longer experimental.
|
|
|
|
*/
|
|
|
|
const backgroundClass = getColorClassName(
|
|
|
|
'background-color',
|
|
|
|
backgroundColor
|
|
|
|
);
|
|
|
|
const textColorClass = getColorClassName( 'color', textColor );
|
|
|
|
|
|
|
|
const colorStyle = {
|
|
|
|
backgroundColor: style?.color?.background,
|
|
|
|
color: style?.color?.text,
|
|
|
|
};
|
|
|
|
|
|
|
|
const colorClassNames = classnames( backgroundClass, textColorClass, {
|
|
|
|
'has-background': backgroundClass || style?.color?.background,
|
|
|
|
'has-text-color': textColorClass || style?.color?.text,
|
2021-08-25 15:42:55 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
const productCount = 0;
|
2021-10-29 02:35:17 +00:00
|
|
|
const productTotal = 0;
|
2021-08-25 15:42:55 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div { ...blockProps }>
|
2021-11-22 23:40:24 +00:00
|
|
|
<BlockControls>
|
|
|
|
<AlignmentControl
|
|
|
|
value={ align }
|
|
|
|
alignmentControls={ [
|
|
|
|
{
|
|
|
|
icon: positionLeft,
|
|
|
|
title: __(
|
|
|
|
'Align button left',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
),
|
|
|
|
align: 'left',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
icon: positionCenter,
|
|
|
|
title: __(
|
|
|
|
'Align button center',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
),
|
|
|
|
align: 'center',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
icon: positionRight,
|
|
|
|
title: __(
|
|
|
|
'Align button right',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
),
|
|
|
|
align: 'right',
|
|
|
|
},
|
|
|
|
] }
|
|
|
|
onChange={ ( newAlign: string ) =>
|
|
|
|
setAttributes( { align: newAlign } )
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</BlockControls>
|
2021-11-17 15:39:07 +00:00
|
|
|
<InspectorControls>
|
|
|
|
<PanelBody
|
|
|
|
title={ __(
|
|
|
|
'Button style',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
>
|
|
|
|
<ToggleControl
|
|
|
|
label={ __(
|
|
|
|
'Use transparent button',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
checked={ transparentButton }
|
|
|
|
onChange={ () =>
|
|
|
|
setAttributes( {
|
|
|
|
transparentButton: ! transparentButton,
|
|
|
|
} )
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</PanelBody>
|
2021-12-10 10:23:16 +00:00
|
|
|
{ isBlockTheme &&
|
2021-11-30 09:42:07 +00:00
|
|
|
isString( themeSlug ) &&
|
|
|
|
themeSlug.length > 0 && (
|
|
|
|
<PanelBody
|
|
|
|
title={ __(
|
|
|
|
'Template Editor',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
>
|
|
|
|
<ExternalLink
|
|
|
|
href={ addQueryArgs(
|
2021-12-10 10:23:16 +00:00
|
|
|
`${ ADMIN_URL }site-editor.php`,
|
2021-11-30 09:42:07 +00:00
|
|
|
{
|
|
|
|
postId: `${ themeSlug }//mini-cart`,
|
|
|
|
postType: 'wp_template_part',
|
|
|
|
}
|
|
|
|
) }
|
|
|
|
>
|
|
|
|
{ __(
|
|
|
|
'Edit template part',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
</ExternalLink>
|
|
|
|
</PanelBody>
|
|
|
|
) }
|
2021-11-17 15:39:07 +00:00
|
|
|
</InspectorControls>
|
2021-12-07 14:29:56 +00:00
|
|
|
<Noninteractive>
|
|
|
|
<button
|
|
|
|
className={ classnames(
|
|
|
|
'wc-block-mini-cart__button',
|
|
|
|
colorClassNames
|
|
|
|
) }
|
2021-11-17 15:39:07 +00:00
|
|
|
style={ colorStyle }
|
2021-12-07 14:29:56 +00:00
|
|
|
>
|
|
|
|
<span className="wc-block-mini-cart__amount">
|
|
|
|
{ formatPrice( productTotal ) }
|
|
|
|
</span>
|
|
|
|
<QuantityBadge
|
|
|
|
count={ productCount }
|
|
|
|
colorClassNames={ colorClassNames }
|
|
|
|
style={ colorStyle }
|
|
|
|
/>
|
|
|
|
</button>
|
|
|
|
</Noninteractive>
|
2021-10-21 09:07:20 +00:00
|
|
|
<CartCheckoutCompatibilityNotice blockName="mini-cart" />
|
2021-08-25 15:42:55 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default MiniCartBlock;
|