Segmented Control

A compact row of exclusive choices with a sliding indicator.

Active issues

Installation

pnpm dlx shadcn@latest add checkfu/uicontext/segmented-control

Usage

import { SegmentedControl } from "./ui/uicontext/components/SegmentedControl.ts"
const Message = UiSchema.TaggedEnum({ PickedRange: { value: Schema.String } })export const RangeControl = (h: Html.H<typeof Message.Type>, range: string) =>	SegmentedControl(h, {		label: "Range",		value: range,		options: [			{ value: "day", label: "Day" },			{ value: "week", label: "Week" },			{ value: "month", label: "Month" },		],		onPick: (value) => Message.PickedRange({ value }),	})

Examples

In a field, full width

With fullWidth the segments share the field's width. The field's label names the group.

Appearance

System follows your device.

Small, with a disabled segment

The arrow keys skip a disabled segment.

Runs this week: 1,284

API reference

Options

SegmentedControl options
NameTypeDefaultDescription
keystring—Names the control among others in the same view; needed for more than one
valuerequiredstring | null—The picked segment's value, or null for none. The caller keeps it.
optionsrequiredSegment[]—Two to eight segments
onPickrequired(value: string) => Message—What a pick asks for: the value to store and pass back as value
labelstring—The accessible name of the group, such as "View"; leave it out inside a FormField
size"sm" | "md"mdThe control's height; md by default.
fullWidthboolean—Stretch to the container's width
disabledboolean—No segment can be picked.
readOnlyboolean—Shows the pick and stays focusable, but no segment can be picked.
namestring—The form field: the picked value submits under name
fieldFieldControlAttrs—A FormField's attributes (with group: true): its label names the control
styleStyle—StyleX styles applied after the control’s own.

Accessibility

  • A radio group and one Tab stop: the arrow keys move and pick, and screen readers hear "radio, 2 of 3".
  • The group is named by label; the sliding indicator and any icons are hidden from screen readers.

Source