Skip to content

Commit

Permalink
add onAnimationEnd function
Browse files Browse the repository at this point in the history
  • Loading branch information
meksiabdou committed Sep 23, 2023
1 parent 3fc70ff commit 7778f11
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 23 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ const styles = StyleSheet.create({
| width | number | 100 |
| value | boolean | false |
| onValueChange | function | undefined |
| onAnimationEnd | function | undefined |
| disabled | boolean | false |
| activeText | string | ON |
| inActiveText | string | OFF |
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1210"
version = "1.3">
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
Expand All @@ -22,6 +22,13 @@
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
shouldAutocreateTestPlan = "YES">
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
Expand Down
16 changes: 14 additions & 2 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,15 @@ export default function App() {
3: true,
});

const [theme, setTheme] = React.useState('light');

return (
<View style={styles.container}>
<View
style={[
styles.container,
{ backgroundColor: theme === 'dark' ? '#666' : '#ccc' },
]}
>
<View>
<Switch
value={value['0']}
Expand All @@ -21,6 +28,7 @@ export default function App() {
inActiveText="حرام"
backgroundInActive={'#ff0000'}
circleInActiveColor={'#fff'}
disabled={false}
circleSize={25}
/>
</View>
Expand All @@ -34,7 +42,7 @@ export default function App() {
switchBorderRadius={30}
switchPaddingLeft={2}
switchPaddingRight={2}
circleInActiveColor='#ff5454'
circleInActiveColor="#ff5454"
switchStyle={{ paddingVertical: 4 }}
/>
</View>
Expand Down Expand Up @@ -76,6 +84,10 @@ export default function App() {
/>
</View>
}
onAnimationEnd={(_value) => {
setTheme(!_value ? 'dark' : 'light');
console.log('theme is ', !_value ? 'dark' : 'light');
}}
/>
</View>
</View>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@meksiabdou/react-native-switch",
"version": "1.0.6",
"version": "1.0.7",
"description": "Customisable switch component for React Native",
"main": "lib/commonjs/index",
"module": "lib/module/index",
Expand Down
57 changes: 38 additions & 19 deletions src/components/switch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,22 @@ import {
StyleSheet,
TouchableWithoutFeedback,
I18nManager,
View,
} from 'react-native';
import Reanimated, {
AnimationCallback,
useAnimatedStyle,
useSharedValue,
withSpring,
runOnJS,
} from 'react-native-reanimated';
import type { SwitchProps } from '../types';

const spring = (_value: any, config: any = { damping: 20, stiffness: 120 }) =>
withSpring(_value, config);
const spring = (
_value: any,
config: any = { damping: 20, stiffness: 120 },
callback?: AnimationCallback
) =>
withSpring(_value, config, callback);

const PADDINGHORIZONTAL = 2;

Expand Down Expand Up @@ -45,6 +50,7 @@ const Switch = (IProps: SwitchProps): JSX.Element => {
switchStyle,
circleChildrenActive,
circleChildrenInActive,
onAnimationEnd,
} = IProps;

const { isRTL } = I18nManager;
Expand Down Expand Up @@ -142,7 +148,6 @@ const Switch = (IProps: SwitchProps): JSX.Element => {
(defaultCircleSize +
(defaultPadding.paddingLeft + defaultPadding.paddingRight)));
if (value) {
circleTranslateX.value = spring(size, { damping: 15, stiffness: 120 });
textTranslateXActive.value = spring(0);
textTranslateXInActive.value = spring(factory * defaultWidth);
if (circleActiveColor) {
Expand All @@ -153,8 +158,17 @@ const Switch = (IProps: SwitchProps): JSX.Element => {
}
circleChildrenActiveOpacity.value = spring(1);
circleChildrenInActiveOpacity.value = spring(0);
circleTranslateX.value = spring(
size,
{ damping: 15, stiffness: 120 },
(finished?: boolean) => {
'worklet';
if (finished && onAnimationEnd) {
runOnJS(onAnimationEnd)(true);
}
}
);
} else {
circleTranslateX.value = spring(0, { damping: 15, stiffness: 120 });
textTranslateXActive.value = spring(-(defaultWidth * factory));
textTranslateXInActive.value = spring(0);
if (circleInActiveColor) {
Expand All @@ -165,6 +179,16 @@ const Switch = (IProps: SwitchProps): JSX.Element => {
}
circleChildrenActiveOpacity.value = spring(0);
circleChildrenInActiveOpacity.value = spring(1);
circleTranslateX.value = spring(
0,
{ damping: 15, stiffness: 120 },
(finished?: boolean) => {
'worklet';
if (finished && onAnimationEnd) {
runOnJS(onAnimationEnd)(false);
}
}
);
}
}, [value, defaultWidth, defaultCircleSize, defaultPadding, isRTL]);

Expand All @@ -176,20 +200,14 @@ const Switch = (IProps: SwitchProps): JSX.Element => {
}
}, [disabled]);

const Button = (props: any) => {
if (typeof onValueChange === 'function' && !disabled) {
return (
<TouchableWithoutFeedback
{...props}
onPress={() => onValueChange(!value)}
/>
);
}
return <View {...props} />;
};

return (
<Button>
<TouchableWithoutFeedback
onPress={() => {
if (!disabled) {
onValueChange?.(!value);
}
}}
>
<Reanimated.View
style={[
styles.switch,
Expand Down Expand Up @@ -277,14 +295,15 @@ const Switch = (IProps: SwitchProps): JSX.Element => {
</Reanimated.View>
</Reanimated.View>
</Reanimated.View>
</Button>
</TouchableWithoutFeedback>
);
};

Switch.defaultProps = {
disabled: false,
value: false,
onValueChange: undefined,
onAnimationEnd: undefined,
activeText: 'ON',
inActiveText: 'OFF',
backgroundActive: '#249c00',
Expand Down
1 change: 1 addition & 0 deletions src/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export interface SwitchProps {
disabled?: boolean;
value: boolean;
onValueChange?: (value: boolean) => void;
onAnimationEnd?: (value?: boolean) => void;
width?: number;
activeText?: string;
inActiveText?: string;
Expand Down

0 comments on commit 7778f11

Please sign in to comment.