# Dialog

A modal window for a focused task, with a title, body and footer actions, in small to full-screen sizes.

- Family: overlay
- Import: `import { Dialog, DialogTrigger } from "@senvenli/react/overlay";`
- Page: https://design.senvenli.com/docs/components/dialog
- JSON: https://design.senvenli.com/docs/components/dialog.json

## Example: A focused task

A title, a few fields, one primary action; Cancel closes.

```tsx
import { Button } from "@senvenli/react/action";
import { Input } from "@senvenli/react/form";
import { Stack } from "@senvenli/react/layout";
import { Dialog, DialogTrigger } from "@senvenli/react/overlay";
import { useState } from "react";

export default function Example() {
  const [isOpen, setIsOpen] = useState(false);
  return (
    <DialogTrigger isOpen={isOpen} onOpenChange={setIsOpen}>
      <Button>Edit profile</Button>
      <Dialog
        title="Edit profile"
        description="Your name is shown to your team."
        primaryAction={{ label: "Save changes", onPress: () => setIsOpen(false) }}
      >
        <Stack space="space-200">
          <Input label="Full name" defaultValue="Ada Lovelace" />
          <Input label="Email" type="email" defaultValue="ada@example.com" />
        </Stack>
      </Dialog>
    </DialogTrigger>
  );
}
```

## Examples

### Submitting a form

The primary action submits the form by its id, so Enter in a field works too.

```tsx
import { Button } from "@senvenli/react/action";
import { Form, Input } from "@senvenli/react/form";
import { Stack } from "@senvenli/react/layout";
import { Dialog, DialogTrigger, toast } from "@senvenli/react/overlay";
import { Select } from "@senvenli/react/selection";
import { useId, useState } from "react";

export default function Example() {
  const [isOpen, setIsOpen] = useState(false);
  const formId = useId();
  return (
    <DialogTrigger isOpen={isOpen} onOpenChange={setIsOpen}>
      <Button iconBefore="user-plus">Invite member</Button>
      <Dialog title="Invite a team member" primaryAction={{ label: "Send invite", form: formId }}>
        <Form
          id={formId}
          onSubmit={(event) => {
            event.preventDefault();
            const email = new FormData(event.currentTarget).get("email");
            setIsOpen(false);
            toast.success("Invite sent", { description: String(email) });
          }}
        >
          <Stack space="space-200">
            <Input label="Email" name="email" type="email" isRequired />
            <Select
              label="Role"
              name="role"
              defaultSelectedKey="member"
              items={[
                { id: "member", label: "Member" },
                { id: "admin", label: "Admin" },
              ]}
            />
          </Stack>
        </Form>
      </Dialog>
    </DialogTrigger>
  );
}
```

### A custom footer

`footer` in place of the two actions: a checkbox at the start, buttons at the end; `useDialogClose()` closes.

```tsx
import { Button } from "@senvenli/react/action";
import { Text } from "@senvenli/react/display";
import { Dialog, DialogTrigger, useDialogClose } from "@senvenli/react/overlay";
import { Checkbox } from "@senvenli/react/selection";

function Footer() {
  const close = useDialogClose();
  return (
    <div className="flex w-full flex-wrap items-center gap-ds-100">
      <div className="me-auto">
        <Checkbox>Don't show this again</Checkbox>
      </div>
      <Button variant="subtle" onPress={close}>
        Later
      </Button>
      <Button variant="primary" onPress={close}>
        Take the tour
      </Button>
    </div>
  );
}

export default function Example() {
  return (
    <DialogTrigger>
      <Button>What's new</Button>
      <Dialog title="What's new" description="Three things changed this month." footer={<Footer />}>
        <Text>Saved views, a faster search and dark mode for everyone.</Text>
      </Dialog>
    </DialogTrigger>
  );
}
```

### Sizes

Small by default; medium, large and xlarge for wider content.

```tsx
import { Button } from "@senvenli/react/action";
import { Text } from "@senvenli/react/display";
import { Stack } from "@senvenli/react/layout";
import { Dialog, DialogTrigger } from "@senvenli/react/overlay";

const SIZES = [
  { size: "small", width: "440px" },
  { size: "medium", width: "600px" },
  { size: "large", width: "800px" },
  { size: "xlarge", width: "1152px" },
] as const;

export default function Example() {
  return (
    <Stack direction="horizontal" space="space-100">
      {SIZES.map(({ size, width }) => (
        <DialogTrigger key={size}>
          <Button>{size}</Button>
          <Dialog size={size} title="Order ORD-1040" secondaryAction={{ label: "Close" }}>
            <Text>
              This dialog is {size}: up to {width} wide, or the viewport less its margins.
            </Text>
          </Dialog>
        </DialogTrigger>
      ))}
    </Stack>
  );
}
```

### Full screen

For a long task: the header and footer stay put, Back and Continue spread apart.

```tsx
import { Button } from "@senvenli/react/action";
import { Input } from "@senvenli/react/form";
import { Stack } from "@senvenli/react/layout";
import { Dialog, DialogTrigger } from "@senvenli/react/overlay";
import { useState } from "react";

export default function Example() {
  const [isOpen, setIsOpen] = useState(false);
  const close = () => setIsOpen(false);
  return (
    <DialogTrigger isOpen={isOpen} onOpenChange={setIsOpen}>
      <Button>Create workspace</Button>
      <Dialog
        size="fullscreen"
        footerLayout="spread"
        title="Create a workspace"
        description="Step 1 of 3 · Name and address"
        secondaryAction={{ label: "Back", iconBefore: "arrow-left", onPress: close }}
        primaryAction={{ label: "Continue", iconAfter: "arrow-right", onPress: close }}
      >
        <div className="mx-auto w-full max-w-120">
          <Stack space="space-200">
            <Input label="Workspace name" defaultValue="Analytical Engines" />
            <Input label="Web address" defaultValue="analytical-engines" description="Lowercase letters, numbers and hyphens." />
          </Stack>
        </div>
      </Dialog>
    </DialogTrigger>
  );
}
```

## Guidelines

### When to use it

- A short task that needs focus without leaving the page: edit, create, confirm details.

### Use something else when

- [drawer](https://design.senvenli.com/docs/components/drawer.md): The task needs a side panel next to the page.
- [alert-dialog](https://design.senvenli.com/docs/components/alert-dialog.md): The user must confirm or cancel one action.

### Do and avoid

- One primary action; the secondary action closes.
- Use fullscreen for long tasks like a signup, xlarge for two- or three-column forms.

## Accessibility

Keyboard:

- Esc: Closes the dialog.
- Tab: Moves focus inside the dialog only.

Built in:

- Focus moves to the first field and returns to the opener on close.
- Named by its title, described by its description.

You provide:

- A title that says what the task is.

## API reference

### Dialog

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| isOpen | `boolean` |  | no | Open without a DialogTrigger (a URL, a row click). Inside a DialogTrigger, put `onOpenChange` on the trigger instead: the trigger owns the state there. |
| defaultOpen | `boolean` |  | no |  |
| onOpenChange | `(isOpen: boolean) => void` |  | no |  |
| title | `React.ReactNode` |  | yes |  |
| description | `React.ReactNode` |  | no | One short line under the title: what the dialog is about. When given, the dialog is described by it (`aria-describedby`); without it (or with `""`, `false`, `null`) nothing is rendered. |
| children | `React.ReactNode` |  | yes |  |
| primaryAction | `DialogAction` |  | no |  |
| secondaryAction | `DialogSecondaryAction` |  | no | "Cancel" by default, closing the dialog; pass a label or a handler to change it. `isDisabled` blocks it, e.g. while the primary action runs; `iconBefore` / `iconAfter` add a decorative icon. `null` leaves it out: a full-screen dialog whose steps carry their own buttons, or a locked completion view, has no secondary action, and with no `primaryAction` either it has no footer at all (the body then takes the bottom padding, and on `fullscreen` reaches down to the home indicator). With `null` and a `danger` primary there is no safe button to focus first, so focus goes to the body's first field (as for any other dialog), or to the dialog itself when the body has none. `data-*` keys reach the button. |
| footer | `React.ReactNode` |  | no | The footer's content, in place of `primaryAction` and `secondaryAction`: a checkbox beside the buttons, three actions, a link. It sits in the same footer (spacing, `footerLayout`, the notch and home-indicator insets); close from it with `useDialogClose()`. As Drawer's `footer`. |
| isCloseButtonHidden | `boolean` |  | no | Leaves out the close button in the header, and does nothing else: Esc and a press outside still follow `isDismissable`. A locked view the user must not dismiss before the flow ends, such as a completion step, sets both: `isCloseButtonHidden` and `isDismissable={false}`. It then closes only when the app closes it (`isOpen`/`onOpenChange`, or an action whose handler does) or through the secondary action, when there is one; a locked dialog inside a DialogTrigger has no close handle, so control it with `isOpen`. Focus stays trapped inside and the title still names the dialog. Give it a way on (a `primaryAction` or a control in the body): with the close button hidden and nothing a keyboard can reach inside, the dialog warns in development (with `isDismissable` Esc still closes it, but a touch user on `fullscreen` has no press outside). |
| initialFocusRef | `RefObject<HTMLElement \| null>` |  | no | The element that takes focus when the dialog opens, in place of the DS rule (the first field, or the secondary action before a `danger` primary). Pass a ref to an element inside the dialog. Left out, or empty when the dialog opens, the rule applies as before. The ref keeps focus only if, once the dialog calls its `focus()`, focus is on that element or inside it; otherwise the rule applies after all. So a ref to an element that cannot take focus (a container without a `tabIndex`, a hidden title) falls through to the rule, and so does a ref whose own focus handler moves focus to an element outside it: the rule then moves focus again, to the safe action or the first field (with neither, focus stays where the handler put it). A handler that moves focus to an element inside the ref keeps it there. To open on a container, give it `tabIndex={-1}`; to open on another element, pass that element's ref. |
| returnFocusRef | `RefObject<HTMLElement \| null>` |  | no | Where focus goes when the dialog closes, in place of the element that opened it (the trigger): the next step's heading, or a control that stays when the trigger is gone. Read when the dialog closes, so it may point at an element mounted by then. If the app has already moved focus elsewhere on close, it stays there; if the ref is empty, focus returns to the trigger as before. |
| titleRef | `React.Ref<HTMLHeadingElement>` |  | no | A ref to the title's heading, for an app that puts focus on the title: on open (a completion view, "Password saved"), or when the dialog's content moves to its next step. Given, the heading takes `tabIndex={-1}`: focusable from code (`titleRef.current.focus()`), never a Tab stop, and a pointer press on it focuses it. It stays the dialog's heading (`role` heading, level 2) and its name (`aria-labelledby`), so a screen reader reads "heading" with the title when focus lands on it. To open on the title, pass the same ref as `initialFocusRef` (`initialFocusRef={titleRef}`): that ref comes first on open, before the first field and the safe secondary action, as for any other element. The title shows the DS focus ring only for keyboard focus (`:focus-visible`), never for a pointer user's; focus on close returns as before (`returnFocusRef`, or the trigger). Left out, the heading renders exactly as before. A hidden title (`isTitleHidden`, fullscreen) is never a focus target: it is `sr-only`, so focus on it would show no indicator. With `isTitleHidden` the ref still holds the heading, but it takes no `tabIndex` and no focus ring, so `titleRef.current.focus()` does nothing and `initialFocusRef={titleRef}` falls through to the dialog's own rule (the first field, or the safe secondary action). To land on something there, aim at a visible element instead: the first field (the default), or a visible heading or control in the body via `initialFocusRef`. |
| partData | `Partial<Record<"header" \| "title" \| "body" \| "footer" \| "close", DataAttributes>>` |  | no | `data-*` attributes for the dialog's parts: `header`, `title`, `body`, `footer` and the `close` button. The dialog itself (`role="dialog"`, `data-slot="dialog"`) takes the `data-*` props given to Dialog (`data-testid="set-password"`); the actions take theirs on `primaryAction` / `secondaryAction`. The footer's `data-layout` (`spread`) is the DS's own, like `data-slot`: `partData.footer` cannot set it. |
| size | `"fullscreen" \| OverlaySize \| "xlarge"` |  | no | `small` (default, 440px), `medium` (600px) or `large` (800px); `xlarge` (1152px) for a form in two or three columns over the page it edits, such as an order edit; or `fullscreen` for a long task that needs the whole screen, such as a signup: it fills the viewport at every width, slides up as a sheet on phones (below 640px) and fades in above. The header (title, close) and the footer (actions) stay put, clear of the notch and the home indicator, while the body scrolls between them. Below its width every size but `fullscreen` takes the viewport less 24px a side, and its body scrolls inside 100dvh less 48px. Focus, Esc and naming are the same at every size. |
| isDismissable | `boolean` |  | no | Scrim click and Esc close it (default). Off for destructive flows with typed input. A `fullscreen` dialog has no scrim, so a press outside it (a toast or banner layered above) never closes it; there this prop controls Esc only. It is independent of `isCloseButtonHidden`: a locked view sets both, `isCloseButtonHidden` and `isDismissable={false}`; a locked dialog inside a DialogTrigger has no close handle, so control it with `isOpen`. |
| footerLayout | `"end" \| "spread"` |  | no | `fullscreen` only. How the footer places its actions: `end` (default) groups them at the inline end; `spread` puts the secondary action at the inline start and the primary at the inline end ("Back" … "Continue"), mirrored in RTL. A lone primary action stays at the end; a lone secondary action (no `primaryAction`) sits at the inline start. |
| headerLeading | `React.ReactNode` |  | no | `fullscreen` only. Shown before the title in the header, such as the product's logo. The title then keeps to one line and truncates with an ellipsis (its full text is still the dialog's name); the close button keeps its place at the inline end. |
| isTitleHidden | `boolean` |  | no | `fullscreen` only. Hides the title visually, for a frame whose header shows a logo instead; the title stays in the DOM and still names the dialog (`aria-labelledby`), so `title` is still required. A description, when given, stays visible. An empty title (`""`, blank, `null`, `false`) would leave the dialog unnamed, and warns in development. |
| motion | `ComponentMotion` |  | no | `auto` (follow the device), `reduce` or `none`, for this component; the provider's setting otherwise. |

### DialogTrigger

Behavior props from React Aria:

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| children | `ReactNode` |  | yes |  |

Also takes 3 standard DOM and ARIA attributes (id, aria-label, event handlers…).

### Tokens it reads

`--ds-blanket`, `--ds-border`, `--ds-border-focused`, `--ds-easing-enter`, `--ds-easing-exit`, `--ds-radius-small`, `--ds-radius-xlarge`, `--ds-shadow-overlay`, `--ds-space-050`, `--ds-space-100`, `--ds-space-200`, `--ds-space-300`, `--ds-surface-overlay`, `--ds-text`, `--ds-text-subtle`

## Related

- [alert-dialog](https://design.senvenli.com/docs/components/alert-dialog.md)
- [drawer](https://design.senvenli.com/docs/components/drawer.md)
- [popover](https://design.senvenli.com/docs/components/popover.md)
