antlitz
Menu

Dialog

A modal window over the page. Renders a native dialog, so the backdrop, moving focus in, Escape, returning focus to the trigger and making the page behind inert all come from the platform — there is no JavaScript in this component.

import { Dialog } from "antlitz";

A Button opens it with an invoker command that names the dialog's id, and another inside it closes it.

<Button command="show-modal" commandFor="rename">Rename</Button>
<Dialog id="rename" title="Rename project">…</Dialog>

API

PropTypeDefault
actionsReactNode—
descriptionReactNode—
radius"none" | "sm" | "md" | "lg" | "full"none
size"sm" | "md" | "lg"md
titleReactNode—

title and description are wired to the dialog with generated ids, so a screen reader announces the title as the dialog's name and reads the description when it opens. actions is a row at the end for the buttons. Every remaining prop reaches the dialog element.

Usage

Delete this project?

Its pages and settings are removed for everyone. This cannot be undone.

export function Basic() {
  return (
    <>
      <Button variant="danger" command="show-modal" commandFor="dialog-basic">
        Delete project
      </Button>
      <Dialog
        id="dialog-basic"
        title="Delete this project?"
        description="Its pages and settings are removed for everyone. This cannot be undone."
        actions={
          <>
            <Button
              variant="outline"
              command="close"
              commandFor="dialog-basic"
              value="cancel"
            >
              Cancel
            </Button>
            <Button
              variant="danger"
              command="close"
              commandFor="dialog-basic"
              value="delete"
            >
              Delete
            </Button>
          </>
        }
      />
    </>
  );
}

Focus moves to the first control in the dialog when it opens and back to the button that opened it when it closes. The page behind cannot be clicked or tabbed into while it is open.

command="close" closes the dialog and leaves the button's value in the dialog's returnValue, so a script listening for close can tell Delete from Cancel.

Forms

Rename project

Shown in the sidebar and in links.

export function Form() {
  return (
    <>
      <Button variant="outline" command="show-modal" commandFor="dialog-form">
        Rename project
      </Button>
      <Dialog
        id="dialog-form"
        title="Rename project"
        actions={
          <>
            <Button variant="outline" command="close" commandFor="dialog-form">
              Cancel
            </Button>
            <Button type="submit" form="dialog-form-fields" value="save">
              Save
            </Button>
          </>
        }
      >
        <form id="dialog-form-fields" method="dialog">
          <Field label="Name" description="Shown in the sidebar and in links.">
            <Input name="name" defaultValue="antlitz" required />
          </Field>
        </form>
      </Dialog>
    </>
  );
}

A form with method="dialog" closes the dialog when it is submitted, after the browser's own validation passes — an empty required field keeps it open. The Save button sits in actions, outside the form, and reaches it through the native form attribute.

Dismissing

Keyboard shortcuts

Click anywhere outside this dialog, or press Escape, to close it.

export function Dismiss() {
  return (
    <>
      <Button
        variant="outline"
        command="show-modal"
        commandFor="dialog-dismiss"
      >
        Open
      </Button>
      <Dialog
        id="dialog-dismiss"
        closedby="any"
        title="Keyboard shortcuts"
        description="Click anywhere outside this dialog, or press Escape, to close it."
      />
    </>
  );
}

Escape always closes a dialog. closedby="any" lets a click on the backdrop close it too, which suits dialogs that only show something. Leave it off where closing by accident would lose the reader's work.

Sizes

Size sm

Width and padding grow together.

Size md

Width and padding grow together.

Size lg

Width and padding grow together.

export function Sizes() {
  return SIZES.map((size) => (
    <div key={size}>
      <Button
        variant="outline"
        command="show-modal"
        commandFor={`dialog-size-${size}`}
      >
        {size}
      </Button>
      <Dialog
        id={`dialog-size-${size}`}
        size={size}
        closedby="any"
        title={`Size ${size}`}
        description="Width and padding grow together."
      />
    </div>
  ));
}

Size sets the width and the padding together. Every size keeps a 16px margin on each side, so on a phone the dialog fills the screen without touching its edges.

Radius

Radius none

Radius sm

Radius md

Radius lg

Radius full

export function Radius() {
  return RADII.map((radius) => (
    <div key={radius}>
      <Button
        variant="outline"
        radius={radius}
        command="show-modal"
        commandFor={`dialog-radius-${radius}`}
      >
        {radius}
      </Button>
      <Dialog
        id={`dialog-radius-${radius}`}
        radius={radius}
        closedby="any"
        title={`Radius ${radius}`}
      />
    </div>
  ));
}

Opening from script

Where invoker commands are missing, or when something other than a button opens the dialog, call showModal() on it through a ref.

dialogRef.current?.showModal();

Scrolling

A modal dialog makes the page inert, but the page still scrolls under it. One rule in the global stylesheet holds it still while any modal dialog is open — this site uses it.

html:has(dialog:modal) {
  overflow: hidden;
}

Dialog leaves it to you because it reaches outside the component, to the whole page.

Not a popover

A Popover floats beside its trigger and leaves the page usable. A dialog takes over until it is dealt with. Reach for a dialog when the reader has to answer before going on.