Segmented

Slant / Controls

A radiogroup of options with roving focus, in two sizes.

Demo

Segmented & slider

Usage

@tuttn/slant
import { Segmented } from '@tuttn/slant'

const views = [{ value: 'list', label: 'List' }, { value: 'board', label: 'Board' }] as const

// Controlled: pass value and onValueChange.
const [view, setView] = useState<'list' | 'board'>('board')
<Segmented options={views} value={view} onValueChange={setView} label="Board view" />

// Uncontrolled: leave value out; it starts from defaultValue (or the first option).
// The compact mono variant hugs its options instead of filling the row.
const ranges = [{ value: '7d', label: '7d' }, { value: '1y', label: '1y', disabled: true }] as const
<Segmented size="sm" options={ranges} defaultValue="7d" label="Range" />

// In a form: a name adds a hidden input holding the chosen value.
<Segmented options={views} defaultValue="list" label="Board view" name="view" />

Props

PropTypeDefaultDescription
optionsreadonly SegmentedOption<T>[]—The choices, in order. `T` is inferred from their values.
valueT—The chosen value. Pass it to control the group; leave it out to let the group keep its own.
defaultValueTthe first enabled optionThe value an uncontrolled group starts with.
onValueChange(value: T) => void—Called with the new value when a click or an arrow key chooses an option.
labelstring—The group's accessible name.
size'sm' | 'md''md'`md` fills its row; `sm` is the compact mono variant that hugs its options.
disabledbooleanfalseDisables every option. A disabled group is not submitted with its form.
namestring—Submits the group with a form: a hidden input with this name carries the chosen value.
formstring—The id of the form the hidden input belongs to, when the group sits outside it.
…restHTMLAttributes<HTMLDivElement>—Other attributes and the ref reach the radiogroup element.

Under <Slant dir="rtl"> the Left and Right arrows swap, so Right steps back through the options; Up and Down work the same in either direction.

Full source