Overlays
Modal
Free20.4 KB gzippedAn accessible modal dialog (WAI-ARIA APG "Dialog (Modal)" pattern). Modal holds the open state; compose it from Modal.Trigger, Modal.Content, Modal.Header, Modal.Title, Modal.Description, Modal.Body, Modal.Footer, and Modal.Close.
Accessibility: WCAG 2.2 AA · No A or AA violations in light or dark mode · Keys: Enter, Space, Escape and 2 more · Full report
Import
import { Modal, ConfirmModal } from "@vjdesigns/vjdesigns-ui";
Importing only Modal adds about 20.4 KB of minified, gzipped JavaScript to your app (React not included). The stylesheet is shared by every component and loaded once: 75.1 KB gzipped.
Examples
Basic
Modal.Trigger and Modal.Close wrap your own buttons. Focus is trapped while open and returns to the trigger on close.
import { Button, Field, Input, Modal, Stack } from "@vjdesigns/vjdesigns-ui";
export function Basic() {
return (
<Modal>
<Modal.Trigger>
<Button>Edit profile</Button>
</Modal.Trigger>
<Modal.Content>
<Modal.Header>
<Modal.Title>Edit profile</Modal.Title>
<Modal.Description>Changes are visible to your whole team.</Modal.Description>
</Modal.Header>
<Modal.Body>
<Stack gap={4}>
<Field label="Display name">
<Input defaultValue="Jordan Lee" />
</Field>
<Field label="Job title">
<Input defaultValue="Product designer" />
</Field>
</Stack>
</Modal.Body>
<Modal.Footer>
<Modal.Close>
<Button variant="ghost" tone="neutral">
Cancel
</Button>
</Modal.Close>
<Modal.Close>
<Button>Save</Button>
</Modal.Close>
</Modal.Footer>
</Modal.Content>
</Modal>
);
}
Confirm
ConfirmModal is a ready-made confirmation dialog (role="alertdialog"). When onConfirm returns a promise, the confirm button shows a spinner until it settles.
import { useState } from "react";
import { Button, ConfirmModal, Stack, Text } from "@vjdesigns/vjdesigns-ui";
export function Confirm() {
const [deleted, setDeleted] = useState(false);
return (
<Stack gap={3} align="center">
<ConfirmModal
trigger={
<Button tone="danger" variant="outline" disabled={deleted}>
Delete project
</Button>
}
tone="danger"
title="Delete “Spring launch”?"
description="This deletes the project and its 24 files. You can't undo this."
confirmLabel="Delete project"
confirmLoadingText="Deleting…"
onConfirm={() => new Promise((resolve) => setTimeout(resolve, 1200)).then(() => setDeleted(true))}
/>
{deleted && (
<Text size="sm" tone="muted">
Project deleted.
</Text>
)}
</Stack>
);
}
Accessibility
A modal dialog is named by Modal.Title and described by Modal.Description. While it's open, everything outside is inert, Tab stays inside, and focus starts in the body (not on the close button). Escape closes it and focus returns to the button that opened it.
import { Button, Field, Input, Modal } from "@vjdesigns/vjdesigns-ui";
export function Accessibility() {
return (
<Modal>
<Modal.Trigger>
<Button variant="outline" tone="neutral">
Report a problem
</Button>
</Modal.Trigger>
<Modal.Content>
<Modal.Header>
<Modal.Title>Report a problem</Modal.Title>
<Modal.Description>We read every report. Please don't include passwords.</Modal.Description>
</Modal.Header>
<Modal.Body>
<Field label="What happened?" required>
<Input fullWidth />
</Field>
</Modal.Body>
<Modal.Footer>
<Modal.Close>
<Button variant="ghost" tone="neutral">
Cancel
</Button>
</Modal.Close>
<Modal.Close>
<Button>Send report</Button>
</Modal.Close>
</Modal.Footer>
</Modal.Content>
</Modal>
);
}
Translated
The close button's name and the confirm dialog's default buttons come from the locale (Spanish here). The dialog keeps the language of the area that opened it.
import { Button, ConfirmModal, LocaleProvider } from "@vjdesigns/vjdesigns-ui";
import es from "@vjdesigns/vjdesigns-ui/locales/es";
export function Translated() {
return (
<LocaleProvider locale="es" messages={es}>
<ConfirmModal
trigger={
<Button variant="outline" tone="danger">
Eliminar proyecto
</Button>
}
title="¿Eliminar este proyecto?"
description="Se borrarán todos sus archivos. No se puede deshacer."
tone="danger"
onConfirm={() => {}}
/>
</LocaleProvider>
);
}
Right to left
The first example again, inside a right-to-left LocaleProvider (Arabic). Layout, spacing and arrow icons mirror, and the components' built-in text is translated. The example's own text stays in English.
Basic
import { LocaleProvider } from "@vjdesigns/vjdesigns-ui";
import ar from "@vjdesigns/vjdesigns-ui/locales/ar";
// Basic is the "Basic" example above.
<LocaleProvider locale="ar" messages={ar}>
<Basic />
</LocaleProvider>
Usage notes
Accessibility:
role="dialog"(oralertdialog) witharia-modal="true", named byModal.Titleand described byModal.Description.- While open, everything outside is
inert(plusaria-hidden), page scrolling is locked, and Tab / Shift+Tab stay inside. - Escape closes the topmost modal. Focus returns to the trigger (or
finalFocusRef) on close. - Stacked modals work: only the topmost one is active.
API reference
Modal
| Prop | Type | Default |
|---|---|---|
openWhether the modal is open (controlled). Pair with | boolean | — |
defaultOpenWhether the modal starts open (uncontrolled). | boolean | false |
onOpenChangeCalled with the next open state when the modal opens or closes. | (open: boolean) => void | — |
sizeWidth on screens 640px and wider: | "sm""md""lg""xl""full" | "md" |
placementVertical position on screens 640px and wider (and on small screens with | "center""top" | "center" |
mobileLayoutLayout below 640px: a bottom | "sheet""card" | "sheet" |
scrollBehavior
| "inside""outside" | "inside" |
closeOnOverlayPressClose when the overlay (backdrop) is pressed. | boolean | true, or false when `role="alertdialog"` |
closeOnEscapeClose when Escape is pressed (topmost modal only). | boolean | true |
initialFocusRefElement to focus when the modal opens. By default focus goes to the first focusable element in | RefObject<HTMLElement | null> | — |
finalFocusRefElement to focus when the modal closes. By default focus returns to the element that was focused before opening (usually the trigger). | RefObject<HTMLElement | null> | — |
roleARIA role. Use | "dialog""alertdialog" | "dialog" |
showCloseShow the built-in close button in the top-end corner. | boolean | true |
closeLabelAccessible name of the built-in close button. Localize it here. | string | "Close" |
children
| ReactNode | — |
Modal.Trigger
Wraps a button so pressing it opens the modal. Focus returns to it on close.
| Prop | Type | Default |
|---|---|---|
childrenRequiredA single element that opens the modal, usually a | ReactElement | — |
Modal.Content
| Prop | Type | Default |
|---|---|---|
overlayClassNameClass name for the overlay (backdrop) behind the dialog. | string | — |
Also accepts native <div> attributes, plus className, style and ref.
Modal.Header
Top section holding Modal.Title and Modal.Description. Leaves room for the close button.
Also accepts standard HTML attributes, plus className, style and ref.
Modal.Title
The dialog's heading. Required: it names the dialog via aria-labelledby (a dev warning appears when it is missing and the content has no aria-label).
| Prop | Type | Default |
|---|---|---|
levelHeading level, chosen to fit the document outline. | 123456 | 2 |
sizeVisual size. | "xs""sm""md""lg""xl""2xl""3xl""4xl""5xl""6xl" | "2xl" |
Also accepts native <h2> attributes, plus className, style and ref.
Modal.Description
Supporting text under the title, wired to the dialog's aria-describedby. Keep it short.
Also accepts native <p> attributes, plus className, style and ref.
Modal.Body
Main content. With scrollBehavior="inside" this is the part that scrolls; when it overflows it becomes a focusable, labelled region so it can be scrolled with the keyboard.
| Prop | Type | Default |
|---|---|---|
scrollRegionLabelAccessible name of the body while it scrolls (it becomes a focusable region so keyboard users can scroll it). Defaults to the title. | string | — |
Also accepts native <div> attributes, plus className, style and ref.
Modal.Footer
Actions row, end-aligned. Put the least destructive action first ("Cancel", then "Save"). Below 640px the buttons stack full width with the last (primary) action on top.
Also accepts standard HTML attributes, plus className, style and ref.
Modal.Close
Wraps a button so pressing it closes the modal.
| Prop | Type | Default |
|---|---|---|
childrenRequiredA single element that closes the modal, usually a | ReactElement | — |
ConfirmModal
A ready-made confirmation (role="alertdialog") built on Modal. Initial focus goes to Cancel, the least destructive action. Pressing the overlay does not close it; Escape and Cancel do.
| Prop | Type | Default |
|---|---|---|
openWhether the dialog is open (controlled). Pair with | boolean | — |
defaultOpenWhether the dialog starts open (uncontrolled). | boolean | false |
onOpenChangeCalled with the next open state. | (open: boolean) => void | — |
triggerOptional element (usually a | ReactElement | — |
titleRequiredQuestion or statement the user must respond to, e.g. "Delete this team?". | ReactNode | — |
descriptionConsequences of the action, e.g. "This can't be undone." Wired to | ReactNode | — |
childrenExtra content between the description and the actions. | ReactNode | — |
confirmLabelLabel of the confirm button. Name the action ("Delete team"), not "OK". | ReactNode | "Confirm" |
cancelLabelLabel of the cancel button. | ReactNode | "Cancel" |
confirmLoadingTextText shown and announced next to the spinner while | ReactNode | — |
toneConfirm button color; | "primary""danger" | "primary" |
onConfirmCalled when the user confirms. May return a promise: the confirm button shows a spinner and the dialog stays open (Escape and Cancel are disabled) until it settles. It closes on success; on rejection it stays open so you can show an error. | () => void | Promise<unknown> | — |
onCancelCalled when the user cancels (Cancel button or Escape). | () => void | — |
sizeWidth on screens 640px and wider. | "sm""md""lg""xl""full" | "sm" |
finalFocusRefElement to focus after closing. Defaults to the trigger. | RefObject<HTMLElement | null> | — |
See what changed in each version in the Changelog.
Accessibility report
Modal targets WCAG 2.2 AA. Below: a live axe-core audit of this page's examples, Lighthouse scores for the page in both modes, the accessibility behavior built into the component, and the checks only a person can do.
axe-core audit (live)
Checking the examples above…
Lighthouse audit
Light mode
Score 10035 passed · 0 failed · 30 not applicable · 10 to check by hand
Dark mode
Score 10035 passed · 0 failed · 30 not applicable · 10 to check by hand
Lighthouse 13.5.0 audits the whole docs page (header and sidebar included), not just the examples, in English. Last run 11/27/2026. Some failed audits carry no weight in the score, so a page can score 100 and still list one.
Built-in behavior
Keyboard
| Key | Action |
|---|---|
| EnterSpace | On the trigger: opens the dialog. |
| Escape | Closes the dialog. |
| TabShift+Tab | Moves through the focusable elements inside, wrapping at the ends (focus stays inside). |
Focus moves in on open and is trapped; the page is inert and can't scroll; focus returns to the trigger on close.
Roles and states
| Element | Role | States and properties |
|---|---|---|
Modal.Content | dialog (alertdialog for ConfirmModal) | aria-modal="true", aria-labelledby → title, aria-describedby → description |
| Trigger | button | aria-haspopup="dialog", aria-expanded |
| Close button | button | aria-label |
Manual checks
Automated tools find roughly a third of accessibility issues. Test these in your app, with your content.
| Check | What to test | WCAG 2.2 |
|---|---|---|
| Keyboard | Every action works with Tab, Shift+Tab, Enter, Space and the arrow keys, and focus never gets stuck. | 2.1.1 (A), 2.1.2 (A) |
| Focus | Focus moves in a logical order, is always visible, and isn't hidden behind sticky headers. | 2.4.3 (A), 2.4.7 (AA), 2.4.11 (AA), 2.4.13 (AAA) |
| Screen readers | Names, roles and states are announced correctly, and status changes are announced without moving focus. | 1.3.1 (A), 4.1.2 (A), 4.1.3 (AA) |
| Color | Meaning never depends on color alone; your own colors meet contrast in both modes. | 1.4.1 (A), 1.4.3 (AA), 1.4.11 (AA), 1.4.6 (AAA) |
| Zoom and reflow | At 200% text size and at 320px wide (400% zoom), nothing is cut off and no sideways scrolling is needed. | 1.4.4 (AA), 1.4.10 (AA), 1.4.12 (AA) |
| Pointer targets | Click and tap targets are at least 24×24px (44×44px for AAA), or spaced so they don't overlap. | 2.5.8 (AA), 2.5.5 (AAA) |
| Motion | Animations respect the reduced-motion setting. | 2.3.3 (AAA) |