slider.tsx 1.9 KB

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