Used with a component · Knob
useKnob
The headless engine behind <Knob>: a value on a 270° sweep, from −135° to +135° with the gap at the bottom. Dragging reads the pointer's angle around the dial's center, so the value follows your finger around the circle instead of a straight-line distance. The arrow keys step, Page Up and Page Down jump 10 steps, and Home and End go to the bounds. It renders nothing: bind rootStyle on your root and draw the dial from --ui-knob-angle and --ui-knob-fraction. The KNOB_SWEEP_* constants give the arc in degrees, for drawing a track that matches.
Install
import { useKnob, KNOB_SWEEP_START_DEG, KNOB_SWEEP_DEG, KNOB_SWEEP_END_DEG } from 'vael-ui'Examples
<script setup lang="ts">
import { ref, useTemplateRef } from 'vue'
import { useKnob } from 'vael-ui'
const volume = ref(40)
const dialEl = useTemplateRef<HTMLElement>('dial')
const { rootStyle, onDialPointerdown, onDialKeydown } = useKnob(volume, {
dialEl,
min: 0,
max: 100,
onCommit: () => console.log('saved', volume.value),
})
</script>
<template>
<div
ref="dial"
class="my-knob"
role="slider"
tabindex="0"
aria-label="Volume"
aria-valuemin="0"
aria-valuemax="100"
:aria-valuenow="volume"
:style="rootStyle"
@pointerdown="onDialPointerdown"
@keydown="onDialKeydown"
>
<span class="my-knob-pointer" />
</div>
</template>
<style>
.my-knob-pointer {
rotate: var(--ui-knob-angle);
}
</style>