Usage
Center modal vertically
Remove header
To remove header set withCloseButton={false}
Change size
You can change modal width by setting size
prop to predefined size or any valid width, for example, 55%
or 50rem
.
Modal
width cannot exceed 100vw
.
Size auto
Modal
with size="auto"
will have width to fit its content:
Fullscreen
Fullscreen modal will take the entire screen, it is usually to change transition to fade
when fullScreen
prop is set:
To switch Modal to fullscreen on devices with small screens only use use-media-query hook.
size
prop is ignored if fullScreen
prop is set:
Customize overlay
Modal
uses Overlay component, you can set any props that Overlay
supports with overlayProps
:
Modal with scroll
Usage with ScrollArea
Change offsets
Use xOffset
/yOffset
to configure horizontal/vertical content offsets:
Change transitions
Modal
is built with Transition component. Use transitionProps
prop to customize any Transition properties:
Initial focus
Modal uses FocusTrap to trap focus. Add data-autofocus
attribute to the element that should receive initial focus.
Control behavior
The following props can be used to control Modal
behavior.
In most cases it is not recommended to turn these features off –
it will make the component less accessible.
trapFocus
– determines whether focus should be trapped inside modalcloseOnEscape
– determines whether the modal should be closed whenEscape
key is pressedcloseOnClickOutside
– determines whether the modal should be closed when user clicks on the overlayreturnFocus
– determines whether focus should be returned to the element that was focused before the modal was opened
Compound components
You can use the following compound components to have full control over the Modal
rendering:
Modal.Root
– context providerModal.Overlay
– render OverlayModal.Content
– main modal element, should include all modal contentModal.Header
– sticky header, usually containsModal.Title
andModal.CloseButton
Modal.Title
–h2
element,aria-labelledby
ofModal.Content
is pointing to this element, usually is rendered insideModal.Header
Modal.CloseButton
– close button, usually rendered insideModal.Header
Modal.Body
– a place for main content,aria-describedby
ofModal.Content
is pointing to this element
Fixed elements offset
Modal
component uses react-remove-scroll
package to lock scroll. To properly size these elements
add a className
to them (documentation):
Accessibility
Modal
component follows WAI-ARIA recommendations on accessibility.
Labels
Set title
props to make component accessible, will add aria-labelledby
to the content element:
To set close button aria-label
use closeButtonProps
:
Keyboard interactions
Key | Description |
---|---|
Escape | Close modal |
Modal component props
Name | Type | Description |
---|---|---|
centered | boolean | Determines whether the modal should be centered vertically, false by default |
children | ReactNode | Modal content |
closeButtonProps | ModalBaseCloseButtonProps | Props added to close button |
closeOnClickOutside | boolean | Determines whether the modal/drawer should be closed when user clicks on the overlay, true by default |
closeOnEscape | boolean | Determines whether onClose should be called when user presses escape key, true by default |
fullScreen | boolean | Determines whether the modal should take the entire screen |
id | string | Id used to connect modal/drawer with body and title |
keepMounted | boolean | If set modal/drawer will not be unmounted from the DOM when it is hidden, display: none styles will be added instead |
lockScroll | boolean | Determines whether scroll should be locked when opened={true}, defaults to true |
onClose * | () => void | Called when modal/drawer is closed |
opened * | boolean | Determines whether modal/drawer is opened |
overlayProps | ModalBaseOverlayProps | Props added to Overlay component, use configure opacity, background color, styles and other properties |
padding | number | "xs" | "sm" | "md" | "lg" | "xl" | Key of theme.spacing or any valid CSS value to set content, header and footer padding, 'md' by default |
portalProps | Omit<PortalProps, "children" | "target" | "withinPortal"> | Props to pass down to the portal when withinPortal is true |
radius | number | "xs" | "sm" | "md" | "lg" | "xl" | Key of theme.radius or any valid CSS value to set border-radius, theme.defaultRadius by default |
returnFocus | boolean | Determines whether focus should be returned to the last active element onClose is called, true by default |
scrollAreaComponent | ScrollAreaComponent | Scroll area component, ScrollArea.Autosize by default |
shadow | MantineShadow | Key of theme.shadows or any valid css box-shadow value, 'xl' by default |
size | number | "xs" | "sm" | "md" | "lg" | "xl" | Controls content width, 'md' by default |
target | string | HTMLElement | Target element or selector where Portal should be rendered, by default new element is created and appended to the document.body |
title | ReactNode | Modal title |
transitionProps | Partial<Omit<TransitionProps, "mounted">> | Props added to Transition component that used to animate overlay and body, use to configure duration and animation type, { duration: 200, transition: 'pop' } by default |
trapFocus | boolean | Determines whether focus should be trapped, true by default |
withCloseButton | boolean | Determines whether close button should be rendered, true by default |
withOverlay | boolean | Determines whether overlay should be rendered, true by default |
withinPortal | boolean | Determines whether component should be rendered inside Portal, true by default |
xOffset | MarginLeft<string | number> | Left/right modal offset, 5vw by default |
yOffset | MarginTop<string | number> | Top/bottom modal offset, 5vh by default |
zIndex | number | z-index CSS property of root element, 200 by default |
Modal component Styles API
Name | Static selector | Description |
---|---|---|
root | .mantine-Modal-root | Root element |
inner | .mantine-Modal-inner | Element used to center modal, has fixed position, takes entire screen |
content | .mantine-Modal-content | Modal.Content root element |
header | .mantine-Modal-header | Contains title and close button |
overlay | .mantine-Modal-overlay | Overlay displayed under the Modal.Content |
title | .mantine-Modal-title | Modal title (h2 tag), displayed in header |
body | .mantine-Modal-body | Modal body, displayed after header |
close | .mantine-Modal-close | Close button |