# Dialog

import { Callout } from "fumadocs-ui/components/callout";
import { Accordion, Accordions } from "fumadocs-ui/components/accordion";
import { Step, Steps } from "fumadocs-ui/components/steps";
import { Tabs, Tab } from "fumadocs-ui/components/tabs";
import {
  Badge,
  Button,
  Card,
  CardContent,
  CardHeader,
  CardTitle,
  Dialog,
  DialogClose,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
  DialogTrigger,
  Input,
} from "@pycolors/ui";
import {
  Accessibility,
  CheckCircle,
  ClipboardCheck,
  FilePenLine,
  FolderPlus,
  Layers3,
  MousePointerClick,
  PanelsTopLeft,
  PencilLine,
  ShieldAlert,
  SquareStack,
  XCircle,
} from "lucide-react";
import { PreferAvoid } from "@/components/docs/prefer-avoid";
import { DocsLinks } from "@/components/docs/docs-links";
import { DocsFeatureGrid } from "@/components/docs/docs-feature-grid";
import { DocsConceptTabs } from "@/components/docs/docs-concept-tabs";

<div className="flex flex-wrap items-center gap-2">
  <Badge variant="outline">
    UI
  </Badge>

  <Badge>
    Dialog
  </Badge>
</div>

## Focused modal tasks

The **Dialog** component renders a modal overlay for focused tasks that need the user’s attention.

Use it for:

* confirmations
* short forms
* destructive decisions
* focused create or edit flows
* tasks that should not navigate away from the current page

Dialog is built on **Radix Dialog** and styled with PyColors tokens such as `bg-card`, `border-border`, and `text-card-foreground`.

<Callout title="Core idea">
  Dialog is for focused decisions or tasks. If the flow becomes long,
  navigational, or multi-step, use a Sheet or a dedicated page.
</Callout>

### Import

```tsx title="dialog-import.tsx"
import {
  Dialog,
  DialogClose,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
  DialogTrigger,
} from "@pycolors/ui";
```

### Basic usage

Use Dialog for a focused form or short task that should stay in context.

<Tabs items={["Preview", "Code"]}>
  <Tab value="Preview">
    <Preview>
      <Dialog>
        <DialogTrigger asChild>
          <Button variant="outline" size="sm">
            Open dialog
          </Button>
        </DialogTrigger>

        <DialogContent>
          <DialogHeader>
            <DialogTitle>
              Edit profile
            </DialogTitle>

            <DialogDescription>
              {"Update your account details. Changes are saved on submit."}
            </DialogDescription>
          </DialogHeader>

          <div className="mt-4 grid gap-3">
            <Input label="Name" placeholder="Patrice" />

            <Input label="Email" placeholder="patrice@pycolors.com" />
          </div>

          <DialogFooter>
            <DialogClose asChild>
              <Button variant="secondary">
                Cancel
              </Button>
            </DialogClose>

            <Button>
              Save
            </Button>
          </DialogFooter>
        </DialogContent>
      </Dialog>
    </Preview>
  </Tab>

  <Tab value="Code">
    ```tsx title="edit-profile-dialog.tsx"
    import {
      Button,
      Dialog,
      DialogClose,
      DialogContent,
      DialogDescription,
      DialogFooter,
      DialogHeader,
      DialogTitle,
      DialogTrigger,
      Input,
    } from "@pycolors/ui";

    export function EditProfileDialog() {
      return (
        <Dialog>
          <DialogTrigger asChild>
            <Button variant="outline" size="sm">
              Open dialog
            </Button>
          </DialogTrigger>

          <DialogContent>
            <DialogHeader>
              <DialogTitle>Edit profile</DialogTitle>
              <DialogDescription>
                Update your account details. Changes are saved on submit.
              </DialogDescription>
            </DialogHeader>

            <div className="mt-4 grid gap-3">
              <Input label="Name" placeholder="Patrice" />
              <Input label="Email" placeholder="patrice@pycolors.com" />
            </div>

            <DialogFooter>
              <DialogClose asChild>
                <Button variant="secondary">Cancel</Button>
              </DialogClose>
              <Button>Save</Button>
            </DialogFooter>
          </DialogContent>
        </Dialog>
      );
    }
    ```
  </Tab>
</Tabs>

### Bounded scrolling

Keep longer review content inside a viewport-bounded surface such as
`flex max-h-[80dvh] flex-col overflow-hidden`. Give the header and footer
`shrink-0`, and put `min-h-0 flex-1 overflow-y-auto` on the content region.
The title and actions then stay visible while the body scrolls.

Give the scrollable region an accessible name and `tabIndex={0}` so keyboard
users can reach and scroll it. The [long-content example](https://ui.pycolors.io/?path=/story/components-dialog--long-content)
demonstrates this layout and checks focus, scrolling and reachable actions.

## When to use Dialog

Use Dialog when the user must complete, confirm, or cancel a focused task.

<DocsFeatureGrid
  columns={3}
  tone="platform"
  items={[
  {
    title: "Confirmation",
    description: "Confirm risky or irreversible actions before they happen.",
    icon: ClipboardCheck,
  },
  {
    title: "Short form",
    description:
      "Edit a small piece of data without leaving the current page.",
    icon: FilePenLine,
  },
  {
    title: "Focused task",
    description:
      "Keep the user focused on one bounded action with clear choices.",
    icon: MousePointerClick,
  },
]}
/>

### Destructive confirmation

Use Dialog for destructive actions that require explicit user intent.

<Tabs items={["Preview", "Code"]}>
  <Tab value="Preview">
    <Preview>
      <Dialog>
        <DialogTrigger asChild>
          <Button variant="destructive" size="sm">
            Delete…
          </Button>
        </DialogTrigger>

        <DialogContent>
          <DialogHeader>
            <DialogTitle>
              Delete project
            </DialogTitle>

            <DialogDescription>
              {"This action can’t be undone. This will permanently delete the project and its data."}
            </DialogDescription>
          </DialogHeader>

          <DialogFooter>
            <DialogClose asChild>
              <Button variant="secondary">
                Cancel
              </Button>
            </DialogClose>

            <Button variant="destructive">
              Delete
            </Button>
          </DialogFooter>
        </DialogContent>
      </Dialog>
    </Preview>
  </Tab>

  <Tab value="Code">
    ```tsx title="delete-dialog.tsx"
    import {
      Button,
      Dialog,
      DialogClose,
      DialogContent,
      DialogDescription,
      DialogFooter,
      DialogHeader,
      DialogTitle,
      DialogTrigger,
    } from "@pycolors/ui";

    export function DeleteDialog() {
      return (
        <Dialog>
          <DialogTrigger asChild>
            <Button variant="destructive" size="sm">
              Delete…
            </Button>
          </DialogTrigger>

          <DialogContent>
            <DialogHeader>
              <DialogTitle>Delete project</DialogTitle>
              <DialogDescription>
                This action can’t be undone. This will permanently delete the
                project and its data.
              </DialogDescription>
            </DialogHeader>

            <DialogFooter>
              <DialogClose asChild>
                <Button variant="secondary">Cancel</Button>
              </DialogClose>
              <Button variant="destructive">Delete</Button>
            </DialogFooter>
          </DialogContent>
        </Dialog>
      );
    }
    ```
  </Tab>
</Tabs>

<Callout title="Destructive flow rule">
  The destructive action should be explicit, visually distinct, and placed after
  the safer option.
</Callout>

### Usage patterns

<Steps>
  <Step>
    #### Trigger with a semantic element

    Use `DialogTrigger asChild` with a `Button` or another semantic trigger.
  </Step>

  <Step>
    #### Provide clear context

    Every dialog should have a `DialogTitle` and a useful `DialogDescription`.
  </Step>

  <Step>
    #### Keep the content focused

    A dialog should usually contain one task, one form, or one decision.
  </Step>

  <Step>
    #### Place actions in the footer

    Keep cancel and confirm actions in `DialogFooter` for predictable placement.
  </Step>

  <Step>
    #### Close only when the task is complete or canceled

    Do not close the dialog automatically if submission fails. Show the error in context.
  </Step>
</Steps>

## Dialog vs Sheet vs Dropdown Menu

| Situation                            | Use           |
| ------------------------------------ | ------------- |
| Confirm destructive action           | Dialog        |
| Short focused form                   | Dialog        |
| Create or edit a small record        | Dialog        |
| Advanced filters                     | Sheet         |
| Side workflow or details panel       | Sheet         |
| Row actions or small contextual menu | DropdownMenu  |
| Long multi-step flow                 | Page or Sheet |

<Callout type="info" title="Decision rule">
  Dialog blocks the page for one focused task. Sheet extends the page for side
  workflows. DropdownMenu exposes quick actions.
</Callout>

## API

#### Components

| Component           | Description                                             |
| ------------------- | ------------------------------------------------------- |
| `Dialog`            | Root container                                          |
| `DialogTrigger`     | Trigger element, usually with `asChild`                 |
| `DialogContent`     | Modal surface, overlay, and close behavior              |
| `DialogHeader`      | Title and description stack helper                      |
| `DialogFooter`      | Action row helper                                       |
| `DialogTitle`       | Accessible dialog title                                 |
| `DialogDescription` | Supporting text                                         |
| `DialogClose`       | Close control, usually wrapping a Button with `asChild` |

#### Props

Dialog components extend their respective Radix Dialog props.

```ts title="dialog-props.ts"
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Root>;
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Trigger>;
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Content>;
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Close>;
```

### Accessibility

* Dialog is built on Radix Dialog primitives.
* Focus is trapped inside the dialog while open.
* Focus is restored to the trigger when the dialog closes.
* Escape should close the dialog unless the flow requires careful custom handling.
* Every dialog needs a clear `DialogTitle`.
* Use `DialogDescription` to explain the task or risk.
* Avoid nested dialogs.
* Do not use Dialog for long navigational flows.

<Callout title="Accessibility rule">
  A dialog without a clear title is not a complete dialog. The title gives
  assistive technology users the context they need.
</Callout>

## Prefer / avoid

<PreferAvoid
  prefer={[
  "one task per dialog",
  "clear title and short description",
  "semantic trigger with `asChild`",
  "cancel and confirm actions in the footer",
  "inline error feedback for failed submissions",
]}
  avoid={[
  "long multi-step workflows",
  "nested dialogs",
  "dialogs without titles",
  "closing on submit errors",
  "using Dialog for simple row actions",
]}
/>

### Product copy guidelines

Dialog copy should be direct and specific.

<DocsConceptTabs
  items={[
  {
    value: 'Create',
    title: 'Create project',
    icon: FolderPlus,
    description: (
      <>
        <p>
          Creation flows should feel lightweight, guided, and reversible.
        </p>

        <div className="mt-3 rounded-[5px] border border-border-subtle bg-muted/30 px-3 py-2 text-sm text-muted-foreground">
          Give your project a name. You can change these details later.
        </div>
      </>
    ),
  },
  {
    value: 'Edit',
    title: 'Edit profile',
    icon: PencilLine,
    description: (
      <>
        <p>
          Editing flows should communicate persistence and reduce ambiguity.
        </p>

        <div className="mt-3 rounded-[5px] border border-border-subtle bg-muted/30 px-3 py-2 text-sm text-muted-foreground">
          Update your account details. Changes are saved when you submit.
        </div>
      </>
    ),
  },
  {
    value: 'Delete',
    title: 'Delete project',
    icon: ShieldAlert,
    description: (
      <>
        <p>
          Destructive flows should clearly communicate risk and permanence.
        </p>

        <div className="mt-3 rounded-[5px] border border-destructive/20 bg-destructive/5 px-3 py-2 text-sm text-destructive">
          This action can’t be undone. This will permanently delete the project and its data.
        </div>
      </>
    ),
  },

]}
/>

## Common questions

<Accordions type="single">
  <Accordion title="Should forms always use Dialog?">
    No. Use Dialog for short, focused forms. Use a page or Sheet for long forms, multi-step flows, or workflows that need more space.
  </Accordion>

  <Accordion title="Can Dialog contain a table?">
    Usually no. Tables often need space, scanning, and row interaction. Use a page
    or Sheet instead.
  </Accordion>

  <Accordion title="Should destructive actions always use Dialog?">
    Use Dialog when the action is risky, irreversible, or needs explicit
    confirmation. For reversible actions, a toast with Undo can be enough after a
    safer interaction.
  </Accordion>

  <Accordion title="Can I hide the title visually?">
    You can visually hide a title if the design requires it, but the accessible title still needs to exist.
  </Accordion>
</Accordions>

## Related

<DocsLinks
  variant="list"
  tone="platform"
  items={[
  {
    title: "Button",
    href: "/docs/ui/button",
    description: "Actions used inside dialog triggers and footers.",
    icon: MousePointerClick,
  },
  {
    title: "Sheet",
    href: "/docs/ui/sheet",
    description:
      "Use Sheet for larger side workflows, filters, and details panels.",
    icon: PanelsTopLeft,
  },
  {
    title: "Overlays pattern",
    href: "/docs/patterns/overlays",
    description:
      "Choose between Dialog, Sheet, and DropdownMenu for product flows.",
    icon: Layers3,
  },
  {
    title: "Accessibility",
    href: "/docs/ui/accessibility",
    description:
      "Build accessible overlays with focus, keyboard, and semantic structure.",
    icon: Accessibility,
  },
]}
/>