shadcn-expansions
UI
Forms
Input
Data

dual-range-slider

An enhancement slider that allows you to select a range of values.

slider
range
dual
filter
input
form
View Docs

Source Code

Files
dual-range-slider.tsx
1"use client";
2
3import * as SliderPrimitive from "@radix-ui/react-slider";
4import * as React from "react";
5
6import { cn } from "@/lib/utils";
7
8interface DualRangeSliderProps
9  extends React.ComponentProps<typeof SliderPrimitive.Root> {
10  labelPosition?: "top" | "bottom";
11  label?: (value: number | undefined) => React.ReactNode;
12}
13
14const DualRangeSlider = React.forwardRef<
15  React.ElementRef<typeof SliderPrimitive.Root>,
16  DualRangeSliderProps
17>(({ className, label, labelPosition = "top", ...props }, ref) => {
18  const initialValue = Array.isArray(props.value)
19    ? props.value
20    : [props.min, props.max];
21
22  return (
23    <SliderPrimitive.Root
24      ref={ref}
25      className={cn(
26        "relative flex w-full touch-none select-none items-center",
27        className,
28      )}
29      {...props}
30    >
31      <SliderPrimitive.Track className="relative h-2 w-full grow overflow-hidden rounded-full bg-secondary">
32        <SliderPrimitive.Range className="absolute h-full bg-primary" />
33      </SliderPrimitive.Track>
34      {initialValue.map((value, index) => (
35        <React.Fragment key={index}>
36          <SliderPrimitive.Thumb className="relative block h-4 w-4 rounded-full border-2 border-primary bg-background ring-offset-background transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50">
37            {label && (
38              <span
39                className={cn(
40                  "absolute flex w-full justify-center",
41                  labelPosition === "top" && "-top-7",
42                  labelPosition === "bottom" && "top-4",
43                )}
44              >
45                {label(value)}
46              </span>
47            )}
48          </SliderPrimitive.Thumb>
49        </React.Fragment>
50      ))}
51    </SliderPrimitive.Root>
52  );
53});
54DualRangeSlider.displayName = "DualRangeSlider";
55
56export { DualRangeSlider };
57