Skip to content
Focsd homeFocsd homeFocsd
Forms

Segmented control

A short row of segments for choosing one value or view, such as Day, Week or Month.

  • Tag: focsd-segmented-control
  • Pattern: Radio group (selection follows focus)
  • Category: Forms

Example

Day Week Month Year Showing the week
HTML
<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.

List Calendar Compact Cozy
HTML
<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>

API

Attributes

AttributePropertyTypeDefault
sizesize's' | 'm' | 'l'm
namenamestring—
disableddisabledbooleanfalse
requiredrequiredbooleanfalse
readonlyreadOnlybooleanfalse
aria-labelhostAriaLabelstring | nullnull
aria-descriptionhostAriaDescriptionstring | nullnull
aria-labelledbyhostAriaLabelledBystring | nullnull
aria-describedbyhostAriaDescribedBystring | nullnull
label-positionlabelPosition'top' | 'start' | 'end' | undefined—
hinthintstring—
errorerror——
warningwarningstring | undefined—
successsuccessstring | undefined—
error-messageerrorMessagestring | undefined—
warning-messagewarningMessagestring | undefined—
warning-patternwarningPatternstring | undefined—
labellabelstring—
valuevaluestring—

Methods

MethodDescription
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

EventDescription
inputFired on every user edit of the value.
changeFired when the user commits a change to the value or state.

Slots

SlotDescription
(default)The focsd-segment elements.

CSS parts

Style the inside of the component with ::part(), for example focsd-segmented-control::part(group).

PartDescription
groupThe group element (role=radiogroup).
labelThe label element.
segmentSet on every segment element.
segment-selectedSet on the selected segment element.
layoutThe wrapper around the label and the body.
bodyThe group and the messages.
hintThe hint text.
messageThe shown message (also error, warning or success).
message-iconThe icon in the message.
announcerThe hidden region that announces message changes.

Accessibility

  • A radio group: the control is named by its label and 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, hint and messages work as on focsd-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

KeyAction
Arrow keysMove to the next or previous segment and select it (Left and Right swap in right-to-left text)
Home / EndFirst / last segment
TabMove 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.