slider.tsx 2.0 KB

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