Todae

Todae

Dialog

Centered native <dialog> via showModal(). open is controlled, or defaultOpen leaves it uncontrolled. Escape and backdrop dismiss are opt-out. No sheet morph.

On this page

Enter is an opacity fade over 280ms while the panel rises into place from dialog.motion.offset (0.5rem) below; exit fades it and lets it sink back. The panel moves by layout, not a transform, so a tooltip inside stays by its trigger. @starting-style where supported; data-entered is the fallback so unsupported engines still tween. Exit stays mounted until transitionend (200ms fallback). Under reduced motion the panel and scrim only fade.

import { Button, Dialog } from '@tounsoo/todae';
import { useState } from 'react';

export default function DialogBasic() {
  const [open, setOpen] = useState(false);
  return (
    <>
      <Button.Root onClick={() => setOpen(true)}>Open dialog</Button.Root>
      <Dialog.Root open={open} onOpenChange={setOpen}>
        <Dialog.Header>
          <Dialog.Title>Invite teammates</Dialog.Title>
          <Dialog.Close />
        </Dialog.Header>
        <Dialog.Description>They get an email with a link to this project.</Dialog.Description>
        <Dialog.Body>The dialog stays centered on every viewport. It does not become a sheet.</Dialog.Body>
        <Dialog.Footer>
          <Dialog.Close>Cancel</Dialog.Close>
          <Button.Root onClick={() => setOpen(false)}>Send invites</Button.Root>
        </Dialog.Footer>
      </Dialog.Root>
    </>
  );
}

Invite teammates

They get an email with a link to this project.

The dialog stays centered on every viewport. It does not become a sheet.

Dark compact, backdrop dismiss off.

import { Button, DensityProvider, Dialog, ThemeProvider } from '@tounsoo/todae';
import { useState } from 'react';

export default function DialogThemed() {
  const [open, setOpen] = useState(false);
  return (
    <ThemeProvider appearance="dark">
      <DensityProvider density="compact">
        <Button.Root onClick={() => setOpen(true)}>Dark compact</Button.Root>
        <Dialog.Root open={open} onOpenChange={setOpen} dismissOnBackdrop={false}>
          <Dialog.Title>Pinned scrim</Dialog.Title>
          <Dialog.Description>Backdrop clicks do nothing. Escape still closes.</Dialog.Description>
          <Dialog.Footer>
            <Dialog.Close>Close</Dialog.Close>
          </Dialog.Footer>
        </Dialog.Root>
      </DensityProvider>
    </ThemeProvider>
  );
}

Pinned scrim

Backdrop clicks do nothing. Escape still closes.

Overlays inherit tokens from ThemeProvider / DensityProvider / MotionProvider. They do not inherit extra classes on the provider node.

iOS may still rubber-band the page behind an open overlay. Chrome may ignore cancel.preventDefault() without user activation. State still resyncs from the native close event.

Opening a dialog leaves the page width unchanged. A page with no scrollbar stays that width, and a page whose scrollbar already takes space keeps that space.

Alert dialog

role="alertdialog" marks a dialog that interrupts to ask for a response, such as confirming a destructive action. A backdrop click no longer closes it; Escape still does. On open, focus goes to its first close part, so a Dialog.Close that says Cancel takes it without an initialFocusRef. It needs a message: render Dialog.Description or pass aria-describedby, or it logs an error in development. Leave out the header close: Cancel is the way out.

Deleting removes the button that opened the dialog, so restoreFocusRef names where focus goes after the exit. Cancel leaves the opener in place, and focus goes back to it. What counts as destructive, and the wording, are the product's.

import { Button, Dialog } from '@tounsoo/todae';
import { useRef, useState } from 'react';

export default function DialogAlert() {
  const [open, setOpen] = useState(false);
  const [deleted, setDeleted] = useState(false);
  const restoreRef = useRef<HTMLButtonElement>(null);
  return (
    <>
      {deleted ? (
        <Button.Root ref={restoreRef} variant="secondary" onClick={() => setDeleted(false)}>
          Restore report.pdf
        </Button.Root>
      ) : (
        <Button.Root onClick={() => setOpen(true)}>Delete report.pdf</Button.Root>
      )}
      <Dialog.Root role="alertdialog" open={open} onOpenChange={setOpen} restoreFocusRef={restoreRef}>
        <Dialog.Title>Delete report.pdf?</Dialog.Title>
        <Dialog.Description>The file is removed for everyone in this project.</Dialog.Description>
        <Dialog.Footer>
          <Dialog.Close>Cancel</Dialog.Close>
          <Button.Root
            onClick={() => {
              setDeleted(true);
              setOpen(false);
            }}
          >
            Delete
          </Button.Root>
        </Dialog.Footer>
      </Dialog.Root>
    </>
  );
}

Delete report.pdf?

The file is removed for everyone in this project.

Icons

Dialog.Close with no content shows the close glyph, a cross unless a GlyphsProvider sets one, and is named by the close string. To use a different icon for one close, put it in a Button.Icon.

import { X } from 'lucide-react';

<Dialog.Close>
  <Button.Icon>
    <X />
  </Button.Icon>
</Dialog.Close>

Accessible composition

Todae handles: Dialog.Root opens a native <dialog> with showModal(), so the browser gives it the dialog role, puts it in the top layer and makes the page behind it inert. Dialog.Title sets the dialog's aria-labelledby and Dialog.Description its aria-describedby; an aria-labelledby or aria-describedby you pass to the root wins. On open, focus goes to initialFocusRef, else, with role="alertdialog", the first tabbable Dialog.Close, else the first tabbable element, else the dialog itself. After the exit, focus goes to restoreFocusRef, else back to what had focus before it opened. Dialog.Close falls back to the close string ("Close") when neither its own text nor an aria-label names it, such as an icon.

You supply: A name: render Dialog.Title, or pass aria-label or aria-labelledby to Dialog.Root. In development, an open dialog with none logs an error. A button that closes it, such as Dialog.Close, above all when dismissOnEscape is false. A restoreFocusRef when the opener is gone by the time it closes, such as a menu item or a Toast.Action. An initialFocusRef when the task does not start at the first control, or when an alert dialog's safe button is not a Dialog.Close. A description for an alert dialog.

import type { ReactNode } from 'react';
import { Dialog, type DialogRootProps } from '@tounsoo/todae';

type ConfirmDialogProps = Omit<DialogRootProps, 'children'> & {
  title: string;
  children: ReactNode;
};

export function ConfirmDialog({ title, children, ...rest }: ConfirmDialogProps) {
  return (
    <Dialog.Root {...rest}>
      <Dialog.Title>{title}</Dialog.Title>
      <Dialog.Body>{children}</Dialog.Body>
      <Dialog.Footer>
        <Dialog.Close>Cancel</Dialog.Close>
      </Dialog.Footer>
    </Dialog.Root>
  );
}

Watch for: A wrapper that renders its own heading instead of Dialog.Title leaves the dialog unnamed unless it passes aria-labelledby. A wrapper that drops the rest props loses aria-*, ref and the focus refs. Dialog.Title always renders an h2, so start headings inside the dialog at h3. Do not render the dialog inside a hidden container such as a closed popover or tab panel: the page goes inert and nothing shows. In an alert dialog, every Dialog.Close counts as a safe choice, so keep the destructive action a plain Button.Root; a disabled Cancel leaves focus to the first tabbable element, so pass initialFocusRef then.

When to use

When: Centered modal task or confirm (native <dialog>, controlled open or uncontrolled defaultOpen). Name via Dialog.Title or aria-label. Escape + backdrop dismiss by default. role="alertdialog" for a confirm that interrupts to ask for a response, such as before a destructive action.

When not: Mobile picker from the bottom → BottomSheet. Full-height side panel → Drawer. Non-modal anchored panel → Popover. Short hint → Tooltip. Needs to become a sheet on mobile (Dialog never morphs).

Props

Dialog.Root props
NameTypeDescription
openboolean

Controlled open state. Leave it out for an uncontrolled dialog.

defaultOpenboolean

Shows the dialog on mount when uncontrolled. There is no trigger part, so once closed it opens again only on remount.

Default: false

onOpenChange(open: boolean, reason: DialogCloseReason) => void

Dismiss requests. When controlled, staying open keeps the dialog open. Reasons: escape, backdrop, close-button, form (a <form method="dialog"> submit; the submitter value is on the element returnValue), native (something else closed the element).

role'dialog' | 'alertdialog'

alertdialog marks a dialog that interrupts to ask for a response, such as confirming a destructive action. A backdrop click then no longer closes it, and on open focus goes to its first close part, such as a Dialog.Close that says Cancel. It needs a Dialog.Description or an aria-describedby for the message.

Default: 'dialog'

dismissOnEscapeboolean

Escape calls onOpenChange(false, "escape").

Default: true

dismissOnBackdropboolean

Scrim click calls onOpenChange(false, "backdrop").

Default: true, or false when role is alertdialog

aria-labelstring

Name when Dialog.Title is omitted.

Dialog.Title props
NameTypeDescription
childrenReact.ReactNode

Title text.

Dialog.Description props
NameTypeDescription
childrenReact.ReactNode

Description text.

Dialog.Close props
NameTypeDescription
aria-labelstring

Accessible name. Text of two or more letters or digits, such as "Cancel", names the button by itself. Otherwise the close string from StringsProvider names it, Close in English.

Default: "Close" when its text, not counting aria-hidden or SVG parts, has fewer than two letters or digits

onClickReact.MouseEventHandler<HTMLButtonElement>

Runs before the close request. Call event.preventDefault() to keep the dialog open.