diff --git a/src/components/Slider.tsx b/src/components/Slider.tsx index f22337126..6489373dc 100644 --- a/src/components/Slider.tsx +++ b/src/components/Slider.tsx @@ -50,7 +50,29 @@ const Slider = ({ panX.value = stepPositions[valueIndex]; }, [sliderWidth, value, steps, stepPositions, panX]); - const gesture = Gesture.Pan() + const findClosestStep = (currentPosition: number): number => { + 'worklet'; + return stepPositions.reduce((closestStep, currentStep) => { + // Calculate the difference between the current step and the current position + const currentDifference = Math.abs(currentStep - currentPosition); + const closestDifference = Math.abs(closestStep - currentPosition); + + // If the current step is closer, update the closest step + return currentDifference < closestDifference ? currentStep : closestStep; + }); + }; + + const tapGesture = Gesture.Tap().onStart((event) => { + const closestStep = findClosestStep(event.x); + panX.value = closestStep; + + if (onValueChange) { + const stepIndex = stepPositions.indexOf(closestStep); + runOnJS(onValueChange)(steps[stepIndex]); + } + }); + + const panGesture = Gesture.Pan() .onStart(() => { prevPanX.value = panX.value; }) @@ -58,15 +80,6 @@ const Slider = ({ panX.value = clamp(prevPanX.value + event.translationX, 0, sliderWidth); }) .onEnd(() => { - const findClosestStep = (currentPosition: number): number => { - return stepPositions.reduce((prev, curr) => { - return Math.abs(curr - currentPosition) < - Math.abs(prev - currentPosition) - ? curr - : prev; - }); - }; - const closestStep = findClosestStep(panX.value); panX.value = withSpring(closestStep); @@ -90,19 +103,23 @@ const Slider = ({ style={styles.container} onLayout={(e): void => setSliderWidth(e.nativeEvent.layout.width)}> - - - {stepPositions.map((pos, index) => ( - - - - ${steps[index]} - + + + + + {stepPositions.map((pos, index) => ( + + + + ${steps[index]} + + + ))} - ))} - + + @@ -126,6 +143,11 @@ const styles = StyleSheet.create({ alignItems: 'center', justifyContent: 'center', }, + trackHitbox: { + justifyContent: 'center', + height: 30, + width: '100%', + }, track: { borderRadius: 3, height: 8,