Skip to content

GhostToggleGroup specification ​

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

Amendment (approved 2026-12-03): a size prop on the themed GhostToggleGroup 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 ​

GhostToggleGroup groups related on/off toggle buttons so users can choose one or several options from a small set—table density controls, layout switches, segmented filters, and formatting toolbars. Applications need toggle-button semantics (aria-pressed), not radio-group semantics, so a pressed option can be toggled off and multiple options can stay on independently when configured. The component composes useControllableState, useGhostId, and useRovingFocus for APG-correct keyboard movement, controlled Vue models, and SSR-safe ids without reimplementing roving tabindex.

Non-goals ​

  • Exclusive single-choice form controls with radio semantics (GhostRadioGroup).
  • Arbitrary toolbar composites with menu buttons (GhostToolbar, GhostDropdownMenu).
  • Form submission values for the group (toggle buttons are not submitted as a group; consumers own form wiring if needed).
  • Animated segmented indicators, overflow “more” menus, or drag reordering.

Public API ​

Parts and exports ​

Headless exports from @ghostvue/vue:

  • GhostToggleGroup (root role="group" host with roving focus)
  • GhostToggleGroupItem (aria-pressed toggle control)
  • useGhostToggleGroup (selection state, registries, toggle helpers)
  • Types: GhostToggleGroupProps, GhostToggleGroupItemProps, GhostToggleGroupExposed, GhostToggleGroupItemExposed, UseGhostToggleGroupOptions, UseGhostToggleGroupReturn, GhostToggleGroupType, GhostToggleGroupOrientation, GhostToggleGroupValue

Themed exports from @ghostvue/themes:

  • GhostToggleGroup, GhostToggleGroupItem (plus re-exported types and styles via default.css)

Props and configuration ​

GhostToggleGroup (root) ​

PropTypeDefaultContract
type'single' | 'multiple''single'Single allows zero or one pressed item; multiple allows any subset.
modelValuestring | string[] | undefinedundefinedControlled selection. Omit for uncontrolled. Type must match type.
defaultValuestring | string[] | undefinedundefinedInitial selection when uncontrolled. Multiple defaults to [] when omitted.
orientation'horizontal' | 'vertical''horizontal'Drives roving arrow keys and optional aria-orientation on the group.
dir'ltr' | 'rtl' | undefinedundefinedExplicit text direction for horizontal roving.
asstring | Component'div'Root element hosting items. Must remain focusable as a group container (not button).
disabledbooleanfalseDisables interaction for all items (items still render; items’ own disabled also applies).

GhostToggleGroupItem ​

PropTypeDefaultContract
asstring | Component'button'Element or component for the control.
valuestringrequiredStable id for selection and v-model.
disabledbooleanfalseSkips roving focus and ignores toggle. Native disabled on <button>; aria-disabled otherwise.

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

v-model contracts ​

GhostToggleGroup owns modelValue via useControllableState:

  • When modelValue is undefined (omitted), internal state starts at defaultValue when provided.
  • In type="single" uncontrolled mode without defaultValue, no item is pressed initially.
  • In type="multiple" uncontrolled mode without defaultValue, internal state is [].
  • update:modelValue emits on every user-driven toggle while uncontrolled; controlled consumers must apply the emitted value.
  • Single: emitted value is string | undefined (pressing the active item clears selection).
  • Multiple: emitted value is string[] (order follows registration order, not click order).
  • value strings are opaque; duplicates among items are unsupported (last registration wins for roving maps).

Events, callbacks, slots, and exposed methods ​

  • GhostToggleGroup emits update:modelValue with [value: string | string[] | undefined].
  • GhostToggleGroup default slot holds items (no slot props).
  • GhostToggleGroup and GhostToggleGroupItem expose { element: HTMLElement | null, focus(), blur() }.

State model ​

  • Single: at most one pressed item; pressing the pressed item clears selection (undefined).
  • Multiple: any subset; pressing toggles membership.
  • data-state="on" | "off" on items mirrors pressed state.
  • Group disabled prevents all toggles without removing items from the DOM.

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

Per-item disabled and group disabled only. No loading or read-only mode.

Consumer data ​

The component does not mutate consumer collections. Item labels are slot content.

Accessibility contract ​

Uses WAI-ARIA toggle button semantics inside a group, not radiogroup / radio.

Semantics and relationships ​

  • GhostToggleGroup: role="group", optional aria-orientation when vertical, aria-disabled="true" when group disabled, data-part="root", class ghost-toggle-group, data-ghost-toggle-group.
  • GhostToggleGroupItem: native button (default) with type="button", aria-pressed="true" | "false", deterministic id from useGhostId (overridable via id), data-part="item", class ghost-toggle-group-item, data-ghost-toggle-group, data-ghost-toggle-group-value.
  • Consumers must label the group via aria-label, aria-labelledby, or visible text plus aria-labelledby. Each item is named by its visible content or aria-label.

Keyboard interaction ​

Roving among items (useRovingFocus on the group root):

KeyOrientationBehavior
ArrowRighthorizontalNext enabled item (RTL: previous).
ArrowLefthorizontalPrevious enabled item (RTL: next).
ArrowDownverticalNext enabled item.
ArrowUpverticalPrevious enabled item.
HomeeitherFirst enabled item.
EndeitherLast enabled item.

Arrow, Home, and End combined with Alt, Ctrl, or Meta, and keys that belong to an IME composition, are not handled so platform shortcuts keep working.

Activation (GhostToggleGroupItem):

KeyBehavior
Space/EnterToggles pressed state (native on <button>).
ClickToggles pressed state.

Disabled items are skipped by roving focus and cannot be toggled.

Focus behavior ​

  • The primary tab stop is the first pressed enabled item in registration order; if none are pressed, the first enabled item. Other enabled items use tabindex="-1" (including native <button> hosts).
  • Arrow keys move focus via useRovingFocus. When focus leaves the group, tab stops reset to the primary stop so a later Tab into the group focuses the expected pressed (or first) item, not the last arrow-focused item.
  • Tab moves into and out of the group at the primary stop only.

Assistive technology expectations ​

Screen readers announce the group label and each item as a toggle button with pressed state via aria-pressed, distinguishing this widget from radio groups where deselecting the checked option is not allowed.

Rendering and customization ​

Stable hooks (not the undecided GHO-38 styling-hooks contract):

  • ghost-toggle-group, ghost-toggle-group-item classes
  • data-part, data-state, data-ghost-toggle-group, data-ghost-toggle-group-value
  • Themed CSS under .ghost-theme / .ghost-themed-scope using existing --ghost-* semantic tokens (segmented control appearance)

Environment behavior ​

SSR and hydration ​

  • useGhostId supplies deterministic SSR-safe ids.
  • useRovingFocus attaches listeners only in onMounted (no document access during SSR setup).

Mobile and touch ​

Pointer activation toggles items; no separate gesture model.

Performance ​

Listeners are limited to roving keydown/focusin on the group root while mounted.

Composition ​

  • useGhostToggleGroup + useControllableState for selection.
  • useGhostId per item (when needed for labeling wiring).
  • useRovingFocus on GhostToggleGroup root.
  • Shared tab-stop sync helper modeled on tabs selection tab stops.

Test plan ​

  • Unit (GhostToggleGroup.test.ts): single/multiple toggle, deselect in single mode, group disabled, item disabled, horizontal/vertical arrows, RTL, Home/End, controlled/uncontrolled, aria-pressed, SSR without roving listeners.
  • Browser (ghost-toggle-group.spec.ts): example gallery keyboard/pointer, Axe zero violations, @component:GhostToggleGroup.
  • Cross-browser (rtl-roving.spec.ts, @cross-browser): RTL layout order, mirrored arrows with real focus while aria-pressed only changes on Space, in Chromium, Firefox, and WebKit. No engine differences found; focus and ARIA assertions are shared.
  • Themed integration: themed root ref reaches group element.
  • Type tests: consumer fixtures for single vs multiple v-model when exports stabilize.

Documentation plan ​

  • docs/components/toggle-group.md with headless and themed examples.
  • Registry entries for toggle-group/basic, toggle-group/multiple, toggle-group/themed.
  • Playground ToggleGroupReference section with unique copy.
  • 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 GhostToggleGroup. 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 with reaction toggles under chat messages and with formatting toggles in a compact toolbar.

The same gap exists on GhostSegmentedControl, 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 GhostToggleGroup in @ghostvue/themes only:

PropTypeDefaultContract
size'sm' | 'md' | 'lg''md'Track height, label size and item padding follow control.size.<size>, matching a GhostButton or GhostInput of the same size.
  • The union is exported as GhostToggleGroupSize, 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 GhostToggleGroup inside .ghost-theme that sets data-size itself gets the same sizing, as with GhostButton.
  • GhostToggleGroupItem takes no size. Every item 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. Item inline padding is S.padding.inline. The track's inset and border do not change with size.
  • Tokens only. No recipe sets its own height, padding or pixel value. Densities change S.* and the control follows.
  • Vertical. Each item keeps the current per-item minimum rule, computed from S.min instead of control.size.md.min.
  • Coarse pointers. Each item 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 item 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 toggle-group/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 items 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 item 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 item 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 item, and items 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 GhostToggleGroup 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 item is wider than 24px and only the track's inset lies above and below it.
  4. Default unchanged. Omitting size renders no data-size. At device scale factor 1 the toggle-group/themed example renders as it does today.
  5. Coarse pointer. With isMobile and hasTouch, every activation target is at least 44px tall at every size.
  6. 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 GhostToggleGroup.
  7. Manifests and API tables. pnpm manifests:write records the prop in packages/themes/manifests/ghost-toggle-group.manifest.json, and pnpm docs:api:write updates the generated API table.

Documentation plan ​

  • A Size section in docs/components/toggle-group.md with one runnable example showing a toggle group 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. Items 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.