1- import { useState } from 'react' ;
1+ import { useEffect , useState } from 'react' ;
22import { Gesture } from 'react-native-gesture-handler' ;
33import { interpolate , runOnJS , useAnimatedProps , useAnimatedStyle , useSharedValue , withSpring , withTiming } from 'react-native-reanimated' ;
44
5- import { type SliderConfig } from './slider-config.type' ;
5+ import { type SliderConfig } from '../slider-config.type' ;
6+ import { normalize } from '../worklets/normalize.worklet' ;
7+ import { calcTranslationXBasedOnValue } from '../worklets/calc-translationX-based-on-value.worklet' ;
68
7- export const useSlider = ( { max, min, step} : SliderConfig , value : number , onChangeValue ?: ( value : number ) => void ) => {
9+ export const useSlider = ( { max, min, step, damping } : SliderConfig , value : number , onChangeValue ?: ( value : number ) => void ) => {
810 const [ sliderWidth , setSliderWith ] = useState ( 0 ) ;
911
1012 const sliding = useSharedValue ( 0 ) ;
@@ -15,30 +17,35 @@ export const useSlider = ({max, min, step}: SliderConfig, value: number, onChang
1517 const filledTrackAnimatedStyle = useAnimatedStyle ( ( ) => ( { flex : interpolate ( thumbTranslationX . value , [ 0 , sliderWidth ] , [ 0 , 1 ] ) } ) , [ sliderWidth ] ) ;
1618 const remainingTrackAnimatedStyle = useAnimatedStyle ( ( ) => ( { flex : interpolate ( thumbTranslationX . value , [ 0 , sliderWidth ] , [ 1 , 0 ] ) } ) , [ sliderWidth ] ) ;
1719
20+ useEffect ( ( ) => {
21+ adjustThumbsPosition ( sliderWidth , true ) ;
22+ } , [ value , sliderWidth ] ) ;
23+
1824 const calcValueBasedOnThumbTranslationX = ( translationX : number ) => {
1925 'worklet' ;
2026
2127 const valueBaseOnThumbTranslateX = Math . round ( interpolate ( translationX , [ 0 , sliderWidth ] , [ min , max ] ) ) ;
22- const roundedValue = step ? Math . round ( valueBaseOnThumbTranslateX / step ) * step : valueBaseOnThumbTranslateX ;
28+ const normalizedValue = normalize ( valueBaseOnThumbTranslateX , step ) ;
2329
24- selectedValue . value = roundedValue ;
30+ selectedValue . value = normalizedValue ;
2531
26- return roundedValue ;
32+ return normalizedValue ;
2733 } ;
2834
2935 const gesture = Gesture . Pan ( )
3036 . onStart ( ( ) => {
3137 sliding . value = withTiming ( 1 ) ;
3238 } )
3339 . onUpdate ( ( e ) => {
40+ const currentTranslationX = Math . max ( 0 , Math . min ( Math . round ( thumbTranslationXContext . value + e . translationX ) , sliderWidth ) ) ;
41+
3442 if ( step ) {
3543 const stepSize = ( sliderWidth / ( max - min ) ) * step ;
36- const currentTranslationX = Math . max ( 0 , Math . min ( Math . round ( thumbTranslationXContext . value + e . translationX ) , sliderWidth ) ) ;
37- const stepSnap = Math . round ( currentTranslationX / stepSize ) * stepSize ;
44+ const stepSnap = normalize ( currentTranslationX , stepSize ) ;
3845
3946 thumbTranslationX . value = stepSnap ;
4047 } else {
41- thumbTranslationX . value = Math . max ( 0 , Math . min ( Math . round ( thumbTranslationXContext . value + e . translationX ) , sliderWidth ) ) ;
48+ thumbTranslationX . value = currentTranslationX ;
4249 }
4350 } )
4451 . onEnd ( ( ) => {
@@ -52,22 +59,22 @@ export const useSlider = ({max, min, step}: SliderConfig, value: number, onChang
5259 } ) ;
5360
5461 const setUpSliderLayout = ( width : number ) => {
55- initializeThumbPosition ( width ) ;
62+ adjustThumbsPosition ( width ) ;
5663 setSliderWith ( width ) ;
5764 } ;
5865
59- const initializeThumbPosition = ( width : number ) => {
60- const initialTthumbTranslationX = ( ( value - min ) / ( max - min ) ) * width ;
66+ const adjustThumbsPosition = ( width : number , isUpdaing : boolean = false ) => {
67+ const initialTthumbTranslationX = calcTranslationXBasedOnValue ( { min, max } , normalize ( value , step ) , width ) ;
6168
62- thumbTranslationX . value = initialTthumbTranslationX ;
69+ thumbTranslationX . value = isUpdaing ? withSpring ( initialTthumbTranslationX , { damping } ) : initialTthumbTranslationX ;
6370 thumbTranslationXContext . value = initialTthumbTranslationX ;
6471 } ;
6572
6673 const slideToTrackPoint = ( pointValue : number ) => {
67- const thumbPointValueTranslationX = ( ( pointValue - min ) / ( max - min ) ) * sliderWidth ;
74+ const thumbPointValueTranslationX = calcTranslationXBasedOnValue ( { min, max} , pointValue , sliderWidth ) ;
6875
6976 sliding . value = withTiming ( 1 ) ;
70- thumbTranslationX . value = withSpring ( thumbPointValueTranslationX , { damping : 20 } , ( ) => {
77+ thumbTranslationX . value = withSpring ( thumbPointValueTranslationX , { damping} , ( ) => {
7178 sliding . value = withTiming ( 0 ) ;
7279 thumbTranslationXContext . value = thumbPointValueTranslationX ;
7380
0 commit comments