vael-ui v0.5.7

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>

Parameters

NameTypeDescription
modelRef<number | [number, number]>The value the slider writes to (first argument). A tuple turns on range mode with two thumbs that can touch but never cross.
trackElRef<HTMLElement | null>The track. Pointer positions are measured against its box, so the value follows the pointer 1:1 along it.
thumbElsRef<HTMLElement[] | null>The thumbs in index order. When you pass them, a drag or a track press focuses the thumb it moves.
minMaybeRefOrGetter<number>The lowest value. Default 0.
maxMaybeRefOrGetter<number>The highest value. Default 100.
stepMaybeRefOrGetter<number>The step every value snaps to, counted from min. Default 1. The arrow keys move one step, Page Up and Page Down move ten.
orientationMaybeRefOrGetter<'horizontal' | 'vertical'>The track's direction. A vertical track counts up from its bottom edge. Default 'horizontal'.
disabledMaybeRefOrGetter<boolean>Ignores pointer and key input while true. Default false.
onCommit() => voidFires once per settled interaction: on pointer release, or after each key press. model updates on every drag frame, so defer an expensive side effect, such as a save request, to here. <Slider> re-emits it as change, like a native range input.

Returns

NameTypeDescription
rootStyleComputedRef<Record<string, string>>Bind it on the root. It sets --ui-slider-p0, the first value as a 0 to 1 fraction of the range, and --ui-slider-p1 in range mode.
onTrackPointerdown(event: PointerEvent) => voidBind it on the track. It jumps the nearest thumb to the press and keeps dragging it until release.
onThumbPointerdown(index: number, event: PointerEvent) => voidBind it on each thumb with .stop, so the track's own listener doesn't run as well.
onThumbKeydown(index: number, event: KeyboardEvent) => voidBind it on each thumb. It handles the arrow keys, Page Up, Page Down, Home and End.
isDraggingRef<boolean>true from pointer down until release. A move counts from its first 4 px.
activeThumbRef<number | null>The index of the thumb a pointer is dragging, or null when idle.