Segmented control
A short row of segments for choosing one value or view, such as Day, Week or Month.
Example
<form class="stack" id="segmented-demo-form"> <focsd-segmented-control label="Calendar view" name="view" value="week" hint="The calendar changes at once"> <focsd-segment value="day">Day</focsd-segment> <focsd-segment value="week">Week</focsd-segment> <focsd-segment value="month">Month</focsd-segment> <focsd-segment value="year" disabled>Year</focsd-segment> </focsd-segmented-control> <output id="segmented-demo-out" for="segmented-demo-form" aria-live="polite" >Showing the week</output ></form>
<script type="module">const form = document.getElementById('segmented-demo-form');const out = document.getElementById('segmented-demo-out');
form.addEventListener('change', (event) => { out.textContent = 'Showing the ' + event.target.value;});</script>Sizes and icons
A segment can carry an icon next to its text. size takes s, m or l.
<div class="stack"> <focsd-segmented-control label="Layout" size="l" value="list"> <focsd-segment value="list" icon="menu">List</focsd-segment> <focsd-segment value="board" icon="calendar">Calendar</focsd-segment> </focsd-segmented-control> <focsd-segmented-control label="Density" size="s" value="cozy"> <focsd-segment value="compact">Compact</focsd-segment> <focsd-segment value="cozy">Cozy</focsd-segment> </focsd-segmented-control></div>Usage
Import the module once, then use the tag like any HTML element. Every framework below renders the same element; see Frameworks for setup.
<script type="module"> import '@focsd-dev/components/focsd-segmented-control.js';</script>
<focsd-segmented-control label="Calendar view" name="view" value="week" hint="The calendar changes at once">…</focsd-segmented-control>import { SegmentedControl } from '@focsd-dev/react';
export function Example() { return <SegmentedControl onChange={(e) => console.log(e.target)} label="Calendar view" name="view" value="week" hint="The calendar changes at once">…</SegmentedControl>;}<script setup>import '@focsd-dev/components/focsd-segmented-control.js';</script>
<template> <focsd-segmented-control @change="onChange" label="Calendar view" name="view" value="week" hint="The calendar changes at once">…</focsd-segmented-control></template>// Add CUSTOM_ELEMENTS_SCHEMA to the component's schemas, then:import '@focsd-dev/components/focsd-segmented-control.js';
// template<focsd-segmented-control (change)="onChange($event)" label="Calendar view" name="view" value="week" hint="The calendar changes at once">…</focsd-segmented-control><script> import '@focsd-dev/components/focsd-segmented-control.js';</script>
<focsd-segmented-control onchange={(e) => console.log(e.target)} label="Calendar view" name="view" value="week" hint="The calendar changes at once">…</focsd-segmented-control>API
Attributes
| Attribute | Property | Type | Default |
|---|---|---|---|
size | size | 's' | 'm' | 'l' | m |
name | name | string | — |
disabled | disabled | boolean | false |
required | required | boolean | false |
readonly | readOnly | boolean | false |
aria-label | hostAriaLabel | string | null | null |
aria-description | hostAriaDescription | string | null | null |
aria-labelledby | hostAriaLabelledBy | string | null | null |
aria-describedby | hostAriaDescribedBy | string | null | null |
label-position | labelPosition | 'top' | 'start' | 'end' | undefined | — |
hint | hint | string | — |
error | error | — | — |
warning | warning | string | undefined | — |
success | success | string | undefined | — |
error-message | errorMessage | string | undefined | — |
warning-message | warningMessage | string | undefined | — |
warning-pattern | warningPattern | string | undefined | — |
label | label | string | — |
value | value | string | — |
Methods
| Method | Description |
|---|---|
describedByIds() | IDs to add to the inner control's aria-describedby: the hint, then the shown message. |
checkValidity() | True when the value is valid; otherwise fires invalid and returns false, like native. |
reportValidity() | Like checkValidity(), and also shows the browser's validation message when invalid. |
setCustomValidity(message) | Set (non-empty) or clear (empty string) a custom validation error, like native controls. |
setValidityState(flags, message, anchor) | For subclasses: set the validity flags and message. anchor is the element the browser points its message at. |
validate() | Show (or clear) the error from the control's own checks: required, type, pattern, min,
max and the like. Uses error-message, else the browser's localized message. An error the
author set (a server error, say) is left as it is. |
clearValidation() | Clear the error and warning that validate() and warning-pattern showed (a form reset does). |
emitValue(commit) | For subclasses: fire the value events, as a native control does on a user edit: input
(bubbles, composed), then with commit also change (bubbles, stays in its tree). |
idrefText(ids) | For subclasses: the text of the elements an ID reference list names, resolved in the host's tree (ID references cannot cross a shadow root), joined by spaces. |
Events
| Event | Description |
|---|---|
input | Fired on every user edit of the value. |
change | Fired when the user commits a change to the value or state. |
Slots
| Slot | Description |
|---|---|
(default) | The focsd-segment elements. |
CSS parts
Style the inside of the component with ::part(), for example focsd-segmented-control::part(group).
| Part | Description |
|---|---|
group | The group element (role=radiogroup). |
label | The label element. |
segment | Set on every segment element. |
segment-selected | Set on the selected segment element. |
layout | The wrapper around the label and the body. |
body | The group and the messages. |
hint | The hint text. |
message | The shown message (also error, warning or success). |
message-icon | The icon in the message. |
announcer | The hidden region that announces message changes. |
Accessibility
- A radio group: the control is named by its
labeland each segment is a radio. Screen readers say "Week, radio button, 2 of 4, selected". - Selection follows focus, so one arrow press both moves and chooses. Use it when the choice takes effect at once, and a radio group when the person confirms with a button.
- One tab stop for the whole row. Disabled segments are skipped; the arrows wrap from the last segment to the first.
- Every segment is at least 44 px tall at every
size, and under 20rem wide the segments stack so the labels never shrink. - It is form-associated:
name,value,required,disabled,hintand messages work as onfocsd-radio-group, and a form reset restores the value it started with. - Not a toolbar: for several buttons that each switch on and off, use
focsd-toggle-group. Choosing a toggle compares them.
Keyboard
| Key | Action |
|---|---|
| Arrow keys | Move to the next or previous segment and select it (Left and Right swap in right-to-left text) |
| Home / End | First / last segment |
| Tab | Move into the group, then out of it |
Sizes and icons
- The icon is decoration: the segment is named by its text, so an icon never replaces the label.
- Size changes the font and the inline padding only. Targets stay 44 px tall.
- In forced colors the selected segment uses the system selected-item colors, not a tint.
Ask the docs
Hi, what are you building?
Ask about any component, theming, forms or frameworks. I answer with the right component, a snippet and a link to its docs.