Slider allows users to adjust a value or control a setting by sliding a handle along a track. It is commonly used to adjust settings such as volume, brightness, or in this case, the width of a box.
Building intuitive UI components like a volume slider can greatly enhance user experience. Here’s a quick guide on how to create a volume slider in React Native with haptic feedback using react-native-awesome-slider, react-native-reanimated, and expo-haptics.
To create the slider, we’ll use the Slider component from react-native-awesome-slider and manage its progress using shared values from react-native-reanimated.
1. Shared Values for Slider Control:useSharedValue from react-native-reanimated allows us to set reactive values for the slider’s progress (progress), minimum value (min), and maximum value (max).
2. Gesture Handling:
The GestureHandlerRootView from react-native-gesture-handler wraps the entire app to ensure gesture-based components like sliders work seamlessly.
** 3. Slider Styling:**
The theme prop lets us customize the slider’s colors, including:
minimumTrackTintColor: Color for the progress track.
maximumTrackTintColor: Color for the unprogressed track.
bubbleBackgroundColor: Background color of the value bubble (if visible)
4. Adding Haptic Feedback:
Using expo-haptics, we trigger a light vibration when the slider moves:
Next, as you can see, we have added a micro-interaction to our volume bar. Whenever the slider is pressed, it scales from 1 to 1.2, providing a smoother, interactive feel. To achieve this, I used react-native-reanimated withSpring animation within onPressIn and onPressOut handlers. This ensures the slider scales up when pressed and returns to its original size upon release, creating an engaging user experience.
Another subtle detail is when the volume bar pointer reaches the far left, the mute icon enlarges, and when it reaches the far right, the volume-high icon enlarges. To achieve this, we first track the progress value. A simple approach is to check if the value is 0 (far left) or 1 (far right), and adjust the icon scale accordingly. Here’s the code that handles this functionality:
This ensures smooth transitions for both icons, adding a polished touch to the volume bar’s micro-interactions