Components / Overlays

Dialog11

A window over the page for a focused task or a decision. Sticker throws it in and lets it wobble, Retro zooms it open like an old Mac, Comic inflates a speech bubble and Arcade boots it up like a screen. It can count down, too.

Dialog · PlaygroundLive
size
role
JSX
<DialogTrigger>
  <Button>Open dialog</Button>
  <Dialog
    title="Rename project"
    description="Everyone on the team sees the new name."
    footer={({ close }) => <Button onPress={close}>Done</Button>}
  >
    The old link keeps working.
  </Dialog>
</DialogTrigger>

Usage

TSX
import { Button, Dialog, DialogTrigger } from "plonk-ui";

export function Rename() {
  return (
    <DialogTrigger>
      <Button>Rename</Button>
      <Dialog
        title="Rename project"
        footer={({ close }) => <Button onPress={close}>Save</Button>}
      >
        …
      </Dialog>
    </DialogTrigger>
  );
}

DialogTrigger connects a button to the dialog: pressing it opens the dialog, and closing returns focus to it. children and footer can be functions that receive close, so a button inside can close the dialog after it's done.

With a form

The body scrolls when it's taller than the screen; the title and the footer stay put.

Dialog · FormLive
Name: Ada Lovelace
TSX
import { useState } from "react";
import { Button, Dialog, DialogTrigger, Input, Switch } from "plonk-ui";

export default function Form() {
  const [name, setName] = useState("Ada Lovelace");

  return (
    <div className="flex flex-col items-center gap-3">
      <DialogTrigger>
        <Button>Edit profile</Button>
        <Dialog
          title="Edit profile"
          description="Changes are visible to everyone on your team."
          footer={({ close }) => (
            <>
              <Button variant="ghost" onPress={close}>
                Cancel
              </Button>
              <Button onPress={close}>Save</Button>
            </>
          )}
        >
          <div className="flex flex-col gap-5">
            <Input
              label="Name"
              value={name}
              onChange={(event) => setName(event.target.value)}
            />
            <Switch label="Show my profile publicly" defaultChecked />
          </div>
        </Dialog>
      </DialogTrigger>
      <span className="font-bold">Name: {name}</span>
    </div>
  );
}

Alert dialog

role="alertdialog" is for decisions the user has to make, like deleting something. It has no close box and clicking outside doesn't close it; the buttons are the way out. Put autoFocus on the safe choice.

Dialog · AlertLive
TSX
import { useState } from "react";
import { Badge, Button, Dialog, DialogTrigger } from "plonk-ui";

export default function Alert() {
  const [deleted, setDeleted] = useState(false);

  if (deleted) {
    return (
      <div className="flex items-center gap-3">
        <Badge variant="danger" dot>
          Project deleted
        </Badge>
        <Button size="sm" variant="ghost" onPress={() => setDeleted(false)}>
          Undo
        </Button>
      </div>
    );
  }

  return (
    <DialogTrigger>
      <Button variant="danger">Delete project</Button>
      <Dialog
        role="alertdialog"
        size="sm"
        title="Delete this project?"
        description="“Spring launch” and its 24 pages will be deleted for good."
        footer={({ close }) => (
          <>
            <Button variant="neutral" onPress={close} autoFocus>
              Cancel
            </Button>
            <Button
              variant="danger"
              onPress={() => {
                setDeleted(true);
                close();
              }}
            >
              Delete
            </Button>
          </>
        )}
      />
    </DialogTrigger>
  );
}

Opening without a trigger

When something other than a button opens the dialog, like a timer or a response from the server, control it with isOpen and onOpenChange:

function SavedNotice({ saved }: { saved: boolean }) {
  const [open, setOpen] = useState(saved);
  return (
    <Dialog isOpen={open} onOpenChange={setOpen} title="Saved">
      Your changes are live.
    </Dialog>
  );
}

Countdown

Pass countdown with a number of seconds and the dialog counts down in big numbers. When it reaches zero, onCountdownEnd is called with close; without it, the dialog just closes. The classic use is a session that's about to time out:

Dialog · CountdownLive
Signed in
TSX
import { useState } from "react";
import { Button, Dialog, DialogTrigger } from "plonk-ui";

export default function Countdown() {
  const [status, setStatus] = useState("Signed in");

  return (
    <div className="flex flex-col items-center gap-3">
      <DialogTrigger>
        <Button variant="secondary">Simulate idle session</Button>
        <Dialog
          role="alertdialog"
          size="sm"
          title="Still there?"
          description="You've been away for a while. We'll sign you out to keep your account safe."
          countdown={20}
          onCountdownEnd={({ close }) => {
            setStatus("Signed out");
            close();
          }}
          footer={({ close }) => (
            <>
              <Button
                variant="ghost"
                onPress={() => {
                  setStatus("Signed out");
                  close();
                }}
              >
                Sign out
              </Button>
              <Button
                autoFocus
                onPress={() => {
                  setStatus("Signed in");
                  close();
                }}
              >
                Stay signed in
              </Button>
            </>
          )}
        />
      </DialogTrigger>
      <span className="font-bold">{status}</span>
    </div>
  );
}

Each personality counts in its own style: a number sticker in Sticker, an inverted box in Retro, a "BAM" burst in Comic and a giant "CONTINUE? 9" in Arcade. In the last three seconds the number turns urgent.

The visible number is hidden from screen readers, who instead hear the time left at 10 and 5 seconds. Translate both texts for your app:

TSX
<Dialog
  title="Oturumun kapanmak üzere"
  countdown={30}
  countdownLabel="Devam?"
  countdownMessage={(seconds) => `${seconds} saniye kaldı`}
/>;

A countdown is a time limit, so give people enough of it: WCAG asks that users can extend a time limit, which is what a "Stay signed in" button does. Use at least 20 seconds, and never count down on a dialog that asks for typing.

Personalities

  • Sticker: thrown in from below, lands with a wobble.
  • Retro: zoom outlines grow from the center like an old Mac, and the dialog is a window with a striped title bar.
  • Comic: a speech bubble that inflates from its tail over a dark halftone backdrop.
  • Arcade: grows in hard steps like a screen powering up, over a scanline backdrop.

With reduced motion, every dialog simply fades in, and the countdown number changes without animating.

Corner hook

Comic draws the dialog as a speech bubble by changing its corner radius. You can do the same with --plonk-dialog-radius, for every dialog or just one:

CSS
[data-plonk-personality="sticker"] .plonk-dialog .plonk-card {
  --plonk-dialog-radius: 2rem;
}

Why a variable and not a class? Tailwind utilities win over Plonk's component styles, so the dialog reads its radius from a variable that you can set anywhere.

Accessibility

  • Focus moves into the dialog when it opens, stays inside while it's open, and returns to the trigger when it closes.
  • Esc closes it, unless isKeyboardDismissDisabled is set.
  • title is the dialog's name and description its description. Without a title, pass aria-label.
  • The close box is a button named by closeLabel ("Close").
  • The page behind is hidden from screen readers and can't scroll while the dialog is open.

API

Dialog

PropTypeDefaultDescription
titleReactNode—Heading, also the dialog's accessible name. In Retro it sits in the window's title bar.
descriptionReactNode—Text under the title, linked to the dialog as its description.
footerRenderSlot—Bottom row, usually buttons. Pass a function to get close: ({ close }) => ….
size"sm" | "md" | "lg""md"Maximum width: sm for confirmations, lg for long content.
role"dialog" | "alertdialog""dialog"alertdialog for decisions the user has to make: no close box, and clicking outside doesn't close it.
isDismissablebooleanrole === "dialog"Close when clicking outside.
hideClosebooleanrole === "alertdialog"Hides the close box.
closeLabelstring"Close"Accessible name of the close box.
countdownnumber—Seconds to count down from, shown big in the dialog. Use at least 20 and offer a button that resets it.
onCountdownEnd(api: { close: () => void; }) => void—Called when the countdown reaches zero, with close. Without it, the dialog closes.
countdownLabelReactNode"Continue?"Text next to the number. Translate it for your app.
countdownMessage(seconds: number) => string`${seconds} seconds left`What screen readers hear at 10 and 5 seconds left. Translate it for your app.
aria-labelstring—Accessible name. Required when there's no title.
classNamestring—Extra classes for the dialog's card.
isOpenboolean—Controlled open state. Use with onOpenChange.
defaultOpenboolean—Open on first render, when uncontrolled.
onOpenChange(isOpen: boolean) => void—Called with true or false when it opens or closes.
isKeyboardDismissDisabledbooleanfalseStops Esc from closing it.

DialogTrigger

Wraps the trigger and the dialog. The first child is the trigger: a Button, or any React Aria pressable.

PropTypeDefaultDescription
isOpenboolean—Controlled open state. Use with onOpenChange.
defaultOpenboolean—Open on first render, when uncontrolled.
onOpenChange(isOpen: boolean) => void—Called with true or false when it opens or closes.