[ Overlays ]
Modal
A dialog that appears above the page and blocks interaction with the content behind it. It traps focus, locks page scroll, closes on Escape or overlay click, and returns focus to the trigger when closed.
Import#
import { Modal } from 'reusify-ui';When to use#
- Confirming destructive or important actions ("Delete project?").
- Short focused tasks that shouldn't send the user to a new page, like inviting a teammate.
- For long forms or side content, a Drawer often works better. For simple messages, use an Alert or Toast.
Examples#
Confirmation dialog#
Modal is controlled: you own the open state. The useDisclosure hook keeps that to one line.
Form inside a modal#
Put the submit button in the footer and connect it with form="…". Add data-autofocus to the field that should receive focus on open.
Sizes#
<Modal size="sm" … /> // 24rem
<Modal size="md" … /> // 32rem (default)
<Modal size="lg" … /> // 48rem
<Modal size="xl" … /> // 64rem
<Modal size="full" … /> // full screenControlling close behaviour#
For critical flows you can turn off overlay-click and Escape closing, and hide the close button.
See the code: the user can only close this modal with the button.
<Modal
open={isOpen}
onClose={close}
closeOnOverlayClick={false}
closeOnEsc={false}
showCloseButton={false}
title="Accept the new terms"
footer={<Button onClick={accept}>I agree</Button>}
/>API reference#
<Modal />
| Prop | Type | Default | Description |
|---|---|---|---|
open* | boolean | - | Whether the modal is visible. |
onClose* | () => void | - | Called on Escape, overlay click or the close button. |
title | ReactNode | - | Dialog heading (also its accessible name). |
description | ReactNode | - | Muted text under the title (its accessible description). |
children | ReactNode | - | Body content. |
footer | ReactNode | - | Right-aligned action row. |
size | 'sm' | 'md' | 'lg' | 'xl' | 'full' | 'md' | Maximum width. |
closeOnOverlayClick | boolean | true | Close when the backdrop is clicked. |
closeOnEsc | boolean | true | Close when Escape is pressed. |
showCloseButton | boolean | true | Show the × button in the header. |
className | string | - | Class for the dialog panel. |
open*-booleanWhether the modal is visible.
onClose*-() => voidCalled on Escape, overlay click or the close button.
title-ReactNodeDialog heading (also its accessible name).
description-ReactNodeMuted text under the title (its accessible description).
children-ReactNodeBody content.
footer-ReactNodeRight-aligned action row.
size'md''sm' | 'md' | 'lg' | 'xl' | 'full'Maximum width.
closeOnOverlayClicktruebooleanClose when the backdrop is clicked.
closeOnEsctruebooleanClose when Escape is pressed.
showCloseButtontruebooleanShow the × button in the header.
className-stringClass for the dialog panel.
Accessibility#
- role="dialog" with aria-modal="true", labelled by the title and described by the description.
- Focus moves into the dialog on open (or to the [data-autofocus] element) and is trapped inside while open.
- Focus returns to the element that opened it when it closes.
- Rendered in a portal on document.body and safe to use with server rendering.