-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* new Switch look * refactor: merge SwitchSetting to SwitchItem * fix: remove margin
- Loading branch information
Showing
12 changed files
with
213 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,124 @@ | ||
import React from 'react'; | ||
import { Pressable, StyleSheet, View, Text } from 'react-native'; | ||
import { Switch, List } from 'react-native-paper'; | ||
import { | ||
StyleProp, | ||
StyleSheet, | ||
TouchableWithoutFeedback, | ||
ViewStyle, | ||
} from 'react-native'; | ||
import React, { useEffect } from 'react'; | ||
import Animated, { | ||
interpolateColor, | ||
useSharedValue, | ||
useAnimatedStyle, | ||
withSpring, | ||
withTiming, | ||
useDerivedValue, | ||
} from 'react-native-reanimated'; | ||
import { ThemeColors } from '@theme/types'; | ||
|
||
interface SwitchProps { | ||
label: string; | ||
description?: string; | ||
onPress: () => void; | ||
theme: ThemeColors; | ||
icon?: string; | ||
value: boolean; | ||
onValueChange: () => void; | ||
size?: number; | ||
style?: StyleProp<ViewStyle>; | ||
} | ||
|
||
const SwitchSetting: React.FC<SwitchProps> = ({ | ||
label, | ||
description, | ||
onPress, | ||
const Switch = ({ | ||
theme, | ||
icon, | ||
value, | ||
}) => { | ||
const styles = StyleSheet.create({ | ||
container: { | ||
paddingHorizontal: 16, | ||
flexDirection: 'row', | ||
justifyContent: 'space-between', | ||
paddingVertical: description ? 16 : 12, | ||
}, | ||
row: { | ||
flexDirection: 'row', | ||
flex: 1, | ||
}, | ||
icon: { | ||
margin: 0, | ||
}, | ||
textContainer: { | ||
marginLeft: icon && 16, | ||
}, | ||
text: { | ||
fontSize: 16, | ||
flex: 1, | ||
textAlignVertical: 'center', | ||
}, | ||
switch: { marginLeft: 8 }, | ||
size = 22, | ||
onValueChange, | ||
style, | ||
}: SwitchProps) => { | ||
// value for Switch Animation | ||
const switchTranslate = useSharedValue(value ? size : size / 6); | ||
// state for activate Switch | ||
// Progress Value | ||
const progress = useDerivedValue(() => { | ||
return withTiming(value ? size : 0); | ||
}); | ||
|
||
// useEffect for change the switchTranslate Value | ||
useEffect(() => { | ||
if (value) { | ||
switchTranslate.value = size; | ||
} else { | ||
switchTranslate.value = size / 6; | ||
} | ||
}, [value, switchTranslate, size]); | ||
|
||
// Circle Animation | ||
const customSpringStyles = useAnimatedStyle(() => { | ||
return { | ||
transform: [ | ||
{ | ||
translateX: withSpring(switchTranslate.value, { | ||
mass: 1, | ||
damping: 15, | ||
stiffness: 120, | ||
overshootClamping: false, | ||
restSpeedThreshold: 0.001, | ||
restDisplacementThreshold: 0.001, | ||
}), | ||
}, | ||
], | ||
}; | ||
}); | ||
|
||
// Background Color Animation | ||
const backgroundColorStyle = useAnimatedStyle(() => { | ||
const backgroundColor = interpolateColor( | ||
progress.value, | ||
[0, size], | ||
[theme.outline, theme.primary], | ||
); | ||
return { | ||
backgroundColor, | ||
}; | ||
}); | ||
|
||
return ( | ||
<Pressable | ||
android_ripple={{ | ||
color: theme.rippleColor, | ||
}} | ||
style={styles.container} | ||
onPress={onPress} | ||
> | ||
<View style={styles.row}> | ||
{icon && ( | ||
<List.Icon color={theme.primary} icon={icon} style={styles.icon} /> | ||
)} | ||
<View style={styles.textContainer}> | ||
<Text style={[styles.text, { color: theme.onSurface }]}>{label}</Text> | ||
{description && ( | ||
<Text style={{ color: theme.onSurfaceVariant }}>{description}</Text> | ||
)} | ||
</View> | ||
</View> | ||
<Switch | ||
value={value} | ||
onValueChange={onPress} | ||
color={theme.primary} | ||
style={styles.switch} | ||
/> | ||
</Pressable> | ||
<TouchableWithoutFeedback onPress={onValueChange}> | ||
<Animated.View | ||
style={[ | ||
styles.container, | ||
style, | ||
{ | ||
width: size * 2 + size / 6, | ||
height: size + size / 3, | ||
borderRadius: size, | ||
}, | ||
backgroundColorStyle, | ||
]} | ||
> | ||
<Animated.View | ||
style={[ | ||
styles.circle, | ||
customSpringStyles, | ||
{ height: size, width: size, borderRadius: size }, | ||
]} | ||
/> | ||
</Animated.View> | ||
</TouchableWithoutFeedback> | ||
); | ||
}; | ||
|
||
export default SwitchSetting; | ||
export default Switch; | ||
|
||
const styles = StyleSheet.create({ | ||
container: { | ||
justifyContent: 'center', | ||
backgroundColor: '#F2F5F7', | ||
}, | ||
circle: { | ||
backgroundColor: 'white', | ||
shadowColor: 'black', | ||
shadowOffset: { | ||
width: 0, | ||
height: 2, | ||
}, | ||
shadowOpacity: 0.2, | ||
shadowRadius: 2.5, | ||
elevation: 4, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.