Capsule Toggle
Choose one of two or three options in a capsule.
import { CapsuleToggle, CapsuleToggleOption } from '@youj/react'
Installation & usage
npm i @youj/reactFor an app already set up with npx @youj/cli init, which installs the peers (@youj/core, @youj/motion, motion and Base UI) and wires the provider. New app? Start at Installation.
Use a capsule toggle for a small choice that sits with other floating controls: a monitor source, a layout, a snap setting. It's a Segmented Control that uses the capsule as its track, so the selected option glides inside the pill. For more than three options, use a Segmented Control or Tabs.
import { CapsuleToggle } from '@youj/react'
<CapsuleToggle
label="Monitor"
value={monitor}
onValueChange={setMonitor}
options={[
{ value: 'source', label: 'Source' },
{ value: 'mix', label: 'Mix' },
{ value: 'master', label: 'Master' },
]}
/>label puts a short name in front of the options. iconOnly shows only each option's icon, and its label becomes the option's name.
Description
A labeled three-way Monitor toggle, and an icon-only Layout toggle between grid and list.
Code
capsule-toggle-demo.tsx, the Demo story. The component itself comes from the package.
Reference
Propsoptions, label, iconOnly… (9)
Props
| Prop | Type | |
|---|---|---|
optionsRequired | readonly CapsuleToggleOption[]Two or three options. | – |
label | stringA short eyebrow at the start ("View"). It names the group too, unless | – |
iconOnly | booleanShows only the icons; each option is named by its label. default false | false |
size | 'sm' | 'default'default 'default' | 'default' |
aria-label | stringNames the control for assistive tech, e.g. "View" or "Range". | – |
variant | 'underline' | 'pill'
default 'underline' | 'underline' |
defaultValue | stringThe segment pressed at first (uncontrolled). Leave it out to start with none pressed. | – |
value | stringThe pressed segment's value (controlled). | – |
sound | booleanWith sound on ( default true | true |
onValueChange | (value: string) => voidCalled with the newly pressed segment's value. Never called with an empty value. | – |
StylingParts and class names
Styling
Parts: capsule-toggle, capsule-label, then the segmented control's own segmented-control, segmented-control-item and toggle-group-indicator.
MotionChoose
Motion
| Moment | Tokens | ||
|---|---|---|---|
| Choosethe selected key glides to the new option, and changes course if you choose again mid-way | spring.smooth0.36s, bounce 0 | 0.36s, bounce 0 |
Reduced motion
The key jumps to the new option.
AccessibilityKeyboard, ARIA
Accessibility
- It's a group named by
labeloraria-label, with one tab stop. The arrow keys move between options. - One option is always selected, and each option says whether it's pressed.
- With sound on, a new choice plays
select.sound={false}turns it off.