# Dialog

- **[Atoms](/components/)**

A modal window that traps focus, closes on Escape and gives focus back. Built on Radix Dialog.

## When to use

- **Use for**: A short task or a decision that needs full attention: rename, confirm, delete.
- **Not for**: Long forms or reading. Use a page. Not for hints. Use a Popover.
- **Pairs with**: Button, Field, DialogClose.

## Preview

dialog.tsx

```
import { Theme } from '@/components/klarhimmel/theme/style';
import { Button } from '@/components/klarhimmel/ui/button';
import { Dialog, DialogClose } from '@/components/klarhimmel/ui/dialog';

export default function Example() {
  return (
    <Theme theme="brand">
      <Dialog
        title="Rename the room"
        trigger={<Button>Rename</Button>}
        footer={<DialogClose><Button>Done</Button></DialogClose>}
      >
        …
      </Dialog>
    </Theme>
  );
}
```

## Import

- **CLI**

  ```
  npx shadcn add @klarhimmel/dialog @klarhimmel/button
  ```
- **Import**

  ```
  import { Dialog, DialogClose } from '@/components/klarhimmel/ui/dialog';
  ```

- **Files**: dialog.tsx, button.tsx, in components/klarhimmel/ui/. With it: editing, icon-button, theme, utils. npm: lucide-react, radix-ui.
- **Render in**: Render it inside <Theme>, once near the root of your app. Tokens and fonts are scoped to it, so outside it the component is unstyled.
- **Limit**: Overlays render into the Theme root with position: fixed. A transform on an ancestor re-parents fixed (a dialog centres in the wrapper) and overflow: hidden clips menus. Render <Theme> at the app root, outside any transformed wrapper.

## Examples

- **Destructive**

  ```
  import { useState } from 'react';
  import { Button } from '@/components/klarhimmel/ui/button';
  import { Dialog, DialogClose } from '@/components/klarhimmel/ui/dialog';
  
  export default function DialogDestructive() {
    const [open, setOpen] = useState(false);
    const [done, setDone] = useState(false);
    return (
      <div className="flex flex-col items-center gap-3">
        <Button variant="secondary" onClick={() => setOpen(true)}>Delete the Garden room</Button>
        <p role="status" className="text-kh-small text-kh-muted">{done ? 'The Garden room was deleted.' : 'Nothing deleted yet.'}</p>
        <Dialog
          alert
          open={open}
          onOpenChange={setOpen}
          title="Delete the Garden room?"
          description="Its 14 upcoming bookings are cancelled and the guests are told. This cannot be undone."
          footer={
            <>
              <DialogClose><Button variant="secondary">Keep the room</Button></DialogClose>
              <Button onClick={() => { setDone(true); setOpen(false); }}>Delete room</Button>
            </>
          }
        />
      </div>
    );
  }
  ```
- **With a validation error**

  ```
  import { useState } from 'react';
  import { Button } from '@/components/klarhimmel/ui/button';
  import { Dialog } from '@/components/klarhimmel/ui/dialog';
  import { Field, Input } from '@/components/klarhimmel/ui/input';
  
  export default function DialogFormError() {
    const [open, setOpen] = useState(false);
    const [name, setName] = useState('');
    const [error, setError] = useState('');
    const [saved, setSaved] = useState('');
    const submit = () => {
      if (!name.trim()) { setError('Give the room a name.'); return; }
      setSaved(name.trim());
      setOpen(false);
    };
    return (
      <div className="flex flex-col items-center gap-3">
        <Button onClick={() => { setName(''); setError(''); setOpen(true); }}>Name the room</Button>
        <p role="status" className="text-kh-small text-kh-muted">{saved ? `Saved as ${saved}.` : 'No name saved yet.'}</p>
        <Dialog
          open={open}
          onOpenChange={setOpen}
          title="Name the room"
          description="Leave the name empty and save to see the error. The dialog stays open until the form is valid."
          footer={
            <>
              <Button variant="secondary" onClick={() => setOpen(false)}>Cancel</Button>
              <Button onClick={submit}>Save name</Button>
            </>
          }
        >
          <Field label="Room name" error={error}>
            {(p) => <Input {...p} value={name} onChange={(e) => { setName(e.target.value); setError(''); }} />}
          </Field>
        </Dialog>
      </div>
    );
  }
  ```
- **Busy: cannot be dismissed while saving**

  ```
  import { useState } from 'react';
  import { Button } from '@/components/klarhimmel/ui/button';
  import { Dialog } from '@/components/klarhimmel/ui/dialog';
  
  export default function DialogSaving() {
    const [open, setOpen] = useState(false);
    const [saving, setSaving] = useState(false);
    const [done, setDone] = useState(false);
    const save = () => {
      setSaving(true);
      window.setTimeout(() => { setSaving(false); setDone(true); setOpen(false); }, 1200);
    };
    return (
      <div className="flex flex-col items-center gap-3">
        <Button variant="secondary" onClick={() => setOpen(true)}>Publish the season prices</Button>
        <p role="status" className="text-kh-small text-kh-muted">{done ? 'The season prices are live.' : 'Not published yet.'}</p>
        <Dialog
          alert
          open={open}
          // While the request runs, a close request (Escape, the X) is ignored: the dialog cannot be dismissed half-way.
          onOpenChange={(next) => { if (!saving) setOpen(next); }}
          title="Publish the season prices?"
          description="Guests see the new prices straight away."
          footer={
            <>
              <Button variant="secondary" disabled={saving} onClick={() => setOpen(false)}>Not yet</Button>
              <Button disabled={saving} onClick={save}>{saving ? 'Publishing…' : 'Publish'}</Button>
            </>
          }
        />
      </div>
    );
  }
  ```
- **Scroll**

  ```
  import { Button } from '@/components/klarhimmel/ui/button';
  import { Dialog, DialogClose } from '@/components/klarhimmel/ui/dialog';
  
  export default function DialogScroll() {
    return (
      <div className="flex items-start justify-center">
        <Dialog
          title="House rules"
          description="Long content scrolls inside the dialog; the title and buttons stay in view."
          trigger={<Button variant="secondary">Read the house rules</Button>}
          footer={<DialogClose><Button>I understand</Button></DialogClose>}
        >
          {Array.from({ length: 10 }, (_, i) => (
            <p key={i}>
              Rule {i + 1}. Quiet hours start at 22:00 in the garden and in the corridors, so that every guest at Exempelhotellet can rest.
            </p>
          ))}
        </Dialog>
      </div>
    );
  }
  ```

## Anatomy

The parts and how they nest.

```
Dialog
├─ trigger (your Button)
├─ title + description
├─ children (the body, scrolls)
└─ footer
   └─ DialogClose (wraps a Button)
```

- **Dialog**: Owns open state and the overlay. Controlled with open, or opened by trigger.
- **title / description**: Props, not parts. The title names the dialog for assistive tech.
- **footer**: Buttons pinned under the scrolling body.
- **DialogClose**: Wraps a footer Button so it closes the dialog.

## Props

- **Dialog.open**: Controlled or uncontrolled state.
- **Dialog.defaultOpen**: Controlled or uncontrolled state.
- **Dialog.onOpenChange**: Controlled or uncontrolled state.
- **Dialog.trigger**: The element that opens it (a Button). Focus returns to it on close. Omit for a controlled dialog.
- **Dialog.title**: Required: names the dialog.
- **Dialog.description**: A line under the title: text, or a small row of parts (a key with its copy button).
- **Dialog.children**
- **Dialog.footer**: Buttons, pinned under the body.
- **Dialog.alert**: An alert dialog (role=alertdialog) for a decision that cannot be missed, like a delete: clicking outside does not close it, Escape still does.
- **Dialog.closeLabel**: The close button's name for screen readers, in the page's language ('Stäng'). Default 'Close'.
- **Dialog.className**: Extra classes, merged after the component's own.
- **DialogClose.children**

## Accessibility

- Tab / Shift+Tab · Cycles inside the dialog; the page behind is unreachable
- Escape · Closes and returns focus to the trigger
- Enter / Space · Activates the focused button

[Accessibility](/get-started/accessibility)

With alert set, a click outside does not close it; Escape still does.

[Previous](/components/checkbox)

[Next](/components/empty-state)
