diff --git a/plugins/woocommerce-blocks/assets/js/base/components/drawer/close-button.tsx b/plugins/woocommerce-blocks/assets/js/base/components/drawer/close-button.tsx new file mode 100644 index 00000000000..bbd61a45102 --- /dev/null +++ b/plugins/woocommerce-blocks/assets/js/base/components/drawer/close-button.tsx @@ -0,0 +1,7 @@ +const DrawerCloseButton = () => { + // The Drawer component will use a portal to render the close button inside + // this div. + return
; +}; + +export default DrawerCloseButton; diff --git a/plugins/woocommerce-blocks/assets/js/base/components/drawer/index.tsx b/plugins/woocommerce-blocks/assets/js/base/components/drawer/index.tsx index 938db7d3bae..17cd69461ec 100644 --- a/plugins/woocommerce-blocks/assets/js/base/components/drawer/index.tsx +++ b/plugins/woocommerce-blocks/assets/js/base/components/drawer/index.tsx @@ -7,7 +7,7 @@ */ import classNames from 'classnames'; import { useDebounce } from 'use-debounce'; -import type { ForwardedRef, KeyboardEvent } from 'react'; +import type { ForwardedRef, KeyboardEvent, RefObject } from 'react'; import { __ } from '@wordpress/i18n'; import { createPortal, @@ -41,6 +41,33 @@ interface DrawerProps { slideOut?: boolean; } +interface CloseButtonPortalProps { + onClick: () => void; + contentRef: RefObject< HTMLDivElement >; +} + +const CloseButtonPortal = ( { + onClick, + contentRef, +}: CloseButtonPortalProps ) => { + const closeButtonWrapper = contentRef?.current?.querySelector( + '.wc-block-components-drawer__close-wrapper' + ); + + return closeButtonWrapper + ? createPortal( + , + closeButtonWrapper + ) + : null; +}; + const UnforwardedDrawer = ( { children, @@ -137,12 +164,9 @@ const UnforwardedDrawer = ( role="document" ref={ contentRef } > - { children } @@ -152,6 +176,7 @@ const UnforwardedDrawer = ( ); }; -export const Drawer = forwardRef( UnforwardedDrawer ); +const Drawer = forwardRef( UnforwardedDrawer ); export default Drawer; +export { default as DrawerCloseButton } from './close-button'; diff --git a/plugins/woocommerce-blocks/assets/js/blocks/mini-cart/mini-cart-contents/block.tsx b/plugins/woocommerce-blocks/assets/js/blocks/mini-cart/mini-cart-contents/block.tsx index bdef74e85d8..a38b05f0054 100644 --- a/plugins/woocommerce-blocks/assets/js/blocks/mini-cart/mini-cart-contents/block.tsx +++ b/plugins/woocommerce-blocks/assets/js/blocks/mini-cart/mini-cart-contents/block.tsx @@ -1,6 +1,7 @@ /** * External dependencies */ +import { DrawerCloseButton } from '@woocommerce/base-components/drawer'; /** * Internal dependencies @@ -17,5 +18,10 @@ export const MiniCartContentsBlock = ( ): JSX.Element => { const { children } = props; - return <>{ children }>; + return ( + <> +