Line data Source code
1 16 : <script setup lang="ts">
2 : import type { HTMLAttributes } from 'vue'
3 : import { computed, useTemplateRef } from 'vue'
4 :
5 : import { cn } from '@comfyorg/tailwind-utils'
6 :
7 : const {
8 : min = 0,
9 : max = 100,
10 : step = 1,
11 : disabled = false,
12 : class: className
13 : } = defineProps<{
14 : class?: HTMLAttributes['class']
15 : disabled?: boolean
16 : max?: number
17 : min?: number
18 : step?: number
19 : }>()
20 :
21 2355 : const modelValue = defineModel<number>({ required: true })
22 0 : const root = useTemplateRef('root')
23 :
24 0 : const progress = computed(() =>
25 : max === min ? 0 : (modelValue.value - min) / (max - min)
26 : )
27 0 : const dashOffset = computed(() => 75 * (1 - progress.value))
28 :
29 0 : function setValue(value: number) {
30 0 : if (disabled) return
31 0 : const stepped = step ? Math.round((value - min) / step) * step + min : value
32 0 : modelValue.value = Math.min(max, Math.max(min, stepped))
33 : }
34 :
35 0 : function updateFromPointer(event: PointerEvent) {
36 0 : const bounds = root.value?.getBoundingClientRect()
37 0 : if (!bounds) return
38 :
39 0 : const x = event.clientX - bounds.left - bounds.width / 2
40 0 : const y = event.clientY - bounds.top - bounds.height / 2
41 : const angle = (Math.atan2(y, x) * 180) / Math.PI
42 0 : const clockwise = (angle + 225 + 360) % 360
43 0 : const position = clockwise > 315 ? 0 : clockwise / 270
44 0 : setValue(min + position * (max - min))
45 : }
46 :
47 0 : function onPointerDown(event: PointerEvent) {
48 0 : root.value?.setPointerCapture(event.pointerId)
49 0 : updateFromPointer(event)
50 : }
51 :
52 0 : function keyboardTarget(key: string): number | undefined {
53 0 : switch (key) {
54 : case 'ArrowUp':
55 : case 'ArrowRight':
56 0 : return modelValue.value + step
57 : case 'ArrowDown':
58 : case 'ArrowLeft':
59 0 : return modelValue.value - step
60 : case 'PageUp':
61 0 : return modelValue.value + 10 * step
62 : case 'PageDown':
63 0 : return modelValue.value - 10 * step
64 : case 'Home':
65 0 : return min
66 : case 'End':
67 0 : return max
68 : }
69 : }
70 :
71 0 : function onKeydown(event: KeyboardEvent) {
72 0 : const target = keyboardTarget(event.key)
73 0 : if (target === undefined) return
74 0 : event.preventDefault()
75 0 : setValue(target)
76 : }
77 0 : </script>
78 :
79 : <template>
80 0 : <div
81 : ref="root"
82 : role="slider"
83 : :tabindex="disabled ? -1 : 0"
84 : :aria-valuemin="min"
85 : :aria-valuemax="max"
86 : :aria-valuenow="modelValue"
87 : :aria-disabled="disabled || undefined"
88 : :class="
89 : cn(
90 : 'size-12 cursor-pointer touch-none outline-none',
91 : disabled && 'cursor-not-allowed opacity-50',
92 : className
93 : )
94 : "
95 : @keydown="onKeydown"
96 : @pointerdown="onPointerDown"
97 : @pointermove="
98 : (event) =>
99 : root?.hasPointerCapture(event.pointerId) && updateFromPointer(event)
100 : "
101 : >
102 : <svg viewBox="0 0 32 32" class="size-full rotate-[-225deg]">
103 : <circle
104 : data-testid="knob-track"
105 : cx="16"
106 : cy="16"
107 : r="12"
108 : pathLength="100"
109 : fill="none"
110 : stroke="currentColor"
111 : stroke-width="3"
112 : stroke-dasharray="75 25"
113 : class="text-secondary"
114 : />
115 : <circle
116 : data-testid="knob-value"
117 : cx="16"
118 : cy="16"
119 : r="12"
120 : pathLength="100"
121 : fill="none"
122 : stroke="currentColor"
123 : stroke-width="3"
124 : stroke-linecap="round"
125 : stroke-dasharray="75 100"
126 : :stroke-dashoffset="dashOffset"
127 : class="text-foreground"
128 : />
129 : </svg>
130 : </div>
131 : </template>
|