Skip to content
Nacre
Color palette
中文

Segmented

stableSegmentedOpen in playground
Segmented
Runs the real component in your browser.

Picks one of 2–5 mutually exclusive values, all visible at a glance; controlled value + onChange, with the selection sliding over on a spring.

Status: stable · Components: Segmented

import { Segmented } from 'nacre'
import { signal } from 'nsh'
import { Segmented } from 'nacre'
export function SegmentedUsage() {
const range = signal<'today' | 'week' | 'month'>('today')
return (
<Segmented
accessibilityLabel="Time range"
value={range()}
onChange={(value) => range.set(value)}
options={[
{ value: 'today', label: 'Today' },
{ value: 'week', label: 'This week' },
{ value: 'month', label: 'This month' },
]}
/>
)
}

count follows the name in tabular digits; the screen reader name combines them (“Pending 3”).

import { signal } from 'nsh'
import { Segmented } from 'nacre'
const sessionTabs = () => [
{ value: 'current' as const, label: 'Current chats' },
{ value: 'pending' as const, label: 'Pending', count: '3' },
{ value: 'closed' as const, label: 'Closed' },
]
export function SegmentedCounts() {
const tab = signal<'current' | 'pending' | 'closed'>('current')
return <Segmented accessibilityLabel="Chats" value={tab()} onChange={(value) => tab.set(value)} options={sessionTabs()} />
}

xs 24 (in section title rows) / sm 28 (default) / md 32; there’s no lg. Controls in one row share one size.

Phone: On phones: xs 28 / sm 32 (default, the iOS segmented control) / md 40, with equal-width options when it fills the row; each option’s tap area is padded to 44.

import { View, signal } from 'nsh'
import { Segmented } from 'nacre'
const sessionTabs = () => [
{ value: 'current' as const, label: 'Current chats' },
{ value: 'pending' as const, label: 'Pending', count: '3' },
{ value: 'closed' as const, label: 'Closed' },
]
export function SegmentedSizes() {
const tab = signal<'current' | 'pending' | 'closed'>('pending')
return (
<View class="gap-2">
{(['xs', 'sm', 'md'] as const).map((size) => (
<Segmented size={size} accessibilityLabel={`$Chats ${size}`} value={tab()} onChange={(value) => tab.set(value)} options={sessionTabs()} />
))}
</View>
)
}

The whole group can’t be pressed and drops to 40% opacity.

import { Segmented } from 'nacre'
export function SegmentedDisabled() {
return (
<Segmented
disabled
accessibilityLabel="Time range (no permission)"
value="today"
onChange={() => {}}
options={[
{ value: 'today', label: 'Today' },
{ value: 'week', label: 'This week' },
]}
/>
)
}
Key Action
Tab The group is one Tab stop: entering lands on the selected segment, pressing again leaves the group
← → Left and Right select adjacent segments, wrapping at the ends; in tabs mode Home and End go to the first and last. Horizontal tabs leave Up and Down to the page.
Space / Return Select the focused segment
  • By default it is a radio group (nsh/base RadioGroup), with the selected state read as checked; when it switches content panels (preview / code, page sections) pass tabs and it becomes a set of tabs (nsh/base Tabs), read as selected. accessibilityLabel says what the group picks (“Time range”).
  • A lighter fill and a single contact shadow mark the selected segment. Keyboard focus adds a 1pt inset outline. Segment width and font weight stay fixed.

Do

  • A few options that should all be visible at a glance
  • Add counts if needed (“Pending 3”)
  • Keep option text short (2–4 characters)

Don’t

  • Use more than 5 options (use a select)
  • Use it as navigation
  • Show selection with font weight
Prop Type Default Description
options * readonly SegmentedOption<T>[] — Segments in display order, each with a value, label and optional count.
value * T — Currently selected value. Update it from onChange.
onChange * (value: T) => void — Called when another segment is selected. Each option uses the test identifier ${testID}-${value}.
size 'xs' | 'sm' | 'md' 'sm' Desktop track heights are 24, 28 and 32 points for xs, sm and md. The default is sm.
accessibilityLabel string — Spoken name for the whole group, such as Time range.
testID string — Base test identifier for the group.
disabled boolean — Disables the entire group.
tabs boolean — Use tab semantics to switch content panels and radio-group semantics to select a value. Set when the component is created.
focusRef (focus: () => void) => void — Provides a function that moves keyboard focus to the selected segment.

* required