Skip to content

Commit 20149e7

Browse files
committed
change animation speed
1 parent 7778f11 commit 20149e7

2 files changed

Lines changed: 52 additions & 43 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@meksiabdou/react-native-switch",
3-
"version": "1.0.7",
3+
"version": "1.0.8",
44
"description": "Customisable switch component for React Native",
55
"main": "lib/commonjs/index",
66
"module": "lib/module/index",

src/components/switch.tsx

Lines changed: 51 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -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

2322
const 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

Comments
 (0)