# GlassTabs

> A segmented control whose selection travels as a glass lens.

```tsx
import NumberFlow from '@number-flow/react'
import { useState } from 'react'

import { GlassTabs } from '@/components/liquid-glass'

const distance = { day: 8.4, week: 52.1, month: 214, year: 2480 }

// Click or drag: the selection slides as a lens that bends the labels it passes.
// The figure rolls to its new value with NumberFlow (`@number-flow/react`).
export default function Demo() {
  const [range, setRange] = useState<keyof typeof distance>('week')
  return (
    <div className="flex flex-col items-center gap-8">
      <div className="text-center">
        <p className="text-6xl font-semibold tracking-tight text-neutral-900 tabular-nums dark:text-white">
          <NumberFlow value={distance[range]} locales="en-US" />
          <span className="ml-1.5 text-2xl font-medium text-neutral-400 dark:text-neutral-500">km</span>
        </p>
        <p className="mt-2 text-sm text-neutral-500 dark:text-neutral-400">Distance this {range}</p>
      </div>
      <GlassTabs
        aria-label="Range"
        size="lg"
        value={range}
        onValueChange={setRange}
        options={[
          { value: 'day', label: 'Day' },
          { value: 'week', label: 'Week' },
          { value: 'month', label: 'Month' },
          { value: 'year', label: 'Year' },
        ]}
      />
    </div>
  )
}
```

**Use it for**

- Switching a view or a range in place: Day / Week / Month, List / Grid.
- Two to five short options.

**Not for**

- Navigating between pages: use links.
- Long lists of options: use a select.

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

> The behaviour (one tab stop, arrow keys, a hidden radio per option for forms) comes from UI SDK's segmented control, so Tabs 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 { GlassTabs } from '@/components/liquid-glass'

const [view, setView] = useState('week')

<GlassTabs
  aria-label="Range"
  value={view}
  onValueChange={setView}
  options={[
    { value: 'day', label: 'Day' },
    { value: 'week', label: 'Week' },
  ]}
/>
```

## Examples

### With icons

```tsx
import { LayoutGrid, List, Rows3 } from 'lucide-react'
import { useState } from 'react'

import { GlassTabs } from '@/components/liquid-glass'

// What each layout looks like: how many blocks, in how many columns, how tall.
const layouts = {
  list: { blocks: 4, columns: 'grid-cols-1', block: 'h-6' },
  grid: { blocks: 6, columns: 'grid-cols-3', block: 'h-14' },
  rows: { blocks: 2, columns: 'grid-cols-1', block: 'h-14' },
}

// Icons next to the labels, at a larger size.
export default function Demo() {
  const [layout, setLayout] = useState<keyof typeof layouts>('grid')
  const { blocks, columns, block } = layouts[layout]
  return (
    <div className="flex flex-col items-center gap-8">
      <div aria-hidden className={`grid h-[120px] w-72 content-start gap-2 ${columns}`}>
        {Array.from({ length: blocks }, (_, i) => (
          <span key={i} className={`rounded-lg bg-black/10 dark:bg-white/10 ${block}`} />
        ))}
      </div>
      <GlassTabs
        aria-label="Layout"
        size="lg"
        value={layout}
        onValueChange={setLayout}
        options={[
          { value: 'list', label: 'List', icon: <List /> },
          { value: 'grid', label: 'Grid', icon: <LayoutGrid /> },
          { value: 'rows', label: 'Rows', icon: <Rows3 /> },
        ]}
      />
    </div>
  )
}
```

When the selection changes, the highlight leaves its option, becomes clear glass that bends the labels it passes, and settles as a flat pill again. Press and hold to open the lens in place; drag and release to choose.

## Your own markup

Already have tabs (Radix Tabs, your own radio group)? Put the lens on them with `GlassLens`, which ships with GlassTabs:

- The container is `relative isolate` and `<GlassLens value={value} />` is its first child.
- Options carry `role="radio"` or `role="tab"` (Radix Tabs works as is)
- The selected option contains the resting highlight, marked `data-glass-highlight`. Its background colour is the lens colour and its corners are the lens corners: `rounded-full` travels as a pill, `rounded-lg` as a rounded rectangle.
- Hide the highlight while the lens moves: `[[data-glass-moving]_&]:opacity-0`.

### A billing switch with GlassLens

```tsx
import NumberFlow from '@number-flow/react'
import { useState } from 'react'

import { GlassLens } from '@/components/liquid-glass'

const plans = {
  Monthly: { price: 12, note: 'per month, cancel any time' },
  Yearly: { price: 96, note: 'per year, four months free' },
}
type Plan = keyof typeof plans
const usd = { style: 'currency', currency: 'USD', maximumFractionDigits: 0 } as const

// Your own segmented control: role="radio" options and a `data-glass-highlight` pill.
// The price rolls to its new value with NumberFlow (`@number-flow/react`).
export default function Demo() {
  const [plan, setPlan] = useState<Plan>('Yearly')
  return (
    <div className="flex flex-col items-center gap-8">
      <div className="text-center">
        <p className="text-6xl font-semibold tracking-tight text-neutral-900 tabular-nums dark:text-white">
          <NumberFlow value={plans[plan].price} locales="en-US" format={usd} />
        </p>
        <p className="mt-2 text-sm text-neutral-500 dark:text-neutral-400">{plans[plan].note}</p>
      </div>
      <div role="radiogroup" aria-label="Billing" className="relative isolate inline-flex gap-1 rounded-2xl bg-neutral-900 p-1.5">
        <GlassLens value={plan} />
        {(Object.keys(plans) as Plan[]).map((p) => (
          <button
            key={p}
            type="button"
            role="radio"
            aria-checked={p === plan}
            onClick={() => setPlan(p)}
            className={`relative h-11 cursor-pointer rounded-xl px-7 text-[15px] font-semibold ${
              p === plan ? 'text-neutral-900 [[data-glass-traveling]_&]:text-white/60' : 'text-white/60'
            }`}
          >
            {p === plan && <span data-glass-highlight className="absolute inset-0 rounded-xl bg-white [[data-glass-moving]_&]:opacity-0" />}
            <span className="relative z-20">{p}</span>
          </button>
        ))}
      </div>
    </div>
  )
}
```

## API reference

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `options` | `{ value, label, icon?, aria-label? }[]` | — | The segments. |
| `value` | `string \| null` | — | Selected value (controlled). |
| `onValueChange` | `(value) => void` | — | Called with the new value. |
| `aria-label` | `string` | — | Required: names the group for screen readers. |
| `name` | `string` | — | Submits the selected value under this name with the form around it. |
| `size` | `'sm' \| 'md' \| 'lg'` | `'md'` | Segment height 28, 32 or 40 px. |
| `highlightClassName` | `string` | `'bg-white dark:bg-neutral-700'` | Classes of the resting pill; its background is the lens colour. |
| `GlassLens value` | `string \| number \| null` | — | Your own markup: the selected value; any change moves the lens. |
| `GlassLens disabled` | `boolean` | `false` | No lens; the highlight simply jumps. |

## Accessibility

- Radiogroup pattern: one tab stop, arrow keys move the selection.
- Icon-only options take an `aria-label` each.
- With reduced motion the pill simply jumps.

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