| 1 | "use client"; |
| 2 | |
| 3 | import * as SliderPrimitive from "@radix-ui/react-slider"; |
| 4 | import * as React from "react"; |
| 5 | |
| 6 | import { cn } from "@/lib/utils"; |
| 7 | |
| 8 | function Slider({ |
| 9 | className, |
| 10 | defaultValue, |
| 11 | value, |
| 12 | min = 0, |
| 13 | max = 100, |
| 14 | ...props |
| 15 | }: React.ComponentProps<typeof SliderPrimitive.Root>) { |
| 16 | const _values = React.useMemo( |
| 17 | () => |
| 18 | Array.isArray(value) |
| 19 | ? value |
| 20 | : Array.isArray(defaultValue) |
| 21 | ? defaultValue |
| 22 | : [min, max], |
| 23 | [value, defaultValue, min, max], |
| 24 | ); |
| 25 | |
| 26 | return ( |
| 27 | <SliderPrimitive.Root |
| 28 | data-slot="slider" |
| 29 | defaultValue={defaultValue} |
| 30 | value={value} |
| 31 | min={min} |
| 32 | max={max} |
| 33 | className={cn( |
| 34 | "relative flex w-full touch-none items-center select-none data-disabled:opacity-50 data-[orientation=vertical]:h-full data-[orientation=vertical]:min-h-44 data-[orientation=vertical]:w-auto data-[orientation=vertical]:flex-col", |
| 35 | className, |
| 36 | )} |
| 37 | {...props} |
| 38 | > |
| 39 | <SliderPrimitive.Track |
| 40 | data-slot="slider-track" |
| 41 | className={cn( |
| 42 | "relative grow overflow-hidden rounded-full bg-muted data-[orientation=horizontal]:h-1.5 data-[orientation=horizontal]:w-full data-[orientation=vertical]:h-full data-[orientation=vertical]:w-1.5", |
| 43 | )} |
| 44 | > |
| 45 | <SliderPrimitive.Range |
| 46 | data-slot="slider-range" |
| 47 | className={cn( |
| 48 | "absolute bg-primary data-[orientation=horizontal]:h-full data-[orientation=vertical]:w-full", |
| 49 | )} |
| 50 | /> |
| 51 | </SliderPrimitive.Track> |
| 52 | {Array.from({ length: _values.length }, (_, index) => ( |
| 53 | <SliderPrimitive.Thumb |
| 54 | data-slot="slider-thumb" |
| 55 | key={index} |
| 56 | className="block size-4 shrink-0 rounded-full border border-primary bg-white shadow-xs ring-ring/50 transition-[color,box-shadow] hover:ring-4 focus-visible:ring-4 focus-visible:outline-hidden disabled:pointer-events-none disabled:opacity-50" |
| 57 | /> |
| 58 | ))} |
| 59 | </SliderPrimitive.Root> |
| 60 | ); |
| 61 | } |
| 62 | |
| 63 | export { Slider }; |
| 64 |