Skip to main content

Slider

npm version npm downloads GitHub source code Contributions welcome npm unpacked size language npm last update npm license

A flexible, performance-optimized, and highly customizable slider component for React Native. Built with React Native's Animated API and integrated with the FlexNative theme system, it provides a smooth and tactile user experience across Android, iOS, and the Web.

The @flexnative/slider package provides a high-quality UI component for selecting a single value or a range of values from a range. It aims to solve common performance issues found in standard React Native sliders while providing deep integration with custom design systems through the FlexNative theme context.

Features

  • Dual Mode Support: Includes both Slider (single thumb) and RangeSlider (dual thumb).
  • Haptic Feedback: Built-in integration with expo-haptics to provide tactile feedback during value changes.
  • Theming: Automatically inherits colors and spacing from the @flexnative/theme-context.
  • Customizable Steps: Support for discrete steps with optional visible labels.
  • Accessible: Follows standard accessibility guidelines for slider inputs.

Installation

Install the package using npm or yarn:

npm install @flexnative/slider

Ensure you have the required peer dependencies installed in your project:

npm install react-native-svg expo-haptics

Basic Usage

Single Slider

The standard Slider component is used for selecting a single numeric value.

import React, { useState } from 'react';
import { Slider } from '@flexnative/slider';

const App = () => {
const [value, setValue] = useState(50);

return (
<Slider
value={value}
onValueChange={setValue}
minimumValue={0}
maximumValue={100}
/>
);
};

Slider Props

PropTypeDefaultDescription
valuenumber0The current value of the slider.
minimumValuenumber0Initial minimum value of the slider.
maximumValuenumber1Initial maximum value of the slider.
stepnumber0Step value of the slider. The value should be between 0 and (maximumValue - minimumValue).
snapPointsnumber[]undefinedSpecific values to snap to. If provided, the slider will only stop at these values.
onValueChange(value: number) => voidundefinedCallback continuously called while the user is dragging the slider.
onSlidingStart(value: number) => voidundefinedCallback called when the user starts changing the value.
onSlidingComplete(value: number) => voidundefinedCallback called when the user finishes changing the value.
disabledbooleanfalseIf true, the user won't be able to interact with the slider.
hideStepsbooleanfalseIf true, visual step indicators on the track are hidden.
minimumTrackTintColorstringundefinedThe color used for the track to the left of the button.
maximumTrackTintColorstringundefinedThe color used for the track to the right of the button.
thumbTintColorstringundefinedThe color used for the thumb.
thumbTouchSize{ width: number; height: number }undefinedThe size of the touch area that allows moving the thumb.
thumbImageImageSourcePropTypeundefinedSets an image for the thumb.
styleStyleProp<ViewStyle>undefinedThe style applied to the slider container.
trackStyleStyleProp<ViewStyle>undefinedThe style applied to the track.
thumbStyleStyleProp<ViewStyle>undefinedThe style applied to the thumb.
animateTransitionsbooleanfalseSet to true if you want to use the default 'spring' animation.
animationType"spring" | "timing""spring"Used to configure the animation type.
animationConfigobjectundefinedUsed to configure the animation parameters.
tooltipBackgroundColorstringundefinedThe background color of the tooltip.
tooltipBorderColorstringundefinedThe border color of the tooltip.
tooltipBorderWidthnumberundefinedThe border width of the tooltip.
accessibilityLabelstringundefinedCustomizes the screen reader description.
testIDstringundefinedUsed to locate this view in end-to-end tests.

Interactive Playground

You can experiment with the Slider component in real-time below.

Live Editor
function SliderPlayground() {
  const [val, setVal] = React.useState(30);

  return (
    <Slider
        value={val}
        onValueChange={setVal}
        minimumValue={0}
        maximumValue={100}
        thumbTintColor="#F5A623"
        maximumTrackTintColor="#E0E0E0"
      />
  );
}
Result
Loading...

Range Slider

The RangeSlider component allows users to select a minimum and maximum value simultaneously.

import React, { useState } from 'react';
import { RangeSlider } from "@flexnative/slider";

function SliderPlayground {
const [rangeValue, setRangeValue] = useState<number[]>([40, 60]);

return (
<RangeSlider
value={rangeValue}
onValueChange={setRangeValue}
minimumValue={0}
maximumValue={100}
step={20}
thumbTintColor="#F5A623"
maximumTrackTintColor="#E0E0E0"
minimumTrackTintColor="#4A90E2"
/>
);
};

RangeSlider Props

PropTypeDefaultDescription
minValuenumber0The current minimum value of the range slider.
maxValuenumber1The current maximum value of the range slider.
minRangenumber0Minimum distance allowed between the two thumbs.
minimumValuenumber0Initial minimum value of the slider.
maximumValuenumber1Initial maximum value of the slider.
stepnumber0Step value of the slider. The value should be between 0 and (maximumValue - minimumValue).
snapPointsnumber[]undefinedSpecific values to snap to. If provided, the slider will only stop at these values.
onValueChange(range: { min: number; max: number }) => voidundefinedCallback continuously called while the user is dragging the slider. Returns an object { min, max }.
onSlidingStart(range: { min: number; max: number }) => voidundefinedCallback called when the user starts changing the value. Returns an object { min, max }.
onSlidingComplete(range: { min: number; max: number }) => voidundefinedCallback called when the user finishes changing the value. Returns an object { min, max }.
disabledbooleanfalseIf true, the user won't be able to interact with the slider.
hideStepsbooleanfalseIf true, visual step indicators on the track are hidden.
minimumTrackTintColorstringundefinedThe color used for the track to the left of the minimum thumb and to the right of the maximum thumb.
maximumTrackTintColorstringundefinedThe color used for the track between the two thumbs.
thumbTintColorstringundefinedThe color used for the thumbs.
thumbTouchSize{ width: number; height: number }undefinedThe size of the touch area that allows moving the thumbs.
thumbImageImageSourcePropTypeundefinedSets an image for the thumbs.
styleStyleProp<ViewStyle>undefinedThe style applied to the slider container.
trackStyleStyleProp<ViewStyle>undefinedThe style applied to the track.
thumbStyleStyleProp<ViewStyle>undefinedThe style applied to the thumbs.
animateTransitionsbooleanfalseSet to true if you want to use the default 'spring' animation for value changes.
animationType"spring" | "timing""spring"Used to configure the animation type for value transitions.
animationConfigobjectundefinedUsed to configure the animation parameters (e.g., duration, damping, stiffness).
tooltipBackgroundColorstringundefinedThe background color of the tooltips displaying the current values.
tooltipBorderColorstringundefinedThe border color of the tooltips.
tooltipBorderWidthnumberundefinedThe border width of the tooltips.
accessibilityLabelstringundefinedCustomizes the screen reader description for the range slider.
testIDstringundefinedUsed to locate this view in end-to-end tests.

Interactive Playground

You can experiment with the Slider component in real-time below.

Live Editor
function SliderPlayground() {
  const [rangeValue, setRangeValue] = useState<number[]>([40, 60]);

  return (
    <RangeSlider
      value={rangeValue}
      onValueChange={setRangeValue}
      minimumValue={0}
      maximumValue={100}
      step={20}
      thumbTintColor="#F5A623"
      maximumTrackTintColor="#E0E0E0"
      minimumTrackTintColor="#4A90E2"
    />
  );
}
Result
Loading...

Dependencies

Dev Dependencies