2021-08-25 15:42:55 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2021-09-15 11:40:36 +00:00
|
|
|
import { renderFrontend } from '@woocommerce/base-utils';
|
2021-08-25 15:42:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2021-09-15 11:40:36 +00:00
|
|
|
import MiniCartBlock from './block';
|
2021-09-02 09:44:25 +00:00
|
|
|
import './style.scss';
|
2021-08-25 15:42:55 +00:00
|
|
|
|
2021-09-02 09:44:25 +00:00
|
|
|
const renderMiniCartFrontend = () => {
|
|
|
|
// Check if button is focused. In that case, we want to refocus it after we
|
|
|
|
// replace it with the React equivalent.
|
|
|
|
let focusedMiniCartBlock: HTMLElement | null = null;
|
|
|
|
/* eslint-disable @wordpress/no-global-active-element */
|
|
|
|
if (
|
|
|
|
document.activeElement &&
|
|
|
|
document.activeElement.classList.contains(
|
|
|
|
'wc-block-mini-cart__button'
|
|
|
|
) &&
|
|
|
|
document.activeElement.parentNode instanceof HTMLElement
|
|
|
|
) {
|
|
|
|
focusedMiniCartBlock = document.activeElement.parentNode;
|
|
|
|
}
|
|
|
|
/* eslint-enable @wordpress/no-global-active-element */
|
|
|
|
|
|
|
|
renderFrontend( {
|
|
|
|
selector: '.wc-block-mini-cart',
|
2021-11-04 11:05:58 +00:00
|
|
|
Block: MiniCartBlock,
|
2021-12-07 10:07:32 +00:00
|
|
|
getProps: ( el ) => {
|
2021-11-17 15:39:07 +00:00
|
|
|
let colorClassNames = '';
|
|
|
|
const button = el.querySelector( '.wc-block-mini-cart__button' );
|
2023-06-30 14:05:27 +00:00
|
|
|
|
|
|
|
if ( button instanceof HTMLButtonElement ) {
|
2021-11-17 15:39:07 +00:00
|
|
|
colorClassNames = button.classList
|
|
|
|
.toString()
|
|
|
|
.replace( 'wc-block-mini-cart__button', '' );
|
|
|
|
}
|
|
|
|
return {
|
2023-06-30 14:05:27 +00:00
|
|
|
initialCartTotals: el.dataset.cartTotals
|
|
|
|
? JSON.parse( el.dataset.cartTotals )
|
|
|
|
: null,
|
|
|
|
initialCartItemsCount: el.dataset.cartItemsCount
|
|
|
|
? parseInt( el.dataset.cartItemsCount, 10 )
|
|
|
|
: 0,
|
2021-11-17 15:39:07 +00:00
|
|
|
isInitiallyOpen: el.dataset.isInitiallyOpen === 'true',
|
|
|
|
colorClassNames,
|
|
|
|
style: el.dataset.style ? JSON.parse( el.dataset.style ) : {},
|
2023-06-12 20:23:52 +00:00
|
|
|
miniCartIcon: el.dataset.miniCartIcon,
|
2022-03-26 00:32:23 +00:00
|
|
|
addToCartBehaviour: el.dataset.addToCartBehaviour || 'none',
|
2024-01-12 12:39:34 +00:00
|
|
|
hasHiddenPrice: el.dataset.hasHiddenPrice !== 'false',
|
2023-07-21 09:35:32 +00:00
|
|
|
priceColor: el.dataset.priceColor
|
|
|
|
? JSON.parse( el.dataset.priceColor )
|
|
|
|
: {},
|
|
|
|
iconColor: el.dataset.iconColor
|
|
|
|
? JSON.parse( el.dataset.iconColor )
|
|
|
|
: {},
|
|
|
|
productCountColor: el.dataset.productCountColor
|
|
|
|
? JSON.parse( el.dataset.productCountColor )
|
|
|
|
: {},
|
2021-12-07 10:07:32 +00:00
|
|
|
contents:
|
|
|
|
el.querySelector( '.wc-block-mini-cart__template-part' )
|
|
|
|
?.innerHTML ?? '',
|
2021-11-17 15:39:07 +00:00
|
|
|
};
|
|
|
|
},
|
2021-09-02 09:44:25 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
// Refocus previously focused button if drawer is not open.
|
|
|
|
if (
|
|
|
|
focusedMiniCartBlock instanceof HTMLElement &&
|
2021-10-25 16:05:01 +00:00
|
|
|
! focusedMiniCartBlock.dataset.isInitiallyOpen
|
2021-09-02 09:44:25 +00:00
|
|
|
) {
|
|
|
|
const innerButton = focusedMiniCartBlock.querySelector(
|
|
|
|
'.wc-block-mini-cart__button'
|
|
|
|
);
|
|
|
|
if ( innerButton instanceof HTMLElement ) {
|
|
|
|
innerButton.focus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
renderMiniCartFrontend();
|