logo

SINGULARITY

DOCS

PurchaseReturn to the Dashboard
Reference

Modal

The modal component offers a robust foundation for building dialogs, popovers, lightboxes, and other similar elements.

The component displays its children node in front of a backdrop. The Modal provides key features:
  • 💄 Manages modal stacking when one-at-a-time just isn't enough.
  • 🔐 Creates a backdrop, for disabling interaction below the modal.
  • 🔐 It disables scrolling of the page content while open.
  • ♿️ It properly manages focus; moving to the modal content, and keeping it there until the modal is closed.
  • ♿️ Adds the appropriate ARIA roles automatically.
The term "modal" is often used interchangeably with "dialog," but this is inaccurate. A modal window refers to a UI element that blocks interaction with the rest of the application.
For creating modal dialogs, consider using the Dialog component instead of directly using Modal. Modal is a lower-level construct utilized by the following components:

Basic modal

Note that you can remove the outline (often blue or gold) by applying the outline: 0 CSS property.

Nested modal

Modals can be nested, such as including a select within a dialog, but stacking more than two modals or using multiple modals with backdrops is not recommended.

Transitions

The modal's open/close state can be animated using a transition component. This component must adhere to the following requirements:
  • Be a direct child descendent of the modal.
  • Have an in prop. This corresponds to the open/close state.
  • Call the onEnter callback prop when the enter transition starts.
  • Call the onExited callback prop when the exit transition is completed. These two callbacks allow the modal to unmount the child content when closed and fully transitioned.
Modal includes built-in support for react-transition-group.
Alternatively, you can utilize react spring for transitions.

Performance

By default, modal content is unmounted when closed. To make content accessible to search engines or to render complex component trees while maintaining interaction responsiveness, consider enabling the keepMounted prop:
<Modal keepMounted />
As with any performance optimization, this approach is not a universal solution. Identify performance bottlenecks before applying these optimization techniques.

Server-side modal

React does not support the createPortal() API on the server. To display a modal on the server, disable the portal feature using the disablePortal prop:

Limitations

Focus trap

The modal ensures focus remains within the component by redirecting it to the modal body if it attempts to escape.
This behavior enhances accessibility but may cause issues. If users need to interact with other page elements, such as a chatbot window, you can disable this behavior:
<Modal disableEnforceFocus />

Accessibility

  • Ensure you include aria-labelledby="id...", referencing the modal title, in the Modal. You may also provide a description using the aria-describedby="id..." prop on the Modal.
    <Modal aria-labelledby="modal-title" aria-describedby="modal-description">
      <h2 id="modal-title">My Title</h2>
      <Typography id="modal-description">My Description</Typography>
    </Modal>
  • The WAI-ARIA Authoring Practices can guide you in setting the initial focus on the most relevant element within your modal content.
  • Remember that a "modal window" overlays either the primary window or another modal window. Content beneath a modal is inert, meaning users cannot interact with it. This may lead to conflicting behaviors.