Software Studio Pro
If you want to become a creator, contact us for your account creation.
BLOG ON
UPDATED ON - 18th July, 2024
PUBLISHED BY
Frontend Developer
When I was looking for a npm module for a good and simple swappable button that could let me customize the design as per my needs, I couldn't find such module. And If I wanted to do all the animations myself, then most of the places I could only find deprecated versions of Pan Gesture Handler tutorials or the one which gave me more trouble with errors in my code. And on top of that even Chat GPT couldn't help me with this LOL and gave me older version solutions.
Seems simple and well put right? Here, you can see a slider button which is getting snapped depending on where it is released. We can trigger a function as well after the swipe is done to the right.
Let's forget about the card design and focus only on the slider UI design and functionality. We are keeping it simple and straight to the point. I've explained few important things in the code snippet.
import React from "react";
import { Dimensions, StyleSheet, Text, View } from "react-native";
import { theme } from "../../../styles/Styles";
import { Gesture, GestureDetector } from "react-native-gesture-handler";
import Animated, { useAnimatedStyle, useSharedValue, withTiming } from "react-native-reanimated";
import { Entypo } from "@expo/vector-icons";
const SliderButton = () => {
const END_POSITION = Dimensions.get("screen").width - 90; // Calculating button width
const onLeft = useSharedValue(true);
const position = useSharedValue(0);
const panGesture = Gesture.Pan() // Defining gesture type to Pan
.runOnJS(true) // This is required if you want to trigger a function on swipe
.onUpdate((e) => {
if (onLeft.value) {
position.value = e.translationX;
} else {
position.value = END_POSITION + e.translationX;
}
})
.onEnd((e) => {
if (position.value > END_POSITION / 1.5) { // This is the snap point, adjust 1.5 accordingly
position.value = withTiming(END_POSITION, { duration: 100 });
onLeft.value = false;
// onSlideCompleted(); You can call any function here when swipe is completed
} else {
position.value = withTiming(0, { duration: 100 });
onLeft.value = true;
}
});
const animatedStyle = useAnimatedStyle(() => ({
transform: [{ translateX: position.value }],
}));
return (
<View style={styles.sliderContainer}>
<Text style={styles.sliderText}>Swipe To Raise The Alert</Text>
<GestureDetector gesture={panGesture}>
<Animated.View style={[styles.swipeBtn, animatedStyle]}>
<Entypo name="chevron-thin-right" size={24} color={theme.dangerColor} />
</Animated.View>
</GestureDetector>
</View>
);
};
export default SliderButton;
const styles = StyleSheet.create({
sliderContainer: {
justifyContent: "center",
alignItems: "center",
flexDirection: "row",
width: "100%",
backgroundColor: "#E64040",
position: "relative",
height: 50,
overflow: "hidden",
borderRadius: 50,
},
sliderText: {
color: "#fff",
fontSize: 18,
},
swipeBtn: {
width: 40,
height: 40,
backgroundColor: "#fff",
position: "absolute",
left: 5,
justifyContent: "center",
alignItems: "center",
borderRadius: 50,
},
});
This is pretty straight forward and simple. Hope you got this right.
React Native doesn't have any perfect and dedicated UI library and in current scenario most of the times it's better to develop the UI ourself. I faced this issue so I solved it and explained here in a simple way covering all the basic functionalities you need to get it done. Hope this helps you out. You can customize the slider further to fit your own needs. The React Native libraries gets updated consistently so if there is any update, I'll try to update it here and keep you posted.
Micro-frontend Architecture: The Future of Scalable Web Applications
Published on - 20th August, 2024
Key Benefits of Outsourcing Software Development for Startups
Published on - 7th August, 2024
SEO for 2024: Complete Guide to Increase Website Ranking
Updated on - 16th July, 2024
Dynamic Sitemap in Next.js
Updated on - 19th May, 2024