Segmented control

One of a few views or modes in a track, over native radios, or links. Plain Blade and CSS; the package's x-ds-glide directive adds a sliding highlight when it is on the page.

Form control. It takes input in a form, so it is drawn on the Every input type block, beside every other control, as it starts and with an error where it has one.

Anatomy

View                                         ← legend (or sr-only with hideLabel)
┌──────────────────────────────────────────┐
│ ┌────────┐                               │ ← track: sunken, bordered, p-0.5
│ │ ☰ List │   ▥ Board    ▦ Calendar       │ ← segments: label > sr-only radio + icon + words
│ └────────┘                               │ ← highlight: one raised thumb, aria-hidden
└──────────────────────────────────────────┘
  • Control, input mode: fieldset.min-w-0, legend, then the track. Host attributes land on the fieldset, except the model.
  • Control, link mode: nav[aria-labelledby], a label span, the track.
  • Track: group/glide, data-ds-glide, x-data with no behaviour name, x-ds-glide (with the bound expression, in input mode). inline-flex, or flex w-full with fullWidth.
  • Highlight: the track's first child, span[data-ds-glide-highlight], hidden until the directive has placed it. Positioned by the custom properties --ds-glide-x/-y/-w/-h, which the directive writes.
  • Segment, input mode: label[data-ds-glide-item] holding input.peer.sr-only (radio), the optional icon and the words (sr-only with iconOnly).
  • Segment, link mode: a[data-ds-glide-item] with the same children, aria-current="page" when active, no href when disabled.

How the control reaches the segments

As in chip-group: name, size and fullWidth reach the segments through @aware; the control reads the rendered slot for the data-ds-segment markers to learn its mode (and throws when both are present); each radio carries a data-ds-segment-input hook that the control replaces with the host's wire:model* / x-model*.

States

StateTreatment
restingtext-fg-body on the sunken track
hovertext-fg
chosen, no scriptthe segment draws the thumb itself: has-checked:bg-surface text-fg shadow-raised
chosen, script placedthe segment's own face goes transparent (group-data-[ds-glide-ready]/glide:), the highlight carries it
focus-visibleoutline-2 outline-focus-ring on the segment (has-focus-visible: for radios)
disabledtext-fg-muted, cursor-not-allowed, skipped
link activethe chosen face, written out, handed to the highlight the same way

The highlight moves with motion-safe:transition-[translate,width,height], duration-popover-enter ease-enter. Resizes and the first placement jump (data-instant), so only a change of choice animates.

Sizes

SizeHeightText
sm32pxtext-meta
md44px below sm, 36px from smtext-body
lg44pxtext-body

The glide directive

x-ds-glide is shared with toolbar and toggle-group. In selected mode (no modifier) it follows the item that is chosen: [aria-current], [aria-checked="true"], [aria-pressed="true"], [aria-selected="true"], or an item holding input:checked. It re-measures on change, on attribute changes inside the container, on resize (ResizeObserver), and when its expression changes, at most once per animation frame. Offsets are read from offsetLeft/offsetTop, which are physical, so right-to-left needs nothing special.

Choices and why

  • Radios, not buttons. A control that chooses a value should post one and take the arrow keys natively. The script adds nothing a user needs.
  • The glide cannot throw. The rule is that a component needing the script fails loudly without it. This one does not need it: the track is a plain x-data and an unregistered directive is skipped by Alpine, so a missing script leaves the CSS face, which is correct. The behaviour suite renders it in a host with no script to hold that.
  • No flash. The highlight is hidden until placed, and the segment's own face stays until the track is marked ready, in the same frame.
  • The bound expression is watched. A host setting the value checks a radio without an event; watching x-model's (or $wire.<prop>'s) value is the only way to see it.
  • Sunken track, raised thumb. In dark mode a surface-on-sunken pair stays lighter than its ground; a tinted track did not.
  • md is responsive like ds::button's, so a control beside buttons lines up with them at every width.

Accessibility

Rolegroup (fieldset) of radios; or navigation of links
NameThe legend, or the nav's label; each segment its words
StateNative checked; aria-current="page" on the active link; aria-disabled on a disabled link
KeyboardRadios: Tab in, arrows move and choose, skipping disabled. Links: Tab and Enter
FocusRing on the segment
Not colour aloneThe raised thumb and its shadow
MotionThe slide, motion-safe only

Help and error

Since batch 30B, help and error in the one field pattern every control shares (partials/field-message, the markup ds::input draws): the help as muted meta text, or in its place the error with its icon in the danger colour, never both. The message's id ({id}-help, {id}-error) is named by the control's aria-describedby, after anything the host passed. With neither prop the control renders exactly as before (tests/FieldMessagesUnchangedTest.php).

The label is drawn in the field label's style (text-body font-medium text-fg, mb-1.5) since 30B, as every other control's; it was smaller and muted (text-meta text-fg-muted, mb-2). The message describes the fieldset or nav; an error also draws the track's border in danger.

Do not

  • Do not make the glide carry state. It is aria-hidden decoration; the radios are the state.
  • Do not use it for tab panels. That is ds::tabs.
  • Do not put the binding on the segments. The control carries it.

From the package repo: edit this page on GitHub.

Was this page helpful?