React Modal component - Material UI (original) (raw)

+

The modal component provides a solid foundation for creating dialogs, popovers, lightboxes, or whatever else.

The component renders its children node in front of a backdrop component. The Modal offers important features:

If you are creating a modal dialog, you probably want to use the Dialog component rather than directly using Modal. Modal is a lower-level construct that is leveraged by the following components:

Basic modal

Press Enter to start editing

Notice that you can disable the outline (often blue or gold) with the outline: 0 CSS property.

Nested modal

Modals can be nested, for example a select within a dialog, but stacking of more than two modals, or any two modals with a backdrop is discouraged.

Press Enter to start editing

Transitions

The open/close state of the modal can be animated with a transition component. This component should respect the following conditions:

Modal has built-in support for react-transition-group.

Alternatively, you can use react-spring.

Performance

The content of modal is unmounted when closed. If you need to make the content available to search engines or render expensive component trees inside your modal while optimizing for interaction responsiveness it might be a good idea to change this default behavior by enabling the keepMounted prop:

As with any performance optimization, this is not a silver bullet. Be sure to identify bottlenecks first, and then try out these optimization strategies.

Server-side modal

React doesn't support the createPortal() API on the server. In order to display the modal, you need to disable the portal feature with the disablePortal prop:

Server-side modal

If you disable JavaScript, you will still see me.

Limitations

Focus trap

The modal moves the focus back to the body of the component if the focus tries to escape it.

This is done for accessibility purposes. However, it might create issues. In the event the users need to interact with another part of the page, for example with a chatbot window, you can disable the behavior:

<Modal disableEnforceFocus />

Accessibility

(WAI-ARIA: https://www.w3.org/WAI/ARIA/apg/patterns/dialog-modal/)

<Modal aria-labelledby="modal-title" aria-describedby="modal-description">  
  <h2 id="modal-title">My Title</h2>  
  <p id="modal-description">My Description</p>  
</Modal>  

Unstyled

Use the Base UI Modal for complete ownership of the component's design, with no Material UI or Joy UI styles to override. This unstyled version of the component is the ideal choice for heavy customization with a smaller bundle size.

API

See the documentation below for a complete reference to all of the props and classes available to the components mentioned here.