# GlassDropdownMenu

> A menu of actions that swells out of the control that opens it as one body of glass: a button of its own, or a whole Button Group.

```tsx
import { ChevronDown, Copy, Pencil, Share2, Trash2 } from "lucide-react";

import {
  GlassDropdownMenu,
  GlassDropdownMenuContent,
  GlassDropdownMenuItem,
  GlassDropdownMenuSeparator,
  GlassDropdownMenuTrigger,
} from "@vitrumui/react/dropdown-menu";

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));
};

// Open it: the list swells out of the whole button, a neck stretches between them and snaps,
// and the actions arrive one by one. Close it and the same motion plays backwards.
// Behind it, only the Press demo's fine grid on the page's own colour, centred on the button:
// plain DOM, every cell draws its own top and left line, fainter towards the edge.
export default function Demo() {
  return (
    <div className="relative isolate flex h-96 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>
      <GlassDropdownMenu>
        <GlassDropdownMenuTrigger className="h-12 px-6 text-[15px]">
          Options
          <ChevronDown className="opacity-60" />
        </GlassDropdownMenuTrigger>
        <GlassDropdownMenuContent>
          <GlassDropdownMenuItem>
            <Pencil />
            Rename
          </GlassDropdownMenuItem>
          <GlassDropdownMenuItem>
            <Copy />
            Duplicate
          </GlassDropdownMenuItem>
          <GlassDropdownMenuItem>
            <Share2 />
            Share
          </GlassDropdownMenuItem>
          <GlassDropdownMenuSeparator />
          <GlassDropdownMenuItem destructive>
            <Trash2 />
            Delete
          </GlassDropdownMenuItem>
        </GlassDropdownMenuContent>
      </GlassDropdownMenu>
    </div>
  );
}
```

**Use it for**

- A handful of actions on one thing, kept out of the way until asked for: Rename, Duplicate, Delete.
- The overflow of a Button Group: the three dots at its end.

**Not for**

- Picking a value that stays selected: use Select.
- Two or three actions that fit on screen: show them in a Button Group.
- Several of these on one screen: one stretching list is a moment, many are noise.

```bash
npm install @vitrumui/react # coming soon
```

> The behaviour (keyboard, typeahead, focus, positioning, ARIA) comes from UI SDK, so Dropdown Menu 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 {
  GlassDropdownMenu,
  GlassDropdownMenuContent,
  GlassDropdownMenuItem,
  GlassDropdownMenuSeparator,
  GlassDropdownMenuTrigger,
} from '@vitrumui/react/dropdown-menu'

<GlassDropdownMenu>
  <GlassDropdownMenuTrigger>Options</GlassDropdownMenuTrigger>
  <GlassDropdownMenuContent>
    <GlassDropdownMenuItem onSelect={rename}>Rename</GlassDropdownMenuItem>
    <GlassDropdownMenuItem onSelect={duplicate}>Duplicate</GlassDropdownMenuItem>
    <GlassDropdownMenuSeparator />
    <GlassDropdownMenuItem destructive onSelect={remove}>Delete</GlassDropdownMenuItem>
  </GlassDropdownMenuContent>
</GlassDropdownMenu>
```

It is Select's motion on a list of actions: the list swells out of the whole trigger, the neck between them stretches and snaps, and the items arrive one after another; closing plays it backwards. The trigger wears Button's clear glass, so it needs something behind it to bend.

## Out of a Button Group

Wrap one group and the content in the menu, and put the trigger on one of the group's items with `asChild`. The whole group is then the anchor: the list swells out of its body and takes its colour, white included. Wrap only the group that opens the menu.

```tsx
<GlassDropdownMenu>
  <GlassButtonGroup>
    <GlassDropdownMenuTrigger asChild>
      <GlassButtonGroupItem aria-label="More" className="px-0"><Ellipsis /></GlassButtonGroupItem>
    </GlassDropdownMenuTrigger>
  </GlassButtonGroup>
  <GlassDropdownMenuContent>
    <GlassDropdownMenuItem>Mark as read</GlassDropdownMenuItem>
    <GlassDropdownMenuItem destructive>Delete</GlassDropdownMenuItem>
  </GlassDropdownMenuContent>
</GlassDropdownMenu>
```

### The three dots of a toolbar

```tsx
import {
  Archive,
  ArrowLeft,
  BellOff,
  Clock,
  Ellipsis,
  Flag,
  MailOpen,
  Tag,
  Trash2,
} from "lucide-react";

import {
  GlassButtonGroup,
  GlassButtonGroupItem,
} from "@vitrumui/react/button-group";
import {
  GlassDropdownMenu,
  GlassDropdownMenuContent,
  GlassDropdownMenuItem,
  GlassDropdownMenuSeparator,
  GlassDropdownMenuTrigger,
} from "@vitrumui/react/dropdown-menu";

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));
};

// The actions of a message. Some share a body of glass, some stand apart: nested groups, with
// the outer one only spacing them. The glass is white unless a group brings its own colour,
// as the back button does. The three dots stand apart and open a menu that stretches out of them.
// Where the row would not fit (a phone), the labels step aside and the icons carry it.
// Point at a body and it swells; press and it grows. Behind them, the Press demo's fine grid
// on the page's own colour, centred on the glass: plain DOM, every cell draws its own top
// and left line, fainter towards the edge.
export default function Demo() {
  return (
    <div className="@container relative isolate flex h-72 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>
      <GlassButtonGroup aria-label="Message">
        <GlassButtonGroup className="bg-neutral-900 text-white">
          <GlassButtonGroupItem aria-label="Back" className="px-0">
            <ArrowLeft />
          </GlassButtonGroupItem>
        </GlassButtonGroup>
        <GlassButtonGroup>
          <GlassButtonGroupItem className="@max-[440px]:px-0">
            <Archive />
            <span className="@max-[440px]:sr-only">Archive</span>
          </GlassButtonGroupItem>
          <GlassButtonGroupItem className="@max-[440px]:px-0">
            <Flag />
            <span className="@max-[440px]:sr-only">Report</span>
          </GlassButtonGroupItem>
          <GlassButtonGroupItem className="@max-[440px]:px-0">
            <Clock />
            <span className="@max-[440px]:sr-only">Snooze</span>
          </GlassButtonGroupItem>
        </GlassButtonGroup>
        <GlassDropdownMenu>
          <GlassButtonGroup>
            <GlassDropdownMenuTrigger asChild>
              <GlassButtonGroupItem aria-label="More" className="px-0">
                <Ellipsis />
              </GlassButtonGroupItem>
            </GlassDropdownMenuTrigger>
          </GlassButtonGroup>
          <GlassDropdownMenuContent>
            <GlassDropdownMenuItem>
              <MailOpen />
              Mark as read
            </GlassDropdownMenuItem>
            <GlassDropdownMenuItem>
              <Tag />
              Add label
            </GlassDropdownMenuItem>
            <GlassDropdownMenuItem>
              <BellOff />
              Mute thread
            </GlassDropdownMenuItem>
            <GlassDropdownMenuSeparator />
            <GlassDropdownMenuItem destructive>
              <Trash2 />
              Delete
            </GlassDropdownMenuItem>
          </GlassDropdownMenuContent>
        </GlassDropdownMenu>
      </GlassButtonGroup>
    </div>
  );
}
```

## API reference

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `open / defaultOpen / onOpenChange` | `boolean / boolean / (open: boolean) => void` | — | On `GlassDropdownMenu`: control the menu yourself, or let it keep its own state. |
| `GlassDropdownMenuTrigger asChild` | `boolean` | `false` | Put the trigger on its single child (a `GlassButtonGroupItem`) instead of a glass button of its own. |
| `GlassDropdownMenuContent side / align` | `'top' \| 'bottom' / 'start' \| 'center' \| 'end'` | `'bottom' / 'center'` | Where the list opens against the trigger: below and centred by default. It flips when there is no room. |
| `GlassDropdownMenuContent sideOffset` | `number` | `12` | The gap between the control and the list, where the neck stretches. |
| `GlassDropdownMenuItem onSelect` | `() => void` | — | Runs when the item is pressed; the menu then closes (`closeOnSelect={false}` keeps it open). |
| `GlassDropdownMenuItem destructive` | `boolean` | `false` | Paints the item red, for Delete. |
| `GlassDropdownMenuItem disabled` | `boolean` | `false` | Dimmed and skipped by the keyboard. |
| `GlassDropdownMenuSeparator` | `className` | — | A hairline between items. |

## Accessibility

- A button with `aria-haspopup` that opens a `menu` of `menuitem`s (the WAI-ARIA menu button pattern).
- Arrow keys, Home and End move through the items, typing a letter jumps to a match, Enter runs the item, Escape closes and returns focus to the trigger.
- An icon-only trigger needs an `aria-label`.
- With reduced motion the list opens and closes at once. Without WebGL2 it is a plain frosted list.

Source: /docs/dropdown-menu
