WordPress.org

Modal Edit

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.

Usage Usage

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>
            : null }
    </div>
) );

Top ↑

Props Props

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.

title title

This property is used as the modal header’s title. It is required for accessibility reasons.

  • Type: String
  • Required: Yes

Top ↑

onRequestClose onRequestClose

This function is called to indicate that the modal should be closed.

  • Type: function
  • Required: Yes

Top ↑

contentLabel contentLabel

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.

  • Type: String
  • Required: No

Top ↑

aria.labelledby aria.labelledby

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.

  • Type: String
  • Required: No
  • Default: modal-heading

Top ↑

aria.describedby aria.describedby

If this property is added, it will be added to the modal content div as aria-describedby.

  • Type: String
  • Required: No

Top ↑

focusOnMount focusOnMount

If this property is true, it will focus the first tabbable element rendered in the modal.

  • Type: boolean
  • Required: No
  • Default: true

Top ↑

shouldCloseOnEsc shouldCloseOnEsc

If this property is added, it will determine whether the modal requests to close when the escape key is pressed.

  • Type: boolean
  • Required: No
  • Default: true

Top ↑

shouldCloseOnClickOutside 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.

  • Type: boolean
  • Required: No
  • Default: true

Top ↑

isDismissable isDismissable

If this property is set to false, the modal will not display a close icon and cannot be dismissed.

  • Type: boolean
  • Required: No
  • Default: true

Top ↑

className className

If this property is added, it will an additional class name to the modal content div.

  • Type: String
  • Required: No

Top ↑

role role

If this property is added, it will override the default role of the modal.

  • Type: String
  • Required: No
  • Default: dialog

Top ↑

overlayClassName overlayClassName

If this property is added, it will an additional class name to the modal overlay div.

  • Type: String
  • Required: No
Skip to toolbar