Skip to content

Action

SegmentedControl

Segmented toggle group for mutually exclusive options.

import { SegmentedControl } from '@var-ui/react';

SegmentedControl

Segmented toggle group for mutually exclusive options. Built on React Aria RadioGroup with an animated indicator. Prefer this over a row of standalone ToggleButtons when only one choice should be active.

Examples

Default

Pass options as { value, label } and seed selection with defaultValue.

Demo.tsxTSX
import { SegmentedControl } from '@var-ui/react';

<SegmentedControl
  aria-label="View"
  defaultValue="list"
  options={[
    { value: 'list', label: 'List' },
    { value: 'grid', label: 'Grid' },
  ]}
/>

Controlled selection

Drive selection with value and onValueChange when the active segment must sync with app state.

Demo.tsxTSX
import { SegmentedControl } from '@var-ui/react';
import { useState } from 'react';

const [value, setValue] = useState<string | null>('list');

<SegmentedControl
  aria-label="View"
  value={value}
  onValueChange={setValue}
  options={[
    { value: 'list', label: 'List' },
    { value: 'grid', label: 'Grid' },
    { value: 'board', label: 'Board' },
  ]}
/>

Props

Accessibility

Each segment is a radio button inside a role="radiogroup"; keyboard users Tab to the checked option (or the first option when none is checked) and move between options with arrow keys (React Aria). Give the group an accessible name with aria-label or aria-labelledby. Options are { value, label, icon?, disabled?, aria-label? }; the control is always single-select.

Icons

An option’s icon renders before its label. It takes a registry name ('colorModeDark') or, in React, any node; in Astro, { svg } markup (for example a glyph from @var-ui/icons/glyphs/<name>). With layout="icon" only the icon shows, and the label becomes the segment’s accessible name and (in Astro) its tooltip. The same options array works in React and Astro, including when it’s built with map, which Astro’s named slots can’t do.

TS
const options = [
  { value: 'list', label: 'List', icon: 'menu' },
  { value: 'grid', label: 'Grid', icon: { svg: gridSvg } }, // Astro; in React pass <GridIcon />
];