Slider
A customizable slider component for selecting values from a range with smooth animations.
Slider
The Slider component allows users to select a value from a range by moving a thumb along a track. It features smooth animations, customizable appearance, and support for both continuous and discrete values.
Features
- Value Range: Supports minimum and maximum value settings
- Smooth Animation: Fluid animation for thumb movement
- Step Configuration: Supports continuous or stepped value selection
- Customizable Appearance: Control for colors, sizes, and track appearance
- Value Labels: Optional display of current value
- Dark Mode Support: Automatic light/dark mode styles
Import
Props
Prop | Type | Default | Description |
---|---|---|---|
value | number | — | Current value of the slider |
onChange | (value: number) => void | — | Function called when the value changes |
onSlidingComplete | (value: number) => void | — | Function called when sliding is completed |
minimumValue | number | 0 | Minimum value of the slider |
maximumValue | number | 1 | Maximum value of the slider |
step | number | 0 | Step value (0 for continuous) |
size | 's' | 'm' | 'l' | 'm' | Size of the slider |
showLabel | boolean | false | Whether to show the current value label |
formatLabel | (value: number) => string | — | Function to format the label text |
className | string | '' | Additional Tailwind classes for the container |
Usage Examples
Basic Slider
Slider with Steps
Slider with Value Label
Different Sized Sliders
Media Player Example
Filter Range Example
Best Practices
Appropriate Value Ranges
Choose appropriate minimum and maximum values for your use case:
Provide Visual Feedback
Show users the current value or provide context for what the slider controls:
Debounce for Performance
For operations that might be expensive, debounce the onChange event and use onSlidingComplete for the final value:
Implementation Details
The Slider component uses React Native's PanGestureHandler
from the react-native-gesture-handler
library and Animated
from react-native-reanimated
to create smooth and responsive interactions.
The component supports both continuous values and stepped values through the step
prop. When a step value is provided (greater than 0), the slider will snap to the nearest step value as the user slides.
The component also supports different sizes through the size
prop, which controls the height of the track and the size of the thumb.
Notes
- For continuous sliders, set
step
to 0 - The thumb responds to both drag gestures and taps on the track
- When
showLabel
is true, the current value is displayed above the thumb - The
formatLabel
function can be used to customize the display of the current value (e.g., adding a unit or formatting as currency) - The component uses
react-native-reanimated
for performant animations