| 4 | import { cn } from "../lib/utils"; |
| 5 | |
| 6 | function Slider({ |
| 7 | className, |
| 8 | defaultValue, |
| 9 | value, |
| 10 | min = 0, |
| 11 | max = 100, |
| 12 | ...props |
| 13 | }: React.ComponentProps<typeof SliderPrimitive.Root>) { |
| 14 | const _values = React.useMemo( |
| 15 | () => (Array.isArray(value) ? value : Array.isArray(defaultValue) ? defaultValue : [min, max]), |
| 16 | [value, defaultValue, min, max], |
| 17 | ); |
| 18 | |
| 19 | return ( |
| 20 | <SliderPrimitive.Root |
| 21 | data-slot="slider" |
| 22 | defaultValue={defaultValue} |
| 23 | value={value} |
| 24 | min={min} |
| 25 | max={max} |
| 26 | className={cn( |
| 27 | "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", |
| 28 | className, |
| 29 | )} |
| 30 | {...props} |
| 31 | > |
| 32 | <SliderPrimitive.Track |
| 33 | data-slot="slider-track" |
| 34 | className={cn( |
| 35 | "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", |
| 36 | )} |
| 37 | > |
| 38 | <SliderPrimitive.Range |
| 39 | data-slot="slider-range" |
| 40 | className={cn( |
| 41 | "absolute bg-primary data-[orientation=horizontal]:h-full data-[orientation=vertical]:w-full", |
| 42 | )} |
| 43 | /> |
| 44 | </SliderPrimitive.Track> |
| 45 | {Array.from({ length: _values.length }, (_, index) => ( |
| 46 | <SliderPrimitive.Thumb |
| 47 | data-slot="slider-thumb" |
| 48 | key={index} |
| 49 | className="block size-4 shrink-0 rounded-full border border-primary bg-white shadow-sm 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" |
| 50 | /> |
| 51 | ))} |
| 52 | </SliderPrimitive.Root> |
| 53 | ); |
| 54 | } |
| 55 | |
| 56 | export { Slider }; |