2021-08-25 15:42:55 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2021-11-17 15:39:07 +00:00
|
|
|
import {
|
|
|
|
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-17 15:39:07 +00:00
|
|
|
import { PanelBody, ToggleControl } from '@wordpress/components';
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import classnames from 'classnames';
|
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 {
|
|
|
|
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 => {
|
|
|
|
const { transparentButton, backgroundColor, textColor, style } = attributes;
|
2021-08-25 15:42:55 +00:00
|
|
|
const blockProps = useBlockProps( {
|
2021-11-17 15:39:07 +00:00
|
|
|
className: classnames( 'wc-block-mini-cart', {
|
|
|
|
'is-transparent': transparentButton,
|
|
|
|
} ),
|
|
|
|
} );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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-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>
|
|
|
|
</InspectorControls>
|
|
|
|
<button
|
|
|
|
className={ classnames(
|
|
|
|
'wc-block-mini-cart__button',
|
|
|
|
colorClassNames
|
|
|
|
) }
|
|
|
|
style={ colorStyle }
|
|
|
|
>
|
2021-10-29 02:35:17 +00:00
|
|
|
<span className="wc-block-mini-cart__amount">
|
|
|
|
{ formatPrice( productTotal ) }
|
|
|
|
</span>
|
2021-11-17 15:39:07 +00:00
|
|
|
<QuantityBadge
|
|
|
|
count={ productCount }
|
|
|
|
colorClassNames={ colorClassNames }
|
|
|
|
style={ colorStyle }
|
|
|
|
/>
|
2021-08-25 15:42:55 +00:00
|
|
|
</button>
|
2021-10-21 09:07:20 +00:00
|
|
|
<CartCheckoutCompatibilityNotice blockName="mini-cart" />
|
2021-08-25 15:42:55 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default MiniCartBlock;
|