# Dialog

> Asks for a decision or input in a window over the page.

A dialog asks for a decision or a short input in a window over the page, which stays dimmed behind it.

## When to use

- Use it for a short form or a choice that needs the whole attention, such as renaming a release.
- Use an [Alert Dialog](https://design.sosialoka.tech/components/alert-dialog/) to confirm an action that can't be undone.
- Use a [Toast](https://design.sosialoka.tech/components/toast/) to confirm something that already happened.
- Don't put long flows in a dialog; use a page.

_Live preview of the React and Vue versions: https://design.sosialoka.tech/components/dialog/_

### Do and don't

- **Do:** Keep a dialog to one decision or a short form.
- **Don't:** Don't open a dialog on top of another dialog.

## Anatomy

1. Overlay
2. Title
3. Body
4. Actions, primary last

Title, description, optional content, then actions in the footer. The window sits on `popover` with a `border`, `radius-xl` (16px) and `shadow-lg`. The page behind it is dimmed with the `overlay` token.

## Variants

There is one style. Put the main action last in the footer.

## Props

| React | Vue | Flutter | Type | Default | Use |
| --- | --- | --- | --- | --- | --- |
| `open` / `defaultOpen` | `v-model:open` / `default-open` | `showShadDialog` | boolean | `false` | Whether it is open. |
| `onOpenChange` | `@update:open` | return value of `showShadDialog` | function | none | Runs when it opens or closes. |
| `DialogTrigger render` | `DialogTrigger as-child` | `onPressed` | element | button | The control that opens it. |

## Code

**React**
```tsx
import { useId } from "react"
import { Button } from "@/components/ui/button"
import {
  Dialog,
  DialogClose,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
  DialogTrigger,
} from "@/components/ui/dialog"
import { Field, FieldLabel } from "@/components/ui/field"
import { Input } from "@/components/ui/input"

export default function DialogDemo() {
  const id = useId()
  return (
    <Dialog>
      <DialogTrigger render={<Button variant="outline" />}>Ubah judul</DialogTrigger>
      <DialogContent>
        <DialogHeader>
          <DialogTitle>Ubah judul rilis</DialogTitle>
          <DialogDescription>Judul baru tampil di semua toko musik setelah dikirim ulang.</DialogDescription>
        </DialogHeader>
        <Field>
          <FieldLabel htmlFor={`${id}-title`}>Judul rilis</FieldLabel>
          <Input id={`${id}-title`} defaultValue="Senja di Kota" />
        </Field>
        <DialogFooter>
          <DialogClose render={<Button variant="outline" />}>Batal</DialogClose>
          <DialogClose render={<Button />}>Simpan</DialogClose>
        </DialogFooter>
      </DialogContent>
    </Dialog>
  )
}
```

**Vue**
```vue
<script setup lang="ts">
import { useId } from "vue"
import { Button } from "@/components/ui/button"
import {
  Dialog,
  DialogClose,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
  DialogTrigger,
} from "@/components/ui/dialog"
import { Field, FieldLabel } from "@/components/ui/field"
import { Input } from "@/components/ui/input"

const id = useId()
</script>

<template>
  <Dialog>
    <DialogTrigger as-child>
      <Button variant="outline">Ubah judul</Button>
    </DialogTrigger>
    <DialogContent>
      <DialogHeader>
        <DialogTitle>Ubah judul rilis</DialogTitle>
        <DialogDescription>Judul baru tampil di semua toko musik setelah dikirim ulang.</DialogDescription>
      </DialogHeader>
      <Field>
        <FieldLabel :for="`${id}-title`">Judul rilis</FieldLabel>
        <Input :id="`${id}-title`" default-value="Senja di Kota" />
      </Field>
      <DialogFooter>
        <DialogClose as-child><Button variant="outline">Batal</Button></DialogClose>
        <DialogClose as-child><Button>Simpan</Button></DialogClose>
      </DialogFooter>
    </DialogContent>
  </Dialog>
</template>
```

**Flutter**
```dart
import 'package:flutter/widgets.dart';
import 'package:shadcn_ui/shadcn_ui.dart';

class DialogDemo extends StatelessWidget {
  const DialogDemo({super.key});

  @override
  Widget build(BuildContext context) {
    return ShadButton.outline(
      child: const Text('Ubah judul'),
      onPressed: () => showShadDialog(
        context: context,
        builder: (context) => ShadDialog(
          title: const Text('Ubah judul rilis'),
          description: const Text('Judul baru tampil di semua toko musik setelah dikirim ulang.'),
          actions: [
            ShadButton.outline(child: const Text('Batal'), onPressed: () => Navigator.of(context).pop()),
            ShadButton(child: const Text('Simpan'), onPressed: () => Navigator.of(context).pop()),
          ],
          child: ShadInputFormField(id: 'release-title', label: const Text('Judul rilis'), initialValue: 'Senja di Kota'),
        ),
      ),
    );
  }
}
```

## Install

**React**
```bash
npx shadcn@latest add https://design.sosialoka.tech/r/react/dialog.json
```

**Vue**
```bash
npx shadcn-vue@latest add https://design.sosialoka.tech/r/vue/dialog.json
```

The button it installs is the Sosialoka button.

## Accessibility

- Renders `role="dialog"` with the title as its name; focus moves into the dialog and returns to the trigger when it closes.
- Escape closes it, and focus stays inside while it is open.
