2021-08-25 15:42:55 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2023-06-19 09:42:37 +00:00
|
|
|
import {
|
|
|
|
InspectorControls,
|
|
|
|
useBlockProps,
|
|
|
|
withColors,
|
|
|
|
__experimentalColorGradientSettingsDropdown as ColorGradientSettingsDropdown,
|
|
|
|
__experimentalUseMultipleOriginColorsAndGradients as useMultipleOriginColorsAndGradients,
|
|
|
|
} from '@wordpress/block-editor';
|
2021-10-29 02:35:17 +00:00
|
|
|
import { formatPrice } from '@woocommerce/price-format';
|
2022-08-02 14:28:52 +00:00
|
|
|
import {
|
|
|
|
PanelBody,
|
|
|
|
ExternalLink,
|
|
|
|
ToggleControl,
|
2023-04-19 15:45:48 +00:00
|
|
|
BaseControl,
|
2023-03-02 14:09:37 +00:00
|
|
|
__experimentalToggleGroupControlOption as ToggleGroupControlOption,
|
|
|
|
__experimentalToggleGroupControl as ToggleGroupControl,
|
2022-08-02 14:28:52 +00:00
|
|
|
} from '@wordpress/components';
|
2021-12-14 00:54:23 +00:00
|
|
|
import { getSetting } from '@woocommerce/settings';
|
2023-06-08 18:29:34 +00:00
|
|
|
import { __, isRTL } from '@wordpress/i18n';
|
2021-12-07 14:29:56 +00:00
|
|
|
import Noninteractive from '@woocommerce/base-components/noninteractive';
|
2023-05-18 13:53:01 +00:00
|
|
|
import { isSiteEditorPage } from '@woocommerce/utils';
|
2023-03-02 14:26:00 +00:00
|
|
|
import type { ReactElement } from 'react';
|
2023-05-18 13:53:01 +00:00
|
|
|
import { select } from '@wordpress/data';
|
2023-06-19 09:42:37 +00:00
|
|
|
import classNames from 'classnames';
|
2023-06-12 20:23:52 +00:00
|
|
|
import { cartOutline, bag, bagAlt } from '@woocommerce/icons';
|
|
|
|
import { Icon } from '@wordpress/icons';
|
2023-06-20 09:52:53 +00:00
|
|
|
import { WC_BLOCKS_IMAGE_URL } from '@woocommerce/block-settings';
|
2021-08-25 15:42:55 +00:00
|
|
|
|
2021-10-29 02:35:17 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import QuantityBadge from './quantity-badge';
|
2023-05-11 12:52:03 +00:00
|
|
|
import './editor.scss';
|
2021-10-29 02:35:17 +00:00
|
|
|
|
2022-03-26 00:32:23 +00:00
|
|
|
interface Attributes {
|
2023-06-12 20:23:52 +00:00
|
|
|
miniCartIcon: 'cart' | 'bag' | 'bag-alt';
|
2022-03-26 00:32:23 +00:00
|
|
|
addToCartBehaviour: string;
|
2022-08-02 14:28:52 +00:00
|
|
|
hasHiddenPrice: boolean;
|
2023-03-14 14:59:46 +00:00
|
|
|
cartAndCheckoutRenderStyle: boolean;
|
2023-06-19 09:42:37 +00:00
|
|
|
priceColor: string;
|
|
|
|
iconColor: string;
|
|
|
|
productCountColor: string;
|
|
|
|
priceColorValue: string;
|
|
|
|
iconColorValue: string;
|
|
|
|
productCountColorValue: string;
|
2022-03-26 00:32:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface Props {
|
|
|
|
attributes: Attributes;
|
|
|
|
setAttributes: ( attributes: Record< string, unknown > ) => void;
|
2023-06-19 09:42:37 +00:00
|
|
|
clientId: number;
|
|
|
|
setPriceColor: ( colorValue: string | undefined ) => void;
|
|
|
|
setIconColor: ( colorValue: string | undefined ) => void;
|
|
|
|
setProductCountColor: ( colorValue: string | undefined ) => void;
|
2022-03-26 00:32:23 +00:00
|
|
|
}
|
|
|
|
|
2023-06-19 09:42:37 +00:00
|
|
|
const Edit = ( {
|
|
|
|
attributes,
|
|
|
|
setAttributes,
|
|
|
|
clientId,
|
|
|
|
setPriceColor,
|
|
|
|
setIconColor,
|
|
|
|
setProductCountColor,
|
|
|
|
}: Props ): ReactElement => {
|
2023-06-12 20:23:52 +00:00
|
|
|
const {
|
2023-06-19 09:42:37 +00:00
|
|
|
cartAndCheckoutRenderStyle,
|
2023-06-12 20:23:52 +00:00
|
|
|
addToCartBehaviour,
|
|
|
|
hasHiddenPrice,
|
2023-06-19 09:42:37 +00:00
|
|
|
priceColorValue,
|
|
|
|
iconColorValue,
|
|
|
|
productCountColorValue,
|
|
|
|
miniCartIcon,
|
2023-06-12 20:23:52 +00:00
|
|
|
} = attributes;
|
2023-06-19 09:42:37 +00:00
|
|
|
|
|
|
|
const className = classNames( {
|
|
|
|
'wc-block-mini-cart': true,
|
|
|
|
'has-price-color': priceColorValue,
|
|
|
|
'has-icon-color': iconColorValue,
|
|
|
|
'has-product-count-color': productCountColorValue,
|
2021-11-17 15:39:07 +00:00
|
|
|
} );
|
|
|
|
|
2023-06-19 09:42:37 +00:00
|
|
|
const blockProps = useBlockProps( { className } );
|
|
|
|
|
2023-05-18 13:53:01 +00:00
|
|
|
const isSiteEditor = isSiteEditorPage( select( 'core/edit-site' ) );
|
2023-03-14 14:59:46 +00:00
|
|
|
|
2021-12-14 00:54:23 +00:00
|
|
|
const templatePartEditUri = getSetting(
|
|
|
|
'templatePartEditUri',
|
|
|
|
''
|
|
|
|
) as string;
|
2021-11-30 09:42:07 +00:00
|
|
|
|
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
|
|
|
|
2023-06-19 09:42:37 +00:00
|
|
|
const colorGradientSettings = useMultipleOriginColorsAndGradients();
|
|
|
|
|
|
|
|
const colorSettings = [
|
|
|
|
{
|
|
|
|
value: priceColorValue,
|
|
|
|
onChange: ( colorValue: string ) => {
|
|
|
|
setPriceColor( colorValue );
|
|
|
|
setAttributes( { priceColorValue: colorValue } );
|
|
|
|
},
|
|
|
|
label: __( 'Price', 'woo-gutenberg-products-block' ),
|
|
|
|
resetAllFilter: () => {
|
|
|
|
setPriceColor( undefined );
|
|
|
|
setAttributes( { priceColorValue: undefined } );
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: iconColorValue,
|
|
|
|
onChange: ( colorValue: string ) => {
|
|
|
|
setIconColor( colorValue );
|
|
|
|
setAttributes( { iconColorValue: colorValue } );
|
|
|
|
},
|
|
|
|
label: __( 'Icon', 'woo-gutenberg-products-block' ),
|
|
|
|
resetAllFilter: () => {
|
|
|
|
setIconColor( undefined );
|
|
|
|
setAttributes( { iconColorValue: undefined } );
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: productCountColorValue,
|
|
|
|
onChange: ( colorValue: string ) => {
|
|
|
|
setProductCountColor( colorValue );
|
|
|
|
setAttributes( { productCountColorValue: colorValue } );
|
|
|
|
},
|
|
|
|
label: __( 'Product count', 'woo-gutenberg-products-block' ),
|
|
|
|
resetAllFilter: () => {
|
|
|
|
setProductCountColor( undefined );
|
|
|
|
setAttributes( { productCountColorValue: undefined } );
|
|
|
|
},
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2021-08-25 15:42:55 +00:00
|
|
|
return (
|
|
|
|
<div { ...blockProps }>
|
2021-11-17 15:39:07 +00:00
|
|
|
<InspectorControls>
|
2022-03-26 00:32:23 +00:00
|
|
|
<PanelBody
|
2023-05-11 12:52:03 +00:00
|
|
|
title={ __( 'Settings', 'woo-gutenberg-products-block' ) }
|
2022-03-26 00:32:23 +00:00
|
|
|
>
|
2023-06-12 20:23:52 +00:00
|
|
|
<ToggleGroupControl
|
|
|
|
className="wc-block-editor-mini-cart__cart-icon-toggle"
|
|
|
|
isBlock={ true }
|
2022-08-02 14:28:52 +00:00
|
|
|
label={ __(
|
2023-06-12 20:23:52 +00:00
|
|
|
'Cart Icon',
|
2022-08-02 14:28:52 +00:00
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
2023-06-12 20:23:52 +00:00
|
|
|
value={ miniCartIcon }
|
2023-06-19 09:42:37 +00:00
|
|
|
onChange={ ( value: 'cart' | 'bag' | 'bag-alt' ) => {
|
2023-06-12 20:23:52 +00:00
|
|
|
setAttributes( {
|
|
|
|
miniCartIcon: value,
|
|
|
|
} );
|
|
|
|
} }
|
|
|
|
>
|
|
|
|
<ToggleGroupControlOption
|
|
|
|
value={ 'cart' }
|
|
|
|
label={ <Icon size={ 32 } icon={ cartOutline } /> }
|
|
|
|
/>
|
|
|
|
<ToggleGroupControlOption
|
|
|
|
value={ 'bag' }
|
|
|
|
label={ <Icon size={ 32 } icon={ bag } /> }
|
|
|
|
/>
|
|
|
|
<ToggleGroupControlOption
|
|
|
|
value={ 'bag-alt' }
|
|
|
|
label={ <Icon size={ 32 } icon={ bagAlt } /> }
|
|
|
|
/>
|
|
|
|
</ToggleGroupControl>
|
|
|
|
<BaseControl
|
|
|
|
id="wc-block-mini-cart__display-toggle"
|
|
|
|
label={ __(
|
|
|
|
'Display',
|
2022-08-02 14:28:52 +00:00
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
2023-06-12 20:23:52 +00:00
|
|
|
>
|
|
|
|
<ToggleControl
|
|
|
|
label={ __(
|
|
|
|
'Display total price',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
help={ __(
|
|
|
|
'Toggle to display the total price of products in the shopping cart. If no products have been added, the price will not display.',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
checked={ ! hasHiddenPrice }
|
|
|
|
onChange={ () =>
|
|
|
|
setAttributes( {
|
|
|
|
hasHiddenPrice: ! hasHiddenPrice,
|
|
|
|
} )
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</BaseControl>
|
2023-03-14 14:59:46 +00:00
|
|
|
{ isSiteEditor && (
|
|
|
|
<ToggleGroupControl
|
2023-05-11 12:52:03 +00:00
|
|
|
className="wc-block-editor-mini-cart__render-in-cart-and-checkout-toggle"
|
2023-03-14 14:59:46 +00:00
|
|
|
label={ __(
|
2023-05-11 12:52:03 +00:00
|
|
|
'Mini-Cart in cart and checkout pages',
|
2023-03-14 14:59:46 +00:00
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
value={ cartAndCheckoutRenderStyle }
|
2023-06-19 09:42:37 +00:00
|
|
|
onChange={ ( value: boolean ) => {
|
2023-03-14 14:59:46 +00:00
|
|
|
setAttributes( {
|
|
|
|
cartAndCheckoutRenderStyle: value,
|
|
|
|
} );
|
|
|
|
} }
|
|
|
|
help={ __(
|
2023-05-11 12:52:03 +00:00
|
|
|
'Select how the Mini-Cart behaves in the Cart and Checkout pages. This might affect the header layout.',
|
2023-03-14 14:59:46 +00:00
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
>
|
|
|
|
<ToggleGroupControlOption
|
|
|
|
value={ 'hidden' }
|
|
|
|
label={ __(
|
|
|
|
'Hide',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
/>
|
|
|
|
<ToggleGroupControlOption
|
|
|
|
value={ 'removed' }
|
|
|
|
label={ __(
|
|
|
|
'Remove',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
/>
|
|
|
|
</ToggleGroupControl>
|
|
|
|
) }
|
2022-03-26 00:32:23 +00:00
|
|
|
</PanelBody>
|
2023-05-11 12:52:03 +00:00
|
|
|
<PanelBody
|
|
|
|
title={ __(
|
|
|
|
'Cart Drawer',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
>
|
|
|
|
{ templatePartEditUri && (
|
|
|
|
<>
|
|
|
|
<img
|
|
|
|
className="wc-block-editor-mini-cart__drawer-image"
|
2023-06-08 18:29:34 +00:00
|
|
|
src={
|
|
|
|
isRTL()
|
2023-06-20 09:52:53 +00:00
|
|
|
? `${ WC_BLOCKS_IMAGE_URL }blocks/mini-cart/cart-drawer-rtl.svg`
|
|
|
|
: `${ WC_BLOCKS_IMAGE_URL }blocks/mini-cart/cart-drawer.svg`
|
2023-06-08 18:29:34 +00:00
|
|
|
}
|
2023-05-11 12:52:03 +00:00
|
|
|
alt=""
|
|
|
|
/>
|
|
|
|
<p>
|
|
|
|
{ __(
|
|
|
|
'When opened, the Mini-Cart drawer gives shoppers quick access to view their selected products and checkout.',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
</p>
|
|
|
|
<p className="wc-block-editor-mini-cart__drawer-link">
|
|
|
|
<ExternalLink href={ templatePartEditUri }>
|
|
|
|
{ __(
|
|
|
|
'Edit Mini-Cart Drawer template',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
</ExternalLink>
|
|
|
|
</p>
|
|
|
|
</>
|
|
|
|
) }
|
|
|
|
<BaseControl
|
|
|
|
id="wc-block-mini-cart__add-to-cart-behaviour-toggle"
|
|
|
|
label={ __(
|
|
|
|
'Behavior',
|
2021-12-14 00:54:23 +00:00
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
>
|
2023-05-11 12:52:03 +00:00
|
|
|
<ToggleControl
|
|
|
|
label={ __(
|
|
|
|
'Open drawer when adding',
|
2022-03-22 01:56:02 +00:00
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
2023-05-11 12:52:03 +00:00
|
|
|
onChange={ ( value ) => {
|
|
|
|
setAttributes( {
|
|
|
|
addToCartBehaviour: value
|
|
|
|
? 'open_drawer'
|
|
|
|
: 'none',
|
|
|
|
} );
|
|
|
|
} }
|
|
|
|
help={ __(
|
|
|
|
'Toggle to open the Mini-Cart drawer when a shopper adds a product to their cart.',
|
2021-11-30 09:42:07 +00:00
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
2023-05-11 12:52:03 +00:00
|
|
|
checked={ addToCartBehaviour === 'open_drawer' }
|
|
|
|
/>
|
|
|
|
</BaseControl>
|
|
|
|
</PanelBody>
|
2021-11-17 15:39:07 +00:00
|
|
|
</InspectorControls>
|
2023-06-19 09:42:37 +00:00
|
|
|
{ colorGradientSettings.hasColorsOrGradients && (
|
|
|
|
// @ts-to-do: Fix outdated InspectorControls type definitions in DefinitelyTyped and/or Gutenberg.
|
|
|
|
<InspectorControls group="color">
|
|
|
|
{ colorSettings.map(
|
|
|
|
( { onChange, label, value, resetAllFilter } ) => (
|
|
|
|
<ColorGradientSettingsDropdown
|
|
|
|
key={ `mini-cart-color-${ label }` }
|
|
|
|
__experimentalIsRenderedInSidebar
|
|
|
|
settings={ [
|
|
|
|
{
|
|
|
|
colorValue: value,
|
|
|
|
label,
|
|
|
|
onColorChange: onChange,
|
|
|
|
isShownByDefault: true,
|
|
|
|
resetAllFilter,
|
|
|
|
enableAlpha: true,
|
|
|
|
},
|
|
|
|
] }
|
|
|
|
panelId={ clientId }
|
|
|
|
{ ...colorGradientSettings }
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
) }
|
|
|
|
</InspectorControls>
|
|
|
|
) }
|
2021-12-07 14:29:56 +00:00
|
|
|
<Noninteractive>
|
2022-03-09 09:13:52 +00:00
|
|
|
<button className="wc-block-mini-cart__button">
|
2022-08-02 14:28:52 +00:00
|
|
|
{ ! hasHiddenPrice && (
|
2023-06-19 09:42:37 +00:00
|
|
|
<span
|
|
|
|
className="wc-block-mini-cart__amount"
|
|
|
|
style={ { color: priceColorValue } }
|
|
|
|
>
|
2022-08-02 14:28:52 +00:00
|
|
|
{ formatPrice( productTotal ) }
|
|
|
|
</span>
|
|
|
|
) }
|
2023-06-12 20:23:52 +00:00
|
|
|
<QuantityBadge
|
|
|
|
count={ productCount }
|
2023-06-19 09:42:37 +00:00
|
|
|
iconColor={ iconColorValue }
|
|
|
|
productCountColor={ productCountColorValue }
|
2023-06-12 20:23:52 +00:00
|
|
|
icon={ miniCartIcon }
|
|
|
|
/>
|
2021-12-07 14:29:56 +00:00
|
|
|
</button>
|
|
|
|
</Noninteractive>
|
2021-08-25 15:42:55 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2023-06-19 09:42:37 +00:00
|
|
|
const miniCartColorAttributes = {
|
|
|
|
priceColor: 'price-color',
|
|
|
|
iconColor: 'icon-color',
|
|
|
|
productCountColor: 'product-count-color',
|
|
|
|
};
|
|
|
|
|
|
|
|
// @ts-expect-error: TypeScript doesn't resolve the shared React dependency and cannot resolve the type returned by `withColors`.
|
|
|
|
// Similar issue example: https://github.com/microsoft/TypeScript/issues/47663
|
|
|
|
const EditWithColors: JSX.Element = withColors( miniCartColorAttributes )(
|
|
|
|
Edit
|
|
|
|
);
|
|
|
|
|
|
|
|
export default EditWithColors;
|