Choose one of two to five options, all in view, drawn as one control over real radio inputs.
<fieldset class="cap-seg" id="seg-window">
<legend>Window</legend>
<div class="cap-seg-options">
<label><input type="radio" name="window" value="24h" checked /> 24 hours</label>
<label><input type="radio" name="window" value="7d" /> 7 days</label>
<label><input type="radio" name="window" value="30d" /> 30 days</label>
</div>
</fieldset>
Installation
// package.json: "capsomer": "github:DrDustinEdwards/capsomer#v0.152.2"
import "capsomer/tokens.css";
import "capsomer/base.css";
import "capsomer/segmented.css";
import "capsomer/tokens.css";
import "capsomer/segmented.css";
import { Segmented } from "capsomer/react/segmented";
Usage
<fieldset class="cap-seg">
<legend>Window</legend>
<div class="cap-seg-options">
<label><input type="radio" name="window" value="24h" checked /> 24 hours</label>
<label><input type="radio" name="window" value="7d" /> 7 days</label>
<label><input type="radio" name="window" value="30d" /> 30 days</label>
</div>
</fieldset>
<Segmented legend="Window" value={win} onChange={setWin} options={[
{ value: "24h", label: "24 hours" }, { value: "7d", label: "7 days" }, { value: "30d", label: "30 days" },
]} />
Examples
Default, nothing chosen yet
<fieldset class="cap-seg" id="seg-layout">
<legend>Compare drafts</legend>
<div class="cap-seg-options">
<label><input type="radio" name="layout" value="side" /> Side by side</label>
<label><input type="radio" name="layout" value="inline" /> Inline</label>
</div>
</fieldset>
Keyboard focus and hover
<p class="cap-muted">Tab into the control to see focus. Its second segment is shown hovered.</p>
<fieldset class="cap-seg" id="seg-theme">
<legend class="cap-sr-only">Theme</legend>
<div class="cap-seg-options">
<label><input type="radio" name="theme" value="system" checked /> System</label>
<label id="seg-hovered"><input type="radio" name="theme" value="light" /> Light</label>
<label><input type="radio" name="theme" value="dark" /> Dark</label>
</div>
</fieldset>
A disabled option
<fieldset class="cap-seg" id="seg-view" aria-describedby="seg-view-why">
<legend>View</legend>
<div class="cap-seg-options">
<label><input type="radio" name="view" value="list" checked /> List</label>
<label><input type="radio" name="view" value="board" disabled /> Board</label>
<label><input type="radio" name="view" value="calendar" /> Calendar</label>
</div>
</fieldset>
<span class="cap-btn-reason" id="seg-view-why">Board needs at least one column. Add one in Settings.</span>
Small, large, with icons, vertical and filling the row
<div class="cap-specimen-row">
<fieldset class="cap-seg" id="seg-sm">
<legend>Density</legend>
<div class="cap-seg-options" data-size="sm">
<label><input type="radio" name="dens" value="compact" checked /> Compact</label>
<label><input type="radio" name="dens" value="comfortable" /> Comfortable</label>
</div>
</fieldset>
<fieldset class="cap-seg" id="seg-lg">
<legend>Chart</legend>
<div class="cap-seg-options" data-size="lg">
<label><input type="radio" name="chart" value="line" checked /> Line</label>
<label><input type="radio" name="chart" value="bar" /> Bar</label>
</div>
</fieldset>
<fieldset class="cap-seg" id="seg-icons">
<legend>List layout</legend>
<div class="cap-seg-options">
<label><input type="radio" name="lay" value="rows" checked /><svg viewBox="0 0 16 16" aria-hidden="true" focusable="false"><path d="M2 4h12M2 8h12M2 12h12" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" fill="none" /></svg> Rows</label>
<label><input type="radio" name="lay" value="grid" /><svg viewBox="0 0 16 16" aria-hidden="true" focusable="false"><path d="M2 2h5v5H2zM9 2h5v5H9zM2 9h5v5H2zM9 9h5v5H9z" fill="currentColor" /></svg> Grid</label>
</div>
</fieldset>
</div>
<div class="cap-specimen-row">
<fieldset class="cap-seg" id="seg-vertical">
<legend>Sort</legend>
<div class="cap-seg-options" data-orientation="vertical">
<label><input type="radio" name="sort" value="new" checked /> Newest</label>
<label><input type="radio" name="sort" value="old" /> Oldest</label>
<label><input type="radio" name="sort" value="az" /> A to Z</label>
</div>
</fieldset>
<fieldset class="cap-seg" id="seg-fill">
<legend>Range</legend>
<div class="cap-seg-options" data-fill>
<label><input type="radio" name="range" value="d" checked /> Day</label>
<label><input type="radio" name="range" value="w" /> Week</label>
<label><input type="radio" name="range" value="m" /> Month</label>
</div>
</fieldset>
</div>
Invalid
<fieldset class="cap-seg" id="seg-invalid" aria-describedby="seg-invalid-e">
<legend>Check every</legend>
<div class="cap-seg-options">
<label><input type="radio" name="every" value="1" aria-invalid="true" /> 1 minute</label>
<label><input type="radio" name="every" value="5" aria-invalid="true" /> 5 minutes</label>
</div>
</fieldset>
<p class="cap-field-error" id="seg-invalid-e">Choose how often to check.</p>
Comfortable density
<fieldset class="cap-seg" data-density="comfortable" id="seg-comf">
<legend>Window</legend>
<div class="cap-seg-options">
<label><input type="radio" name="windowc" value="24h" checked /> Last day</label>
<label><input type="radio" name="windowc" value="7d" /> Last week</label>
</div>
</fieldset>
Cards, each chosen option in its tone
<fieldset class="cap-seg" id="seg-cards">
<legend>Decline type</legend>
<div class="cap-seg-options" data-layout="cards">
<label data-tone="info"><input type="radio" name="decline" value="soft" checked /> <strong>Soft decline</strong><br />Insufficient funds</label>
<label data-tone="warn"><input type="radio" name="decline" value="network" /> <strong>Network error</strong><br />Processing failure</label>
<label><input type="radio" name="decline" value="expired" /> <strong>Expired card</strong><br />Card needs updating</label>
<label data-tone="crit"><input type="radio" name="decline" value="fraud" /> <strong>Fraud flag</strong><br />Bank flagged the charge</label>
</div>
</fieldset>
API reference
Segmented
| Prop | Type | Required | Does |
|---|
legend | ReactNode | Yes | What is being chosen. Always given; hideLegend keeps it for screen readers only, where the context already says it (a theme switch in the top bar). |
|---|
hideLegend | boolean | | |
|---|
options | Array<SegmentedOption<V>> | Yes | Two to five. |
|---|
value | V | null | Yes | |
|---|
onChange | (value: V) => void | Yes | |
|---|
name | string | | The radios' name, for a form; one is made up otherwise. |
|---|
describedBy | string | | The id of text that says why an option is disabled. |
|---|
size | "sm" | "default" | "lg" | | |
|---|
orientation | "horizontal" | "vertical" | | |
|---|
fill | boolean | | Fill the row, the segments sharing it. |
|---|
layout | "segments" | "cards" | | "cards": each option its own bordered card, two to a row and four from 640px. |
|---|
invalid | boolean | | Wrong: marks the radios aria-invalid; say why in text that describedBy names. |
|---|
CSS variables
| Variable | Default |
|---|
--cap-seg-fs | var(--fs-body) |
|---|
--cap-seg-h | max(var(--target), calc(var(--control) - 6px)) |
|---|
--cap-seg-tone | var(--accent) |
|---|
--cap-seg-tone-soft | var(--accent-soft) |
|---|
--cap-seg-tone-text | var(--accent-text) |
|---|
Parts and data attributes
| Class | Is |
|---|
.cap-seg | the root |
|---|
.cap-seg-options | a part |
|---|
| Attribute | Values |
|---|
data-fill | present or absent |
|---|
data-layout | cards |
|---|
data-orientation | vertical |
|---|
data-size | lg sm |
|---|
data-tone | crit info ok warn |
|---|
| State | Read from |
|---|
:checked | the platform |
|---|
:disabled | the platform |
|---|
:focus-visible | the platform |
|---|
:hover | the platform |
|---|
[aria-invalid="true"] | an attribute |
|---|
Accessibility
| Key | Does |
|---|
| Tab | Enters the control at the chosen segment (or the first, if none is chosen), and leaves it in one step |
| Arrow keys | Move to the next or previous segment and choose it, skipping a disabled one |
| Space | Chooses the focused segment, when none is chosen yet |
- A group named by its legend, holding radios named by their labels.
- Each segment is at least
var(--target) tall and the whole segment is the hit area. - Forced colors: the chosen segment is drawn in Highlight; a disabled one in GrayText.