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.
A modal is a type of floating window that appears in front of content to provide critical information or ask for a decision. Modals disable all other functionality when they appear. A modal remains on screen until the user confirms it, dismisses it, or takes the required action.
While modals can be an effective way to disclose additional controls or information, they can also be a source of interruption for the user. For this reason, always question whether a modal is necessary, and work to avoid the situations in which they are required.
Modals are used for:
A modal is a type of window. Access to the rest of the UI is disabled until the modal is addressed. All modals are interruptive by design – their purpose is to have the user focus on content, so the modal surface appears in front of all other surfaces.
To clarify that the rest of the screen is inaccessible and to focus attention on the modal, surfaces behind the modal are scrimmed — they get a temporary overlay to obscure their content and make it less prominent.
A modal’s purpose is communicated through its title and button text.
Titles should:
Do This modal title poses a specific question, concisely explains the purpose the request, and provides clear actions.
Don’t This modal creates ambiguity, and therefore unease — it leaves the user unsure about how to respond, or causes them to second-guess their answer.
Side-by-side buttons display two text buttons next to one another.
Use stacked buttons when you need to accommodate longer button text. Always place confirming actions above dismissive actions.
Modals appear without warning, requiring users to stop their current task. They should be used sparingly — not every choice or setting warrants this kind of abrupt interruption.
Modals retain focus until dismissed or the user completes an action, like choosing a setting. They shouldn’t be obscured by other elements or appear partially on screen.
Most modal content should avoid scrolling. Scrolling is permissible if the modal content exceeds the height of the modal (e.g. a list component with many rows). When a modal scrolls, the modal title is pinned at the top and the buttons are pinned at the bottom. This ensures that content remains visible alongside the title and buttons, even while scrolling.
Modals don’t scroll with elements outside of the modal, like the background.
When viewing a scrollable list of options, the modal title and buttons remain fixed.
Modals are dismissible in three ways:
esc
keyIf the user’s ability to dismiss a modal is disabled, they must choose a modal action to proceed.
The modal is used to create an accessible modal over an application.
Note: The API for this modal has been mimicked to resemble react-modal
.
The following example shows you how to properly implement a modal. For the modal to properly work it's important you implement the close logic for the modal properly.
import { Button, Modal } from '@wordpress/components';
import { withState } from '@wordpress/compose';
const MyModal = withState( {
isOpen: false,
} )( ( { isOpen, setState } ) => (
<div>
<Button isDefault onClick={ () => setState( { isOpen: true } ) }>Open Modal</Button>
{ isOpen && (
<Modal
title="This is my modal"
onRequestClose={ () => setState( { isOpen: false } ) }>
<Button isDefault onClick={ () => setState( { isOpen: false } ) }>
My custom close button
</Button>
</Modal>
) }
</div>
) );
The set of props accepted by the component will be specified below. Props not included in this set will be applied to the input elements.
This property is used as the modal header's title. It is required for accessibility reasons.
String
This function is called to indicate that the modal should be closed.
function
If this property is added, it will be added to the modal content div
as aria-label
.
You are encouraged to use this if aria.labelledby
is not provided.
String
If this property is added, it will be added to the modal content div
as aria-labelledby
.
You are encouraged to use this when the modal is visually labelled.
String
modal-heading
If this property is added, it will be added to the modal content div
as aria-describedby
.
String
If this property is true, it will focus the first tabbable element rendered in the modal.
boolean
If this property is added, it will determine whether the modal requests to close when the escape key is pressed.
boolean
If this property is added, it will determine whether the modal requests to close when a mouse click occurs outside of the modal content.
boolean
If this property is set to false, the modal will not display a close icon and cannot be dismissed.
boolean
If this property is added, it will an additional class name to the modal content div
.
String
If this property is added, it will override the default role of the modal.
String
dialog
If this property is added, it will an additional class name to the modal overlay div
.
String
Notice
.