Modal
Love them or hate them, <Modal />
provides a solid foundation for creating dialogs, lightboxes, or whatever else.
The Modal component renders its children
node in front of a backdrop component.
The Modal offers a few helpful features over using just a <Portal/>
component and some styles:
- Manages dialog stacking when one-at-a-time just isn't enough.
- Creates a backdrop, for disabling interaction below the modal.
- It properly manages focus; moving to the modal content, and keeping it there until the modal is closed.
- It disables scrolling of the page content while open.
- Adds the appropriate ARIA roles are automatically.
- Easily-pluggable animations via a
<Transition/>
component.
Note that, in the same way the backdrop element prevents users from clicking or interacting
with the page content underneath the Modal, screen readers also need to be signaled to not to
interact with page content while the Modal is open. To do this, we use a common technique of applying
the aria-hidden='true'
attribute to the non-Modal elements in the Modal container
. This means that for
a Modal to be truly modal, it should have a container
that is outside your app's
React hierarchy (such as the default: document.body).
API
import Modal from 'react-overlays/Modal'
autoFocus
When true
The modal will automatically shift focus to itself when it opens, and
replace it to the last focused element when it closes. This also
works correctly with any Modal children that have the autoFocus
prop.
Generally this should never be set to false
as it makes the Modal less
accessible to assistive technologies, like screen readers.
true
backdrop
Include a backdrop component.
true
backdropTransition
A react-transition-group@2.0.0
<Transition/>
component used
to control animations for the backdrop components.
children
className
container
A DOM element, a ref
to an element, or function that returns either. The Modal is appended to it's container
element.
For the sake of assistive technologies, the container should usually be the document body, so that the rest of the page content can be placed behind a virtual backdrop as well as a visual one.
containerClassName
A css class or set of classes applied to the modal container when the modal is open, and removed when it is closed.
enforceFocus
When true
The modal will prevent focus from leaving the Modal while open.
Generally this should never be set to false
as it makes the Modal less
accessible to assistive technologies, like screen readers.
true
keyboard
Close the modal when escape key is pressed
true
manager
A ModalManager instance used to track and manage the state of open Modals. Useful when customizing how modals interact within a container
onBackdropClick
A callback fired when the backdrop, if specified, is clicked.
onEnter
Callback fired before the Modal transitions in
onEntered
Callback fired after the Modal finishes transitioning in
onEntering
Callback fired as the Modal begins to transition in
onEscapeKeyDown
A callback fired when the escape key, if specified in keyboard
, is pressed.
If preventDefault() is called on the keyboard event, closing the modal will be cancelled.
onExit
Callback fired right before the Modal transitions out
onExited
Callback fired after the Modal finishes transitioning out
onExiting
Callback fired as the Modal begins to transition out
onHide
A callback fired when either the backdrop is clicked, or the escape key is pressed.
The onHide
callback only signals intent from the Modal,
you must actually set the show
prop to false
for the Modal to close.
() => {}
onShow
A callback fired when the Modal is opening.
renderBackdrop
A function that returns a backdrop component. Useful for custom backdrop rendering.
(props: RenderModalBackdropProps) => <div {...props} />
renderDialog
A function that returns the dialog component. Useful for custom rendering. Note: the component should make sure to apply the provided ref.
renderDialog={props => <MyDialog {...props} />}
restoreFocus
When true
The modal will restore focus to previously focused element once
modal is hidden
true
restoreFocusOptions
Options passed to focus function when restoreFocus
is set to true
role
'dialog'
show
Set the visibility of the Modal
false
style
transition
A react-transition-group@2.0.0
<Transition/>
component used
to control animations for the dialog component.