Modals give users information and choices related to a task they’re trying to accomplish. They can contain critical information, require decisions, or involve multiple tasks.
import { Modal } from '@wordpress/components';
Links
Props
| Name | Default | Description |
|---|---|---|
aria |
| |
bodyOpenClassName |
Class name added to the body element when the modal is open. | |
children Required |
The children elements. | |
className |
If this property is added, it will an additional class name to the modal
content | |
closeButtonLabel |
Label on the close button. | |
contentLabel |
If this property is added, it will be added to the modal content Titles are required for accessibility reasons, see | |
focusOnMount |
Determines focus behavior when the modal opens.
| |
headerActions |
Elements that are injected into the modal header to the left of the close button (if rendered).
Hidden if | |
icon |
If this property is added, an icon will be added before the title. | |
isDismissible |
If this property is set to false, the modal will not display a close icon and cannot be dismissed. | |
isFullScreen |
This property when set to | |
size |
If this property is added it will cause the modal to render at a preset
width, or expand to fill the screen. This prop will be ignored if
Note: | |
onKeyDown |
Handle the key down on the modal frame | |
onRequestClose Required |
This function is called to indicate that the modal should be closed. | |
overlayClassName |
If this property is added, it will an additional class name to the modal
overlay | |
role |
If this property is added, it will override the default role of the modal. | |
shouldCloseOnClickOutside |
If this property is added, it will determine whether the modal requests to close when a mouse click occurs outside of the modal content. | |
shouldCloseOnEsc |
If this property is added, it will determine whether the modal requests to close when the escape key is pressed. | |
style |
If this property is added, it will be added to the modal frame | |
title |
This property is used as the modal header’s title. Titles are required for accessibility reasons, see | |
__experimentalHideHeader |
When set to Warning: This property is still experimental. “Experimental” means this is an early implementation subject to drastic and breaking changes. |
Examples
Default
const Default = ( { onRequestClose, ...args } ) => {
const [ isOpen, setOpen ] = useState( false );
const openModal = () => setOpen( true );
const closeModal: ModalProps[ 'onRequestClose' ] = ( event ) => {
setOpen( false );
onRequestClose( event );
};
return (
<>
<Button
__next40pxDefaultSize
variant="secondary"
onClick={ openModal }
>
Open Modal
</Button>
{ isOpen && (
<Modal onRequestClose={ closeModal } { ...args }>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et magna
aliqua. Ut enim ad minim veniam, quis nostrud
exercitation ullamco laboris nisi ut aliquip ex ea ea
commodo consequat. Duis aute irure dolor in
reprehenderit in voluptate velit esse cillum dolore eu
fugiat nulla pariatur. Excepteur sint occaecat cupidatat
non proident, sunt in culpa qui officia deserunt mollit
anim id est laborum.
</p>
<InputControl
__next40pxDefaultSize
style={ { marginBottom: '20px' } }
/>
<Button
__next40pxDefaultSize
variant="secondary"
onClick={ closeModal }
>
Close Modal
</Button>
</Modal>
) }
</>
);
};
With size: small
const WithsizeSmall = ( { onRequestClose, ...args } ) => {
const [ isOpen, setOpen ] = useState( false );
const openModal = () => setOpen( true );
const closeModal: ModalProps[ 'onRequestClose' ] = ( event ) => {
setOpen( false );
onRequestClose( event );
};
return (
<>
<Button
__next40pxDefaultSize
variant="secondary"
onClick={ openModal }
>
Open Modal
</Button>
{ isOpen && (
<Modal onRequestClose={ closeModal } { ...args }>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et magna
aliqua. Ut enim ad minim veniam, quis nostrud
exercitation ullamco laboris nisi ut aliquip ex ea ea
commodo consequat. Duis aute irure dolor in
reprehenderit in voluptate velit esse cillum dolore eu
fugiat nulla pariatur. Excepteur sint occaecat cupidatat
non proident, sunt in culpa qui officia deserunt mollit
anim id est laborum.
</p>
<InputControl
__next40pxDefaultSize
style={ { marginBottom: '20px' } }
/>
<Button
__next40pxDefaultSize
variant="secondary"
onClick={ closeModal }
>
Close Modal
</Button>
</Modal>
) }
</>
);
};
With Header Actions
The headerActions prop can be used to add auxiliary actions to the header, for example a fullscreen mode toggle.
const WithHeaderActions = ( { onRequestClose, ...args } ) => {
const [ isOpen, setOpen ] = useState( false );
const openModal = () => setOpen( true );
const closeModal: ModalProps[ 'onRequestClose' ] = ( event ) => {
setOpen( false );
onRequestClose( event );
};
return (
<>
<Button
__next40pxDefaultSize
variant="secondary"
onClick={ openModal }
>
Open Modal
</Button>
{ isOpen && (
<Modal onRequestClose={ closeModal } { ...args }>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et magna
aliqua. Ut enim ad minim veniam, quis nostrud
exercitation ullamco laboris nisi ut aliquip ex ea ea
commodo consequat. Duis aute irure dolor in
reprehenderit in voluptate velit esse cillum dolore eu
fugiat nulla pariatur. Excepteur sint occaecat cupidatat
non proident, sunt in culpa qui officia deserunt mollit
anim id est laborum.
</p>
<InputControl
__next40pxDefaultSize
style={ { marginBottom: '20px' } }
/>
<Button
__next40pxDefaultSize
variant="secondary"
onClick={ closeModal }
>
Close Modal
</Button>
</Modal>
) }
</>
);
};



