Used with a component · Slider
useSlider
The headless engine behind <Slider>. It maps pointer and keyboard input onto a number or a [start, end] tuple, snaps to step and keeps two thumbs from crossing. It writes no inline style on the thumbs or the fill. Instead, rootStyle sets two 0 to 1 fractions, --ui-slider-p0 and --ui-slider-p1, and your CSS places everything from them. <Slider> adds the markup, ARIA, labels and the focus ring. Use the composable when you need a track a component can't give you, such as a scrubber or a custom range picker.
Install
import { useSlider } from 'vael-ui'Examples
<script setup lang="ts">
import { ref, useTemplateRef } from 'vue'
import { useSlider } from 'vael-ui'
const volume = ref(40)
const trackEl = useTemplateRef<HTMLElement>('track')
const { rootStyle, onTrackPointerdown, onThumbPointerdown, onThumbKeydown } = useSlider(volume, {
trackEl,
step: 5,
})
</script>
<template>
<div class="volume" :style="rootStyle">
<div ref="track" class="volume-track" @pointerdown="onTrackPointerdown">
<span
class="volume-thumb"
role="slider"
tabindex="0"
aria-label="Volume"
:aria-valuenow="volume"
aria-valuemin="0"
aria-valuemax="100"
@pointerdown.stop="onThumbPointerdown(0, $event)"
@keydown="onThumbKeydown(0, $event)"
/>
</div>
</div>
</template>
<style>
/* Without touch-action: none, a touch drag scrolls the page instead. */
.volume-track { position: relative; block-size: 4px; touch-action: none; }
.volume-thumb {
position: absolute;
inset-inline-start: calc(var(--ui-slider-p0) * 100%);
translate: -50% 0;
}
</style>