Skip to content
Vertical Jump Designs-uiFront-End Studio
Get Pro

v3.9 is out.

Overlays

Modal

Free20.4 KB gzipped

An 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

tsx
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.

tsx
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.

tsx
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.

tsx
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.

tsx
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

tsx
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" (or alertdialog) with aria-modal="true", named by Modal.Title and described by Modal.Description.
  • While open, everything outside is inert (plus aria-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

Modal props
PropTypeDefault
open

Whether the modal is open (controlled). Pair with onOpenChange.

boolean—
defaultOpen

Whether the modal starts open (uncontrolled).

booleanfalse
onOpenChange

Called with the next open state when the modal opens or closes.

(open: boolean) => void—
size

Width on screens 640px and wider: sm 30rem, md 40rem, lg 48rem, xl 64rem, full the whole viewport. Below 640px every size except full becomes a full-width sheet at the bottom of the screen.

"sm""md""lg""xl""full""md"
placement

Vertical position on screens 640px and wider (and on small screens with mobileLayout="card").

"center""top""center"
mobileLayout

Layout below 640px: a bottom sheet, or a card inset from the edges and positioned by placement. Ignored when size="full".

"sheet""card""sheet"
scrollBehavior

inside: the modal fits the viewport and Modal.Body scrolls. outside: the modal grows and the overlay scrolls.

"inside""outside""inside"
closeOnOverlayPress

Close when the overlay (backdrop) is pressed.

booleantrue, or false when `role="alertdialog"`
closeOnEscape

Close when Escape is pressed (topmost modal only).

booleantrue
initialFocusRef

Element to focus when the modal opens. By default focus goes to the first focusable element in Modal.Body (not the close button), then the dialog itself. For alertdialog it goes to the least destructive action: a Modal.Close in the footer.

RefObject<HTMLElement | null>—
finalFocusRef

Element to focus when the modal closes. By default focus returns to the element that was focused before opening (usually the trigger).

RefObject<HTMLElement | null>—
role

ARIA role. Use alertdialog for confirmations that need a response; it also stops overlay presses from closing the modal by default.

"dialog""alertdialog""dialog"
showClose

Show the built-in close button in the top-end corner.

booleantrue
closeLabel

Accessible name of the built-in close button. Localize it here.

string"Close"
children

Modal.Trigger and Modal.Content.

ReactNode—

Modal.Trigger

Wraps a button so pressing it opens the modal. Focus returns to it on close.

Modal.Trigger props
PropTypeDefault
childrenRequired

A single element that opens the modal, usually a Button. It receives aria-haspopup="dialog", aria-expanded, and an onClick (its own onClick runs first; call event.preventDefault() to cancel opening).

ReactElement—

Modal.Content

Modal.Content props
PropTypeDefault
overlayClassName

Class 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).

Modal.Title props
PropTypeDefault
level

Heading level, chosen to fit the document outline.

1234562
size

Visual 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.

Modal.Body props
PropTypeDefault
scrollRegionLabel

Accessible 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.

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.

Modal.Close props
PropTypeDefault
childrenRequired

A single element that closes the modal, usually a Button such as "Cancel". Its own onClick runs first; call event.preventDefault() to keep the modal open. In an alertdialog, the first Modal.Close in the footer gets initial focus as the least destructive action.

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.

ConfirmModal props
PropTypeDefault
open

Whether the dialog is open (controlled). Pair with onOpenChange.

boolean—
defaultOpen

Whether the dialog starts open (uncontrolled).

booleanfalse
onOpenChange

Called with the next open state.

(open: boolean) => void—
trigger

Optional element (usually a Button) that opens the dialog.

ReactElement—
titleRequired

Question or statement the user must respond to, e.g. "Delete this team?".

ReactNode—
description

Consequences of the action, e.g. "This can't be undone." Wired to aria-describedby.

ReactNode—
children

Extra content between the description and the actions.

ReactNode—
confirmLabel

Label of the confirm button. Name the action ("Delete team"), not "OK".

ReactNode"Confirm"
cancelLabel

Label of the cancel button.

ReactNode"Cancel"
confirmLoadingText

Text shown and announced next to the spinner while onConfirm is pending.

ReactNode—
tone

Confirm button color; danger also shows a warning icon.

"primary""danger""primary"
onConfirm

Called 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>—
onCancel

Called when the user cancels (Cancel button or Escape).

() => void—
size

Width on screens 640px and wider.

"sm""md""lg""xl""full""sm"
finalFocusRef

Element 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)

Running accessibility audit

Checking the examples above…

Lighthouse audit

Light mode

Score 100

35 passed · 0 failed · 30 not applicable · 10 to check by hand

Dark mode

Score 100

35 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

Keyboard
KeyAction
EnterSpaceOn the trigger: opens the dialog.
EscapeCloses the dialog.
TabShift+TabMoves 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

Roles and states
ElementRoleStates and properties
Modal.Contentdialog (alertdialog for ConfirmModal)aria-modal="true", aria-labelledby → title, aria-describedby → description
Triggerbuttonaria-haspopup="dialog", aria-expanded
Close buttonbuttonaria-label

Manual checks

Automated tools find roughly a third of accessibility issues. Test these in your app, with your content.

Manual accessibility checks
CheckWhat to testWCAG 2.2
KeyboardEvery 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)
FocusFocus 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 readersNames, 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)
ColorMeaning 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 reflowAt 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 targetsClick 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)
MotionAnimations respect the reduced-motion setting.2.3.3 (AAA)