Skip to content

GhostSegmentedControl ​

GhostSegmentedControl is a single-select segmented bar for switching views, layout modes, or filters. It reuses useGhostToggleGroup toggle-button semantics (aria-pressed, roving focus) and adds a sliding data-part="indicator" positioned from CSS variables—no layout reads during SSR.

See the specification for the accessibility contract.

API traffic

Time window and metric switchers with GhostSegmentedControl.

API traffic

Switch time windows or metric series with arrow keys.

984.2k

+11.6% vs prior 7d

Steady weekday traffic across EU and US.

The API traffic card uses one GhostSegmentedControl for the time window and another for the metric. In Time window, press ArrowRight from 7d to focus 30d, then press Space to update the traffic summary. Each bar keeps one selected segment, so reactivating 30d keeps the same time window.

Examples ​

Themed ​

Segmented control themed

Token-driven thumb with reduced-motion support.

Size ​

Size

Small segmented control in a toolbar beside a small input and button.

Set size to sm, md (the default) or lg on the themed root to match a GhostButton or GhostInput of the same size. The track takes its height from control.size.<size>, its label size and segment padding follow, and every segment uses the root size. Each density changes those tokens, and on touch screens every size keeps a 44px segment.

Usage ​

vue
<script setup lang="ts">
import { ref } from 'vue'
import {
  GhostSegmentedControl,
  GhostSegmentedControlItem,
} from '@ghostvue/themes'

const view = ref('list')
</script>

<template>
  <GhostSegmentedControl v-model="view" aria-label="Content view">
    <GhostSegmentedControlItem value="list">List</GhostSegmentedControlItem>
    <GhostSegmentedControlItem value="grid">Grid</GhostSegmentedControlItem>
  </GhostSegmentedControl>
</template>

By default deselectable is false, so the active segment stays pressed when clicked again (typical view switching). Set deselectable to allow clearing the selection like GhostToggleGroup single mode.

Advanced: headless composition ​

Headless ​

Segmented control Headless

Single-select view switcher with sliding indicator vars.

Use the parts from @ghostvue/vue when your application paints the segments and indicator.

API ​

GhostSegmentedControl ​

PropTypeDefaultDescription
asGhostSegmentedControlElement"div"Root host element.
defaultValueGhostSegmentedControlValue—Initial selection when modelValue is omitted.
deselectablebooleanfalseWhen false, pressing the active segment keeps it selected (default view switching).
dir'ltr' | 'rtl'—Explicit text direction for horizontal roving.
disabledbooleanfalseDisables every item.
modelValueGhostSegmentedControlValue—Controlled selection.
orientation'horizontal' | 'vertical'"horizontal"Arrow-key roving layout and indicator slide axis.
SlotSlot propsDescription
default—Segment items.

@ghostvue/themes adds these to the headless GhostSegmentedControl API, which it forwards:

PropTypeDefaultDescription
size'sm' | 'md' | 'lg'"md"Track height, label size, and segment padding from control.size.<size>, matching a GhostButton or GhostInput of the same size. Segments take the root size. The default omits data-size so CSS can treat a missing hook as md.

Emits: update:modelValue with string | undefined.

Exposed: { element, focus(), blur() }.

GhostSegmentedControlItem ​

PropTypeDefaultDescription
asGhostSegmentedControlItemElement"button"Tag name or component for the segment control.
disabledbooleanfalseDisables toggling and roving focus for this item.
valuestringRequiredStable value for this item in the group model.
SlotSlot propsDescription
default—Primary content.

Exposed: { element, focus(), blur() }.

Stable styling hooks ​

  • Classes: ghost-segmented-control, ghost-segmented-control-item, ghost-segmented-control-indicator
  • Attributes: data-ghost-segmented-control, data-ghost-segmented-control-value, data-indicator-visible, data-part, data-state (on / off)
  • CSS variables: --ghost-segmented-item-count, --ghost-segmented-selected-index

Themed styles ship from @ghostvue/themes (GhostSegmentedControl, GhostSegmentedControlItem).

Sizing ​

Themed segments always share the control equally, because the sliding indicator is positioned as an equal fraction of the track. By default the control is an inline grid that sizes to its widest label. When space is limited, label text wraps within its segment. To stretch the control across a container, set display: grid (or a width) on the root:

css
.view-switcher {
  display: grid;
}

Existing block-level display: flex overrides that fill their container (as in the Popover sharing example), or flex roots with an explicit width, keep equal segments through a flex: 1 1 0 fallback. A shrink-to-fit inline-flex root divides the combined label widths evenly, so longer labels can wrap even when the container has space for a wider control.

Vertical controls keep equal rows on the default grid, even when a narrow width makes a label wrap. A vertical display: flex override keeps equal rows only while every label fits on one line, because each flex row takes its own label's height. Keep the default grid for vertical labels that can wrap.

Accessibility ​

  • Root: role="group" with an accessible name.
  • Items: native button (default) with aria-pressed="true" | "false".
  • Indicator: aria-hidden="true", decorative only.
  • Arrow keys, Home, End, and Tab behavior match GhostToggleGroup single mode.