# Dialog

shadcn dialog with a striped title bar and a dithered overlay.

Source: https://www.ui95.dev/ui/dialog

## Examples

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

import type { ShowcaseDemo } from "../types"

function renderTitleBarClose() {
  return (
    <Dialog>
      <DialogTrigger asChild>
        <Button size="sm" className="self-center">
          Open
        </Button>
      </DialogTrigger>
      <DialogContent>
        <DialogHeader>
          <DialogTitle>Readme.txt</DialogTitle>
          <DialogDescription>
            Close with the glyph in the title bar, the Escape key or a click on the overlay.
          </DialogDescription>
        </DialogHeader>
      </DialogContent>
    </Dialog>
  )
}

function renderFooterClose() {
  return (
    <Dialog>
      <DialogTrigger asChild>
        <Button size="sm" variant="secondary" className="self-center">
          Open
        </Button>
      </DialogTrigger>
      <DialogContent showCloseButton={false}>
        <DialogHeader>
          <DialogTitle>Tip of the day</DialogTitle>
          <DialogDescription>Save often. The disk is full of surprises.</DialogDescription>
        </DialogHeader>
        <DialogFooter showCloseButton />
      </DialogContent>
    </Dialog>
  )
}

function renderConfirm() {
  return (
    <Dialog>
      <DialogTrigger asChild>
        <Button size="sm" variant="destructive" className="self-center">
          Format C:
        </Button>
      </DialogTrigger>
      <DialogContent>
        <DialogHeader>
          <DialogTitle>Confirm</DialogTitle>
          <DialogDescription>All data on drive C: will be lost. Proceed?</DialogDescription>
        </DialogHeader>
        <DialogFooter>
          <DialogClose asChild>
            <Button variant="outline">No</Button>
          </DialogClose>
          <DialogClose asChild>
            <Button>Yes</Button>
          </DialogClose>
        </DialogFooter>
      </DialogContent>
    </Dialog>
  )
}

export const demo: ShowcaseDemo = {
  span: "tall",
  variants: [
    { label: "title bar close", render: renderTitleBarClose },
    { label: "footer close", render: renderFooterClose },
    { label: "confirm", render: renderConfirm },
  ],
}
```

## Installation

```bash
npx shadcn@latest add https://www.ui95.dev/r/dialog.json
```

Run it in a Radix shadcn project that already has the UI95 theme.

Dependencies:

```bash
npm install cn radix-ui
```

Add these items first:

- [button](https://www.ui95.dev/ui/button)
- [pixel-glyphs](https://www.ui95.dev/ui/pixel-glyphs)

`components/ui/dialog.tsx`

```tsx
"use client"

import { cn } from "cn"
import { Dialog as DialogPrimitive } from "radix-ui"
import * as React from "react"

import { Button } from "@/components/ui/button"
import { CloseGlyph } from "@/components/ui/pixel-glyphs"

function Dialog({
  ...props
}: React.ComponentProps<typeof DialogPrimitive.Root>) {
  return <DialogPrimitive.Root data-slot="dialog" {...props} />
}

function DialogTrigger({
  ...props
}: React.ComponentProps<typeof DialogPrimitive.Trigger>) {
  return <DialogPrimitive.Trigger data-slot="dialog-trigger" {...props} />
}

function DialogPortal({
  ...props
}: React.ComponentProps<typeof DialogPrimitive.Portal>) {
  return <DialogPrimitive.Portal data-slot="dialog-portal" {...props} />
}

function DialogClose({
  ...props
}: React.ComponentProps<typeof DialogPrimitive.Close>) {
  return <DialogPrimitive.Close data-slot="dialog-close" {...props} />
}

function DialogOverlay({
  className,
  ...props
}: React.ComponentProps<typeof DialogPrimitive.Overlay>) {
  return (
    <DialogPrimitive.Overlay
      data-slot="dialog-overlay"
      className={cn("fixed inset-0 isolate z-50 bg-ink-0 dither-25", className)}
      {...props}
    />
  )
}

function DialogContent({
  className,
  children,
  showCloseButton = true,
  container,
  ...props
}: React.ComponentProps<typeof DialogPrimitive.Content> & {
  showCloseButton?: boolean
  container?: HTMLElement | null
}) {
  return (
    <DialogPortal container={container}>
      <DialogOverlay />
      <DialogPrimitive.Content
        data-slot="dialog-content"
        className={cn(
          "fixed top-1/2 left-1/2 z-50 grid w-full max-w-[calc(100%-2rem)] -translate-x-1/2 -translate-y-1/2 gap-4 pixel-border border-border bg-popover p-4 text-popover-foreground offset-shadow-3 outline-none sm:max-w-sm",
          className
        )}
        {...props}
      >
        {children}
        {showCloseButton && (
          <DialogPrimitive.Close data-slot="dialog-close" asChild>
            <Button
              variant="ghost"
              className="absolute top-1 right-1 text-titlebar-foreground"
              size="icon-xs"
            >
              <CloseGlyph />
              <span className="sr-only">Close</span>
            </Button>
          </DialogPrimitive.Close>
        )}
      </DialogPrimitive.Content>
    </DialogPortal>
  )
}

function DialogHeader({ className, ...props }: React.ComponentProps<"div">) {
  return (
    <div
      data-slot="dialog-header"
      className={cn("flex flex-col gap-3", className)}
      {...props}
    />
  )
}

function DialogFooter({
  className,
  showCloseButton = false,
  children,
  ...props
}: React.ComponentProps<"div"> & {
  showCloseButton?: boolean
}) {
  return (
    <div
      data-slot="dialog-footer"
      className={cn(
        "flex flex-col-reverse gap-3 pt-1 sm:flex-row sm:justify-end",
        className
      )}
      {...props}
    >
      {children}
      {showCloseButton && (
        <DialogPrimitive.Close asChild>
          <Button variant="default">Close</Button>
        </DialogPrimitive.Close>
      )}
    </div>
  )
}

function DialogTitle({
  className,
  ...props
}: React.ComponentProps<typeof DialogPrimitive.Title>) {
  return (
    <DialogPrimitive.Title
      data-slot="dialog-title"
      className={cn(
        "-mx-4 -mt-4 flex h-8 items-center gap-3 border-b-(length:--pixel) border-border bg-titlebar pr-9 pl-3 font-pixel text-[16px] leading-none text-titlebar-foreground uppercase after:h-4 after:min-w-6 after:flex-1 after:stripes after:content-['']",
        className
      )}
      {...props}
    />
  )
}

function DialogDescription({
  className,
  ...props
}: React.ComponentProps<typeof DialogPrimitive.Description>) {
  return (
    <DialogPrimitive.Description
      data-slot="dialog-description"
      className={cn(
        "text-xl leading-tight text-popover-foreground *:[a]:underline *:[a]:underline-offset-3",
        className
      )}
      {...props}
    />
  )
}

export {
  Dialog,
  DialogClose,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogOverlay,
  DialogPortal,
  DialogTitle,
  DialogTrigger,
}
```

## API reference

### Dialog

No props of its own. It forwards the props of the element or primitive it wraps.

### DialogClose

No props of its own. It forwards the props of the element or primitive it wraps.

### DialogContent

| Prop | Type | Default |
| --- | --- | --- |
| `showCloseButton` | `boolean` | `true` |
| `container` | `HTMLElement` | - |

### DialogDescription

No props of its own. It forwards the props of the element or primitive it wraps.

### DialogFooter

| Prop | Type | Default |
| --- | --- | --- |
| `showCloseButton` | `boolean` | `false` |

### DialogHeader

No props of its own. It forwards the props of the element or primitive it wraps.

### DialogOverlay

No props of its own. It forwards the props of the element or primitive it wraps.

### DialogPortal

No props of its own. It forwards the props of the element or primitive it wraps.

### DialogTitle

No props of its own. It forwards the props of the element or primitive it wraps.

### DialogTrigger

No props of its own. It forwards the props of the element or primitive it wraps.

## Used by

- [command](https://www.ui95.dev/ui/command)
- [error-dialog](https://www.ui95.dev/ui/error-dialog)
