Segmented Control
Picks one of a few options from a row of buttons, with an indicator that slides to the choice.
It works like a radio group: one value and a single tab stop, with the arrow keys moving
between items. Name it with aria-label, or put it in a Field whose label names it. The docs
site’s own theme and mode switchers are segmented controls.
Sizes and disabled items
size="sm" suits toolbars. A disabled item is skipped by the arrow keys.
Dependencies
What it's built from and what's built from it in Mocha, read from the imports in its source.
| Uses | Textprimitive |
|---|---|
| Used by | Nothing yet. |
| Packages | @ui-sdk/core@0.1.0-beta.20@ui-sdk/react@0.1.0-beta.20 |
Installation
Set the project up for Mocha once first, as Getting started shows. Then add it with the CLI, or by hand.
With the CLI
Manually
Install its packages:
Add the components it uses first, listed under Dependencies.
Then copy the source:
API reference
Generated from Mocha's types. Props marked ours are added here; the rest come from the ui-sdk part it wraps. Every part also accepts its element's native attributes.
SegmentedControl
Picks one of a few options from a row of buttons, with an indicator that slides to the choice. The indicator is built in. Name the control with aria-label, or put it in a Field whose label names it.
- sizeours
Height of the items. Their text is small and bold at either size, like a button's.
Type "sm" | "md" · Default md
- children
Content, or a function of the part's state that returns it.
Type ReactNode | ((state: SegmentedControlState) => ReactNode)
- className
Class names, or a function of the part's state that returns them.
Type string | ((state: SegmentedControlState) => string)
- controller
Checks items from anywhere. Create one with
createSegmentedControlController().Type { getValue: () => T | null; setValue: (value: T | null, details?: SegmentedControlChangeOptions) => boolean; focus: () => void; }
- debug
Logs every state change with its reason to the console, in development builds.
Type boolean
- defaultValue
The item checked at first, when uncontrolled. A form reset returns to it.
Type string | null
- disabled
Disables every item: they can't be focused or changed, and the value isn't submitted.
Type boolean
- form
The id of a form to belong to, when the segmented control isn't inside that form.
Type string
- loop
Whether arrow keys wrap from the last item to the first.
Type boolean · Default true
- name
The name the value submits under. Without one, the segmented control submits nothing.
Type string
- onValueChange
Runs when a user or a form reset wants to change the value.
details.reasonsays why, anddetails.cancel()stops it.Type (value: T | null, details: ChangeDetails<SegmentedControlValueChangeReason>) => void
- orientation
Which way the items run, for styling and for screen readers. Arrow keys on both axes move between items either way, as in any radio group.
Type SegmentedControlOrientation · Default "horizontal"
- readOnly
Keeps the items focusable and the value submitted, but stops changes.
Type boolean
- render
Renders your own element: one that receives the part's props, or a function of the props and the state.
Type RenderProp<SegmentedControlState>
- required
Blocks form submission until an item is checked, with the browser's own message.
Type boolean
- style
Styles, or a function of the part's state that returns them.
Type CSSProperties | ((state: SegmentedControlState) => CSSProperties)
- value
The checked item's value. Pass it to control the segmented control.
nullchecks nothing.Type string | null
SegmentedControlIndicator
The sliding background behind the checked item. SegmentedControl renders one for you.
- children
Content, or a function of the part's state that returns it.
Type ReactNode | ((state: NoInfer<SegmentedControlIndicatorState>) => ReactNode)
- className
Class names, or a function of the part's state that returns them.
Type string | ((state: NoInfer<SegmentedControlIndicatorState>) => string)
- followHover
Whether it follows the item under the pointer, and goes back to the checked one when the pointer leaves the control.
Type boolean
- render
Renders your own element: one that receives the part's props, or a function of the props and the state.
Type RenderProp<NoInfer<SegmentedControlIndicatorState>>
- style
Styles, or a function of the part's state that returns them.
Type CSSProperties | ((state: NoInfer<SegmentedControlIndicatorState>) => CSSProperties)
SegmentedControlItem
One option. Its value becomes the control's value when it's checked.
- children
Content, or a function of the part's state that returns it.
Type ReactNode | ((state: SegmentedControlItemState) => ReactNode)
- className
Class names, or a function of the part's state that returns them.
Type string | ((state: SegmentedControlItemState) => string)
- disabled
Stops presses. The button is natively disabled, so it leaves the tab order and its form doesn't submit through it, unless
focusableWhenDisabledis set.Type boolean · Default false
- render
Renders your own element: one that receives the part's props, or a function of the props and the state.
Type RenderProp<SegmentedControlItemState>
- style
Styles, or a function of the part's state that returns them.
Type CSSProperties | ((state: SegmentedControlItemState) => CSSProperties)
- value*
What the value becomes when this item is checked.
Type string