Cómo funciona
The panel is a vertical flex: header and footer stay put and only the body scrolls, with dividers that fade in by scroll position and a 16px fade at the clipped edge. On opening, the component measures the element that had focus (the button that opened it) and sets the panel's transform-origin there, so the dialog grows out of that button; without a visible opener it rises from below. Closing is controlled from outside. While open, focus is trapped, siblings are inert, page scroll is locked (unless contained) and Esc does not propagate. With busy set, nothing can close it. The panel unmounts after the exit animation. The CSS includes 40px pill buttons (ds-modal__btn: --primary ink, --secondary grey, --danger).
Úsalo para
- A small task on desktop that should not leave the page: edit a record, fill a few fields, read some detail.
- Content the user must handle before continuing (modal): the page behind is temporarily inactive.
- Content bigger than a popover but not worth a separate page.
Busca otra opción cuando
- Mobile (viewport under 768px): use a bottom sheet; the open and close props are the same.
- Confirming a dangerous action such as delete: use a confirm dialog that focuses Cancel and handles submitting and failure.
- Just saying an action succeeded: use a toast.
- Dialogs inside dialogs: for steps, swap the content inside one dialog.
- Long details to compare with the page, or large forms: use a drawer or a separate page.
Uso
import { useState } from 'react'
import { Modal } from './Modal.jsx'
export function EditPrice() {
const [open, setOpen] = useState(false)
const [saving, setSaving] = useState(false)
const save = async () => {
setSaving(true) // while saving, the close button, mask and Esc cannot close it
await savePrice()
setSaving(false)
setOpen(false)
}
return (
<>
<button className="ds-modal__btn ds-modal__btn--primary" onClick={() => setOpen(true)}>Edit price</button>
<Modal
open={open}
onClose={() => setOpen(false)}
title="Edit price"
size="md"
busy={saving}
footer={
<>
<button className="ds-modal__btn ds-modal__btn--secondary" disabled={saving} onClick={() => setOpen(false)}>
Cancel
</button>
<button className="ds-modal__btn ds-modal__btn--primary" aria-busy={saving || undefined} onClick={save}>
Save
</button>
</>
}
>
<PriceForm />
</Modal>
</>
)
}Propiedades
| Propiedad | Tipo | Por defecto | Qué hace |
|---|---|---|---|
open | boolean | — | Whether the modal is open. |
onClose | () => void | — | Asks to close (close button, mask click, Esc); the caller sets open to false. |
title | ReactNode | — | Title, also the accessible name of the dialog (aria-labelledby). Pass aria-label yourself when there is no title. |
children | ReactNode | — | Body. When it overflows, only the body area scrolls while title and footer stay. |
footer | ReactNode | — | Footer, pill buttons right-aligned with Cancel on the left and Confirm on the right. Omit for no footer. |
size | 'sm' | 'md' | 'lg' | 'sm' | Width tier: 400 (confirmations), 520 (forms), 720 (complex content); 16px of margin when the viewport is narrower. |
closable | boolean | true | Shows the close button at the top right. |
maskClosable | boolean | true | Whether clicking the mask closes the modal. |
closeOnEsc | boolean | true | Whether Esc closes the modal. |
busy | boolean | false | Submitting: the close button, mask and Esc do not close it, and the panel gets aria-busy. |
contained | boolean | false | Limits the modal to the nearest positioned ancestor (absolute). By default it is portaled to body over the whole viewport. |
initialFocus | RefObject<HTMLElement | null> | — | Element to focus first after opening, such as the first form field. Defaults to the dialog itself. |
labels | object | — | Replaces built-in text (close). Shallow-merged over the English defaults. |
...rest | HTMLAttributes<HTMLDivElement> | — | className and other attributes go to the panel (the role=dialog element), such as aria-describedby and style. |
Accesibilidad
- The panel is role="dialog" with aria-modal="true" and its title linked by aria-labelledby; pass aria-describedby for extra description.
- On open, focus moves into the dialog (or initialFocus) and Tab cycles inside; on close, focus returns to the opener.
- Siblings are inert while open; outside contained mode, page scrolling is locked too.
- Esc and mask clicks close it (except while busy); Esc does not propagate, and Esc during IME composition does not close it.
- The close button is a real button with an accessible name, a 32px circle with a 44px hit area; footer pill buttons are 40px with a 44px hit area.
- A scrollable body is keyboard-focusable so arrow keys scroll it; the focus ring is drawn inside.
- With reduced motion the panel only fades; button spinners slow down but keep going.
Movimiento
Enter: the mask (dimmed, 6px blur) fades in over 240ms; the panel grows from the opener, scale 0.9 to 1 with a fade, 240ms ease-out (without an opener it rises 12px and scales from 0.96). Exit is faster: 160ms ease-in, shrinking back toward the opener, then unmount. Dividers fade in 160ms; pill buttons press to 0.97. Reduced motion: fades only.
Añádelo a tu proyecto
Copia la instrucción en Codex o Claude Code. El agente lee el código publicado, te muestra lo que va a escribir y añade el componente junto a los tuyos.
Modal.jsxmodal.csstokens.cssREADME.md
Los dos archivos se instalan juntos. La hoja de estilos lee las variables de tokens.css y lleva valores de reserva, así que también funciona sola.

