Components

Dialog

A modal window over a dimmed page, for a short form, a long read or a decision. Built on ui-sdk’s Dialog and Alert Dialog.

Usage

import {
  Dialog,
  DialogBody,
  DialogClose,
  DialogCloseButton,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
  DialogTrigger,
} from 'nyctoui/dialog'

<Dialog>
  <DialogTrigger render={<Button />}>Rename project</DialogTrigger>
  <DialogContent size="sm">
    <DialogHeader>
      <DialogTitle>Rename project</DialogTitle>
      <DialogCloseButton />
    </DialogHeader>
    <DialogBody>…</DialogBody>
    <DialogFooter>
      <DialogClose render={<Button />}>Cancel</DialogClose>
      <Button variant="primary">Save</Button>
    </DialogFooter>
  </DialogContent>
</Dialog>

Focus moves into the dialog when it opens and back to the trigger when it closes. Esc and a click on the backdrop close it. The dialog opens in the upper part of the screen, so dialogs of any height start at the same place.

Every dialog needs a DialogTitle, which names it for screen readers, and a way to close it from inside: a DialogCloseButton or a Cancel button.

Good for

  • Confirming “Delete workspace” in an admin tool, with an alert dialog.
  • Creating a new project or inviting a teammate from a short form.
  • Reading terms or release notes without leaving the page.
  • Editing one record’s details from a table in a CRM.

Anatomy

<Dialog>
  <DialogTrigger />
  <DialogContent>
    <DialogHeader>
      <DialogTitle />
      <DialogCloseButton />
      <DialogDescription />
    </DialogHeader>
    <DialogBody />
    <DialogFooter>
      <DialogClose />
    </DialogFooter>
  </DialogContent>
</Dialog>

Examples

Confirmation

AlertDialog asks for a decision. It is announced as an alert and ignores clicks on the backdrop, so the choice is made on purpose. It takes the same content parts. Skip the close button and the body: a title that asks the question, a line on what happens next, and two buttons.

Form

Wrap the parts in a <form> so Enter submits, and close the dialog from onSubmit. A select inside opens above the dialog.

Long content

When the content is taller than the screen, only the body scrolls; the header and the actions stay in view.

Sizes

size sets the widest the dialog gets. On a narrow screen it fills the width less a 16px gutter.

Opened from a menu

A dialog doesn’t need a trigger. Control it with open and set that from a menu item. Focus returns to the menu’s button on close.

Nested

A dialog opened from inside another stacks on top of it, and the one behind steps back a little.

Best practices

Do
  • Give every dialog a DialogTitle that says what it’s for, like “Invite teammates”.
  • Use an AlertDialog for destructive actions, and label the button with what happens: “Delete project”, not “OK”.
  • Say what will happen in the description of a confirmation, so people know the cost before they agree.
  • Set disablePointerDismissal on a form, so a stray click outside doesn’t throw away what was typed.
  • Keep the dialog open until the action finishes, with the button in its loading state, then close it.
Don’t
  • Open a dialog for a message that could be a toast or a line on the page.
  • Stack dialogs. Turn the flow into steps inside one dialog; an alert dialog confirming a destructive step is the one exception.
  • Grow a dialog into a whole page of content. If it needs to scroll a lot, give it its own page.

API reference

Dialog

Holds the open state. Renders no element.

PropTypeDefault
defaultOpen
Starts open. For uncontrolled use.
booleanfalse
open
Whether the dialog is open. For controlled use.
boolean—
onOpenChange
Called when the dialog opens or closes.
(open: boolean, details) => void—
onOpenChangeComplete
Called once the open or close animation has finished.
(open: boolean) => void—
modal
Traps focus, locks page scroll and blocks the page. trap-focus keeps only the focus trap.
boolean | 'trap-focus'true
disablePointerDismissal
Keeps the dialog open on a click outside.
booleanfalse

AlertDialog

A Dialog announced as an alert that ignores outside clicks. Renders no element.

PropTypeDefault
defaultOpen
Starts open. For uncontrolled use.
booleanfalse
open
Whether the dialog is open. For controlled use.
boolean—
onOpenChange
Called when the dialog opens or closes.
(open: boolean, details) => void—
onOpenChangeComplete
Called once the open or close animation has finished.
(open: boolean) => void—
modal
Traps focus, locks page scroll and blocks the page. trap-focus keeps only the focus trap.
boolean | 'trap-focus'true

DialogTrigger / AlertDialogTrigger

The button that opens the dialog. Renders a <button>.

PropTypeDefault
render
Replaces the rendered element, e.g. render={<Button />}.
ReactElement | function—

DialogContent

The window, with the backdrop behind it. Renders a <div> in the top layer.

PropTypeDefault
size
Maximum width: 440, 560 or 800px.
'sm' | 'md' | 'lg''md'
initialFocus
The element to focus on open. Defaults to the first focusable one.
boolean | RefObject | function—
finalFocus
The element to focus on close. Defaults to the trigger.
boolean | RefObject | function—
sx
StyleX styles merged after the built-in ones.
StyleXStyles—

DialogHeader

Lays out the title, description and close button. Renders a <div>.

PropTypeDefault
sx
StyleX styles merged after the built-in ones.
StyleXStyles—

DialogTitle

Labels the dialog. Renders an <h2>.

PropTypeDefault
render
Replaces the rendered element, e.g. render={<Button />}.
ReactElement | function—
sx
StyleX styles merged after the built-in ones.
StyleXStyles—

DialogDescription

Describes the dialog to assistive tech. Renders a <p>.

PropTypeDefault
render
Replaces the rendered element, e.g. render={<Button />}.
ReactElement | function—
sx
StyleX styles merged after the built-in ones.
StyleXStyles—

DialogBody

The scrolling content between hairlines. Renders a <div>.

PropTypeDefault
sx
StyleX styles merged after the built-in ones.
StyleXStyles—

DialogFooter

Actions aligned to the end. Renders a <div>.

PropTypeDefault
sx
StyleX styles merged after the built-in ones.
StyleXStyles—

DialogClose

Closes the dialog. Renders a <button>.

PropTypeDefault
render
Replaces the rendered element, e.g. render={<Button />}.
ReactElement | function—

DialogCloseButton

A ghost icon Button with an X that closes the dialog.

PropTypeDefault
aria-label
The accessible name of the button.
string'Close'
sx
StyleX styles merged after the built-in ones.
StyleXStyles—