Introduction
Modal is a lower-level construct that is leveraged by Dialog, Drawer, Menu, and Popover components. It provides a solid foundation for creating dialogs, popovers, lightboxes, or whatever else.Basic Usage
Props
open
- Type:
boolean - Required: Yes
true, the component is shown.
onClose
- Type:
(event: {}, reason: 'backdropClick' | 'escapeKeyDown') => void - Default:
undefined
reason parameter indicates why the modal is closing.
children
- Type:
React.ReactElement - Required: Yes
BackdropComponent
- Type:
React.ElementType - Default:
styled(Backdrop)
slots.backdrop instead.
BackdropProps
- Type:
Partial<BackdropProps> - Default:
undefined
slotProps.backdrop instead.
closeAfterTransition
- Type:
boolean - Default:
false
container
- Type:
HTMLElement | (() => HTMLElement) - Default:
document.body
disableAutoFocus
- Type:
boolean - Default:
false
true, the modal will not automatically shift focus to itself when it opens, and will not restore focus to previously focused element when it closes.
disableEnforceFocus
- Type:
boolean - Default:
false
true, the modal will not prevent focus from leaving the modal while open.
disablePortal
- Type:
boolean - Default:
false
children will be under the DOM hierarchy of the parent component.
disableRestoreFocus
- Type:
boolean - Default:
false
true, the modal will not restore focus to previously focused element once modal is hidden or unmounted.
disableScrollLock
- Type:
boolean - Default:
false
hideBackdrop
- Type:
boolean - Default:
false
true, the backdrop is not rendered.
keepMounted
- Type:
boolean - Default:
false
slots
- Type:
{ root?: React.ElementType, backdrop?: React.ElementType } - Default:
{}
slotProps
- Type:
{ root?: object | function, backdrop?: object | function } - Default:
{}
Focus Management
The modal automatically manages focus:- When opened, focus moves to the modal content
- While open, focus is trapped within the modal
- When closed, focus returns to the element that triggered the modal
Customizing Focus Behavior
Transitions
Use transitions to animate the modal entrance and exit:Server-Side Rendering
Modal supports server-side rendering. Thecontainer and disablePortal props can help manage portal behavior during SSR:
Accessibility
The modal should have:- An accessible name using
aria-labelledbyoraria-label - An accessible description using
aria-describedby - Keyboard support for closing (ESC key)
- Focus management
Performance
Keep Mounted
For better performance in scenarios where the modal opens/closes frequently:Disable Scroll Lock
If you’re experiencing performance issues with the scroll lock:Common Patterns
Nested Modals
When to Use
- Use Modal when you need low-level control over modal behavior
- Use Dialog for standard dialog boxes with actions
- Use Drawer for navigation panels
- Use Popover for contextual content