---
title: Dialog for Astro
description: "Build Astro modal dialogs with titles, descriptions, and actions. Explore shadcn style examples, installation, and keyboard behavior."
seo:
    title: Dialog for Astro | Shadcn & Tailwind CSS
related:
    - /components/alert-dialog
    - /components/sheet
    - /components/scroll-area
sidebar:
    label: "Dialog"
---

```astro
---
import { Button } from "@/components/ui/button"
import {
  Dialog,
  DialogClose,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
  DialogTrigger,
} from "@/components/ui/dialog"
import { Input } from "@/components/ui/input"
import { Label } from "@/components/ui/label"
---

<Dialog>
  <DialogTrigger variant="outline">Edit Profile</DialogTrigger>
  <DialogContent>
    <DialogHeader>
      <DialogTitle>Edit profile</DialogTitle>
      <DialogDescription>
        Review the name and contact details used for this project.
      </DialogDescription>
    </DialogHeader>
    <div class="grid gap-4">
      <div class="grid gap-2">
        <Label for="dialog-name">Name</Label>
        <Input id="dialog-name" value="Morgan Reed" />
      </div>
      <div class="grid gap-2">
        <Label for="dialog-username">Username</Label>
        <Input id="dialog-username" value="@morgan" />
      </div>
    </div>
    <DialogFooter>
      <DialogClose variant="outline">Cancel</DialogClose>
      <Button>Save changes</Button>
    </DialogFooter>
  </DialogContent>
</Dialog>
```

## Installation

### Command

```sh
bunx --bun shadcn@latest add @gnostic/dialog
```

### Manual

Complete the [Installation guide](/docs/installation) first. Then copy the component files below.

Install the component packages:

```sh
bun add @data-slot/dialog
```

#### src/components/ui/dialog/dialog-close.astro

```astro
---
import type { ComponentProps } from "astro/types"
import X from "@lucide/astro/icons/x"

import { cn } from "cn"
import { Button } from "../button/index.ts"

type Props = ComponentProps<typeof Button>

const { class: className, ...props } = Astro.props
---

<Button
  aria-label="Close dialog"
  class={cn("relative", className)}
  data-slot="dialog-close"
  {...props}
>
  {/* Slot allows for custom icons/components with fallback X icon */}
  <slot>
    <X />
  </slot>
</Button>
```

#### src/components/ui/dialog/dialog-content.astro

```astro
---
import type { HTMLAttributes } from "astro/types"
import XIcon from "@lucide/astro/icons/x"

import { cn } from "cn"
import { Button } from "../button/index.ts"
import DialogOverlay from "./dialog-overlay.astro"
import DialogPortal from "./dialog-portal.astro"
import type { DialogContentProps } from "./types.ts"

type Props = HTMLAttributes<"div"> & DialogContentProps

const {
  class: className,
  showCloseButton = true,
  style,
  ...props
} = Astro.props

// The footer bleeds into the default padding. Keep caller padding overrides.
const padded = cn("p-6", className).split(/\s+/).includes("p-6")
---

<DialogPortal>
  <DialogOverlay />
  <div
    data-slot="dialog-content"
    data-dialog-padded={padded ? "" : undefined}
    hidden
    class={cn("font-sans text-popover-foreground bg-popover fixed top-1/2 left-1/2 z-50 flex w-full max-w-[calc(100%-2rem)] -translate-x-1/2 -translate-y-1/2 flex-col gap-6 rounded-2xl border p-6 text-sm shadow-lg/5 transition-[scale,opacity] duration-200 outline-none not-dark:bg-clip-padding before:pointer-events-none before:absolute before:inset-0 before:rounded-[calc(var(--radius-2xl)-1px)] before:shadow-[0_1px_--theme(--color-black/4%)] has-data-[slot=dialog-panel]:min-h-0 has-data-[slot=dialog-panel]:gap-0 has-data-[slot=dialog-panel]:p-0 data-ending-style:scale-98 data-ending-style:opacity-0 data-starting-style:scale-98 data-starting-style:opacity-0 sm:max-w-lg dark:before:shadow-[0_-1px_--theme(--color-white/6%)] @container/dialog", className)}
    style={`z-index: calc(var(--dialog-content-stack-index, 0) * 2 + 101);${style ? ` ${style}` : ""}`}
    {...props}
  >
    {showCloseButton && (
      <Button
        type="button"
        data-slot="dialog-close"
        data-dialog-dismiss
        variant="ghost"
        size="icon-sm"
        class="absolute top-4 right-4"
      >
        <XIcon />
        <span class="sr-only">Close</span>
      </Button>
    )}
    <slot />
  </div>
</DialogPortal>
```

#### src/components/ui/dialog/dialog-description.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"p">

const { class: className, ...props } = Astro.props
---

<p
  data-slot="dialog-description"
  class={cn("text-muted-foreground text-sm", className)}
  {...props}
>
  <slot />
</p>
```

#### src/components/ui/dialog/dialog-footer.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"
import DialogClose from "./dialog-close.astro"

type Props = HTMLAttributes<"div"> & {
  showCloseButton?: boolean
}

const { class: className, showCloseButton = false, ...props } = Astro.props
---

<div
  data-slot="dialog-footer"
  class={cn("bg-muted/72 flex flex-col-reverse gap-2 border-t px-6 py-4 in-[[data-slot=dialog-content]:has([data-slot=dialog-panel])]:mx-0 in-[[data-slot=dialog-content]:has([data-slot=dialog-panel])]:mb-0 in-[[data-slot=dialog-content][data-dialog-padded]]:-mx-6 in-[[data-slot=dialog-content][data-dialog-padded]]:-mb-6 sm:flex-row sm:justify-end sm:rounded-b-[calc(var(--radius-2xl)-1px)]", className)}
  {...props}
>
  <slot />
  {showCloseButton && <DialogClose variant="outline">Close</DialogClose>}
</div>
```

#### src/components/ui/dialog/dialog-header.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"div">

const { class: className, ...props } = Astro.props
---

<div
  data-slot="dialog-header"
  class={cn("flex flex-col gap-2 in-[[data-slot=dialog-content]:has([data-slot=dialog-panel])]:p-6 in-[[data-slot=dialog-content]:has([data-slot=dialog-panel])]:pb-3", className)}
  {...props}
>
  <slot />
</div>
```

#### src/components/ui/dialog/dialog-overlay.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"div">

const { class: className, style: styleProp, ...props } = Astro.props

const style =
  typeof styleProp === "string"
    ? `z-index: calc(var(--dialog-overlay-stack-index, 0) * 2 + 100); ${styleProp}`
    : {
        "z-index": "calc(var(--dialog-overlay-stack-index, 0) * 2 + 100)",
        ...(styleProp ? styleProp : {}),
      }
---

<div
  data-slot="dialog-overlay"
  hidden
  class={cn("fixed inset-0 z-50 bg-black/32 backdrop-blur-sm transition-opacity duration-200 data-ending-style:opacity-0 data-starting-style:opacity-0", className)}
  style={style}
  {...props}
>
  <slot />
</div>
```

#### src/components/ui/dialog/dialog-panel.astro

```astro
---
import type { HTMLAttributes } from "astro/types"
import { ScrollArea } from "../scroll-area/index.ts"

import { cn } from "cn"

type Props = HTMLAttributes<"div"> & {
  scrollFade?: boolean
}

const { class: className, scrollFade = false, ...props } = Astro.props
---

<ScrollArea overscrollContain scrollFade={scrollFade}>
  <div
    class={cn(
      "p-6 in-[[data-slot=dialog-content]:has([data-slot=dialog-footer]:not(.border-t):not([data-slot=dialog-footer]))]:pb-1 in-[[data-slot=dialog-content]:has([data-slot=dialog-header])]:pt-1",
      className,
    )}
    data-slot="dialog-panel"
    {...props}
  >
    <slot />
  </div>
</ScrollArea>
```

#### src/components/ui/dialog/dialog-portal.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"div">

const { class: className, ...props } = Astro.props
---

<div data-slot="dialog-portal" class={cn(className)} {...props}>
  <slot />
</div>
```

#### src/components/ui/dialog/dialog-title.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"h2">

const { class: className, ...props } = Astro.props
---

<h2
  data-slot="dialog-title"
  class={cn("font-heading text-xl leading-none font-semibold", className)}
  {...props}
>
  <slot />
</h2>
```

#### src/components/ui/dialog/dialog-trigger.astro

```astro
---
import type { ComponentProps } from "astro/types"

import { Button } from "../button/index.ts"

type Props = ComponentProps<typeof Button>

const { ...props } = Astro.props
---

<Button data-slot="dialog-trigger" {...props}>
  <slot />
</Button>
```

#### src/components/ui/dialog/dialog.astro

```astro
---
import type { HTMLAttributes } from "astro/types"
import type { DialogOptions } from "./types.ts"

type Props = HTMLAttributes<"div"> & DialogOptions

const {
  class: className,
  defaultOpen,
  closeOnClickOutside,
  closeOnEscape,
  lockScroll,
  alertDialog,
  ...props
} = Astro.props
---

<div
  data-slot="dialog"
  data-default-open={defaultOpen}
  data-close-on-click-outside={closeOnClickOutside}
  data-close-on-escape={closeOnEscape}
  data-lock-scroll={lockScroll}
  data-alert-dialog={alertDialog}
  class={className}
  {...props}
>
  <slot />
</div>

<script>
  import { create } from "@data-slot/dialog"
  import type { DialogController } from "@data-slot/dialog"

  const controllers: DialogController[] = []
  const initialize = () => controllers.push(...create())
  initialize()
  document.addEventListener("astro:page-load", initialize)
  document.addEventListener("astro:before-swap", () => {
    for (const controller of controllers) controller.destroy()
    controllers.length = 0
  })
</script>
```

#### src/components/ui/dialog/index.ts

```ts
export { default as Dialog } from './dialog.astro'
export { default as DialogPortal } from './dialog-portal.astro'
export { default as DialogOverlay } from './dialog-overlay.astro'
export { default as DialogPopup } from './dialog-content.astro'
export { default as DialogContent } from './dialog-content.astro'
export { default as DialogTrigger } from './dialog-trigger.astro'
export { default as DialogHeader } from './dialog-header.astro'
export { default as DialogTitle } from './dialog-title.astro'
export { default as DialogDescription } from './dialog-description.astro'
export { default as DialogFooter } from './dialog-footer.astro'
export { default as DialogClose } from './dialog-close.astro'
export { default as DialogPanel } from './dialog-panel.astro'
export type { DialogOptions, DialogContentProps } from './types.ts'
```

#### src/components/ui/dialog/types.ts

```ts
import type { DialogOptions as DialogOptionsType } from '@data-slot/dialog'

/** Dialog configuration rendered as data attributes. */
export type DialogOptions = Pick<
    DialogOptionsType,
    | 'defaultOpen'
    | 'closeOnClickOutside'
    | 'closeOnEscape'
    | 'lockScroll'
    | 'alertDialog'
>

/** Close control for the dialog panel. */
export type DialogContentProps = {
    /**
     * Whether to show the close button in the top right corner of the dialog.
     *
     * @default true
     */
    showCloseButton?: boolean
}
```

Component classes use the preset configured during project setup. Select the same preset in the docs before copying Manual source. To change installed component classes, edit or reinstall their source. See [Theming](/docs/theming) for details.


## Usage

```ts
import { Button } from "@/components/ui/button"
import {
    Dialog,
    DialogClose,
    DialogContent,
    DialogDescription,
    DialogFooter,
    DialogHeader,
    DialogTitle,
    DialogTrigger,
} from "@/components/ui/dialog"
```

```astro
<Dialog>
  <DialogTrigger>Open</DialogTrigger>
  <DialogContent>
    <DialogHeader>
      <DialogTitle>Are you absolutely sure?</DialogTitle>
      <DialogDescription>
        Removing this draft deletes its saved content. Export a copy first if you need to keep it.
      </DialogDescription>
    </DialogHeader>
    <DialogFooter>
      <DialogClose variant="outline">Cancel</DialogClose>
      <Button>Continue</Button>
    </DialogFooter>
  </DialogContent>
</Dialog>
```

## Composition

Arrange the parts as shown for a `Dialog`:

```text
Dialog
├── DialogTrigger
└── DialogContent
    ├── DialogHeader
    │   ├── DialogTitle
    │   └── DialogDescription
    └── DialogFooter
```

## Examples

### With Form

Replace the default close control with your own button.

```astro
---
import { Button } from "@/components/ui/button"
import {
  Dialog,
  DialogClose,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
  DialogTrigger,
} from "@/components/ui/dialog"
---

<Dialog>
  <DialogTrigger variant="outline">Open Custom Dialog</DialogTrigger>
  <DialogContent showCloseButton={false}>
    <DialogHeader>
      <DialogTitle>Share project</DialogTitle>
      <DialogDescription>
        Invite a teammate or copy the share link below.
      </DialogDescription>
    </DialogHeader>
    <div class="rounded-lg border p-4 text-sm">
      https://ui.gnosticai.com/project/acme-studio
    </div>
    <DialogFooter>
      <DialogClose variant="outline">Not now</DialogClose>
      <Button>Copy link</Button>
    </DialogFooter>
  </DialogContent>
</Dialog>
```

### No Close Button

Use `showCloseButton={false}` to hide the close button.

```astro
---
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogHeader,
  DialogTitle,
  DialogTrigger,
} from "@/components/ui/dialog"
---

<Dialog closeOnClickOutside={false} closeOnEscape={false}>
  <DialogTrigger variant="outline">Open Blocking Dialog</DialogTrigger>
  <DialogContent showCloseButton={false}>
    <DialogHeader>
      <DialogTitle>Processing payment</DialogTitle>
      <DialogDescription>
        Please wait while we confirm your payment details.
      </DialogDescription>
    </DialogHeader>
    <div class="rounded-lg border p-4 text-sm">
      Do not close this window until the request completes.
    </div>
  </DialogContent>
</Dialog>
```

### With Sticky Footer

Keep actions visible while the content scrolls.

```astro
---
import { Button } from "@/components/ui/button"
import {
  Dialog,
  DialogClose,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
  DialogTrigger,
} from "@/components/ui/dialog"
---

<Dialog>
  <DialogTrigger variant="outline">Open Sticky Footer</DialogTrigger>
  <DialogContent class="max-h-[min(85vh,42rem)] overflow-hidden p-0">
    <DialogHeader class="border-b px-6 pt-6 pb-4">
      <DialogTitle>Review document</DialogTitle>
      <DialogDescription>
        The actions stay visible while the content area scrolls.
      </DialogDescription>
    </DialogHeader>
    <div class="overflow-y-auto px-6 py-4">
      <div class="text-muted-foreground grid gap-4 text-sm">
        <p>1. Confirm the page title describes the service.</p>
        <p>2. Check each action link with the client.</p>
        <p>3. Replace sample image descriptions with accurate text.</p>
        <p>4. Review the contact address and phone number.</p>
        <p>5. Keep headings in a clear order.</p>
        <p>6. Check the mobile menu with a keyboard.</p>
        <p>7. Test the page in light and dark mode.</p>
        <p>8. Read error messages next to their fields.</p>
        <p>9. Remove sample content before publishing.</p>
        <p>10. Record the final handoff notes with the project.</p>
      </div>
    </div>
    <DialogFooter class="border-t px-6 py-4">
      <DialogClose variant="outline">Cancel</DialogClose>
      <Button>Approve</Button>
    </DialogFooter>
  </DialogContent>
</Dialog>
```

### Scrollable Content

Long content can scroll while the header stays in view.

```astro
---
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogHeader,
  DialogTitle,
  DialogTrigger,
} from "@/components/ui/dialog"
---

<Dialog>
  <DialogTrigger variant="outline">Open Scrollable Dialog</DialogTrigger>
  <DialogContent class="max-h-[min(85vh,42rem)] overflow-hidden p-0">
    <DialogHeader class="bg-popover border-b px-6 pt-6 pb-4">
      <DialogTitle>Terms of service</DialogTitle>
      <DialogDescription>
        Scroll through the content while the title and description stay visible.
      </DialogDescription>
    </DialogHeader>
    <div class="overflow-y-auto px-6 py-4">
      <div class="text-muted-foreground grid gap-4 text-sm">
        <p>
          A weekly review starts with unfinished tasks and the questions that
          prevent progress. Record a next step and an owner before adding more
          work.
        </p>
        <p>
          Integer posuere erat a ante venenatis dapibus posuere velit aliquet.
        </p>
        <p>Donec ullamcorper nulla non metus auctor fringilla.</p>
        <p>
          Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor.
        </p>
        <p>Nullam quis risus eget urna mollis ornare vel eu leo.</p>
        <p>Morbi leo risus, porta ac consectetur ac, vestibulum at eros.</p>
        <p>Maecenas faucibus mollis interdum. Aenean eu leo quam.</p>
        <p>Etiam porta sem malesuada magna mollis euismod.</p>
        <p>Praesent commodo cursus magna, vel scelerisque nisl consectetur.</p>
        <p>
          Curabitur blandit tempus porttitor. Nullam id dolor id nibh ultricies.
        </p>
        <p>
          Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum
          nibh.
        </p>
        <p>Vestibulum id ligula porta felis euismod semper.</p>
      </div>
    </div>
  </DialogContent>
</Dialog>
```

## Props

### DialogOptions

<AutoTypeTable path="../../packages/ui/src/components/ui/dialog/types.ts" name="DialogOptions" />

Read the [installed source](/r/dialog.json) for the complete prop types.

## API Reference

See the [data-slot docs](https://github.com/bejamas/data-slot/blob/main/packages/dialog/README.md) for more information on interactivity.

### Events

Dispatch these events on the dialog root. The root emits the outbound events.

#### Outbound events

| Event           | Detail              |
| --------------- | ------------------- |
| `dialog:change` | `{ open: boolean }` |

#### Inbound events

| Event        | Detail              |
| ------------ | ------------------- |
| `dialog:set` | `{ open: boolean }` |

## When to use Dialog

Use a Dialog when a task needs a separate layer above the page. Compose title, description, content, and controls under one root. DialogPanel is useful for a separate body region; older direct content compositions remain supported.

## Keyboard and behavior

The data-slot controller supplies modal focus and dismissal behavior. Give the dialog an accessible title, test Tab and Shift+Tab, and check focus after closing. A form inside the dialog still needs its own submission and error handling.

## Theme and layout

The default panel stays centered and uses shared surface colors. Long content needs an intentional height limit and scrolling region. This Astro layout does not reproduce every Coss nested-dialog transform or mobile viewport animation.
