Skip to content

Commit 94bee23

Browse files
committed
refactor(switch): ♻️ add memoised variables & useAnimatedReaction for controlled animation
1 parent 22dc93c commit 94bee23

File tree

1 file changed

+30
-8
lines changed

1 file changed

+30
-8
lines changed

src/components/switch/Switch.tsx

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { forwardRef } from "react";
1+
import React, { forwardRef, useMemo } from "react";
22
import {
33
Gesture,
44
GestureDetector,
@@ -8,6 +8,7 @@ import {
88
interpolate,
99
interpolateColor,
1010
runOnJS,
11+
useAnimatedReaction,
1112
useAnimatedStyle,
1213
useSharedValue,
1314
withSpring,
@@ -21,6 +22,7 @@ import { createComponent } from "../../utils/createComponent";
2122

2223
export type SwitchSize = "sm" | "md" | "lg" | "xl";
2324
export type SwitchTheme = "base" | "primary";
25+
2426
export interface SwitchProps extends BoxProps {
2527
/**
2628
* Default Value of the switch
@@ -126,6 +128,7 @@ const RNSwitch: React.FC<Partial<SwitchProps>> = forwardRef<
126128
onChange: onStateChange,
127129
});
128130

131+
const thumbAnimated = useSharedValue(switchState ? 1 : 0);
129132
/**
130133
* Setting Active/Inactive and Default Colors
131134
*/
@@ -166,15 +169,34 @@ const RNSwitch: React.FC<Partial<SwitchProps>> = forwardRef<
166169
/**
167170
* The Switch Animation Helpers
168171
*/
169-
const interpolatedWidths = switchTheme.size[size]?.switchInterpolateWidths;
172+
const interpolatedWidths = useMemo(() => {
173+
return switchTheme.size[size]?.switchInterpolateWidths;
174+
// eslint-disable-next-line react-hooks/exhaustive-deps
175+
}, [size]);
170176

171-
const translatedThumbDistance = switchTheme.size[size]?.thumbTranslateValue;
172-
const initTranslatedThumbDistance =
173-
switchTheme.size[size]?.thumbInitTranslateValue;
174-
const intermediateThumbTranslateValue =
175-
switchTheme.size[size]?.thumbIntermediateTranslateValue;
177+
const translatedThumbDistance = useMemo(() => {
178+
return switchTheme.size[size]?.thumbTranslateValue;
179+
// eslint-disable-next-line react-hooks/exhaustive-deps
180+
}, [size]);
181+
const initTranslatedThumbDistance = useMemo(() => {
182+
return switchTheme.size[size]?.thumbInitTranslateValue;
183+
// eslint-disable-next-line react-hooks/exhaustive-deps
184+
}, [size]);
185+
const intermediateThumbTranslateValue = useMemo(() => {
186+
return switchTheme.size[size]?.thumbIntermediateTranslateValue;
187+
// eslint-disable-next-line react-hooks/exhaustive-deps
188+
}, [size]);
176189

177-
const thumbAnimated = useSharedValue(switchState ? 1 : 0);
190+
useAnimatedReaction(
191+
() => switchState,
192+
() => {
193+
if (switchState) {
194+
thumbAnimated.value = withSpring(1, SPRING_CONFIG);
195+
} else {
196+
thumbAnimated.value = withSpring(0, SPRING_CONFIG);
197+
}
198+
},
199+
);
178200

179201
const animatedSwitchBackground = useAnimatedStyle(() => {
180202
return {

0 commit comments

Comments
 (0)