Skip to content

Commit

Permalink
Refactor switch
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhenjaHorbach committed Apr 20, 2024
1 parent 0da9f7f commit 3f893fd
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions src/components/Switch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ function Switch({isOn, onToggle, accessibilityLabel, disabled}: SwitchProps) {
const offsetX = useRef(new Animated.Value(isOn ? OFFSET_X.ON : OFFSET_X.OFF));
const theme = useTheme();

const handleSwitchPress = () => {
InteractionManager.runAfterInteractions(() => {
onToggle(!isOn);
});
};

useEffect(() => {
Animated.timing(offsetX.current, {
toValue: isOn ? OFFSET_X.ON : OFFSET_X.OFF,
Expand All @@ -44,16 +50,8 @@ function Switch({isOn, onToggle, accessibilityLabel, disabled}: SwitchProps) {
<PressableWithFeedback
disabled={disabled}
style={[styles.switchTrack, !isOn && styles.switchInactive]}
onPress={() => {
InteractionManager.runAfterInteractions(() => {
onToggle(!isOn);
});
}}
onLongPress={() => {
InteractionManager.runAfterInteractions(() => {
onToggle(!isOn);
});
}}
onPress={handleSwitchPress}
onLongPress={handleSwitchPress}
role={CONST.ROLE.SWITCH}
aria-checked={isOn}
accessibilityLabel={accessibilityLabel}
Expand Down

0 comments on commit 3f893fd

Please sign in to comment.