# GlassSelect

> A select whose list swells out of its button as one body of glass, holds on by a neck that stretches and snaps, and closes by the same motion played backwards.

```tsx
import { useState } from 'react'

import { GlassSelect } from '@/components/liquid-glass/glass-select'

const options = [
  { value: 'standard', label: 'Standard' },
  { value: 'express', label: 'Express' },
  { value: 'same-day', label: 'Same day' },
  { value: 'pickup', label: 'Pickup (closed today)', disabled: true },
]

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 options arrive one by one. Close it and the same motion plays backwards.
// Behind it, only a fine grid on the page's own colour, centred on the button (which sits
// 14px below the middle, under its label).
// The grid is plain DOM: every cell draws its own top and left line, fainter towards the edge.
export default function Demo() {
  const [value, setValue] = useState<string | null>(null)
  return (
    <div className="relative isolate flex h-96 w-full max-w-xl items-center justify-center">
      <div aria-hidden className="absolute top-[calc(50%+14px)] 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>
      <GlassSelect label="Delivery" placeholder="Choose a speed" options={options} value={value} onValueChange={setValue} />
    </div>
  )
}
```

**Use it for**

- Picking one option from a short list that should stay out of the way until asked for.
- A form field: the value is submitted under `name`.

**Not for**

- Two to five options that fit on one line: use Tabs.
- An on/off setting: use Switch.
- Several of these on one screen: one stretching list is a moment, a form full of them is noise.

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

> The behaviour (keyboard, typeahead, focus, positioning, ARIA) comes from UI SDK, so Select 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 { GlassSelect } from '@/components/liquid-glass/glass-select'

const [speed, setSpeed] = useState<string | null>(null)

<GlassSelect
  label="Delivery"
  placeholder="Choose a speed"
  value={speed}
  onValueChange={setSpeed}
  options={[
    { value: 'standard', label: 'Standard' },
    { value: 'express', label: 'Express' },
    { value: 'pickup', label: 'Pickup', disabled: true },
  ]}
/>
```

Opening, the list swells out of the whole button, the neck between them stretches and snaps, both halves take the jolt and the options arrive one after another. Closing is the opening played backwards, frame for frame; reopen it on the way and it carries on from there. The button wears Button's glass, with its light and shadow. Once the neck lets go, the list settles into Card's frost with no light of its own: a thin line draws its edge and a soft shadow lifts it off the page. The glass is clear, so it needs something behind it to bend.

## Your own popup

The stretch is not tied to a select. `GlassPopup`, which ships with it, puts the same motion on any control and the surface it opens (a menu, a popover):

```tsx
import { GlassPopupAnchor, GlassPopupSurface, useGlassPopup } from '@/components/liquid-glass'

const popup = useGlassPopup()

<MenuTrigger data-glass-host className="rounded-full px-5 h-12 bg-white/60 dark:bg-white/10">
  <GlassPopupAnchor popup={popup} />
  Options
</MenuTrigger>
<MenuContent data-glass-popup data-glass-skip sideOffset={12} className="rounded-3xl p-1.5">
  <GlassPopupSurface popup={popup} />
  <div role="menu">
    <MenuItem style={{ '--glass-i': 0 }}>Rename</MenuItem>
    <MenuItem style={{ '--glass-i': 1 }}>Duplicate</MenuItem>
  </div>
</MenuContent>
```

- `GlassPopupAnchor` is the first child of the control (marked `data-glass-host`); `GlassPopupSurface` is the first child of the content (marked `data-glass-popup`).
- The content carries `data-open` while open and `data-ending-style` while it closes, and stays mounted until its animations finish: UI SDK's popups do this.
- Items are `role="option"`, `role="menuitem"` or `data-glass-item`; `--glass-i` is each one's place in the queue.
- The surface takes its corners from the content's own `rounded-*` class; leave about 12 px between it and the control for the neck.

## API reference

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `label` | `ReactNode` | — | The field's label, tied to the button. |
| `options` | `{ value: string; label: ReactNode; disabled?: boolean }[]` | — | The choices, in order. |
| `value` | `string \| null` | — | The selected value (controlled). `null` = nothing selected. |
| `defaultValue` | `string \| null` | — | The starting value when the select keeps its own state. |
| `onValueChange` | `(value: string \| null) => void` | — | Called with the new value. |
| `placeholder` | `string` | — | Shown on the button while nothing is selected. |
| `name` | `string` | — | Submits the value with a form under this name. |
| `disabled` | `boolean` | `false` | Dimmed and not interactive. |
| `className` | `string` | — | Classes for the field (label and button); it is 16rem wide by default. |
| `useGlassPopup({ matchWidth })` | `boolean` | `false` | Your own popup: keep the surface exactly as wide as its control. |

## Accessibility

- A `combobox` button that opens a `listbox` (the WAI-ARIA select pattern), labelled by `label`.
- Arrow keys, Home and End move through the options, typing a letter jumps to a match, Enter selects, Escape closes and returns focus to the button. Disabled options are skipped.
- With reduced motion the list opens and closes at once, with no stretch or jolt.
- Without WebGL2 it is a plain frosted list.

Source: https://onemorethink.dev/docs/select
