# GlassDialog

> A pane of glass that rises over the whole page: the page dims and waits, the pane comes up in the middle of the screen in Card's frosted glass, a little past its size, then settled, and keeps the focus until it is closed.

```tsx
import { HugeiconsIcon } from '@hugeicons/react'
import { PencilIcon } from '@hugeicons/core-free-icons'
import { useState } from 'react'

import { GlassDialog, GlassDialogClose, GlassDialogContent, GlassDialogDescription, GlassDialogFooter, GlassDialogTitle, GlassDialogTrigger } from '@/components/liquid-glass/glass-dialog'

const COLS = 13
const ROWS = 7

/** 1 around the middle of the grid, falling to 0 at its edge. */
const fade = (col: number, row: number) => {
  const distance = Math.hypot((col - (COLS - 1) / 2) / (COLS / 2), (row - (ROWS - 1) / 2) / (ROWS / 2))
  return Math.min(1, Math.max(0, (1 - distance) / 0.45))
}

// Press it: the page dims and a pane of glass rises in the middle of the screen, a little past
// its size, then settled. Focus lands in the field with the name selected; Enter saves, and
// Escape, Cancel or a press outside closes it and gives focus back to the button.
// Behind the button, only the Press demo's fine grid on the page's own colour, centred on it:
// plain DOM, every cell draws its own top and left line, fainter towards the edge.
export default function Demo() {
  const [name, setName] = useState('Spring launch')
  return (
    <div className="relative isolate flex h-[22rem] w-full max-w-xl items-center justify-center">
      <div aria-hidden className="absolute top-1/2 left-1/2 -z-10 grid -translate-x-1/2 -translate-y-1/2 grid-cols-[repeat(13,40px)]">
        {Array.from({ length: COLS * ROWS }, (_, i) => (
          <span key={i} className="relative size-10" style={{ opacity: fade(i % COLS, Math.floor(i / COLS)) }}>
            <span className="absolute inset-x-0 top-0 h-px bg-black/10 dark:bg-white/10" />
            <span className="absolute inset-y-0 left-0 w-px bg-black/10 dark:bg-white/10" />
          </span>
        ))}
      </div>
      <GlassDialog>
        <GlassDialogTrigger size="lg" icon={<HugeiconsIcon icon={PencilIcon} />} iconPosition="start">
          {name}
        </GlassDialogTrigger>
        <GlassDialogContent>
          <GlassDialogTitle>Rename board</GlassDialogTitle>
          <GlassDialogDescription>Everyone on the team will see the new name.</GlassDialogDescription>
          {/* A form with method="dialog" closes the dialog when it is submitted, and only if it is valid. */}
          <form method="dialog" className="flex flex-col gap-4" onSubmit={(event) => setName(String(new FormData(event.currentTarget).get('name')).trim())}>
            <input
              name="name"
              aria-label="Board name"
              defaultValue={name}
              required
              autoComplete="off"
              className="h-11 rounded-2xl bg-current/8 px-4 outline-none placeholder:opacity-50 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-[#3291FF]"
            />
            <GlassDialogFooter>
              <GlassDialogClose>Cancel</GlassDialogClose>
              <GlassDialogClose type="submit" variant="solid">
                Save
              </GlassDialogClose>
            </GlassDialogFooter>
          </form>
        </GlassDialogContent>
      </GlassDialog>
    </div>
  )
}
```

**Use it for**

- A task the rest of the page should wait for: renaming something, a short form, a choice that needs a sentence of context.
- Something that can be opened from anywhere: a menu item, a keyboard shortcut, your own code. It belongs to the page, not to one button.

**Not for**

- A little more about one control: use Popover, which comes out of its button and leaves the page usable.
- A list of actions: use Menu or Dropdown Menu. Picking a value: use Select.
- News that asks nothing of the visitor (saved, copied): do not take the page away for it.

```bash
npx shadcn@latest add https://liquidglass.onemorethink.dev/r/dialog.json
```

> The behaviour (the top layer, the inert page, the focus trap, Escape, the outside press, forms, ARIA) comes from UI SDK, so Dialog needs `@ui-sdk/core` and `@ui-sdk/react`. The install command adds them at the exact version this was built on: UI SDK is in beta and its API may still change.

## Usage

```tsx
import {
  GlassDialog,
  GlassDialogClose,
  GlassDialogContent,
  GlassDialogDescription,
  GlassDialogFooter,
  GlassDialogTitle,
  GlassDialogTrigger,
} from '@/components/liquid-glass/glass-dialog'

<GlassDialog>
  <GlassDialogTrigger>Archive</GlassDialogTrigger>
  <GlassDialogContent>
    <GlassDialogTitle>Archive this board?</GlassDialogTitle>
    <GlassDialogDescription>It leaves the sidebar. You can bring it back from Settings.</GlassDialogDescription>
    <GlassDialogFooter>
      <GlassDialogClose>Cancel</GlassDialogClose>
      <GlassDialogClose variant="solid" onClick={archive}>Archive</GlassDialogClose>
    </GlassDialogFooter>
  </GlassDialogContent>
</GlassDialog>
```

The page dims and stops answering, and the pane comes up in the middle of the screen: it fades up as it grows from a little under its size, lands a little past it and settles. It wears the glass of every surface that opens over the page (a Popover, a Menu, the list of a Select), tuned so that what is on the pane reads over a page of text: a 60% tint (white, or black on a dark page), a 10 px frost in place of Card's 3, and a rim that bends the page it covers, close to the edge. It takes Card's `tint`, `blur` and `optics`, and its text follows the tone of the glass. It only fades and grows about its own middle and never slides, so the page the glass shows is the page behind it at every moment. Closing is quicker than opening: the pane sinks a little and goes, and the dim lifts with it. A press on a Close part lights the pane where it lands. The trigger is a GlassButton and takes its props (`material`, `size`, `icon`); `asChild` puts it on an element of your own.

## Forms

Put a `<form method="dialog">` in the pane and give the main action `type="submit"`: the form closes the dialog when it is submitted, and only once it is valid, so Enter in a field saves. The submit button's `value` comes back as `details.returnValue` in `onOpenChange`. Focus starts in the first field with its text selected; `initialFocus` on the content picks another place. The example at the top of the page is such a form.

```tsx
<GlassDialogContent>
  <GlassDialogTitle>Rename board</GlassDialogTitle>
  <form method="dialog" onSubmit={(event) => rename(new FormData(event.currentTarget))}>
    <input name="name" required defaultValue={name} />
    <GlassDialogFooter>
      <GlassDialogClose>Cancel</GlassDialogClose>
      <GlassDialogClose type="submit" variant="solid">Save</GlassDialogClose>
    </GlassDialogFooter>
  </form>
</GlassDialogContent>
```

## Opening it yourself

A dialog does not need a trigger. Control it with `open` and `onOpenChange`, or create a controller and open it from anywhere: a menu item, a row of a table, your own code. `controller.open()` returns a Promise that resolves when the dialog closes, with the reason and the `returnValue`. Focus goes back to wherever it was before the dialog opened.

```tsx
import { createDialogController, GlassDialog, GlassDialogContent } from '@/components/liquid-glass/glass-dialog'

const [rename] = useState(() => createDialogController())

<GlassMenu>
  <GlassMenuTrigger aria-label="More" className="w-12 px-0"><Ellipsis /></GlassMenuTrigger>
  <GlassMenuContent>
    <GlassMenuItem onSelect={() => rename.open()}>Rename…</GlassMenuItem>
  </GlassMenuContent>
</GlassMenu>

<GlassDialog controller={rename}>
  <GlassDialogContent>…</GlassDialogContent>
</GlassDialog>
```

## A question that must be answered

`alert` on `GlassDialog` makes it an alert dialog, for a question the visitor has to answer before anything happens: deleting something, leaving unsaved work. It is the same pane in the same glass. What changes is how it is left: a press outside does not close it, focus starts on the first Close part, so put the answer that loses nothing first, and screen readers hear it as an `alertdialog`. Escape still closes it, as that first answer would. `variant="destructive"` paints the Close part that deletes red.

### Empty the trash?

```tsx
import { HugeiconsIcon } from '@hugeicons/react'
import { Delete02Icon, RotateCcwIcon } from '@hugeicons/core-free-icons'
import { useState } from 'react'

import { GlassButton } from '@/components/liquid-glass'
import { createConfirm, GlassDialog, GlassDialogClose, GlassDialogContent, GlassDialogDescription, GlassDialogFooter, GlassDialogTitle } from '@/components/liquid-glass/glass-dialog'

const COLS = 13
const ROWS = 7

/** 1 around the middle of the grid, falling to 0 at its edge. */
const fade = (col: number, row: number) => {
  const distance = Math.hypot((col - (COLS - 1) / 2) / (COLS / 2), (row - (ROWS - 1) / 2) / (ROWS / 2))
  return Math.min(1, Math.max(0, (1 - distance) / 0.45))
}

// A question that must be answered before anything is deleted. `alert` makes the dialog an
// alert dialog: a press outside does not close it, and focus starts on Cancel, the answer that
// loses nothing. It is asked from code: `ask()` opens the dialog below and resolves `true` only
// when the Close part with `value="confirm"` closed it; Cancel and Escape resolve `false`.
// Behind the button, only the Press demo's fine grid on the page's own colour, centred on it:
// plain DOM, every cell draws its own top and left line, fainter towards the edge.
export default function Demo() {
  const [ask] = useState(() => createConfirm())
  const [emptied, setEmptied] = useState(false)
  const empty = async () => {
    if (await ask()) setEmptied(true)
  }
  return (
    <div className="relative isolate flex h-[22rem] w-full max-w-xl items-center justify-center">
      <div aria-hidden className="absolute top-1/2 left-1/2 -z-10 grid -translate-x-1/2 -translate-y-1/2 grid-cols-[repeat(13,40px)]">
        {Array.from({ length: COLS * ROWS }, (_, i) => (
          <span key={i} className="relative size-10" style={{ opacity: fade(i % COLS, Math.floor(i / COLS)) }}>
            <span className="absolute inset-x-0 top-0 h-px bg-black/10 dark:bg-white/10" />
            <span className="absolute inset-y-0 left-0 w-px bg-black/10 dark:bg-white/10" />
          </span>
        ))}
      </div>
      {emptied ? (
        <GlassButton size="lg" icon={<HugeiconsIcon icon={RotateCcwIcon} />} iconPosition="start" onClick={() => setEmptied(false)}>
          Put them back
        </GlassButton>
      ) : (
        <GlassButton size="lg" icon={<HugeiconsIcon icon={Delete02Icon} />} iconPosition="start" onClick={() => void empty()}>
          Empty trash
        </GlassButton>
      )}
      <GlassDialog alert controller={ask.controller}>
        <GlassDialogContent>
          <GlassDialogTitle>Empty the trash?</GlassDialogTitle>
          <GlassDialogDescription>Three boards will be deleted for good. This cannot be undone.</GlassDialogDescription>
          <GlassDialogFooter>
            <GlassDialogClose>Cancel</GlassDialogClose>
            <GlassDialogClose variant="destructive" value="confirm">
              Empty trash
            </GlassDialogClose>
          </GlassDialogFooter>
        </GlassDialogContent>
      </GlassDialog>
    </div>
  )
}
```

To ask from code, create the question once with `createConfirm()`, give its `controller` to the dialog and `value="confirm"` to the Close part that agrees. Calling it opens the dialog and returns a Promise of the answer: `true` for that part, `false` for every other way out. Questions asked while one is open wait their turn.

```tsx
import { createConfirm, GlassDialog, GlassDialogClose, GlassDialogContent, GlassDialogFooter, GlassDialogTitle } from '@/components/liquid-glass/glass-dialog'

const [ask] = useState(() => createConfirm())

async function remove() {
  if (await ask()) await deleteBoard()
}

<GlassDialog alert controller={ask.controller}>
  <GlassDialogContent>
    <GlassDialogTitle>Delete this board?</GlassDialogTitle>
    <GlassDialogFooter>
      <GlassDialogClose>Cancel</GlassDialogClose>
      <GlassDialogClose variant="destructive" value="confirm">Delete</GlassDialogClose>
    </GlassDialogFooter>
  </GlassDialogContent>
</GlassDialog>
```

## Inside a frame

`inline` on `GlassDialogContent` keeps the pane and its dim inside the positioned box around the dialog instead of over the page: an app preview, a panel, a card. Pair it with `modal="trap-focus"`, which keeps Tab inside the pane and leaves the page around the frame usable.

### An album in its own frame

```tsx
import { HugeiconsIcon } from '@hugeicons/react'
import { Link02Icon } from '@hugeicons/core-free-icons'
import { useState } from 'react'

import { GlassDialog, GlassDialogClose, GlassDialogContent, GlassDialogDescription, GlassDialogFooter, GlassDialogTitle, GlassDialogTrigger } from '@/components/liquid-glass/glass-dialog'

const photos = ['#0a84ff', '#ff6b3d', '#ffd60a', '#30d158', '#bf5af2', '#ff375f', '#64d2ff', '#ff9f0a']

// An album in a frame of its own, as an app preview would be. `inline` keeps the dialog and its
// dim inside the frame (the nearest positioned box), and `modal="trap-focus"` leaves the page
// around it usable. The pane's rim bends the photos it covers: flat colour, plain DOM.
// (The frame clips instead of hiding its overflow: a box that hides it can still be scrolled,
// and focus moving to the button would scroll it by the few px its glass reaches past the edge.)
export default function Demo() {
  const [copied, setCopied] = useState(false)
  return (
    <div className="relative isolate flex h-[26rem] w-full max-w-xl items-end justify-center overflow-clip rounded-[28px] bg-white pb-8 text-neutral-900 dark:bg-neutral-900 dark:text-white">
      <div aria-hidden className="absolute inset-0 -z-10 flex flex-col p-5">
        <div className="flex items-baseline justify-between px-1 pb-4">
          <span className="text-lg font-semibold tracking-tight">Lisbon, May</span>
          <span className="text-sm opacity-60">8 photos</span>
        </div>
        <div className="grid flex-1 grid-cols-4 gap-3">
          {photos.map((colour) => (
            <span key={colour} className="rounded-2xl" style={{ background: colour }} />
          ))}
        </div>
      </div>
      <GlassDialog modal="trap-focus">
        <GlassDialogTrigger size="lg" icon={<HugeiconsIcon icon={Link02Icon} />} iconPosition="start">
          {copied ? 'Link copied' : 'Share album'}
        </GlassDialogTrigger>
        <GlassDialogContent inline>
          <GlassDialogTitle>Share this album</GlassDialogTitle>
          <GlassDialogDescription>Anyone with the link can see all 8 photos. You can turn it off at any time.</GlassDialogDescription>
          <GlassDialogFooter>
            <GlassDialogClose>Not now</GlassDialogClose>
            <GlassDialogClose variant="solid" onClick={() => setCopied(true)}>
              Copy link
            </GlassDialogClose>
          </GlassDialogFooter>
        </GlassDialogContent>
      </GlassDialog>
    </div>
  )
}
```

## Long content

The pane is never a scroll box itself: the glass is drawn a little past its edge, and a scroll box would cut it. It is a column no taller than the screen, so give the part that should scroll `min-h-0 overflow-y-auto` and keep the title and the actions outside it.

## API reference

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `open / defaultOpen / onOpenChange` | `boolean / boolean / (open: boolean, details) => void` | — | On `GlassDialog`: control the dialog yourself, or let it keep its own state. `details.reason` says why it changed and `details.returnValue` what closed it. |
| `alert` | `boolean` | `false` | An alert dialog: always modal, a press outside does not close it, focus starts on the first Close part, and its role is `alertdialog`. Its controller comes from `createAlertDialogController()` or `createConfirm()`. |
| `modal` | `boolean \| 'trap-focus'` | `true` | `true`: the page is inert and does not scroll. `trap-focus`: Tab stays in the pane, the page still scrolls and takes presses. `false`: a pane beside the page, with no dim. |
| `dismissible` | `boolean` | `true` | Whether Escape and a press outside close it. `false` while a form has unsaved changes; `onDismissAttempt` then tells you someone tried. |
| `controller` | `DialogController` | — | From `createDialogController()`: opens and closes the dialog from anywhere, and connects triggers outside it. |
| `GlassDialogTrigger material / size / icon / iconPosition / tint / pull` | `GlassButton props` | — | The trigger is a GlassButton: clear glass, `md`, by default. |
| `GlassDialogTrigger asChild` | `boolean` | `false` | Put the trigger on its single child (your own button, a `GlassButtonGroupItem`) instead of a GlassButton. |
| `GlassDialogTrigger shortcut` | `string` | — | Keys that open it, as `mod+k` (Command on Apple platforms, Control elsewhere). |
| `GlassDialogContent dim` | `number` | `0.3` | How dark the page goes behind the pane, 0–1. `0` leaves it as it is. |
| `GlassDialogContent tint / blur / optics` | `number / number / GlassOptics` | `0.6 / 10.25` | The pane's glass, as on Card: how much tint covers the page, how much it is frosted (px), and the fine-tuning. |
| `GlassDialogContent inline` | `boolean` | `false` | Keep the pane and its dim inside the positioned box around the dialog instead of over the page. |
| `GlassDialogContent initialFocus / finalFocus` | `string \| Element \| () => Element` | — | Where focus goes as it opens (default: the first focusable element) and after it closes (default: where it was). |
| `GlassDialogContent className` | `string` | — | Classes for the pane: `w-[min(28rem,calc(100%-2rem))]`, `rounded-[28px]`, `p-6`, `gap-4` by default. |
| `GlassDialogTitle / GlassDialogDescription` | `className` | — | Name the dialog and say what it is for. |
| `GlassDialogFooter` | `className` | — | The row of actions: side by side from the right, stacked on a phone. |
| `GlassDialogClose variant` | `'quiet' \| 'solid' \| 'destructive'` | `'quiet'` | A soft button in the pane's own tone, the main action, or the main action in red when it deletes. It closes the dialog; `onClick` runs as it does. |
| `GlassDialogClose type / value / action` | `'submit' / string / () => unknown` | — | `type="submit"` submits the `<form method="dialog">` around it. `value` is reported as the `returnValue`. An `action` that returns a Promise keeps the dialog open, and the button pending, until it settles. |

## Accessibility

- A modal `dialog` (an `alertdialog` with `alert`) named by its title and described by its description: always give it a `GlassDialogTitle`.
- Focus moves into the pane as it opens and Tab stays inside it; the rest of the page is inert and does not scroll.
- Escape, a press outside and every Close part close it, and focus returns to the trigger, or to where it was. An alert dialog is not closed by a press outside.
- An icon-only trigger or Close part needs an `aria-label`.
- With reduced motion the pane only fades. Without WebGL2 it is a plain frosted panel.

Source: https://liquidglass.onemorethink.dev/docs/dialog
