Segmented control
Usage
Use the v-model directive to control the selected value, and the options prop to list the choices. The aria-label names the control for screen readers.
<script setup lang="ts">
import { ref } from 'vue'
const transactionType = ref('all')
const transactionTypeOptions = [
{ value: 'all', label: 'All' },
{ value: 'income', label: 'Income' },
{ value: 'expenses', label: 'Expenses' },
]
</script>
<template>
<KSegmentedControl v-model="transactionType" aria-label="Transaction type" :options="transactionTypeOptions" />
</template>
Options
Each option has a value and a label. Add an icon to show it before the label, and disabled to make it unavailable.
<script setup lang="ts">
const billingCycleOptions = [
{ value: 'monthly', label: 'Monthly' },
{ value: 'yearly', label: 'Yearly' },
{ value: 'lifetime', label: 'Lifetime', disabled: true },
]
</script>
<template>
<KSegmentedControl v-model="billingCycle" aria-label="Billing cycle" :options="billingCycleOptions" />
</template>
Size
Use the size prop to change the height of the control, from 3xs to 4xl. Defaults to md.
<KSegmentedControl v-model="transactionType" aria-label="Transaction type" size="xs" :options="transactionTypeOptions" />
<KSegmentedControl v-model="transactionType" aria-label="Transaction type" size="md" :options="transactionTypeOptions" />
<KSegmentedControl v-model="transactionType" aria-label="Transaction type" size="xl" :options="transactionTypeOptions" />
<KSegmentedControl v-model="transactionType" aria-label="Transaction type" size="3xl" :options="transactionTypeOptions" />
Pill
Set :pill="false" to use rounded corners instead of the default pill shape.
<KSegmentedControl
v-model="transactionType"
aria-label="Transaction type"
:pill="false"
:options="transactionTypeOptions"
/>
Icons and square
Pass any Vue component as the icon of an option, like a Lucide icon. Use the square prop to show only the icons: each label becomes the accessible name of its option.
<script setup lang="ts">
import { ChartLine, LayoutGrid, List, Table2 } from 'lucide-vue-next'
const reportViewOptions = [
{ value: 'chart', label: 'Chart', icon: ChartLine },
{ value: 'table', label: 'Table', icon: Table2 },
]
const accountLayoutOptions = [
{ value: 'list', label: 'List view', icon: List },
{ value: 'grid', label: 'Grid view', icon: LayoutGrid },
]
</script>
<template>
<KSegmentedControl v-model="reportView" aria-label="Report view" :options="reportViewOptions" />
<KSegmentedControl v-model="accountLayout" aria-label="Account layout" square :options="accountLayoutOptions" />
<KSegmentedControl
v-model="accountLayout"
aria-label="Account layout"
square
:pill="false"
:options="accountLayoutOptions"
/>
</template>
Block
Use the block prop to fill the width of the container, with options of equal width.
<KSegmentedControl v-model="spendingPeriod" aria-label="Spending period" block :options="periodOptions" />
Disabled
Use the disabled prop to disable the whole control.
<KSegmentedControl v-model="transactionType" aria-label="Transaction type" disabled :options="transactionTypeOptions" />
Examples
Many options
When the options are wider than their container, the control scrolls horizontally. The selected option is scrolled into view when the page loads, and again whenever another option close to an edge is selected.
<div class="max-w-xs">
<KSegmentedControl v-model="statementMonth" aria-label="Statement month" :options="monthOptions" />
</div>
Customization
Use the ui prop to change the classes of each part of the control, and class for the root element. Your classes always win: rounded-none replaces the default rounded-full instead of being added to it.
<KSegmentedControl
v-model="transactionType"
aria-label="Transaction type"
:ui="{ root: 'rounded-none', thumb: 'rounded-none', option: 'rounded-none' }"
:options="transactionTypeOptions"
/>
API
Props
| Prop | Type | Default |
|---|---|---|
ariaLabel | string | |
options | SegmentedControlOption[] | |
modelValue | string | |
name | string | useId() |
size | '3xs' | '2xs' | 'xs' | 'sm' | 'md' | 'lg' | 'xl' | '2xl' | '3xl' | '4xl' | 'md' |
pill | boolean | true |
block | boolean | false |
square | boolean | false |
disabled | boolean | false |
ui | { root?, thumb?, option?, input?, content?, icon? } | |
class | string |
SegmentedControlOption
| Property | Type |
|---|---|
value | string |
label | string |
icon | Component |
disabled | boolean |
Emits
| Event | Payload |
|---|---|
update:modelValue | string |