Skip to content
Esc
↑↓navigate↵open⌘Jpreview
On this page

Dialog for Astro

Build Astro modal dialogs with titles, descriptions, and actions. Explore shadcn style examples, installation, and keyboard behavior.

API Reference

Installation

bunx --bun shadcn@latest add @gnostic/dialog
npx shadcn@latest add @gnostic/dialog
pnpm dlx shadcn@latest add @gnostic/dialog

Component classes: Default Gnostic.

Complete the Installation guide first. Then copy the component files below.

Install component packages

bun add @data-slot/dialog

Copy the files

src/components/ui/dialog/dialog-close.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
---
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
---
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
---
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
---
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
---
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
---
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
---
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
---
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
---
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
---
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
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
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
}

Usage

import { Button } from "@/components/ui/button"
import {
    Dialog,
    DialogClose,
    DialogContent,
    DialogDescription,
    DialogFooter,
    DialogHeader,
    DialogTitle,
    DialogTrigger,
} from "@/components/ui/dialog"
<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:

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

Examples

With Form

Replace the default close control with your own button.

No Close Button

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

Keep actions visible while the content scrolls.

Scrollable Content

Long content can scroll while the header stays in view.

Props

DialogOptions

PropType
defaultOpen?boolean

Initial open state

Typeboolean
closeOnClickOutside?boolean

Close when clicking overlay

Typeboolean
closeOnEscape?boolean

Close when pressing Escape

Typeboolean
lockScroll?boolean

Lock body scroll when open

Typeboolean
alertDialog?boolean

Use alertdialog role for blocking confirmations

Typeboolean

Read the installed source for the complete prop types.

API Reference

See the data-slot docs 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.