Skip to content

Commit

Permalink
feat: 🔥 Updated Example Project
Browse files Browse the repository at this point in the history
  • Loading branch information
Karthik-B-06 committed Aug 29, 2020
1 parent e6c7522 commit 6a0e016
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
30 changes: 23 additions & 7 deletions example/RNSwitchExample/App.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,37 @@
import React from 'react';
import React, {useState} from 'react';
import {SafeAreaView, StatusBar, StyleSheet, Text, View} from 'react-native';
import RNSwitch from './src/Switch';

const App: () => React$Node = () => {
const App = () => {
const [switchState, setSwitchState] = useState(false);
const handleOnPressSwitch = (value) => {
setSwitchState(value);
};
return (
<>
<StatusBar barStyle="dark-content" />
<SafeAreaView style={styles.container}>
<Text style={styles.header}>React Native Reanimated Switch</Text>
<Text style={styles.header}>Default </Text>
<RNSwitch handleOnPress={() => {}} />
<RNSwitch handleOnPress={handleOnPressSwitch} value={switchState} />
<Text style={styles.header}>Color Customisable </Text>
<View style={styles.switchContainer}>
<RNSwitch handleOnPress={() => {}} activeTrackColor="#FE5F8F" />
<RNSwitch handleOnPress={() => {}} activeTrackColor="#667eea" />
<RNSwitch handleOnPress={() => {}} activeTrackColor="#ed8936" />
<RNSwitch handleOnPress={() => {}} activeTrackColor="#feb2b2" />
<RNSwitch
handleOnPress={handleOnPressSwitch}
activeTrackColor="#FE5F8F"
/>
<RNSwitch
handleOnPress={handleOnPressSwitch}
activeTrackColor="#667eea"
/>
<RNSwitch
handleOnPress={handleOnPressSwitch}
activeTrackColor="#ed8936"
/>
<RNSwitch
handleOnPress={handleOnPressSwitch}
activeTrackColor="#feb2b2"
/>
</View>
</SafeAreaView>
</>
Expand Down
18 changes: 10 additions & 8 deletions example/RNSwitchExample/src/Switch.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ const RNSwitch = ({
activeTrackColor,
inActiveTrackColor,
thumbColor,
value,
}) => {
const [switchTranslate] = useState(new Animated.Value(0));
const [on, setOn] = useState(false);
useEffect(() => {
if (on) {
if (value) {
spring(switchTranslate, {
toValue: 21,
mass: 1,
Expand All @@ -33,19 +33,19 @@ const RNSwitch = ({
restDisplacementThreshold: 0.001,
}).start();
}
}, [on, switchTranslate]);
}, [value, switchTranslate]);
const interpolateBackgroundColor = {
backgroundColor: interpolateColors(switchTranslate, {
inputRange: [0, 22],
outputColorRange: [inActiveTrackColor, activeTrackColor],
}),
};
const handlePressAction = () => {
handleOnPress(!on);
setOn(!on);
};
const memoizedOnSwitchPressCallback = React.useCallback(() => {
handleOnPress(!value);
}, [handleOnPress, value]);

return (
<Pressable onPress={handlePressAction}>
<Pressable onPress={memoizedOnSwitchPressCallback}>
<Animated.View
style={[styles.containerStyle, interpolateBackgroundColor]}>
<Animated.View
Expand Down Expand Up @@ -97,12 +97,14 @@ RNSwitch.propTypes = {
activeTrackColor: PropTypes.string,
inActiveTrackColor: PropTypes.string,
thumbColor: PropTypes.string,
value: PropTypes.bool,
};

RNSwitch.defaultProps = {
activeTrackColor: '#007AFF',
inActiveTrackColor: '#F2F5F7',
thumbColor: '#FFF',
value: false,
};

export default RNSwitch;

0 comments on commit 6a0e016

Please sign in to comment.