@@ -17,8 +17,7 @@ const spring = (
1717 _value : any ,
1818 config : any = { damping : 20 , stiffness : 120 } ,
1919 callback ?: AnimationCallback
20- ) =>
21- withSpring ( _value , config , callback ) ;
20+ ) => withSpring ( _value , config , callback ) ;
2221
2322const PADDINGHORIZONTAL = 2 ;
2423
@@ -125,6 +124,54 @@ const Switch = (IProps: SwitchProps): JSX.Element => {
125124 } ;
126125 } ) ;
127126
127+ const translateXSpringConfig = { damping : 15 , stiffness : 300 } ;
128+
129+ const onActive = ( size : number , factory : number ) => {
130+ textTranslateXActive . value = spring ( 0 ) ;
131+ textTranslateXInActive . value = spring ( factory * defaultWidth ) ;
132+ if ( circleActiveColor ) {
133+ circleColor . value = spring ( circleActiveColor , {
134+ damping : 20 ,
135+ stiffness : 100 ,
136+ } ) ;
137+ }
138+ circleChildrenActiveOpacity . value = spring ( 1 ) ;
139+ circleChildrenInActiveOpacity . value = spring ( 0 ) ;
140+ circleTranslateX . value = spring (
141+ size ,
142+ translateXSpringConfig ,
143+ ( finished ?: boolean ) => {
144+ 'worklet' ;
145+ if ( finished && onAnimationEnd ) {
146+ runOnJS ( onAnimationEnd ) ( true ) ;
147+ }
148+ }
149+ ) ;
150+ } ;
151+
152+ const onInActive = ( _ : number , factory : number ) => {
153+ textTranslateXActive . value = spring ( - ( defaultWidth * factory ) ) ;
154+ textTranslateXInActive . value = spring ( 0 ) ;
155+ if ( circleInActiveColor ) {
156+ circleColor . value = spring ( circleInActiveColor , {
157+ damping : 20 ,
158+ stiffness : 100 ,
159+ } ) ;
160+ }
161+ circleChildrenActiveOpacity . value = spring ( 0 ) ;
162+ circleChildrenInActiveOpacity . value = spring ( 1 ) ;
163+ circleTranslateX . value = spring (
164+ 0 ,
165+ translateXSpringConfig ,
166+ ( finished ?: boolean ) => {
167+ 'worklet' ;
168+ if ( finished && onAnimationEnd ) {
169+ runOnJS ( onAnimationEnd ) ( false ) ;
170+ }
171+ }
172+ ) ;
173+ } ;
174+
128175 useEffect ( ( ) => {
129176 setDefaultWidth ( isNumbre ( width , 100 ) ) ;
130177 } , [ width ] ) ;
@@ -148,47 +195,9 @@ const Switch = (IProps: SwitchProps): JSX.Element => {
148195 ( defaultCircleSize +
149196 ( defaultPadding . paddingLeft + defaultPadding . paddingRight ) ) ) ;
150197 if ( value ) {
151- textTranslateXActive . value = spring ( 0 ) ;
152- textTranslateXInActive . value = spring ( factory * defaultWidth ) ;
153- if ( circleActiveColor ) {
154- circleColor . value = spring ( circleActiveColor , {
155- damping : 20 ,
156- stiffness : 100 ,
157- } ) ;
158- }
159- circleChildrenActiveOpacity . value = spring ( 1 ) ;
160- circleChildrenInActiveOpacity . value = spring ( 0 ) ;
161- circleTranslateX . value = spring (
162- size ,
163- { damping : 15 , stiffness : 120 } ,
164- ( finished ?: boolean ) => {
165- 'worklet' ;
166- if ( finished && onAnimationEnd ) {
167- runOnJS ( onAnimationEnd ) ( true ) ;
168- }
169- }
170- ) ;
198+ onActive ( size , factory ) ;
171199 } else {
172- textTranslateXActive . value = spring ( - ( defaultWidth * factory ) ) ;
173- textTranslateXInActive . value = spring ( 0 ) ;
174- if ( circleInActiveColor ) {
175- circleColor . value = spring ( circleInActiveColor , {
176- damping : 20 ,
177- stiffness : 100 ,
178- } ) ;
179- }
180- circleChildrenActiveOpacity . value = spring ( 0 ) ;
181- circleChildrenInActiveOpacity . value = spring ( 1 ) ;
182- circleTranslateX . value = spring (
183- 0 ,
184- { damping : 15 , stiffness : 120 } ,
185- ( finished ?: boolean ) => {
186- 'worklet' ;
187- if ( finished && onAnimationEnd ) {
188- runOnJS ( onAnimationEnd ) ( false ) ;
189- }
190- }
191- ) ;
200+ onInActive ( size , factory ) ;
192201 }
193202 } , [ value , defaultWidth , defaultCircleSize , defaultPadding , isRTL ] ) ;
194203
0 commit comments