Slider
A slider with numeric bounds, steps, range selection, marks, vertical layout, optional inputs, keyboard support, and exposed values.
Import
import { HuiSlider } from '@nscode/handy-ui'
import type { SliderProps, SliderEmits, SliderExpose, SliderSize } from '@nscode/handy-ui'
Internal state and controlled updates
Omitting modelValue, including with an update listener only, stores values internally, initially min or [min,max]. A passed prop, including undefined, is controlled. Rejected updates restore parent positions; crossed range handles keep their identity. A drag emits change once on release, using the actual accepted or normalized value, or the final request when rejected. Switching controlled mode cancels the old drag.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiSlider, HuiButton, HuiSpace, HuiSwitch } from '@nscode/handy-ui'
const q1Redraw = ref(0)
const q1Last = ref('No request')
const q1Range = ref(false)
const q1Single = ref(0)
const q1Pair = ref<[number, number]>([0, 100])
</script>
<template>
<div class="slider-q1-demo">
<HuiSpace
><HuiButton class="q1-redraw" @click="q1Redraw++">Parent redraw {{ q1Redraw }}</HuiButton
><span>Range</span><HuiSwitch v-model="q1Range" /></HuiSpace
><HuiSpace direction="vertical" fill>
<div>
<p>Omitted prop: internal state</p>
<div class="q1-local"><HuiSlider :range="q1Range" show-input /></div>
</div>
<div>
<p>Update listener only: internal state</p>
<div class="q1-listener">
<HuiSlider
:range="q1Range"
show-input
@update:model-value="q1Last = JSON.stringify($event)"
/>
</div>
</div>
<div>
<p>Explicit undefined: controlled</p>
<div class="q1-empty">
<HuiSlider :range="q1Range" show-input :model-value="undefined" />
</div>
</div>
<div>
<p>Fixed controlled value: parent rejects</p>
<div class="q1-fixed">
<HuiSlider
:range="q1Range"
show-input
:model-value="q1Range ? [20, 80] : 20"
@update:model-value="q1Last = JSON.stringify($event)"
/>
</div>
</div>
<div>
<p>Two-way binding</p>
<div class="q1-bound">
<HuiSlider :range="q1Range" show-input v-if="!q1Range" v-model="q1Single" /><HuiSlider
:range="q1Range"
show-input
v-else
v-model="q1Pair"
/>
</div>
</div>
<p class="q1-notification">Latest request: {{ q1Last }}</p></HuiSpace
>
</div>
</template>
Examples
Binding, Bounds, Steps, and Stops
Use min/max to define the interval. Step points start at min; nonpositive steps use 1. show-stops draws an internal stop for each step. Disabled sliders ignore pointer and keyboard input.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiSlider, HuiSpace } from '@nscode/handy-ui'
const basic = ref(30)
const bounded = ref(-5)
const stepped = ref(40)
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSlider v-model="basic" />
<HuiSlider v-model="bounded" :min="-10" :max="10" :step="0.5" />
<HuiSlider v-model="stepped" :step="10" show-stops />
<HuiSlider :model-value="35" disabled />
<span>Values: {{ basic }}, {{ bounded }}, {{ stepped }}</span>
</HuiSpace>
</template>
Range Selection and Numeric Inputs
Range values must use a tuple type. Dragging thumbs past one another is allowed; emitted ranges are ordered from low to high. show-input adds one numeric field, or two for a range, without step buttons.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiSlider, HuiSpace } from '@nscode/handy-ui'
const single = ref(30)
const interval = ref<[number, number]>([20, 60])
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSlider v-model="single" show-input />
<HuiSlider v-model="interval" range show-input />
<span>Single: {{ single }}; range: {{ interval }}</span>
</HuiSpace>
</template>
Sizes
Large/default/small root heights are 40/32/24px, thumb diameters 20/16/12px, and track thicknesses 6/4/4px. Numeric fields use the same size. Omitted sizes inherit form/provider settings.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiSlider, HuiSpace } from '@nscode/handy-ui'
import type { SliderSize } from '@nscode/handy-ui'
const sizes: SliderSize[] = ['large', 'default', 'small']
const value = ref(40)
</script>
<template>
<HuiSpace direction="vertical" fill>
<div v-for="size in sizes" :key="size">
<p>{{ size }}</p>
<HuiSlider v-model="value" :size="size" show-input />
</div>
</HuiSpace>
</template>
Marks and Vertical Layout
Mark keys are numeric positions and values are labels. In vertical mode, height sets the track height; horizontal sliders fill the available width. Vertical sliders reserve space below the track for labels.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiSlider, HuiSpace } from '@nscode/handy-ui'
const horizontal = ref(40)
const vertical = ref(60)
const marks = { 0: 'Low', 25: '25', 50: 'Medium', 75: '75', 100: 'High' }
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSlider v-model="horizontal" :marks="marks" :step="5" />
<HuiSlider v-model="vertical" vertical height="220px" :marks="marks" :step="5" />
</HuiSpace>
</template>
Events, Keyboard, and Exposed Values
Update events follow dragging continuously. Change fires when a completed operation changes the value: pointer release, track click, keyboard action, or numeric input. Form validation follows change, not every drag frame.
Arrow keys move one step. Home selects min; End selects the last step point at or below max. An arrow step that would exceed bounds leaves the value unchanged. PageUp and PageDown move about a tenth of the range (rounded to whole steps, at least one) and stop at the last step point or min.
Screen readers read the mark label when the value sits exactly on a marks entry (for example "37°C") and the number otherwise. In range mode the two thumbs are named "Minimum" and "Maximum" by value, after the form item label when inside one (for example "Price Minimum"). focus() targets the last active thumb, falling back to the first; blur() affects only active elements inside this slider.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiSlider, HuiSpace } from '@nscode/handy-ui'
import type { SliderExpose } from '@nscode/handy-ui'
const control = ref<SliderExpose>()
const interval = ref<[number, number]>([20, 60])
const event = ref('Waiting')
const inspect = () => {
event.value = `first=${control.value?.firstValue}, second=${control.value?.secondValue}`
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace wrap>
<HuiButton @mousedown.prevent @click="control?.focus()">focus()</HuiButton>
<HuiButton @mousedown.prevent @click="control?.blur()">blur()</HuiButton>
<HuiButton @click="inspect">Read exposed thumb values</HuiButton>
</HuiSpace>
<HuiSlider
ref="control"
v-model="interval"
range
@update:model-value="event = 'update: ' + $event"
@change="event = 'change: ' + $event"
@focus="event = 'focus'"
@blur="event = 'blur'"
/>
<span>{{ event }}; sorted range: {{ interval }}</span>
</HuiSpace>
</template>
Form Integration
<script setup lang="ts">
import { reactive } from 'vue'
import { HuiForm, HuiFormItem, HuiSlider, HuiSpace } from '@nscode/handy-ui'
import type { FormRule } from '@nscode/handy-ui'
const model = reactive({ volume: 20 })
const rules: Record<string, FormRule[]> = {
volume: [{ min: 10, message: 'Choose at least 10', trigger: 'change' }]
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiForm :model="model" :rules="rules"
><HuiFormItem label="Volume" prop="volume"
><HuiSlider v-model="model.volume" show-input /></HuiFormItem
></HuiForm>
<HuiSlider :model-value="40" :validate-event="false" />
</HuiSpace>
</template>
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| modelValue | number | [number, number] | undefined | Numeric value or ordered range; omitted: min or [min,max] A passed prop is controlled; omitted uses internal state. |
| min | number | 0 | Minimum value |
| max | number | 100 | Maximum value |
| step | number | 1 | Step from min; nonpositive values use 1 |
| disabled | boolean | false | Disable interaction |
| showInput | boolean | false | Show numeric field(s) |
| showStops | boolean | false | Draw internal step points |
| range | boolean | false | Use two thumbs and a tuple value |
| vertical | boolean | false | Use vertical layout |
| marks | Record<number, string> | — | Labels by numeric position |
| height | string | '200px' | Track height in vertical mode |
| size | 'large' | 'default' | 'small' | — | Inherited size |
| validateEvent | boolean | true | Validate on completed changes, not every drag frame |
Events
| Event | Payload | Description |
|---|---|---|
| update:modelValue | (value: number | [number, number]) | Value updates, including during dragging |
| change | (value: number | [number, number]) | A completed user operation changes the value |
| 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 |
| sliderRef | HTMLElement | undefined | Current root element |
| firstValue | number | Current first thumb value |
| secondValue | number | Second thumb value in range mode; otherwise equal to firstValue |
Types
import type { SliderSize, SliderProps, SliderEmits, SliderExpose } from '@nscode/handy-ui'
export type SliderSize = 'large' | 'default' | 'small'
export interface SliderProps {
modelValue?: number | [number, number]
min?: number
max?: number
step?: number
disabled?: boolean
showInput?: boolean
showStops?: boolean
range?: boolean
vertical?: boolean
marks?: Record<number, string>
height?: string
validateEvent?: boolean
size?: SliderSize
}
export interface SliderEmits {
focus: [event: FocusEvent]
blur: [event: FocusEvent]
'update:modelValue': [value: number | [number, number]]
change: [value: number | [number, number]]
}
export interface SliderExpose {
focus: () => void
blur: () => void
sliderRef: HTMLElement | undefined
firstValue: number
secondValue: number
}