Skip to content

Unstyled Modal

The ModalUnstyled component lets you create dialogs, popovers, lightboxes, and other elements that force the user to take action before continuing.

Introduction

ModalUnstyled is a utility component that renders its children in front of a backdrop. This lets you create an element that your users must interact with before continuing in the parent application.

Features

  • 🥞 Manages modal stacking when more than one is needed
  • 🪟 Creates a backdrop to disable interaction with the rest of the app
  • 🔐 Disables page scrolling while open
  • ⌨️ Manages focus correctly between the modal and its parent app
  • ♿️ Adds the appropriate ARIA roles automatically

ModalUnstyled is a lower-level construct that is used in the following Material UI components:

Component

Usage

After installation, you can start building with this component using the following basic elements:

import ModalUnstyled from '@mui/base/ModalUnstyled';

export default function MyApp() {
  return <ModalUnstyled>{/* the modal's content */}</ModalUnstyled>;
}

Basics

The following demo shows how to create and style a basic modal. Click Open modal to see how it behaves:

Customization

Nested modal

The following demo shows how to nest one modal within another:

Transitions

You can animate the open and close states of a modal using a transition component, as long as that component fulfills the following requirements:

  • Is a direct child descendent of the modal
  • Has an in prop—this corresponds to the open/close state
  • Calls the onEnter callback prop when the enter transition starts
  • Calls the onExited callback prop when the exit transition is completed

ModalUnstyled has built-in support for react-transition-group:

You can also use react-spring with ModalUnstyled, but it will require additional custom configuration:

Performance

The modal's content is unmounted when it is not open. This means that it will need to be re-mounted each time it is opened.

If you are rendering expensive component trees inside your modal, and you want to optimize for interaction responsiveness, you can change this default behavior by enabling the keepMounted prop.

You can also use the keepMounted prop if you want to make the content of the modal available to search engines (even when the modal is closed).

The following demo shows how to apply this prop to keep the modal mounted:

As with any performance optimization, the keepMounted prop won't necessarily solve all of your problems. Explore other possible bottlenecks in performance where you could make more considerable improvements before implementing this prop.

Server-side modal

React doesn't support the createPortal() API on the server.

In order to display a modal rendered on the server, you need to disable the portal feature with the disablePortal prop, as shown in the following demo:

Limitations

Focus trap

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

This is done for accessibility purposes, but it can potentially create issues for your users.

If the user needs to interact with another part of the page—for example, to interact with a chatbot window while a modal is open in the parent app—you can disable the default behavior with the disableEnforceFocus prop.

Accessibility

See the WAI-ARIA guide on the Dialog (Modal) pattern for complete details on accessibility best practices.

  • All interactive elements must have an accessible name. Use the aria-labelledby="id..." to give your Modal component an accessible name. You can also use aria-describedby="id..." to provide a description of the 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>
    
  • Follow the WAI-ARIA authoring practices to help you set the initial focus on the most relevant element based on the content of the modal.