# Error dialog

Stackable error dialogs that cascade diagonally.

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

## Examples

```tsx
import { type ReactNode, useState } from "react"

import { ErrorDialog, ErrorDialogStack, type ErrorEntry } from "@/components/retro/error-dialog"

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

const CASCADE: readonly ErrorEntry[] = [
  { id: "1", message: "Not found." },
  { id: "2", message: "Fail." },
  { id: "3", message: "Deadline moved. Again." },
]

type FrameProps = {
  className: string
  children: (container: HTMLElement) => ReactNode
}

function ContainedFrame({ className, children }: FrameProps) {
  const [container, setContainer] = useState<HTMLDivElement | null>(null)

  return (
    <div ref={setContainer} inert className={`relative w-full contain-layout ${className}`}>
      {container && children(container)}
    </div>
  )
}

function ignoreDismiss() {}

export const demo: ShowcaseDemo = {
  span: "wide",
  variants: [
    {
      label: "single",
      render: () => (
        <ContainedFrame className="h-56">
          {(container) => <ErrorDialog message="Motivation.dll is missing." container={container} />}
        </ContainedFrame>
      ),
    },
    {
      label: "cascade",
      render: () => (
        <ContainedFrame className="right-6 h-72">
          {(container) => (
            <ErrorDialogStack errors={CASCADE} onDismiss={ignoreDismiss} container={container} />
          )}
        </ContainedFrame>
      ),
    },
  ],
}
```

## Installation

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

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

Add these items first:

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

`components/retro/error-dialog.tsx`

```tsx
"use client"

import * as React from "react"

import { Button } from "@/components/ui/button"
import {
  Dialog,
  DialogClose,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogTitle,
} from "@/components/ui/dialog"
import { ErrorGlyph } from "@/components/ui/pixel-glyphs"

type ErrorDialogProps = {
  message: React.ReactNode
  title?: string
  actionLabel?: string
  cascadeIndex?: number
  open?: boolean
  onOpenChange?: (open: boolean) => void
  container?: HTMLElement | null
}

type ErrorEntry = {
  id: string
  message: React.ReactNode
  title?: string
}

type ErrorDialogStackProps = {
  errors: readonly ErrorEntry[]
  onDismiss: (id: string) => void
  container?: HTMLElement | null
}

const CASCADE_STEP_PIXELS = 12

function preventOutsideDismiss(event: Event) {
  event.preventDefault()
}

function readCascadeTranslate(cascadeIndex: number): string {
  const offset = `${cascadeIndex} * var(--pixel) * ${CASCADE_STEP_PIXELS}`
  return `calc(-50% + ${offset}) calc(-50% + ${offset})`
}

function ErrorDialog({
  message,
  title = "Error",
  actionLabel = "OK",
  cascadeIndex = 0,
  open = true,
  onOpenChange,
  container,
}: ErrorDialogProps) {
  return (
    <Dialog open={open} onOpenChange={onOpenChange} modal={false}>
      <DialogContent
        data-slot="error-dialog"
        container={container}
        className="w-80 gap-5 sm:max-w-80"
        style={{ translate: readCascadeTranslate(cascadeIndex) }}
        onInteractOutside={preventOutsideDismiss}
      >
        <DialogTitle>{title}</DialogTitle>
        <div className="flex items-center gap-4 px-1">
          <ErrorGlyph />
          <DialogDescription>{message}</DialogDescription>
        </div>
        <DialogFooter className="sm:justify-center">
          <DialogClose asChild>
            <Button className="min-w-28">{actionLabel}</Button>
          </DialogClose>
        </DialogFooter>
      </DialogContent>
    </Dialog>
  )
}

function ErrorDialogStack({ errors, onDismiss, container }: ErrorDialogStackProps) {
  return errors.map((error, index) => (
    <ErrorDialog
      key={error.id}
      title={error.title}
      message={error.message}
      cascadeIndex={index}
      container={container}
      onOpenChange={(isOpen) => {
        if (!isOpen) {
          onDismiss(error.id)
        }
      }}
    />
  ))
}

export {
  ErrorDialog,
  type ErrorDialogProps,
  ErrorDialogStack,
  type ErrorDialogStackProps,
  type ErrorEntry,
}
```

## API reference

### ErrorDialog

| Prop | Type | Default |
| --- | --- | --- |
| `message` * | `React.ReactNode` | - |
| `title` | `string` | `"Error"` |
| `actionLabel` | `string` | `"OK"` |
| `cascadeIndex` | `number` | `0` |
| `open` | `boolean` | `true` |
| `onOpenChange` | `((open: boolean) => void)` | - |
| `container` | `HTMLElement` | - |

### ErrorDialogStack

| Prop | Type | Default |
| --- | --- | --- |
| `errors` * | `readonly ErrorEntry[]` | - |
| `onDismiss` * | `(id: string) => void` | - |
| `container` | `HTMLElement` | - |
