Skip to content

GhostSegmentedControl specification ​

Status: Proposed for GHO-101. Spec and implementation ship together; Zac approves both in PR review.

Amendment (approved 2026-12-03): a size prop on the themed GhostSegmentedControl is recorded in the Control size amendment. The maintainer @zprue743 approved the recommended contract decision (Q1–Q4) on 2026-12-03.

Purpose and user problems ​

GhostSegmentedControl is a styled single-select control for switching views, layout modes, or filters where one segment is always visually active—a segmented bar with a sliding selection indicator. It reuses GhostToggleGroup / useGhostToggleGroup toggle-button semantics (aria-pressed, roving focus) rather than radio semantics, because the same headless model already powers toolbar toggles and only needs a different chrome.

Consumers get equal-width segments, a theme token–driven sliding thumb, and prefers-reduced-motion support without wiring layout measurements.

Non-goals ​

  • Multiple independent selections (type="multiple"). Use GhostToggleGroup.
  • Radio / form submission semantics (GhostRadioGroup).
  • Per-segment unequal widths driven by getBoundingClientRect or other post-layout reads (equal 1/n segments only; see Sizing).
  • Overflow menus, drag reorder, or icons-only segments without visible labels.
  • Changing GhostToggleGroup public props or behavior.

Public API ​

Parts and exports ​

Headless exports from @ghostvue/vue:

  • GhostSegmentedControl (root role="group" with sliding data-part="indicator")
  • GhostSegmentedControlItem (aria-pressed segment)
  • useGhostSegmentedControl (single-mode toggle group + indicator indices)
  • Types: GhostSegmentedControlProps, GhostSegmentedControlItemProps, GhostSegmentedControlExposed, GhostSegmentedControlItemExposed, GhostSegmentedControlValue, GhostSegmentedControlOrientation, UseGhostSegmentedControlOptions, UseGhostSegmentedControlReturn

Themed exports from @ghostvue/themes:

  • GhostSegmentedControl, GhostSegmentedControlItem (plus types and styles via default.css)

Props and configuration ​

GhostSegmentedControl (root) ​

PropTypeDefaultContract
modelValuestring | undefinedundefinedControlled selection. Omit for uncontrolled.
defaultValuestring | undefinedundefinedInitial selection when uncontrolled.
orientation'horizontal' | 'vertical''horizontal'Roving arrows and indicator slide axis.
dir'ltr' | 'rtl' | undefinedundefinedExplicit text direction for horizontal roving.
asstring | Component'div'Root host; must not be interactive.
disabledbooleanfalseDisables every item.
deselectablebooleanfalseWhen false, pressing the active segment keeps it pressed (typical view switching).

GhostSegmentedControlItem ​

Same contract as GhostToggleGroupItem: as (default 'button'), required value, optional disabled.

Native attributes fall through to each part’s rendered root.

v-model contracts ​

modelValue is string | undefined. Behavior follows single-mode useGhostToggleGroup except when deselectable is false: clicking the active segment does not emit undefined. Controlled consumers must apply emitted values on user interaction while uncontrolled.

Events, callbacks, slots, and exposed methods ​

  • Emits update:modelValue with [value: string | undefined].
  • Default slot holds items (no slot props).
  • Root and items expose { element, focus(), blur() }.

State model ​

  • At most one pressed item (aria-pressed="true").
  • data-state="on" | "off" on items.
  • Root sets CSS variables from registration order (never from layout): --ghost-segmented-item-count, --ghost-segmented-selected-index (-1 when none selected).
  • Sliding data-part="indicator" is present in SSR markup; position uses those variables only.

Loading, empty, disabled, read-only, and error states ​

Group and per-item disabled only.

Consumer data ​

The component does not mutate consumer collections. Labels are slot content.

Accessibility contract ​

WAI-ARIA toggle button in a group, matching GhostToggleGroup single mode.

Semantics and relationships ​

  • Root: role="group", optional aria-orientation when vertical, aria-disabled when disabled, data-part="root", class ghost-segmented-control, data-ghost-segmented-control.
  • Indicator: data-part="indicator", aria-hidden="true", not focused.
  • Item: native button default, type="button", aria-pressed, useGhostId, data-part="item", class ghost-segmented-control-item, data-ghost-segmented-control, data-ghost-segmented-control-value.
  • Group label via aria-label or aria-labelledby.

Keyboard interaction ​

Same roving and activation table as GhostToggleGroup (arrows, Home, End, Space/Enter, click). Disabled items skipped.

Focus behavior ​

Same primary tab stop rules as GhostToggleGroup via applyToggleGroupSelectionTabStops.

Assistive technology expectations ​

Toggle buttons with pressed state—not radios.

Rendering and customization ​

  • Classes: ghost-segmented-control, ghost-segmented-control-item, ghost-segmented-control-indicator
  • Attributes: data-ghost-segmented-control, data-ghost-segmented-control-value, data-part, data-state
  • Themed CSS under .ghost-theme using --ghost-* tokens; indicator motion uses --ghost-motion-* with @media (prefers-reduced-motion: reduce) snapping position without transition.

Sizing ​

The indicator is positioned as an equal 1/n fraction of the padded track, so the themed layout must give every segment exactly 1/n of the track width (or height when vertical). The supported sizing contract:

  • Default: the root is an inline-grid of minmax(0, 1fr) columns. It sizes to content, and every column takes the widest label's width.
  • Full width: set display: grid (or a definite width) on the root; the columns stay equal across the container.
  • Constrained width: label text wraps inside each segment so the indicator covers the text while the tracks stay equal.
  • Flex override (compatibility): a root overridden to block-level display: flex (which fills its container) or given an explicit width keeps equal segments through the items' flex: 1 1 0 and min-width: 0 fallback. A shrink-to-fit inline-flex root can make long labels wrap even when the container has space for a wider control.
  • Vertical: controls stack equal minmax(0, 1fr) rows at max-content width. Every row takes the tallest label's height, so rows stay equal when a constrained width makes one label wrap. A vertical root overridden to display: flex uses flex-direction: column, and its rows stay equal only while every label has the same height (for example, single-line labels). A column flex root has no definite height to split, so each row takes its own label's height. A min-height: 0 reset would collapse auto-height roots instead, so it is not applied. Keep the default grid when vertical labels can wrap.

Environment behavior ​

SSR and hydration ​

  • Item count and selected index come from registration order and model value only.
  • No window / document layout reads during SSR setup for indicator position.
  • Indicator markup is identical server and client for the same props/state.
  • useRovingFocus attaches listeners in onMounted only (inherited pattern).

Mobile and touch ​

Pointer toggles segments; equal-width 1/n targets per Sizing.

Performance ​

One passive indicator element; roving listeners on the group root while mounted.

Composition ​

  • useGhostSegmentedControl delegates to useGhostToggleGroup with type: 'single'.
  • useGhostId, useRovingFocus, applyToggleGroupSelectionTabStops.
  • No changes to GhostToggleGroup.vue or its exported props.

Test plan ​

  • Unit (GhostSegmentedControl.test.ts): selection, deselectable, disabled, roving keys, controlled/uncontrolled, indicator CSS variables, aria-pressed, SSR HTML includes indicator variables.
  • SSR hydration: expectSsrHydration with default selection and ids.
  • Browser (ghost-segmented-control.spec.ts): pointer/keyboard, Axe, @component:GhostSegmentedControl. Geometry checks assert the indicator matches the selected segment for unequal labels (docs home) and that a full-width display: flex override (popover/share-settings, in docs-popover-showcase.spec.ts) keeps every segment at 1/n of the track. A vertical sizing check keeps rows equal under the indicator for a narrow-width grid with one wrapping label, and for a display: flex override with single-line labels. The RTL indicator alignment and mirrored roving case is tagged @cross-browser and also runs in Firefox and WebKit.
  • Themed integration: themed wrapper ref reaches root.
  • Visual: segmented-control/themed in examples-visual via pnpm test:visual:docker.

Documentation plan ​

  • docs/components/segmented-control.md with examples.
  • Registry: segmented-control/basic, segmented-control/themed.
  • Playground SegmentedControlReference.
  • Manifests via pnpm manifests:write.

Compatibility and release impact ​

Additive minor Changeset for @ghostvue/vue and @ghostvue/themes.

Open architectural questions ​

None.

Control size amendment ​

Important

Approved 2026-12-03 by @zprue743. This amendment adds a size prop to the themed GhostSegmentedControl. The contract decision records the chosen options. Target release: 0.4.0.

Why a size option ​

The themed track is one medium control tall (control.size.md.min). In a toolbar of size="sm" buttons and inputs it is taller than its neighbours, and there is no supported way to match them. App builders hit this in a toolbar, in reaction toggles under chat messages, and in a compact Light/Dark switch in a header.

The same gap exists on GhostToggleGroup, GhostTabsList and GhostSelectTrigger. Each spec carries a matching amendment, so all four follow GhostButton and GhostInput, which already take size: 'sm' | 'md' | 'lg'.

Public API ​

Themed GhostSegmentedControl in @ghostvue/themes only:

PropTypeDefaultContract
size'sm' | 'md' | 'lg''md'Track height, label size and segment padding follow control.size.<size>, matching a GhostButton or GhostInput of the same size.
  • The union is exported as GhostSegmentedControlSize, following GhostButtonSize and GhostInputSize.
  • The wrapper renders data-size="sm" or data-size="lg" on the root and omits it for md, as GhostButton and GhostInput do. The wrapper consumes size, so it never reaches the DOM as a size attribute.
  • data-size on the root is the styling hook. A headless GhostSegmentedControl inside .ghost-theme that sets data-size itself gets the same sizing, as with GhostButton.
  • GhostSegmentedControlItem takes no size. Every segment in a track uses the root size.
  • Headless @ghostvue/vue does not change: no prop, type or manifest change.

Sizing contract ​

For each size, S below means control.size.<size>.

  • Height (fine pointers, horizontal). The root's border-box block size equals S.min, the same as a GhostInput with the same size in the same density. This holds at device scale factor 1 and 2. Labels that wrap may still grow the track, as today.
  • Label and padding. Label font size is S.font. Segment inline padding is S.padding.inline. The track's inset and border do not change with size, so the indicator geometry does not change.
  • Tokens only. No recipe sets its own height, padding or pixel value. Densities change S.* and the control follows.
  • Vertical. Each segment keeps the current per-segment minimum rule, computed from S.min instead of control.size.md.min.
  • Coarse pointers. Each segment still takes the whole control height as its minimum, read from S.min. The generated tokens set sm, md and lg to the same 44px minimum under (pointer: coarse) in every density, so sm never shrinks a touch target.
  • Device scale factor 2. The current segment minimum is the control size less twice the track inset and twice --ghost-border-hairline-width. At device scale factor 2 that token resolves to 0.5px, but Chromium computes the track's border-top-width as 1px. Measured on the playground example segmented-control/themed: the track is 36px at 1x and 37px at 2x, beside a 36px control.size.md.min. The size guarantee above must hold at 2x, so it cannot depend on that subtraction for the horizontal track. One way is to give the track min-block-size: S.min with box-sizing: border-box and let segments stretch to fill it. The implementation chooses the mechanism; the acceptance cases fix the outcome.

Accessibility ​

Roles, names, states, keyboard and focus do not change. The segment is the activation target and is shorter than the track by the inset and border. At sm in compact density (28px control, 4px inset) a horizontal segment is about 18px tall. This is the same situation as md in compact density today (about 22px). WCAG 2.2 target size (2.5.8) is met through its spacing exception: only the track's own inset lies above and below a horizontal segment, and segments are wider than 24px. An acceptance case checks this geometry. Coarse pointers keep the 44px minimum.

Implementation acceptance cases ​

  1. Size parity (browser). A playground fixture renders one row per density (compact, comfortable, spacious). Each row holds GhostButton, GhostInput and GhostSegmentedControl at sm, md and lg. At every size and density, the root's border-box block size equals the GhostInput of the same size (difference under 0.5px). One fixture serves all four amended components.
  2. Device scale factor 2. The parity comparison also runs in Chromium at deviceScaleFactor: 2 for the horizontal track at every size.
  3. Target geometry. At sm in compact density, a horizontal segment is wider than 24px and only the track's inset lies above and below it.
  4. Indicator. At sm and lg the indicator still matches the selected segment, using the existing geometry check for unequal labels.
  5. Default unchanged. Omitting size renders no data-size. At device scale factor 1 the segmented-control/themed visual snapshot does not change.
  6. Coarse pointer. With isMobile and hasTouch, every activation target is at least 44px tall at every size.
  7. Wrapper (unit). size="sm" and size="lg" render the matching data-size; md and an omitted size render none. No size attribute reaches the DOM. A type test rejects size="xl" on GhostSegmentedControl.
  8. Manifests and API tables. pnpm manifests:write records the prop in packages/themes/manifests/ghost-segmented-control.manifest.json, and pnpm docs:api:write updates the generated API table.

Documentation plan ​

  • A Size section in docs/components/segmented-control.md with one runnable example showing a segmented control at size="sm" beside a small GhostButton and GhostInput.
  • Generated API tables and the themed manifest list the prop.
  • A @ghostvue/themes Changeset.

Compatibility ​

Additive. The default stays md. A size attribute passed today falls through to the root div and has no effect; after the change it is a typed prop. At device scale factor 2 in Chromium, a default md track becomes 1px shorter (37px to 36px), which is the fix the size guarantee requires. Minor Changeset for @ghostvue/themes.

Contract decision ​

Recorded 2026-12-03. @zprue743 took the recommended option for each question, and Q1, Q2 and Q4 have the same answer in all four amended specs. Each question lists the chosen option first and the rejected alternative after it.

  1. Q1 — Where size lives. Chosen: on the root only. Segments take no size, so one track never mixes heights.
  2. Q2 — Themed only. Chosen: size belongs to the themed wrappers, as for GhostButton and GhostInput. Headless components stay unstyled and data-size is the styling hook. Rejected: add size to the headless props as a data attribute only.
  3. Q3 — The 2x guarantee. Chosen: guarantee equal heights with GhostInput at device scale factor 1 and 2, and accept the 1px change to the default md track at 2x in Chromium. Rejected: guarantee 1x only and track the 2x difference separately.
  4. Q4 — Release and delivery. Chosen: 0.4.0, with all four components in one implementation change so they share the parity fixture. Rejected: one change per component, in the order segmented control, toggle group, select trigger, tabs.