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.
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.
Time window and metric switchers with GhostSegmentedControl.
Switch time windows or metric series with arrow keys.
984.2k
+11.6% vs prior 7d
Steady weekday traffic across EU and US.
<script setup lang="ts">
import { computed, ref } from 'vue'
import {
GhostCard,
GhostSegmentedControl,
GhostSegmentedControlItem,
} from '@ghostvue/themes'
type Period = '24h' | '7d' | '30d'
type Metric = 'requests' | 'latency' | 'errors'
const period = ref<Period>('7d')
const metric = ref<Metric>('requests')
const dataset: Record<
Period,
Record<Metric, { primary: string; delta: string; note: string }>
> = {
'24h': {
requests: {
primary: '142.8k',
delta: '+4.2% vs prior 24h',
note: 'Peak 9,400 req/min at 14:20 UTC.',
},
latency: {
primary: '38 ms',
delta: '-3 ms vs prior 24h',
note: 'p95 edge response time.',
},
errors: {
primary: '0.04%',
delta: '-0.01% vs prior 24h',
note: '57 failed requests across 4 regions.',
},
},
'7d': {
requests: {
primary: '984.2k',
delta: '+11.6% vs prior 7d',
note: 'Steady weekday traffic across EU and US.',
},
latency: {
primary: '41 ms',
delta: '-2 ms vs prior 7d',
note: 'p95 edge response time.',
},
errors: {
primary: '0.06%',
delta: 'No change vs prior 7d',
note: '590 failed requests across 4 regions.',
},
},
'30d': {
requests: {
primary: '4.12M',
delta: '+18.4% vs prior 30d',
note: 'Includes Nov 12 webhook backfill.',
},
latency: {
primary: '44 ms',
delta: '+1 ms vs prior 30d',
note: 'p95 edge response time.',
},
errors: {
primary: '0.08%',
delta: '-0.03% vs prior 30d',
note: '3,296 failed requests across 4 regions.',
},
},
}
const current = computed(() => dataset[period.value][metric.value])
</script>
<template>
<section class="analytics" aria-labelledby="analytics-title">
<GhostCard>
<template #header>
<div class="analytics__header">
<div>
<h2 id="analytics-title" class="analytics__title">API traffic</h2>
<p class="analytics__lead">
Switch time windows or metric series with arrow keys.
</p>
</div>
<GhostSegmentedControl
v-model="period"
aria-label="Time window"
class="analytics__period"
>
<GhostSegmentedControlItem value="24h">
24h
</GhostSegmentedControlItem>
<GhostSegmentedControlItem value="7d">7d</GhostSegmentedControlItem>
<GhostSegmentedControlItem value="30d">
30d
</GhostSegmentedControlItem>
</GhostSegmentedControl>
</div>
</template>
<div class="analytics__body">
<GhostSegmentedControl v-model="metric" aria-label="Metric">
<GhostSegmentedControlItem value="requests">
Requests
</GhostSegmentedControlItem>
<GhostSegmentedControlItem value="latency">
Latency
</GhostSegmentedControlItem>
<GhostSegmentedControlItem value="errors">
Errors
</GhostSegmentedControlItem>
</GhostSegmentedControl>
<div class="analytics__stat" role="status">
<p class="analytics__value">{{ current.primary }}</p>
<p class="analytics__delta">{{ current.delta }}</p>
<p class="analytics__note">{{ current.note }}</p>
</div>
</div>
</GhostCard>
</section>
</template>
<style scoped>
.analytics {
max-inline-size: 36rem;
}
.analytics__header {
display: grid;
gap: var(--ghost-space-gap-md);
padding-block-end: var(--ghost-space-stack-sm);
}
.analytics__title {
color: var(--ghost-text-default);
font-size: var(--ghost-type-size-lg);
font-weight: var(--ghost-type-weight-semibold);
line-height: var(--ghost-type-line-tight);
margin: 0;
}
.analytics__lead,
.analytics__note {
color: var(--ghost-text-muted);
font-size: var(--ghost-type-size-sm);
line-height: var(--ghost-type-line-snug);
margin: 0;
overflow-wrap: anywhere;
}
.analytics__body {
display: grid;
gap: var(--ghost-space-stack-md);
}
.analytics__stat {
display: grid;
gap: var(--ghost-space-gap-xs);
}
.analytics__value {
color: var(--ghost-text-default);
font-size: var(--ghost-type-size-2xl);
font-variant-numeric: tabular-nums;
font-weight: var(--ghost-type-weight-semibold);
line-height: var(--ghost-type-line-tight);
margin: 0;
}
.analytics__delta {
color: var(--ghost-text-default);
font-size: var(--ghost-type-size-sm);
font-weight: var(--ghost-type-weight-medium);
line-height: var(--ghost-type-line-snug);
margin: 0;
}
@container ghost-demo (min-width: 30rem) {
.analytics__header {
align-items: start;
grid-template-columns: minmax(0, 1fr) auto;
}
}
</style>
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.
Token-driven thumb with reduced-motion support.
<script setup lang="ts">
import {
GhostSegmentedControl,
GhostSegmentedControlItem,
} from '@ghostvue/themes'
</script>
<template>
<GhostSegmentedControl default-value="week" aria-label="Time range">
<GhostSegmentedControlItem value="day">Day</GhostSegmentedControlItem>
<GhostSegmentedControlItem value="week">Week</GhostSegmentedControlItem>
<GhostSegmentedControlItem value="month">Month</GhostSegmentedControlItem>
</GhostSegmentedControl>
</template>
Small segmented control in a toolbar beside a small input and button.
<script setup lang="ts">
import { ref } from 'vue'
import {
GhostButton,
GhostInput,
GhostSegmentedControl,
GhostSegmentedControlItem,
} from '@ghostvue/themes'
type Range = 'day' | 'week' | 'month'
const range = ref<Range>('week')
const search = ref('')
</script>
<template>
<!--
Every control in the toolbar takes size="sm", so the segmented track is as
tall as the small input and button beside it.
-->
<div class="report-toolbar">
<GhostInput
v-model="search"
size="sm"
type="search"
aria-label="Search projects"
placeholder="Search projects"
/>
<GhostSegmentedControl v-model="range" size="sm" aria-label="Report range">
<GhostSegmentedControlItem value="day">Day</GhostSegmentedControlItem>
<GhostSegmentedControlItem value="week">Week</GhostSegmentedControlItem>
<GhostSegmentedControlItem value="month">
Month
</GhostSegmentedControlItem>
</GhostSegmentedControl>
<GhostButton size="sm" variant="outline" tone="neutral">Export</GhostButton>
</div>
</template>
<style scoped>
.report-toolbar {
align-items: center;
display: flex;
flex-wrap: wrap;
gap: var(--ghost-space-gap-sm);
}
</style>
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.
<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.
Single-select view switcher with sliding indicator vars.
<script setup lang="ts">
import { ref } from 'vue'
import { GhostSegmentedControl, GhostSegmentedControlItem } from '@ghostvue/vue'
const view = ref('list')
</script>
<template>
<GhostSegmentedControl v-model="view" aria-label="Content view">
<GhostSegmentedControlItem value="list">List</GhostSegmentedControlItem>
<GhostSegmentedControlItem value="grid">Grid</GhostSegmentedControlItem>
<GhostSegmentedControlItem value="board">Board</GhostSegmentedControlItem>
</GhostSegmentedControl>
</template>
<style scoped>
/*
* Headless path ships no visuals; this is consumer CSS for the sample. Items
* share equal columns so the indicator can slide from the CSS variables the
* library sets. The indicator renders inside GhostSegmentedControl, so scoped
* CSS needs :deep(). Highlight follows light and dark mode without a theme.
*/
.ghost-segmented-control {
display: inline-grid;
grid-auto-columns: 1fr;
grid-auto-flow: column;
padding-block-end: 0.375rem;
position: relative;
}
.ghost-segmented-control :deep([data-part='indicator']) {
background: Highlight;
block-size: 0.1875rem;
border-radius: 999px;
inline-size: calc(100% / var(--ghost-segmented-item-count));
inset-block-end: 0;
inset-inline-start: calc(
100% * var(--ghost-segmented-selected-index) /
var(--ghost-segmented-item-count)
);
pointer-events: none;
position: absolute;
}
.ghost-segmented-control
:deep([data-part='indicator'][data-indicator-visible='false']) {
display: none;
}
</style>
Use the parts from @ghostvue/vue when your application paints the segments and indicator.
GhostSegmentedControl | Prop | Type | Default | Description |
|---|---|---|---|
as | GhostSegmentedControlElement | "div" | Root host element. |
defaultValue | GhostSegmentedControlValue | — | Initial selection when modelValue is omitted. |
deselectable | boolean | false | When false, pressing the active segment keeps it selected (default view switching). |
dir | 'ltr' | 'rtl' | — | Explicit text direction for horizontal roving. |
disabled | boolean | false | Disables every item. |
modelValue | GhostSegmentedControlValue | — | Controlled selection. |
orientation | 'horizontal' | 'vertical' | "horizontal" | Arrow-key roving layout and indicator slide axis. |
| Slot | Slot props | Description |
|---|---|---|
default | — | Segment items. |
@ghostvue/themes adds these to the headless GhostSegmentedControl API, which it forwards:
| Prop | Type | Default | Description |
|---|---|---|---|
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 | Prop | Type | Default | Description |
|---|---|---|---|
as | GhostSegmentedControlItemElement | "button" | Tag name or component for the segment control. |
disabled | boolean | false | Disables toggling and roving focus for this item. |
value | string | Required | Stable value for this item in the group model. |
| Slot | Slot props | Description |
|---|---|---|
default | — | Primary content. |
Exposed: { element, focus(), blur() }.
ghost-segmented-control, ghost-segmented-control-item, ghost-segmented-control-indicatordata-ghost-segmented-control, data-ghost-segmented-control-value, data-indicator-visible, data-part, data-state (on / off)--ghost-segmented-item-count, --ghost-segmented-selected-indexThemed styles ship from @ghostvue/themes (GhostSegmentedControl, GhostSegmentedControlItem).
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:
.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.
role="group" with an accessible name.button (default) with aria-pressed="true" | "false".aria-hidden="true", decorative only.Home, End, and Tab behavior match GhostToggleGroup single mode.