Segmented
A compact option selector with icons, disabled items, numeric or string values, keyboard navigation, form integration, and public focus methods.
Import
import { HuiSegmented } from '@nscode/handy-ui'
import type {
SegmentedProps,
SegmentedOption,
SegmentedEmits,
SegmentedExpose
} from '@nscode/handy-ui'
Internal state and controlled updates
Before a selection is stored, the first option remains a display fallback. Its first click commits once; repeated selection does not emit change. Explicit undefined remains controlled with the same fallback. Omitting the prop, including when only listening for updates, uses internal state that survives unrelated parent renders. A passed prop remains controlled; rejecting an update preserves the parent value. Switching modes reports a development warning. No defaultValue prop is added.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiSegmented, HuiButton, HuiSpace } from '@nscode/handy-ui'
const q1Redraw = ref(0)
const q1Last = ref('No request')
const q1Model = ref('a')
</script>
<template>
<div class="q1-state-demo segmented-q1-demo">
<HuiButton class="q1-redraw" @click="q1Redraw++">Parent redraw {{ q1Redraw }}</HuiButton>
<HuiSpace direction="vertical" fill>
<div>
<p>Omitted prop: internal state</p>
<div class="q1-local"><HuiSegmented :options="['a', 'b', 'c']" /></div>
</div>
<div>
<p>Update listener only: internal state</p>
<div class="q1-listener">
<HuiSegmented :options="['a', 'b', 'c']" @update:model-value="q1Last = String($event)" />
</div>
</div>
<div>
<p>Explicit undefined: controlled</p>
<div class="q1-empty">
<HuiSegmented :options="['a', 'b', 'c']" :model-value="undefined" />
</div>
</div>
<div>
<p>Fixed controlled value: parent rejects</p>
<div class="q1-fixed">
<HuiSegmented
:options="['a', 'b', 'c']"
:model-value="'b'"
@update:model-value="q1Last = String($event)"
/>
</div>
</div>
<div>
<p>Two-way binding</p>
<div class="q1-bound"><HuiSegmented :options="['a', 'b', 'c']" v-model="q1Model" /></div>
</div>
<p class="q1-notification">Bound value: {{ q1Model }}; latest request: {{ q1Last }}</p>
</HuiSpace>
</div>
</template>
Examples
String, Numeric, and Object Options
Options can be strings, numbers, or objects. Object values are required and unique. Labels fall back to the value; payload is application data ignored by the component. Numeric 1 differs from string '1'.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiSegmented, HuiSpace } from '@nscode/handy-ui'
import type { SegmentedOption } from '@nscode/handy-ui'
const view = ref<string | number>('Day')
const count = ref<string | number>(20)
const selected = ref<string | number>('list')
const options: SegmentedOption[] = [
{ value: 'list', label: 'List', icon: 'menu', title: 'List view', payload: { compact: true } },
{ value: 'grid', label: 'Grid', icon: 'search', title: 'Grid view' },
{ value: 'locked', label: 'Unavailable', disabled: true }
]
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSegmented v-model="view" :options="['Day', 'Week', 'Month']" />
<HuiSegmented v-model="count" :options="[10, 20, 50]" />
<HuiSegmented v-model="selected" :options="options" />
<span>Selections: {{ view }}, {{ count }}, {{ selected }}</span>
</HuiSpace>
</template>
Sizes, Full Width, and Disabled State
Large/default/small heights are 40/32/24px. With block, the control fills its container. Disabled options are skipped by keyboard navigation; disabled blocks the entire component.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiSegmented, HuiSpace } from '@nscode/handy-ui'
import type { SegmentedProps } from '@nscode/handy-ui'
const sizes: NonNullable<SegmentedProps['size']>[] = ['large', 'default', 'small']
const value = ref<string | number>('Day')
const options = ['Day', 'Week', 'Month']
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace v-for="size in sizes" :key="size"
><span>{{ size }}</span
><HuiSegmented v-model="value" :options="options" :size="size"
/></HuiSpace>
<HuiSegmented v-model="value" :options="options" block />
<HuiSegmented v-model="value" :options="options" disabled />
</HuiSpace>
</template>
Dynamic Options
The selected background follows actual item offsets when options or container width change, including when a hidden component becomes visible. If no matching value exists, no item is selected; an undefined value only displays the first option as selected and does not emit automatically.
<script setup lang="ts">
import { computed, ref } from 'vue'
import { HuiButton, HuiSegmented, HuiSpace } from '@nscode/handy-ui'
const detailed = ref(false)
const value = ref<string | number>('Day')
const options = computed(() =>
detailed.value ? ['Day', 'Week', 'Month', 'Year'] : ['Day', 'Week']
)
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiButton @click="detailed = !detailed">Change option set</HuiButton>
<HuiSegmented v-model="value" :options="options" />
<span>Selected: {{ value }}</span>
</HuiSpace>
</template>
Keyboard, Events, and Focus Methods
All four arrow keys wrap through available options. Home/End focus and select the first/last available item, skipping disabled options. Enter/Space select the focused item. The group has one Tab stop: it follows focus inside the group and returns to the actual selected value on exit, including when a controlled parent rejects or normalizes an update. Value changes emit update before change; selecting the current value emits neither. Focus/blur events occur once when entering/leaving the group.
Composing input, key code 229, Alt/Ctrl/Meta combinations, and already consumed events are ignored. PageUp/PageDown and typeahead are not navigation keys. Stable option values preserve focus across reordering; removing, filtering, or disabling a focused option restores focus nearby without changing selection. Data updates do not steal focus from outside the group; focus() does not select an option.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiSegmented, HuiSpace } from '@nscode/handy-ui'
import type { SegmentedExpose } from '@nscode/handy-ui'
const control = ref<SegmentedExpose>()
const value = ref<string | number>('Day')
const event = ref('Waiting')
const options = [{ value: 'Day' }, { value: 'Week', disabled: true }, { value: 'Month' }]
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace wrap>
<HuiButton @mousedown.prevent @click="control?.focus()">focus()</HuiButton>
<HuiButton @mousedown.prevent @click="control?.blur()">blur()</HuiButton>
</HuiSpace>
<HuiSegmented
ref="control"
v-model="value"
:options="options"
aria-label="Period"
@change="event = 'change: ' + $event"
@focus="event = 'focus'"
@blur="event = 'blur'"
/>
<span>{{ event }}</span>
</HuiSpace>
</template>
Form Integration
<script setup lang="ts">
import { reactive } from 'vue'
import { HuiForm, HuiFormItem, HuiSegmented, HuiSpace } from '@nscode/handy-ui'
import type { FormRule } from '@nscode/handy-ui'
const model = reactive<{ period: string | number }>({ period: 'Day' })
const rules: Record<string, FormRule[]> = {
period: [{ required: true, message: 'Choose a period', trigger: 'change' }]
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiForm :model="model" :rules="rules"
><HuiFormItem label="Period" prop="period"
><HuiSegmented v-model="model.period" :options="['Day', 'Week']" /></HuiFormItem
></HuiForm>
<HuiSegmented model-value="Day" :options="['Day', 'Week']" :validate-event="false" />
</HuiSpace>
</template>
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| modelValue | string | number | undefined | Selected value. Omit it for internal state initially undefined; a passed prop is controlled. An undefined value uses the first item only as a display fallback. |
| options | SegmentedOption[] | string[] | number[] | [] | Items to display |
| size | 'large' | 'default' | 'small' | — | Inherited size |
| disabled | boolean | false | Disable all options |
| block | boolean | false | Fill available width |
| validateEvent | boolean | true | Validate when the model changes |
Events
| Event | Payload | Description |
|---|---|---|
| update:modelValue | (value: string | number) | A user selects a different model value |
| change | (value: string | number) | Follows update:modelValue |
| focus | (event: FocusEvent) | Focus enters the component |
| blur | (event: FocusEvent) | Focus leaves the component |
Slots
No slots.
Methods
| Name | Type | Description |
|---|---|---|
| focus | () => void | Focus the current or first available control unless disabled or unmounted |
| blur | () => void | Blur only an active element inside this component in its owner document |
Types
import type {
SegmentedOption,
SegmentedProps,
SegmentedEmits,
SegmentedExpose
} from '@nscode/handy-ui'
export interface SegmentedOption {
value: string | number
label?: string
disabled?: boolean
icon?: string
title?: string
payload?: any
}
export interface SegmentedProps {
modelValue?: string | number
options?: SegmentedOption[] | string[] | number[]
size?: 'small' | 'default' | 'large'
disabled?: boolean
block?: boolean
validateEvent?: boolean
}
export interface SegmentedEmits {
(e: 'focus', event: FocusEvent): void
(e: 'blur', event: FocusEvent): void
(e: 'update:modelValue', value: string | number): void
(e: 'change', value: string | number): void
}
export interface SegmentedExpose {
focus: () => void
blur: () => void
}