dual-range-slider.tsx

Overview

dual-range-slider.tsx defines two React components, DualRangeSlider and SingleFormSlider, which provide customizable range slider UI elements built atop the @radix-ui/react-slider primitive components. These components facilitate selecting one or two numeric values within a defined range, with support for custom labels and flexible positioning. The file leverages Radix UI's accessibility and behavior features while adding styling and label rendering logic.

Both components forward refs and support a wide range of props inherited from Radix's Slider root component.


Component Details

DualRangeSlider

Description

DualRangeSlider is a React forwardRef component that renders a horizontal slider with two thumbs by default, allowing users to select a numeric range. It can also handle single-value sliders by passing a single value array. The component supports optional labels displayed either above or below each thumb.

Props

Prop

Type

Default

Description

labelPosition

'top' \

'bottom'

'top'

label

[(value: number \

undefined) => ReactNode](/projects/311/74002)

undefined

All other props

React.ComponentProps

Inherited props for slider configuration, including value, min, max, step, onValueChange, etc.

Usage Example

<DualRangeSlider
  min={0}
  max={100}
  value={[20, 80]}
  onValueChange={(values) => console.log(values)}
  label={(val) => `${val}%`}
  labelPosition="bottom"
/>

Implementation Details


SingleFormSlider

Description

SingleFormSlider is a controlled single-thumb slider component that wraps DualRangeSlider. It simplifies usage for single-value sliders by abstracting away the array value format and managing the onChange handler.

Props

Prop

Type

Description

value

number

The current value of the slider.

onChange

(value: number) => void

Callback invoked when the slider value changes.

Other props

`Omit<React.ComponentPropsWithoutRef, 'onChange'

'value'>`

Usage Example

<SingleFormSlider
  min={0}
  max={50}
  value={25}
  onChange={(val) => setValue(val)}
/>

Implementation Details


Important Implementation Details


Interaction with Other System Parts


Visual Diagram

componentDiagram
    component DualRangeSlider {
      +props: DualRangeSliderProps
      +render()
    }
    component SingleFormSlider {
      +props: SingleSliderProps
      +render()
    }
    component SliderPrimitive.Root
    component SliderPrimitive.Track
    component SliderPrimitive.Range
    component SliderPrimitive.Thumb

    DualRangeSlider --> SliderPrimitive.Root
    DualRangeSlider --> SliderPrimitive.Track
    DualRangeSlider --> SliderPrimitive.Range
    DualRangeSlider --> SliderPrimitive.Thumb

    SingleFormSlider --> DualRangeSlider

Summary

This file provides flexible and accessible range slider components built on Radix UI primitives. The DualRangeSlider supports dual-thumb ranges with customizable labels, while SingleFormSlider simplifies single-value slider use cases. Both use modern React patterns (forwardRef, render props) and integrate seamlessly with the Radix UI ecosystem and Tailwind CSS styling conventions.