Slider.tsx 1.4 KB

12345678910111213141516171819202122232425262728293031
  1. // Props to https://github.com/shadcn-ui/ui/issues/885#issuecomment-2059600641
  2. // @mildtomato - consider using this as the main Slider component in packages/ui
  3. import { Slider as SliderPrimitive } from 'radix-ui'
  4. import { ComponentPropsWithoutRef, ElementRef, forwardRef, Fragment } from 'react'
  5. import { cn } from 'ui'
  6. export const Slider = forwardRef<
  7. ElementRef<typeof SliderPrimitive.Root>,
  8. ComponentPropsWithoutRef<typeof SliderPrimitive.Root>
  9. >(({ className, ...props }, ref) => {
  10. const initialValue = Array.isArray(props.value) ? props.value : [props.min, props.max]
  11. return (
  12. <SliderPrimitive.Root
  13. ref={ref}
  14. className={cn('relative flex w-full touch-none select-none items-center', className)}
  15. {...props}
  16. >
  17. <SliderPrimitive.Track className="relative h-2 w-full grow overflow-hidden rounded-full bg-secondary">
  18. <SliderPrimitive.Range className="absolute h-full bg-primary" />
  19. </SliderPrimitive.Track>
  20. {initialValue.map((_, index) => (
  21. <Fragment key={index}>
  22. <SliderPrimitive.Thumb className="block h-4 w-4 rounded-full border-2 border-primary bg-background ring-offset-background transition-colors focus-visible:outline-hidden focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50" />
  23. </Fragment>
  24. ))}
  25. </SliderPrimitive.Root>
  26. )
  27. })
  28. Slider.displayName = SliderPrimitive.Root.displayName