|
| 1 | +import {useEffect, useState} from 'react'; |
| 2 | +import {type LayoutChangeEvent} from 'react-native'; |
| 3 | +import {Gesture} from 'react-native-gesture-handler'; |
| 4 | +import { |
| 5 | + interpolate, |
| 6 | + runOnJS, |
| 7 | + useAnimatedProps, |
| 8 | + useAnimatedStyle, |
| 9 | + useSharedValue, |
| 10 | + withSpring, |
| 11 | + withTiming, |
| 12 | + type SharedValue, |
| 13 | +} from 'react-native-reanimated'; |
| 14 | + |
| 15 | +import {type SliderConfig} from '../slider-config.type'; |
| 16 | +import {normalize} from '../worklets/normalize.worklet'; |
| 17 | +import {validateSliderRange} from './validate-slider-range'; |
| 18 | +import {calcTranslationXBasedOnValue} from '../worklets/calc-translationX-based-on-value.worklet'; |
| 19 | + |
| 20 | +enum ThumbType { |
| 21 | + MIN = 'MIN', |
| 22 | + MAX = 'MAX', |
| 23 | +} |
| 24 | + |
| 25 | +export const useRangeSlider = ({max, min, step, damping}: SliderConfig, range: number[], onChangeRange?: (range: number[]) => void) => { |
| 26 | + const [sliderLayout, setSliderLayout] = useState({width: 0, height: 0, x: 0, y: 0}); |
| 27 | + |
| 28 | + const [minValue, maxValue] = [range[0] ?? 0, range[1] ?? 0]; |
| 29 | + |
| 30 | + const thumbMinSliding = useSharedValue(0); |
| 31 | + const thumbMaxSliding = useSharedValue(0); |
| 32 | + const thumbMinTranslationX = useSharedValue(0); |
| 33 | + const thumbMaxTranslationX = useSharedValue(0); |
| 34 | + const thumbsTranslationXContext = useSharedValue({min: 0, max: 0}); |
| 35 | + const selectedRange = useSharedValue([minValue, maxValue]); |
| 36 | + |
| 37 | + useEffect(() => { |
| 38 | + validateSliderRange(range); |
| 39 | + adjustThumbsPosition(sliderLayout.width, true); |
| 40 | + }, [range, sliderLayout]); |
| 41 | + |
| 42 | + const filledTrackAnimatedStyle = useAnimatedStyle(() => { |
| 43 | + const remainingTrackFlex = interpolate(thumbMinTranslationX.value, [0, sliderLayout.width], [0, 1]); |
| 44 | + const remainingTrackAfterFlex = interpolate(thumbMaxTranslationX.value, [0, sliderLayout.width], [0, 1]); |
| 45 | + |
| 46 | + return {flex: Math.abs(remainingTrackFlex - remainingTrackAfterFlex)}; |
| 47 | + }, [sliderLayout]); |
| 48 | + |
| 49 | + const remainingTrackBeforeAnimatedStyle = useAnimatedStyle( |
| 50 | + () => ({flex: interpolate(thumbMinTranslationX.value, [0, sliderLayout.width], [0, 1])}), |
| 51 | + [sliderLayout] |
| 52 | + ); |
| 53 | + |
| 54 | + const remainingTrackAfterAnimatedStyle = useAnimatedStyle( |
| 55 | + () => ({flex: interpolate(thumbMaxTranslationX.value, [0, sliderLayout.width], [1, 0])}), |
| 56 | + [sliderLayout] |
| 57 | + ); |
| 58 | + |
| 59 | + const calcValueBasedOnThumbTranslationX = (translationX: number, thumbType: ThumbType) => { |
| 60 | + 'worklet'; |
| 61 | + |
| 62 | + const value = Math.round(interpolate(translationX, [0, sliderLayout.width], [min, max])); |
| 63 | + const normalizedValue = normalize(value, step); |
| 64 | + |
| 65 | + selectedRange.value = |
| 66 | + thumbType === ThumbType.MIN ? [normalizedValue, selectedRange.value[1] ?? 0] : [selectedRange.value[0] ?? 0, normalizedValue]; |
| 67 | + |
| 68 | + return normalizedValue; |
| 69 | + }; |
| 70 | + |
| 71 | + const updateTranslationX = (thumbTranslationX: SharedValue<number>, translationX: number) => { |
| 72 | + 'worklet'; |
| 73 | + |
| 74 | + if (step) { |
| 75 | + const stepSize = (sliderLayout.width / (max - min)) * step; |
| 76 | + const stepSnap = normalize(translationX, stepSize); |
| 77 | + |
| 78 | + thumbTranslationX.value = stepSnap; |
| 79 | + } else { |
| 80 | + thumbTranslationX.value = translationX; |
| 81 | + } |
| 82 | + }; |
| 83 | + |
| 84 | + const gesture = Gesture.Pan() |
| 85 | + .onStart((e) => { |
| 86 | + const touchX = e.x - sliderLayout.x; |
| 87 | + |
| 88 | + const distanceToMinThumb = Math.abs(touchX - thumbMinTranslationX.value); |
| 89 | + const distanceToMaxThumb = Math.abs(touchX - thumbMaxTranslationX.value); |
| 90 | + const activeThumb = touchX < thumbMaxTranslationX.value && distanceToMinThumb <= distanceToMaxThumb ? thumbMinSliding : thumbMaxSliding; |
| 91 | + |
| 92 | + activeThumb.value = withTiming(1); |
| 93 | + }) |
| 94 | + .onUpdate((e) => { |
| 95 | + if (thumbMinSliding.value) { |
| 96 | + const currentMinTranslationX = Math.max(0, Math.min(thumbMaxTranslationX.value, e.translationX + thumbsTranslationXContext.value.min)); |
| 97 | + |
| 98 | + updateTranslationX(thumbMinTranslationX, currentMinTranslationX); |
| 99 | + } else if (thumbMaxSliding.value) { |
| 100 | + const currentMaxTranslationX = Math.min( |
| 101 | + sliderLayout.width, |
| 102 | + Math.max(thumbMinTranslationX.value, e.translationX + thumbsTranslationXContext.value.max) |
| 103 | + ); |
| 104 | + |
| 105 | + updateTranslationX(thumbMaxTranslationX, currentMaxTranslationX); |
| 106 | + } |
| 107 | + }) |
| 108 | + .onEnd(() => { |
| 109 | + thumbMinSliding.value = withTiming(0); |
| 110 | + thumbMaxSliding.value = withTiming(0); |
| 111 | + thumbsTranslationXContext.value = {min: thumbMinTranslationX.value, max: thumbMaxTranslationX.value}; |
| 112 | + |
| 113 | + if (onChangeRange) { |
| 114 | + runOnJS(onChangeRange)(selectedRange.value); |
| 115 | + } |
| 116 | + }); |
| 117 | + |
| 118 | + const setUpSliderLayout = (e: LayoutChangeEvent) => { |
| 119 | + const layout = e.nativeEvent.layout; |
| 120 | + |
| 121 | + adjustThumbsPosition(layout.width); |
| 122 | + setSliderLayout(layout); |
| 123 | + }; |
| 124 | + |
| 125 | + const adjustThumbsPosition = (width: number, isUpdating: boolean = false) => { |
| 126 | + const initialStartTthumbTranslationX = calcTranslationXBasedOnValue({min, max}, normalize(minValue, step), width); |
| 127 | + const initiaEndTthumbTranslationX = calcTranslationXBasedOnValue({min, max}, normalize(maxValue, step), width); |
| 128 | + |
| 129 | + thumbMinTranslationX.value = isUpdating ? withSpring(initialStartTthumbTranslationX, {damping}) : initialStartTthumbTranslationX; |
| 130 | + thumbMaxTranslationX.value = isUpdating ? withSpring(initiaEndTthumbTranslationX, {damping}) : initiaEndTthumbTranslationX; |
| 131 | + thumbsTranslationXContext.value = {min: initialStartTthumbTranslationX, max: initiaEndTthumbTranslationX}; |
| 132 | + }; |
| 133 | + |
| 134 | + const slideToTrackPoint = (pointValue: number) => { |
| 135 | + if (typeof selectedRange.value[0] !== 'number' || typeof selectedRange.value[1] !== 'number') { |
| 136 | + return; |
| 137 | + } |
| 138 | + |
| 139 | + const diffFromMinThumb = Math.abs(pointValue - selectedRange.value[0]); |
| 140 | + const diffFromMaxThumb = Math.abs(pointValue - selectedRange.value[1]); |
| 141 | + const [activeThumbSliding, activeThumbTranslationX] = |
| 142 | + diffFromMinThumb < diffFromMaxThumb ? [thumbMinSliding, thumbMinTranslationX] : [thumbMaxSliding, thumbMaxTranslationX]; |
| 143 | + const thumbPointValueTranslationX = calcTranslationXBasedOnValue({min, max}, pointValue, sliderLayout.width); |
| 144 | + |
| 145 | + activeThumbSliding.value = withTiming(1); |
| 146 | + activeThumbTranslationX.value = withSpring(thumbPointValueTranslationX, {damping}, () => { |
| 147 | + activeThumbSliding.value = withTiming(0); |
| 148 | + thumbsTranslationXContext.value = {min: thumbMinTranslationX.value, max: thumbMaxTranslationX.value}; |
| 149 | + |
| 150 | + if (onChangeRange) { |
| 151 | + runOnJS(onChangeRange)(selectedRange.value); |
| 152 | + } |
| 153 | + }); |
| 154 | + }; |
| 155 | + |
| 156 | + const animMinValueProps = useAnimatedProps(() => ({ |
| 157 | + text: `${calcValueBasedOnThumbTranslationX(thumbMinTranslationX.value, ThumbType.MIN)}`, |
| 158 | + })); |
| 159 | + |
| 160 | + const animMaxValueProps = useAnimatedProps(() => ({ |
| 161 | + text: `${calcValueBasedOnThumbTranslationX(thumbMaxTranslationX.value, ThumbType.MAX)}`, |
| 162 | + })); |
| 163 | + |
| 164 | + return { |
| 165 | + gesture, |
| 166 | + selectedRange, |
| 167 | + thumbMinSliding, |
| 168 | + thumbMaxSliding, |
| 169 | + animMinValueProps, |
| 170 | + animMaxValueProps, |
| 171 | + filledTrackAnimatedStyle, |
| 172 | + remainingTrackAfterAnimatedStyle, |
| 173 | + remainingTrackBeforeAnimatedStyle, |
| 174 | + |
| 175 | + setUpSliderLayout, |
| 176 | + slideToTrackPoint, |
| 177 | + }; |
| 178 | +}; |
0 commit comments