Skip to content

Commit 6131e77

Browse files
committed
fixed animation
1 parent 20149e7 commit 6131e77

4 files changed

Lines changed: 48 additions & 55 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,4 @@ android/keystores/debug.keystore
6666
# generated by bob
6767
lib/
6868
yarn.lock
69+
.yarn

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@meksiabdou/react-native-switch",
3-
"version": "1.0.8",
4-
"description": "Customisable switch component for React Native",
3+
"version": "1.1.0",
4+
"description": "Customizable switch component for React Native",
55
"main": "lib/commonjs/index",
66
"module": "lib/module/index",
77
"types": "lib/typescript/index.d.ts",
@@ -74,7 +74,7 @@
7474
"peerDependencies": {
7575
"react": "*",
7676
"react-native": "*",
77-
"react-native-reanimated" : "*"
77+
"react-native-reanimated": "*"
7878
},
7979
"jest": {
8080
"preset": "react-native",
@@ -150,5 +150,5 @@
150150
]
151151
]
152152
},
153-
"dependencies": {}
153+
"packageManager": "yarn@4.5.3+sha512.3003a14012e2987072d244c720506549c1aab73ee728208f1b2580a9fd67b92d61ba6b08fe93f6dce68fd771e3af1e59a0afa28dd242dd0940d73b95fedd4e90"
154154
}

src/components/switch.tsx

Lines changed: 41 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -10,46 +10,53 @@ import Reanimated, {
1010
useSharedValue,
1111
withSpring,
1212
runOnJS,
13+
withTiming,
1314
} from 'react-native-reanimated';
1415
import type { SwitchProps } from '../types';
1516

1617
const spring = (
1718
_value: any,
18-
config: any = { damping: 20, stiffness: 120 },
19+
config: any = { damping: 15, stiffness: 120 },
1920
callback?: AnimationCallback
2021
) => withSpring(_value, config, callback);
2122

23+
const timing = (
24+
_value: any,
25+
config: any = { duration: 350 },
26+
callback?: AnimationCallback
27+
) => withTiming(_value, config, callback);
28+
2229
const PADDINGHORIZONTAL = 2;
2330

24-
const isNumbre = (value: any, defaultValue = 0) => {
31+
const isNumber = (value: any, defaultValue = 0) => {
2532
value = Number(value);
2633
if (typeof value === 'number' && !isNaN(value) && value !== null) {
2734
return value;
2835
}
2936
return defaultValue;
3037
};
3138

32-
const Switch = (IProps: SwitchProps): JSX.Element => {
39+
const Switch = (IProps: SwitchProps): React.JSX.Element => {
3340
const {
34-
value,
35-
activeText,
36-
inActiveText,
37-
backgroundActive,
38-
backgroundInActive,
39-
circleActiveColor,
40-
circleInActiveColor,
41-
circleSize,
42-
width,
43-
onValueChange,
44-
switchBorderRadius,
41+
value = false,
42+
activeText = 'ON',
43+
inActiveText = 'OFF',
44+
backgroundActive = '#249c00',
45+
backgroundInActive = '#333',
46+
circleActiveColor = '#fff',
47+
circleInActiveColor = '#fff',
48+
circleSize = 30,
49+
width = 100,
50+
onValueChange = undefined,
51+
switchBorderRadius = 30,
4552
textStyle,
46-
disabled,
47-
switchPaddingRight,
48-
switchPaddingLeft,
53+
disabled = false,
54+
switchPaddingRight = PADDINGHORIZONTAL,
55+
switchPaddingLeft = PADDINGHORIZONTAL,
4956
switchStyle,
5057
circleChildrenActive,
5158
circleChildrenInActive,
52-
onAnimationEnd,
59+
onAnimationEnd = undefined,
5360
} = IProps;
5461

5562
const { isRTL } = I18nManager;
@@ -62,17 +69,17 @@ const Switch = (IProps: SwitchProps): JSX.Element => {
6269
const circleColor = useSharedValue<string | undefined>(circleInActiveColor);
6370

6471
const [defaultWidth, setDefaultWidth] = useState<number>(
65-
isNumbre(width, 100)
72+
isNumber(width, 100)
6673
);
6774
const [defaultCircleSize, setDefaultCircleSize] = useState<number>(
68-
isNumbre(circleSize, 30)
75+
isNumber(circleSize, 30)
6976
);
7077
const [defaultPadding, setDefaultPadding] = useState<{
7178
paddingLeft: number;
7279
paddingRight: number;
7380
}>({
74-
paddingLeft: isNumbre(switchPaddingLeft, PADDINGHORIZONTAL),
75-
paddingRight: isNumbre(switchPaddingRight, PADDINGHORIZONTAL),
81+
paddingLeft: isNumber(switchPaddingLeft, PADDINGHORIZONTAL),
82+
paddingRight: isNumber(switchPaddingRight, PADDINGHORIZONTAL),
7683
});
7784

7885
const circleStyle = useAnimatedStyle(() => {
@@ -127,8 +134,8 @@ const Switch = (IProps: SwitchProps): JSX.Element => {
127134
const translateXSpringConfig = { damping: 15, stiffness: 300 };
128135

129136
const onActive = (size: number, factory: number) => {
130-
textTranslateXActive.value = spring(0);
131-
textTranslateXInActive.value = spring(factory * defaultWidth);
137+
textTranslateXActive.value = timing(0);
138+
textTranslateXInActive.value = timing(factory * defaultWidth);
132139
if (circleActiveColor) {
133140
circleColor.value = spring(circleActiveColor, {
134141
damping: 20,
@@ -150,8 +157,8 @@ const Switch = (IProps: SwitchProps): JSX.Element => {
150157
};
151158

152159
const onInActive = (_: number, factory: number) => {
153-
textTranslateXActive.value = spring(-(defaultWidth * factory));
154-
textTranslateXInActive.value = spring(0);
160+
textTranslateXActive.value = timing(-(defaultWidth * factory));
161+
textTranslateXInActive.value = timing(0);
155162
if (circleInActiveColor) {
156163
circleColor.value = spring(circleInActiveColor, {
157164
damping: 20,
@@ -173,18 +180,18 @@ const Switch = (IProps: SwitchProps): JSX.Element => {
173180
};
174181

175182
useEffect(() => {
176-
setDefaultWidth(isNumbre(width, 100));
183+
setDefaultWidth(isNumber(width, 100));
177184
}, [width]);
178185

179186
useEffect(() => {
180187
setDefaultPadding({
181-
paddingLeft: isNumbre(switchPaddingLeft, PADDINGHORIZONTAL),
182-
paddingRight: isNumbre(switchPaddingRight, PADDINGHORIZONTAL),
188+
paddingLeft: isNumber(switchPaddingLeft, PADDINGHORIZONTAL),
189+
paddingRight: isNumber(switchPaddingRight, PADDINGHORIZONTAL),
183190
});
184191
}, [switchPaddingLeft, switchPaddingRight]);
185192

186193
useEffect(() => {
187-
setDefaultCircleSize(isNumbre(circleSize, 30));
194+
setDefaultCircleSize(isNumber(circleSize, 30));
188195
}, [circleSize]);
189196

190197
useEffect(() => {
@@ -199,6 +206,7 @@ const Switch = (IProps: SwitchProps): JSX.Element => {
199206
} else {
200207
onInActive(size, factory);
201208
}
209+
// eslint-disable-next-line react-hooks/exhaustive-deps
202210
}, [value, defaultWidth, defaultCircleSize, defaultPadding, isRTL]);
203211

204212
useEffect(() => {
@@ -207,6 +215,7 @@ const Switch = (IProps: SwitchProps): JSX.Element => {
207215
} else {
208216
opacity.value = spring(1);
209217
}
218+
// eslint-disable-next-line react-hooks/exhaustive-deps
210219
}, [disabled]);
211220

212221
return (
@@ -221,7 +230,7 @@ const Switch = (IProps: SwitchProps): JSX.Element => {
221230
style={[
222231
styles.switch,
223232
{
224-
borderRadius: isNumbre(switchBorderRadius, 30),
233+
borderRadius: isNumber(switchBorderRadius, 30),
225234
width: defaultWidth,
226235
},
227236
switchStyle,
@@ -279,7 +288,7 @@ const Switch = (IProps: SwitchProps): JSX.Element => {
279288
{
280289
width: defaultCircleSize,
281290
height: defaultCircleSize,
282-
borderRadius: isNumbre(switchBorderRadius, 30),
291+
borderRadius: isNumber(switchBorderRadius, 30),
283292
},
284293
circleStyle,
285294
]}
@@ -308,23 +317,6 @@ const Switch = (IProps: SwitchProps): JSX.Element => {
308317
);
309318
};
310319

311-
Switch.defaultProps = {
312-
disabled: false,
313-
value: false,
314-
onValueChange: undefined,
315-
onAnimationEnd: undefined,
316-
activeText: 'ON',
317-
inActiveText: 'OFF',
318-
backgroundActive: '#249c00',
319-
backgroundInActive: '#333',
320-
circleActiveColor: '#fff',
321-
circleInActiveColor: '#fff',
322-
circleSize: 30,
323-
switchBorderRadius: 30,
324-
width: 100,
325-
switchPaddingRight: PADDINGHORIZONTAL,
326-
switchPaddingLeft: PADDINGHORIZONTAL,
327-
};
328320

329321
const styles = StyleSheet.create({
330322
switch: {

src/types/index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ export interface SwitchProps {
77
onValueChange?: (value: boolean) => void;
88
onAnimationEnd?: (value?: boolean) => void;
99
width?: number;
10-
activeText?: string;
11-
inActiveText?: string;
10+
activeText?: string | null;
11+
inActiveText?: string | null;
1212
circleSize?: number;
1313
switchBorderRadius?: number;
1414
backgroundActive?: string;

0 commit comments

Comments
 (0)