suiss/ui ยท Pattern
Segment Picker
A segmented control for one exclusive choice that becomes a native select on mobile.
Segment Picker is an open-source pattern from suiss/ui for React. The @esuiss/ui CLI copies its source into your project, so you own the code.
SegmentPicker presents a small set of mutually exclusive options, such as a time period or a view mode. On desktop it renders a suiss/ui ToggleGroup in which exactly one segment stays pressed. On mobile it renders a NativeSelect so the choice fits narrow screens and uses the platform picker.
Use it for two to five short options that change how content is displayed.
Installation
Works in any project with a components.json. See Installation for the one-line registry setup. Or use the suiss/ui CLI:
-
Add the pieces it builds on: Native Select, Toggle Group,
use-mobile. -
Copy and paste the following code into your project.
- Update the import paths to match your project setup.
Usage
Composition
Use the following composition to build a SegmentPicker:
Examples
Without value
Omit value and defaultValue to start with no segment pressed. The mobile select then shows an empty disabled option.
Outline
Set variant="outline" to give the desktop segments a border.
Spaced
Set spacing to put a gap between the desktop segments; 0 joins them.
Sizes
Use the size prop to choose sm, default or lg. On mobile, lg falls back to the default size.
With icons
Give icon-only segments an aria-label. It names the segment and is also used as the mobile option text.
With icons and text
Combines icons and text. Set label on segments with mixed content to give the mobile option its text.
Disabled item
Set disabled on a SegmentPickerItem to prevent selecting that segment.
Disabled
Set disabled on SegmentPicker to disable every segment.
Controlled
Control the value with value and onValueChange. onValueChange fires only when the selection changes; clicking the pressed segment keeps it selected.
Accessibility
- On desktop the root is a
groupnamed byaria-label, and each segment is a button witharia-pressed. - Disabled segments are disabled buttons and cannot be selected.
- On mobile the control is a native
select.
API Reference
SegmentPicker
The root that renders the toggle group or native select and holds the value.
| Prop | Type | Default |
|---|---|---|
value | string | - |
defaultValue | string | - |
onValueChange | (value: string) => void | - |
variant | "default" | "outline" | "default" |
size | "sm" | "default" | "lg" | "default" |
spacing | number | 0 |
id | string | - |
name | string | - |
disabled | boolean | - |
className | string | - |
children | React.ReactNode | - |
aria-label | string | - |
aria-describedby | string | - |
The value is uncontrolled by default; use defaultValue, or value with onValueChange to control it. variant and spacing apply only on desktop. id and className go to the group on desktop and to the select on mobile. With name, desktop renders a hidden input so the value is submitted with a form.
The picker switches to the native select below 48rem, via useIsMobile. It sets data-slot="segment-picker", and each item sets data-slot="segment-picker-item", for styling.
SegmentPickerItem
One option, rendered as a toggle on desktop and an option on mobile.
| Prop | Type | Default |
|---|---|---|
value | string | Required |
label | string | - |
disabled | boolean | - |
className | string | - |
children | React.ReactNode | - |
aria-label | string | - |
On mobile each option shows label, then aria-label, then string children, then value. On desktop, label is the segment content when there are no children, and className goes to the toggle. SegmentPickerItem throws when used outside SegmentPicker.