|
| 1 | +import React, { useEffect } from 'react'; |
| 2 | +import { View, Text, StyleSheet, TextInput } from 'react-native'; |
| 3 | +import { GestureDetector, Gesture } from 'react-native-gesture-handler'; |
| 4 | +import Animated, { |
| 5 | + useSharedValue, |
| 6 | + useAnimatedStyle, |
| 7 | + withSpring, |
| 8 | + useAnimatedProps, |
| 9 | +} from 'react-native-reanimated'; |
| 10 | +import { runOnJS } from 'react-native-reanimated'; |
| 11 | +import { scheduleOnRN } from 'react-native-worklets'; |
| 12 | + |
| 13 | +const SLIDER_HEIGHT = 100; |
| 14 | +const THUMB_SIZE = 30; |
| 15 | +const TRACK_HEIGHT = SLIDER_HEIGHT - THUMB_SIZE; |
| 16 | + |
| 17 | +const AText = Animated.createAnimatedComponent(TextInput); |
| 18 | + |
| 19 | +interface VerticalSliderProps { |
| 20 | + label: string; |
| 21 | + value: number; |
| 22 | + labelColor?: string; |
| 23 | + valueColor?: string; |
| 24 | + onValueChange: (val: number) => void; |
| 25 | + possibleValues?: number[]; |
| 26 | +} |
| 27 | + |
| 28 | +const VerticalSlider: React.FC<VerticalSliderProps> = ({ |
| 29 | + label, |
| 30 | + value, |
| 31 | + labelColor = '#333', |
| 32 | + valueColor = '#555', |
| 33 | + onValueChange, |
| 34 | + possibleValues |
| 35 | +}) => { |
| 36 | + const progress = useSharedValue(value); |
| 37 | + const startValue = useSharedValue(0); |
| 38 | + |
| 39 | + useEffect(() => { |
| 40 | + progress.value = withSpring(value); |
| 41 | + }, [value, progress]); |
| 42 | + |
| 43 | + const findClosestValue = (currentValue: number): number => { |
| 44 | + 'worklet'; |
| 45 | + if (!possibleValues || possibleValues.length === 0) { |
| 46 | + return currentValue; |
| 47 | + } |
| 48 | + |
| 49 | + let closest = possibleValues[0]; |
| 50 | + let minDistance = Math.abs(currentValue - closest); |
| 51 | + |
| 52 | + for (const possibleValue of possibleValues) { |
| 53 | + const distance = Math.abs(currentValue - possibleValue); |
| 54 | + if (distance < minDistance) { |
| 55 | + minDistance = distance; |
| 56 | + closest = possibleValue; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + return closest; |
| 61 | + }; |
| 62 | + |
| 63 | + const handleValueChange = (val: number) => { |
| 64 | + onValueChange(val); |
| 65 | + }; |
| 66 | + |
| 67 | + const gesture = Gesture.Pan() |
| 68 | + .onStart(() => { |
| 69 | + startValue.value = progress.value; |
| 70 | + }) |
| 71 | + .onUpdate((e) => { |
| 72 | + const change = -e.translationY / TRACK_HEIGHT; |
| 73 | + const newValue = startValue.value + change; |
| 74 | + const clampedValue = Math.min(Math.max(newValue, 0), 1); |
| 75 | + progress.value = clampedValue; |
| 76 | + }) |
| 77 | + .onEnd(() => { |
| 78 | + if (possibleValues && possibleValues.length > 0) { |
| 79 | + const snappedValue = findClosestValue(progress.value); |
| 80 | + progress.value = withSpring(snappedValue); |
| 81 | + scheduleOnRN(handleValueChange, snappedValue); |
| 82 | + } else { |
| 83 | + const finalValue = Math.min(Math.max(progress.value, 0), 1); |
| 84 | + scheduleOnRN(handleValueChange, finalValue); |
| 85 | + } |
| 86 | + }); |
| 87 | + |
| 88 | + const thumbStyle = useAnimatedStyle(() => { |
| 89 | + const translateY = (1 - progress.value) * TRACK_HEIGHT; |
| 90 | + return { |
| 91 | + transform: [{ translateY }], |
| 92 | + }; |
| 93 | + }); |
| 94 | + |
| 95 | + const renderTickMarks = () => { |
| 96 | + if (!possibleValues || possibleValues.length === 0) { |
| 97 | + return null; |
| 98 | + } |
| 99 | + |
| 100 | + return possibleValues.map((val, index) => { |
| 101 | + const position = (1 - val) * TRACK_HEIGHT + THUMB_SIZE / 2; |
| 102 | + return ( |
| 103 | + <View |
| 104 | + key={index} |
| 105 | + style={[ |
| 106 | + styles.tickMark, |
| 107 | + { |
| 108 | + top: position, // Center the tick mark |
| 109 | + }, |
| 110 | + ]} |
| 111 | + /> |
| 112 | + ); |
| 113 | + }); |
| 114 | + }; |
| 115 | + |
| 116 | + const displayProps = useAnimatedProps(() => { |
| 117 | + if (possibleValues && possibleValues.length > 0) { |
| 118 | + return { |
| 119 | + defaultValue: '', |
| 120 | + text: '', |
| 121 | + }; |
| 122 | + } |
| 123 | + |
| 124 | + return { |
| 125 | + defaultValue: (value * 100).toString(), |
| 126 | + text: (progress.value * 100).toFixed(0), |
| 127 | + }; |
| 128 | + }); |
| 129 | + |
| 130 | + return ( |
| 131 | + <View style={styles.sliderContainer}> |
| 132 | + <Text style={[styles.sliderLabel, { color: labelColor }]}>{label}</Text> |
| 133 | + <View style={styles.sliderTrackContainer}> |
| 134 | + <View style={styles.sliderTrack} /> |
| 135 | + {renderTickMarks()} |
| 136 | + <GestureDetector gesture={gesture}> |
| 137 | + <Animated.View style={[styles.sliderThumbHitArea, thumbStyle]}> |
| 138 | + <View style={styles.sliderThumb} /> |
| 139 | + </Animated.View> |
| 140 | + </GestureDetector> |
| 141 | + </View> |
| 142 | + <AText style={[styles.sliderValue, { color: valueColor }]} editable={false} animatedProps={displayProps} /> |
| 143 | + {/* <Text style={[styles.sliderValue, { color: valueColor }]}> |
| 144 | + {getDisplayValue()} |
| 145 | + </Text> */} |
| 146 | + </View> |
| 147 | + ); |
| 148 | +}; |
| 149 | + |
| 150 | +const styles = StyleSheet.create({ |
| 151 | + sliderContainer: { |
| 152 | + alignItems: 'center', |
| 153 | + gap: 5, |
| 154 | + height: SLIDER_HEIGHT + 40, |
| 155 | + }, |
| 156 | + sliderLabel: { |
| 157 | + fontWeight: 'bold', |
| 158 | + fontSize: 12, |
| 159 | + }, |
| 160 | + sliderTrackContainer: { |
| 161 | + width: 40, |
| 162 | + height: SLIDER_HEIGHT, |
| 163 | + justifyContent: 'center', |
| 164 | + alignItems: 'center', |
| 165 | + position: 'relative', |
| 166 | + }, |
| 167 | + sliderTrack: { |
| 168 | + position: 'absolute', |
| 169 | + width: 4, |
| 170 | + height: '100%', |
| 171 | + backgroundColor: '#111', |
| 172 | + borderRadius: 2, |
| 173 | + }, |
| 174 | + tickMark: { |
| 175 | + position: 'absolute', |
| 176 | + width: 12, |
| 177 | + height: 2, |
| 178 | + backgroundColor: '#fff', |
| 179 | + borderRadius: 1, |
| 180 | + left: '50%', |
| 181 | + marginLeft: -6, // Center horizontally |
| 182 | + }, |
| 183 | + sliderThumbHitArea: { |
| 184 | + position: 'absolute', |
| 185 | + top: 0, |
| 186 | + width: 40, |
| 187 | + height: THUMB_SIZE, |
| 188 | + justifyContent: 'center', |
| 189 | + alignItems: 'center', |
| 190 | + }, |
| 191 | + sliderThumb: { |
| 192 | + width: 30, |
| 193 | + height: 15, |
| 194 | + backgroundColor: '#222', |
| 195 | + borderWidth: 1, |
| 196 | + borderColor: '#fff', |
| 197 | + borderRadius: 2, |
| 198 | + shadowColor: '#000', |
| 199 | + shadowOffset: { width: 0, height: 2 }, |
| 200 | + shadowOpacity: 0.5, |
| 201 | + shadowRadius: 2, |
| 202 | + }, |
| 203 | + sliderValue: { |
| 204 | + fontSize: 10, |
| 205 | + fontVariant: ['tabular-nums'], |
| 206 | + }, |
| 207 | +}); |
| 208 | + |
| 209 | +export default VerticalSlider; |
0 commit comments