File size: 2,954 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
import { Icon, close as closeIcon } from '@wordpress/icons';
import clsx from 'clsx';
import { useCallback } from 'react';
import Modal from 'react-modal';
import ButtonBar from './button-bar';
import type { BaseButton } from './button-bar';
import type { PropsWithChildren } from 'react';
import './style.scss';
type Props = {
additionalClassNames?: Parameters< typeof clsx >[ 0 ];
additionalOverlayClassNames?: Parameters< typeof clsx >[ 0 ];
baseClassName?: string;
buttons?: ( React.ReactElement | BaseButton )[];
className?: string;
isBackdropVisible?: boolean;
isFullScreen?: boolean;
isVisible: boolean;
label?: string;
leaveTimeout?: number;
onClose?: ( action?: string ) => void;
shouldCloseOnEsc?: boolean;
showCloseIcon?: boolean;
shouldCloseOnOverlayClick?: boolean;
labelledby?: string;
describedby?: string;
bodyOpenClassName?: string;
};
const Dialog = ( {
additionalClassNames,
additionalOverlayClassNames,
buttons,
baseClassName = 'dialog',
className,
children,
isBackdropVisible = true,
isFullScreen = true,
isVisible = false,
label = '',
leaveTimeout = 200,
onClose,
shouldCloseOnEsc,
showCloseIcon = false,
shouldCloseOnOverlayClick = true,
labelledby,
describedby,
bodyOpenClassName,
}: PropsWithChildren< Props > ) => {
const close = useCallback( () => onClose?.(), [ onClose ] );
const onButtonClick = useCallback(
( button: BaseButton ) => {
if ( button.onClick ) {
button.onClick( () => onClose?.( button.action ) );
} else {
onClose?.( button.action );
}
},
[ onClose ]
);
// Previous implementation used a `<Card />`, styling still relies on the 'card' class being present
const dialogClassName = clsx( baseClassName, 'card', additionalClassNames );
const backdropClassName = clsx( baseClassName + '__backdrop', additionalOverlayClassNames, {
'is-full-screen': isFullScreen,
'is-hidden': ! isBackdropVisible,
} );
const contentClassName = clsx( baseClassName + '__content', className );
return (
<Modal
aria={ { labelledby, describedby } }
isOpen={ isVisible }
onRequestClose={ close }
closeTimeoutMS={ leaveTimeout }
contentLabel={ label }
overlayClassName={ backdropClassName } // We use flex here which react-modal doesn't
className={ dialogClassName }
htmlOpenClassName="ReactModal__Html--open"
role="dialog"
shouldCloseOnEsc={ shouldCloseOnEsc }
shouldCloseOnOverlayClick={ shouldCloseOnOverlayClick }
bodyOpenClassName={ bodyOpenClassName }
>
{ showCloseIcon && (
<button
aria-label="Close"
className="dialog__action-buttons-close"
onClick={ () => onClose?.( this ) }
>
<Icon icon={ closeIcon } size={ 24 } />
</button>
) }
<div className={ contentClassName } tabIndex={ -1 }>
{ children }
</div>
<ButtonBar
buttons={ buttons }
onButtonClick={ onButtonClick }
baseClassName={ baseClassName }
/>
</Modal>
);
};
export default Dialog;
|