Blocks Design System

Modal

A dialog on top of the page, on Base UI Dialog. It is modal: while it is open, the rest of the page is hidden from assistive technology (aria-hidden on the content outside the portal) and focus moves into the dialog and stays there; Escape closes it, and focus returns to what opened it. The name Modal is kept from June; the element has the dialog role, named by its header.

tsx
import { Modal, ModalBody, ModalClose, ModalContent, ModalFooter, ModalHeader, ModalTrigger } from '@blocks/design-system'

From a Server Component, import the parts by name; client-authored code can use Modal.Trigger, Modal.Content and the rest.

Parts

PartRole
ModalTriggerOpens the modal. Use render to make it a Button.
ModalContentBackdrop and dialog, in a portal. Shows a corner close button unless showCloseButton={false}.
ModalHeaderThe title: the dialog’s accessible name.
ModalBodyThe main text: the dialog’s accessible description.
ModalFooterActions, aligned to the end.
ModalCloseCloses the modal: an icon button, or your own element through render.

Uncontrolled

Source · modal/station-info.tsx
modal/station-info.tsx
// A Server Component: uncontrolled, with named parts. The trigger opens it,
// the corner button and Escape close it.
import { Button, Modal, ModalBody, ModalContent, ModalHeader, ModalTrigger } from '@blocks/design-system'

export default function StationInfo() {
  return (
    <Modal>
      <ModalTrigger render={<Button type="button" variant="ghost" />}>About this station</ModalTrigger>
      <ModalContent>
        <ModalHeader>Mount Hood Test Site</ModalHeader>
        <ModalBody>Elevation 5,370 ft. Reporting hourly since 1980.</ModalBody>
      </ModalContent>
    </Modal>
  )
}
Station info

Controlled, with a form

Own open with onOpenChange when the page reacts to the result. Focus the field people came to edit with initialFocus. The dialog stays mounted while it animates out, so ignore a second submit once it’s closing.

Launch checklist · renamed 0 times

Source · modal/rename-item.tsx
modal/rename-item.tsx
'use client'

import { Body, Button, Field, Modal, Stack } from '@blocks/design-system'
import { type FormEvent, useRef, useState } from 'react'

export default function RenameItem() {
  const [open, setOpen] = useState(false)
  const [name, setName] = useState('Launch checklist')
  const [renames, setRenames] = useState(0)
  const nameInput = useRef<HTMLInputElement>(null)

  function confirm(event: FormEvent<HTMLFormElement>) {
    event.preventDefault()
    // The dialog stays mounted while it animates out; a second click must not rename again.
    if (!open) return
    const next = new FormData(event.currentTarget).get('name')
    setName(String(next))
    setRenames((count) => count + 1)
    setOpen(false)
  }

  return (
    <Stack gap="sm" align="start">
      <Body margin="none" role="status">
        {name} · renamed {renames} {renames === 1 ? 'time' : 'times'}
      </Body>
      <Modal open={open} onOpenChange={setOpen}>
        <Modal.Trigger render={<Button type="button" />}>Rename item</Modal.Trigger>
        <Modal.Content initialFocus={nameInput}>
          <Modal.Header>Rename item</Modal.Header>
          <Stack as="form" gap="md" onSubmit={confirm}>
            <Modal.Body>The new name shows on the board and in notifications.</Modal.Body>
            <Field name="name">
              <Field.Label>Name</Field.Label>
              <Field.Control ref={nameInput} defaultValue={name} required />
            </Field>
            <Modal.Footer>
              <Modal.Close render={<Button type="button" variant="ghost" />}>Cancel</Modal.Close>
              <Button type="submit" variant="primary">
                Rename
              </Button>
            </Modal.Footer>
          </Stack>
        </Modal.Content>
      </Modal>
    </Stack>
  )
}
Rename an item

Themes and portals

ModalContent renders at the end of document.body. Inside a subtree with its own data-ds-theme, pass container with an element in that subtree so the dialog inherits the theme.

Source · modal/themed-container.tsx
modal/themed-container.tsx
'use client'

import { Box, Button, Modal } from '@blocks/design-system'
import { useState } from 'react'

// The panel forces a dark theme; the modal mounts inside it to inherit that.
export default function ThemedContainer() {
  const [panel, setPanel] = useState<HTMLElement | null>(null)
  return (
    <Box as="section" ref={setPanel} data-ds-theme="dark" aria-label="Dark editor" padding="lg" backgroundColor="bg" color="text">
      <Modal>
        <Modal.Trigger render={<Button type="button" />}>Open editor settings</Modal.Trigger>
        <Modal.Content container={panel}>
          <Modal.Header>Editor settings</Modal.Header>
          <Modal.Body>These settings use the editor’s dark theme.</Modal.Body>
        </Modal.Content>
      </Modal>
    </Box>
  )
}
In a dark editor

Props

Modal props
PropTypeDefaultDescription
actionsRefRefObject<DialogRootActions | null>—A ref to imperative actions. - `unmount`: Manually unmounts the dialog. Call this after any externally controlled closing animation finishes. - `close`: Closes the dialog imperatively when called.
defaultOpenbooleanfalseWhether the dialog is initially open. To render a controlled dialog, use the `open` prop instead.
defaultTriggerIdstring | null—ID of the trigger that the dialog is associated with. This is useful in conjunction with the `defaultOpen` prop to create an initially open dialog.
disablePointerDismissalbooleanfalseWhether to prevent the dialog from closing on outside presses. For non-modal dialogs, this also prevents the dialog from closing when focus moves outside of it.
handleDialogHandle<unknown>—A handle to associate the dialog with a trigger. If specified, allows external triggers to control the dialog's open state. Can be created with the Dialog.createHandle() method.
modalboolean | "trap-focus"trueDetermines if the dialog enters a modal state when open. - `true`: user interaction is limited to just the dialog: focus is trapped, document page scroll is locked, and pointer interactions on outside elements are disabled. - `false`: user interaction with the rest of the document is allowed. - `'trap-focus'`: focus is trapped inside the dialog, but document page scroll is not locked and pointer interactions outside of it remain enabled. When `modal` is `true` or `'trap-focus'`, render `<Dialog.Close>` inside `<Dialog.Popup>` so touch screen readers can escape the popup.
onOpenChange((open: boolean, eventDetails: DialogRootChangeEventDetails) => void)—Event handler called when the dialog is opened or closed.
onOpenChangeComplete((open: boolean) => void)—Event handler called after any animations complete when the dialog is opened or closed.
openboolean—Whether the dialog is currently open.
triggerIdstring | null—ID of the trigger that the dialog is associated with. This is useful in conjunction with the `open` prop to create a controlled dialog. There's no need to specify this prop when the dialog is uncontrolled (that is, when the `open` prop is not set).
ModalTrigger props
PropTypeDefaultDescription
handleDialogHandle<unknown>—A handle to associate the trigger with a dialog. Can be created with the Dialog.createHandle() method.
idstring—ID of the trigger. In addition to being forwarded to the rendered element, it is also used to specify the active trigger for the dialog in controlled mode (with the DialogRoot `triggerId` prop).
nativeButtonbooleantrueWhether the component renders a native `<button>` element when replacing it via the `render` prop. Set to `false` if the rendered element is not a button (for example, `<div>`).
payloadunknown—A payload to pass to the dialog when it is opened.
renderReactElement<unknown, string | JSXElementConstructor<any>> | ComponentRenderFn<HTMLProps, DialogTriggerState>—Allows you to replace the component's HTML element with a different tag, or compose it with another component. Accepts a `ReactElement` or a function that returns the element to render.

Also accepts styling escape hatches and a ref, and the native attributes of the element it renders.

ModalContent props
PropTypeDefaultDescription
asElementType—The element or component to render, e.g. `as={NextLink}`.
containerHTMLElement | ShadowRoot | RefObject<HTMLElement | ShadowRoot | null> | null—Where the modal mounts. Defaults to `document.body`; pass an element inside a `data-ds-theme` subtree so the modal inherits its theme.
finalFocusboolean | RefObject<HTMLElement | null> | ((closeType: InteractionType) => boolean | void | HTMLElement | null)—The element to focus when the modal closes. Defaults to the trigger.
initialFocusboolean | RefObject<HTMLElement | null> | ((openType: InteractionType) => boolean | void | HTMLElement | null)—The element to focus when the modal opens. Defaults to the first focusable element.
showCloseButtonbooleantrueShows a close button in the corner. Defaults to `true`.

Also accepts Box layout props, styling escape hatches, margin props, padding props and a ref, and the native attributes of the element it renders.

ModalHeader props
PropTypeDefaultDescription
asElementType—The element or component to render, e.g. `as={NextLink}`.

Also accepts Box layout props, styling escape hatches, margin props, padding props and a ref, and the native attributes of the element it renders.

ModalBody props
PropTypeDefaultDescription
asElementType—The element or component to render, e.g. `as={NextLink}`.

Also accepts Box layout props, styling escape hatches, margin props, padding props and a ref, and the native attributes of the element it renders.

ModalFooter props
PropTypeDefaultDescription
asElementType—The element or component to render, e.g. `as={NextLink}`.

Also accepts Box layout props, styling escape hatches, margin props, padding props and a ref, and the native attributes of the element it renders.

ModalClose props
PropTypeDefaultDescription
nativeButtonbooleantrueWhether the component renders a native `<button>` element when replacing it via the `render` prop. Set to `false` if the rendered element is not a button (for example, `<div>`).
renderReactElement<unknown, string | JSXElementConstructor<any>> | ComponentRenderFn<HTMLProps, DialogCloseState>—Allows you to replace the component's HTML element with a different tag, or compose it with another component. Accepts a `ReactElement` or a function that returns the element to render.

Also accepts styling escape hatches and a ref, and the native attributes of the element it renders.

Accessibility

  • Always render ModalHeader; it names the dialog.
  • Put the primary action last in the footer and make Cancel a ModalClose.
  • Use a modal for a focused task. Information that people need next to the page belongs in a Popover or on the page.