Skip to content

Commit

Permalink
fix: switch button
Browse files Browse the repository at this point in the history
  • Loading branch information
YoanRos committed Aug 23, 2024
1 parent aa71ad9 commit 49b0f16
Showing 1 changed file with 45 additions and 43 deletions.
88 changes: 45 additions & 43 deletions expo/src/components/SwitchButtons.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React, { useCallback, useRef } from "react";
import { Animated, findNodeHandle, PanResponder, StyleSheet, View } from "react-native";
import { capture } from "../services/sentry";
import { Animated, PanResponder, StyleSheet, View } from "react-native";
import TextStyled from "./TextStyled";

const SwitchButtons = ({ leftContent, rightContent, handleSwitchChange, initPosition = 1 }) => {
Expand All @@ -27,40 +26,42 @@ const SwitchButtons = ({ leftContent, rightContent, handleSwitchChange, initPosi
[]
);
const onTerminate = async (evt) => {
const newX = evt.nativeEvent.locationX;
const width = await new Promise((res) =>
insideContainerRef.current.measureLayout(
findNodeHandle(containerRef.current),
(x, y, width) => {
try {
const newX = evt.nativeEvent.locationX;

// Measure the width of the inside container
const width = await new Promise((res) =>
insideContainerRef.current.measure((x, y, width, height, pageX, pageY) => {
res(width);
},
(error) => capture("error finding ref", { extra: { error } })
)
);
const newPosition = Number(newX > width / 2);
const buttonWidth = await new Promise((res) =>
buttonRef.current.measureLayout(
findNodeHandle(insideContainerRef.current),
(x, y, width) => {
})
);

const newPosition = Number(newX > width / 2);

// Measure the width of the button
const buttonWidth = await new Promise((res) =>
buttonRef.current.measure((x, y, width, height, pageX, pageY) => {
res(width);
},
(error) => capture("error finding ref", { extra: { error } })
)
);
})
);

Animated.parallel([
Animated.timing(translateX, {
toValue: newPosition * (buttonWidth - 4),
duration: 250,
useNativeDriver: true,
}),
Animated.timing(position, {
toValue: newPosition,
duration: 250,
useNativeDriver: true,
}),
]).start();

Animated.parallel([
Animated.timing(translateX, {
toValue: newPosition * (buttonWidth - 4),
duration: 250,
useNativeDriver: true,
}),
Animated.timing(position, {
toValue: newPosition,
duration: 250,
useNativeDriver: true,
}),
]).start();
handleSwitchChange(newPosition === 0 ? leftContent : rightContent);
handleSwitchChange(newPosition === 0 ? leftContent : rightContent);
} catch (error) {
console.error("Error during onTerminate:", error);
}
};

const panResponder = useRef(
Expand All @@ -85,13 +86,10 @@ const SwitchButtons = ({ leftContent, rightContent, handleSwitchChange, initPosi
ref={containerRef}
style={[styles.container]}
{...(panResponder?.panHandlers || {})}
pointerEvents="box-only">
pointerEvents="box-only"
>
<View ref={insideContainerRef} style={styles.content}>
<Animated.View
ref={buttonRef}
onLayout={onLayout}
style={[styles.button, { transform: [{ translateX }] }]}
/>
<Animated.View ref={buttonRef} onLayout={onLayout} style={[styles.button, { transform: [{ translateX }] }]} />
</View>
<View style={styles.textsContainer}>
<View style={styles.textContainer}>
Expand All @@ -105,7 +103,8 @@ const SwitchButtons = ({ leftContent, rightContent, handleSwitchChange, initPosi
extrapolate: "clamp",
}),
},
]}>
]}
>
<TextStyled bold color={"#000"}>
{leftContent}
</TextStyled>
Expand All @@ -120,7 +119,8 @@ const SwitchButtons = ({ leftContent, rightContent, handleSwitchChange, initPosi
extrapolate: "clamp",
}),
},
]}>
]}
>
<TextStyled bold color={"#fff"}>
{leftContent}
</TextStyled>
Expand All @@ -137,7 +137,8 @@ const SwitchButtons = ({ leftContent, rightContent, handleSwitchChange, initPosi
extrapolate: "clamp",
}),
},
]}>
]}
>
<TextStyled bold color={"#000"}>
{rightContent}
</TextStyled>
Expand All @@ -152,7 +153,8 @@ const SwitchButtons = ({ leftContent, rightContent, handleSwitchChange, initPosi
extrapolate: "clamp",
}),
},
]}>
]}
>
<TextStyled bold color={"#fff"}>
{rightContent}
</TextStyled>
Expand Down

0 comments on commit 49b0f16

Please sign in to comment.