2023-05-16 08:41:26 +00:00
|
|
|
/**
|
|
|
|
* Some code of the Drawer component is based on the Modal component from Gutenberg:
|
|
|
|
* https://github.com/WordPress/gutenberg/blob/trunk/packages/components/src/modal/index.tsx
|
|
|
|
*/
|
2021-09-02 09:44:25 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import classNames from 'classnames';
|
2023-05-16 08:41:26 +00:00
|
|
|
import { useDebounce } from 'use-debounce';
|
2023-05-30 13:39:19 +00:00
|
|
|
import type { ForwardedRef, KeyboardEvent, RefObject } from 'react';
|
2023-05-16 08:41:26 +00:00
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import {
|
|
|
|
createPortal,
|
|
|
|
useEffect,
|
|
|
|
useRef,
|
|
|
|
forwardRef,
|
|
|
|
} from '@wordpress/element';
|
2024-05-03 03:28:39 +00:00
|
|
|
import { close, Icon } from '@wordpress/icons';
|
2023-05-16 08:41:26 +00:00
|
|
|
import {
|
|
|
|
useFocusOnMount,
|
|
|
|
useConstrainedTabbing,
|
|
|
|
useMergeRefs,
|
|
|
|
} from '@wordpress/compose';
|
2024-05-16 16:32:22 +00:00
|
|
|
import { useFocusReturn } from '@woocommerce/base-utils';
|
2021-09-02 09:44:25 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2023-05-16 08:41:26 +00:00
|
|
|
import Button from '../button';
|
|
|
|
import * as ariaHelper from './utils/aria-helper';
|
2021-09-02 09:44:25 +00:00
|
|
|
import './style.scss';
|
|
|
|
|
|
|
|
interface DrawerProps {
|
|
|
|
children: JSX.Element;
|
2021-09-07 08:27:16 +00:00
|
|
|
className?: string;
|
2021-09-02 09:44:25 +00:00
|
|
|
isOpen: boolean;
|
|
|
|
onClose: () => void;
|
|
|
|
slideIn?: boolean;
|
|
|
|
slideOut?: boolean;
|
|
|
|
}
|
|
|
|
|
2023-05-30 13:39:19 +00:00
|
|
|
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(
|
|
|
|
<Button
|
|
|
|
className="wc-block-components-drawer__close"
|
|
|
|
onClick={ onClick }
|
2024-05-03 03:28:39 +00:00
|
|
|
removeTextWrap
|
|
|
|
aria-label={ __( 'Close', 'woocommerce' ) }
|
|
|
|
>
|
|
|
|
<Icon icon={ close } />
|
|
|
|
</Button>,
|
2023-05-30 13:39:19 +00:00
|
|
|
closeButtonWrapper
|
|
|
|
)
|
|
|
|
: null;
|
|
|
|
};
|
|
|
|
|
2023-05-16 08:41:26 +00:00
|
|
|
const UnforwardedDrawer = (
|
|
|
|
{
|
|
|
|
children,
|
|
|
|
className,
|
|
|
|
isOpen,
|
|
|
|
onClose,
|
|
|
|
slideIn = true,
|
|
|
|
slideOut = true,
|
|
|
|
}: DrawerProps,
|
|
|
|
forwardedRef: ForwardedRef< HTMLDivElement >
|
|
|
|
): JSX.Element | null => {
|
2021-09-02 09:44:25 +00:00
|
|
|
const [ debouncedIsOpen ] = useDebounce< boolean >( isOpen, 300 );
|
|
|
|
const isClosing = ! isOpen && debouncedIsOpen;
|
2023-05-16 08:41:26 +00:00
|
|
|
const bodyOpenClassName = 'drawer-open';
|
|
|
|
|
|
|
|
const onRequestClose = () => {
|
|
|
|
document.body.classList.remove( bodyOpenClassName );
|
|
|
|
ariaHelper.showApp();
|
|
|
|
onClose();
|
|
|
|
};
|
|
|
|
|
|
|
|
const ref = useRef< HTMLDivElement >();
|
|
|
|
const focusOnMountRef = useFocusOnMount();
|
|
|
|
const constrainedTabbingRef = useConstrainedTabbing();
|
|
|
|
const focusReturnRef = useFocusReturn();
|
|
|
|
const contentRef = useRef< HTMLDivElement >( null );
|
|
|
|
|
|
|
|
useEffect( () => {
|
|
|
|
if ( isOpen ) {
|
|
|
|
ariaHelper.hideApp( ref.current );
|
|
|
|
document.body.classList.add( bodyOpenClassName );
|
|
|
|
}
|
|
|
|
}, [ isOpen, bodyOpenClassName ] );
|
|
|
|
|
|
|
|
const overlayRef = useMergeRefs( [ ref, forwardedRef ] );
|
|
|
|
const drawerRef = useMergeRefs( [
|
|
|
|
constrainedTabbingRef,
|
|
|
|
focusReturnRef,
|
|
|
|
focusOnMountRef,
|
|
|
|
] );
|
2021-09-02 09:44:25 +00:00
|
|
|
|
|
|
|
if ( ! isOpen && ! isClosing ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-05-16 08:41:26 +00:00
|
|
|
function handleEscapeKeyDown( event: KeyboardEvent< HTMLDivElement > ) {
|
|
|
|
if (
|
|
|
|
// Ignore keydowns from IMEs
|
|
|
|
event.nativeEvent.isComposing ||
|
|
|
|
// Workaround for Mac Safari where the final Enter/Backspace of an IME composition
|
|
|
|
// is `isComposing=false`, even though it's technically still part of the composition.
|
|
|
|
// These can only be detected by keyCode.
|
|
|
|
event.keyCode === 229
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( event.code === 'Escape' && ! event.defaultPrevented ) {
|
|
|
|
event.preventDefault();
|
|
|
|
onRequestClose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return createPortal(
|
|
|
|
// eslint-disable-next-line jsx-a11y/no-static-element-interactions
|
|
|
|
<div
|
|
|
|
ref={ overlayRef }
|
|
|
|
className={ classNames(
|
2021-09-02 09:44:25 +00:00
|
|
|
'wc-block-components-drawer__screen-overlay',
|
|
|
|
{
|
2022-06-15 09:56:52 +00:00
|
|
|
'wc-block-components-drawer__screen-overlay--is-hidden':
|
|
|
|
! isOpen,
|
|
|
|
'wc-block-components-drawer__screen-overlay--with-slide-in':
|
|
|
|
slideIn,
|
|
|
|
'wc-block-components-drawer__screen-overlay--with-slide-out':
|
|
|
|
slideOut,
|
2021-09-02 09:44:25 +00:00
|
|
|
}
|
|
|
|
) }
|
2023-05-16 08:41:26 +00:00
|
|
|
onKeyDown={ handleEscapeKeyDown }
|
2023-06-28 12:43:03 +00:00
|
|
|
onClick={ ( e ) => {
|
|
|
|
// If click was done directly in the overlay element and not one
|
|
|
|
// of its descendants, close the drawer.
|
|
|
|
if ( e.target === ref.current ) {
|
|
|
|
onRequestClose();
|
|
|
|
}
|
|
|
|
} }
|
2021-09-02 09:44:25 +00:00
|
|
|
>
|
2023-05-16 08:41:26 +00:00
|
|
|
<div
|
|
|
|
className={ classNames(
|
|
|
|
className,
|
|
|
|
'wc-block-components-drawer'
|
|
|
|
) }
|
|
|
|
ref={ drawerRef }
|
|
|
|
role="dialog"
|
|
|
|
tabIndex={ -1 }
|
|
|
|
>
|
|
|
|
<div
|
|
|
|
className="wc-block-components-drawer__content"
|
|
|
|
role="document"
|
|
|
|
ref={ contentRef }
|
|
|
|
>
|
2023-05-30 13:39:19 +00:00
|
|
|
<CloseButtonPortal
|
|
|
|
contentRef={ contentRef }
|
2023-05-16 08:41:26 +00:00
|
|
|
onClick={ onRequestClose }
|
|
|
|
/>
|
|
|
|
{ children }
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>,
|
|
|
|
document.body
|
2021-09-02 09:44:25 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2023-05-30 13:39:19 +00:00
|
|
|
const Drawer = forwardRef( UnforwardedDrawer );
|
2023-05-16 08:41:26 +00:00
|
|
|
|
2021-09-02 09:44:25 +00:00
|
|
|
export default Drawer;
|
2023-05-30 13:39:19 +00:00
|
|
|
export { default as DrawerCloseButton } from './close-button';
|