Modus UI

Range Slider

React Native (Expo)

A React Native slider with two thumbs for choosing a range, where the thumbs never cross, values snap to a step, and each thumb is adjustable for screen readers.

A slider with two thumbs for choosing a range, such as a price band. Each thumb drags on its own and can never cross the other, values snap to step, a bubble shows the value above the thumb you hold, and tapping the bare track moves the nearest thumb. Both thumbs are adjustable controls for screen readers. Built for React Native and Expo with react-native only; this page previews it in the browser through react-native-web.

Installation

CLI

npx @modus-ui/cli add range-slider

Manual

  1. Copy the code from the component tab.
  2. Create a file at components/ui/range-slider.tsx.
  3. Paste the code.

It needs no packages beyond react-native, which your Expo or React Native app already has.

Usage

tsx
import { useState } from "react";
import { RangeSlider } from "@/components/ui/range-slider";

export default function Example() {
  const [price, setPrice] = useState<[number, number]>([100, 300]);

  return (
    <RangeSlider
      label="Price"
      value={price}
      onValueChange={setPrice}
      min={0}
      max={500}
      step={5}
      format={(value) => `$${value}`}
    />
  );
}

Props

PropTypeDefaultDescription
value[number, number]-The selected range as [low, high].
onValueChange(value: [number, number]) => void-Called as either thumb moves.
onSlidingComplete(value: [number, number]) => void-Called once when a drag ends, or after a tap on the track.
minnumber0The lowest value.
maxnumber100The highest value.
stepnumber1Values snap to multiples of this from min.
minGapnumber0The least the two thumbs may be apart.
showBubblesbooleantrueFloats the thumb's value above it while it is held.
disabledbooleanfalseDims the slider and ignores input.
labelstring"Range"Spoken name of the range; the thumbs add "minimum" and "maximum".
format(value: number) => string-Turns a value into the text shown in the bubble and read out.
colorsPartial<RangeSliderColors>-Overrides track, fill, thumb, bubble and bubbleText.
styleStyleProp<ViewStyle>-Style for the slider.

Comments

  • No comments yet.