|
| 1 | +<script setup lang="ts"> |
| 2 | +import { type PropType, shallowRef, toRef, useAttrs } from 'vue' |
| 3 | +import { useControllableState } from '../hooks/useControllableState.ts' |
| 4 | +import { isFunction, isNumber } from '../utils/is.ts' |
| 5 | +import { clamp, getDecimalCount, roundValue } from '../utils/number.ts' |
| 6 | +import { composeEventHandlers } from '../utils/composeEventHandlers.ts' |
| 7 | +import { ARROW_KEYS, Collection, PAGE_KEYS, type SliderContext, type SliderProps, provideSliderContext } from './Slider.ts' |
| 8 | +import { getClosestValueIndex, getNextSortedValues, hasMinStepsBetweenValues } from './utils.ts' |
| 9 | +import SliderHorizontal from './SliderHorizontal.vue' |
| 10 | +import SliderVertical from './SliderVertical.vue' |
| 11 | +
|
| 12 | +defineOptions({ |
| 13 | + name: 'Slider', |
| 14 | + inheritAttrs: false, |
| 15 | +}) |
| 16 | +
|
| 17 | +const props = defineProps({ |
| 18 | + as: { |
| 19 | + type: [String, Object] as PropType<SliderProps['as']>, |
| 20 | + required: false, |
| 21 | + default: 'span', |
| 22 | + }, |
| 23 | + asChild: { |
| 24 | + type: [String, Object] as PropType<SliderProps['asChild']>, |
| 25 | + required: false, |
| 26 | + default: undefined, |
| 27 | + }, |
| 28 | + name: { |
| 29 | + type: String, |
| 30 | + required: false, |
| 31 | + default: undefined, |
| 32 | + }, |
| 33 | + disabled: { |
| 34 | + type: Boolean, |
| 35 | + required: false, |
| 36 | + default: false, |
| 37 | + }, |
| 38 | + orientation: { |
| 39 | + type: String as PropType<Required<SliderProps>['orientation']>, |
| 40 | + required: false, |
| 41 | + default: 'horizontal', |
| 42 | + }, |
| 43 | + dir: { |
| 44 | + type: String as PropType<Required<SliderProps>['dir']>, |
| 45 | + required: false, |
| 46 | + default: undefined, |
| 47 | + }, |
| 48 | + min: { |
| 49 | + type: Number, |
| 50 | + required: false, |
| 51 | + default: 0, |
| 52 | + }, |
| 53 | + max: { |
| 54 | + type: Number, |
| 55 | + rqeuired: false, |
| 56 | + default: 100, |
| 57 | + }, |
| 58 | + step: { |
| 59 | + type: Number, |
| 60 | + required: false, |
| 61 | + default: 1, |
| 62 | + }, |
| 63 | + minStepsBetweenThumbs: { |
| 64 | + type: Number, |
| 65 | + required: false, |
| 66 | + default: 0, |
| 67 | + }, |
| 68 | + value: { |
| 69 | + type: [Array] as PropType<Required<SliderProps>['value']>, |
| 70 | + required: false, |
| 71 | + default: undefined, |
| 72 | + }, |
| 73 | + defaultValue: { |
| 74 | + type: [Array] as PropType<Required<SliderProps>['defaultValue']>, |
| 75 | + required: false, |
| 76 | + default(rawProps: Record<string, unknown>) { |
| 77 | + return isNumber(rawProps.min) ? [rawProps.min] : [0] |
| 78 | + }, |
| 79 | + }, |
| 80 | + inverted: { |
| 81 | + type: Boolean, |
| 82 | + required: false, |
| 83 | + default: false, |
| 84 | + }, |
| 85 | +}) |
| 86 | +const emit = defineEmits<{ |
| 87 | + 'update:value': [value: number[]] |
| 88 | + 'valueCommit': [value: number[]] |
| 89 | +}>() |
| 90 | +const attrs = useAttrs() |
| 91 | +const elRef = shallowRef<HTMLSpanElement>() |
| 92 | +
|
| 93 | +const thumbRefs: SliderContext['thumbs'] = new Set() |
| 94 | +const valueIndexToChangeRef: SliderContext['valueIndexToChangeRef'] = { value: 0 } |
| 95 | +
|
| 96 | +const values = useControllableState( |
| 97 | + props, |
| 98 | + (v) => { |
| 99 | + const thumbs = Array.from(thumbRefs) |
| 100 | + thumbs[valueIndexToChangeRef.value]?.focus() |
| 101 | + emit('update:value', v) |
| 102 | + }, |
| 103 | + 'value', |
| 104 | + props.defaultValue, |
| 105 | +) |
| 106 | +
|
| 107 | +let valuesBeforeSlideStartRef = values.value |
| 108 | +
|
| 109 | +function handleSlideStart(value: number) { |
| 110 | + if (props.disabled) |
| 111 | + return |
| 112 | + const closestIndex = getClosestValueIndex(values.value, value) |
| 113 | + updateValues(value, closestIndex) |
| 114 | +} |
| 115 | +
|
| 116 | +function handleSlideMove(value: number) { |
| 117 | + if (props.disabled) |
| 118 | + return |
| 119 | + updateValues(value, valueIndexToChangeRef.value) |
| 120 | +} |
| 121 | +
|
| 122 | +function handleSlideEnd() { |
| 123 | + if (props.disabled) |
| 124 | + return |
| 125 | + const prevValue = valuesBeforeSlideStartRef[valueIndexToChangeRef.value] |
| 126 | + const nextValue = values.value[valueIndexToChangeRef.value] |
| 127 | + const hasChanged = nextValue !== prevValue |
| 128 | + if (hasChanged) |
| 129 | + emit('valueCommit', values.value) |
| 130 | +} |
| 131 | +
|
| 132 | +function handleStepKeydown({ event, direction: stepDirection }: { event: KeyboardEvent, direction: number }) { |
| 133 | + if (props.disabled) |
| 134 | + return |
| 135 | + const isPageKey = PAGE_KEYS.includes(event.key) |
| 136 | + const isSkipKey = isPageKey || (event.shiftKey && ARROW_KEYS.includes(event.key)) |
| 137 | + const multiplier = isSkipKey ? 10 : 1 |
| 138 | + const atIndex = valueIndexToChangeRef.value |
| 139 | + const value = values.value[atIndex]! |
| 140 | + const stepInDirection = props.step * multiplier * stepDirection |
| 141 | + updateValues(value + stepInDirection, atIndex, { commit: true }) |
| 142 | +} |
| 143 | +
|
| 144 | +function updateValues(value: number, atIndex: number, { commit } = { commit: false }) { |
| 145 | + const decimalCount = getDecimalCount(props.step) |
| 146 | + const snapToStep = roundValue(Math.round((value - props.min) / props.step) * props.step + props.min, decimalCount) |
| 147 | + const nextValue = clamp(snapToStep, [props.min, props.max]) |
| 148 | +
|
| 149 | + const prevValues = values.value |
| 150 | + const nextValues = getNextSortedValues(values.value, nextValue, atIndex) |
| 151 | + if (hasMinStepsBetweenValues(nextValues, props.minStepsBetweenThumbs * props.step)) { |
| 152 | + valueIndexToChangeRef.value = nextValues.indexOf(nextValue) |
| 153 | + const hasChanged = String(nextValues) !== String(prevValues) |
| 154 | + if (hasChanged && commit) |
| 155 | + emit('valueCommit', nextValues) |
| 156 | + values.value = nextValues |
| 157 | + } |
| 158 | +} |
| 159 | +
|
| 160 | +const onPointerdown = composeEventHandlers<PointerEvent>((event) => { |
| 161 | + isFunction(attrs.onPointerdown) && attrs.onPointerdown(event) |
| 162 | +}, () => { |
| 163 | + if (!props.disabled) |
| 164 | + valuesBeforeSlideStartRef = values.value |
| 165 | +}) |
| 166 | +
|
| 167 | +Collection.provideCollectionContext(elRef) |
| 168 | +
|
| 169 | +provideSliderContext({ |
| 170 | + name: toRef(props, 'name'), |
| 171 | + disabled: toRef(props, 'disabled'), |
| 172 | + min: toRef(props.min), |
| 173 | + max: toRef(props.max), |
| 174 | + valueIndexToChangeRef, |
| 175 | + thumbs: thumbRefs, |
| 176 | + values, |
| 177 | + orientation: toRef(props.orientation), |
| 178 | +}) |
| 179 | +</script> |
| 180 | + |
| 181 | +<template> |
| 182 | + <component |
| 183 | + :is="orientation === 'horizontal' ? SliderHorizontal : SliderVertical" |
| 184 | + :ref="(el: any) => elRef = el?.$el" |
| 185 | + :as="as" |
| 186 | + :as-child="asChild" |
| 187 | + :aria-disabled="disabled" |
| 188 | + :data-disabled="disabled ? '' : undefined" |
| 189 | + :dir="dir" |
| 190 | + v-bind="{ |
| 191 | + ...attrs, |
| 192 | + onPointerdown, |
| 193 | + }" |
| 194 | + :min="min" |
| 195 | + :max="max" |
| 196 | + :inverted="inverted" |
| 197 | + @slide-start="handleSlideStart" |
| 198 | + @slide-move="handleSlideMove" |
| 199 | + @slide-end="handleSlideEnd" |
| 200 | + @step-keydown="handleStepKeydown" |
| 201 | + > |
| 202 | + <slot /> |
| 203 | + </component> |
| 204 | +</template> |
0 commit comments