The Coline design system: accessible React components for Coline Apps, themed live by the host workspace, and for Coline Sites in light and dark. Tailwind v4 stylesheet included. MIT.
| 5 |
| 6 | import { cn } from "../lib/utils" |
| 7 |
| 8 | function Slider({ |
| 9 | className, |
| 10 | defaultValue, |
| 11 | value, |
| 12 | min = 0, |
| 13 | max = 100, |
| 14 | "aria-label": ariaLabel, |
| 15 | "aria-labelledby": ariaLabelledBy, |
| 16 | ...props |
| 17 | }: React.ComponentProps<typeof SliderPrimitive.Root>) { |
| 18 | const _values = React.useMemo( |
| 19 | () => |
| 20 | Array.isArray(value) |
| 21 | ? value |
| 22 | : Array.isArray(defaultValue) |
| 23 | ? defaultValue |
| 24 | : [min, max], |
| 25 | [value, defaultValue, min, max] |
| 26 | ) |
| 27 |
| 28 | // Build optional props to satisfy exactOptionalPropertyTypes |
| 29 | const optionalProps: Record<string, unknown> = {} |
| 30 | if (defaultValue !== undefined) optionalProps.defaultValue = defaultValue |
| 31 | if (value !== undefined) optionalProps.value = value |
| 32 | if (min !== undefined) optionalProps.min = min |
| 33 | if (max !== undefined) optionalProps.max = max |
| 34 |
| 35 | return ( |
| 36 | <SliderPrimitive.Root |
| 37 | data-slot="slider" |
| 38 | {...optionalProps} |
| 39 | className={cn( |
| 40 | "relative flex w-full touch-none items-center select-none data-disabled:opacity-50 data-[orientation=vertical]:h-full data-[orientation=vertical]:min-h-40 data-[orientation=vertical]:w-auto data-[orientation=vertical]:flex-col", |
| 41 | className |
| 42 | )} |
| 43 | {...props} |
| 44 | > |
| 45 | <SliderPrimitive.Track |
| 46 | data-slot="slider-track" |
| 47 | className="relative grow overflow-hidden rounded-4xl bg-muted data-[orientation=horizontal]:h-3 data-[orientation=horizontal]:w-full data-[orientation=vertical]:h-full data-[orientation=vertical]:w-3" |
| 48 | > |
| 49 | <SliderPrimitive.Range |
| 50 | data-slot="slider-range" |
| 51 | className="absolute bg-primary select-none data-[orientation=horizontal]:h-full data-[orientation=vertical]:w-full" |
| 52 | /> |
| 53 | </SliderPrimitive.Track> |
| 54 | {Array.from({ length: _values.length }, (_, index) => ( |
| 55 | <SliderPrimitive.Thumb |
| 56 | data-slot="slider-thumb" |
| 57 | // The thumb is the focusable `role="slider"`, so it carries the name. |
| 58 | {...(ariaLabel ? { "aria-label": ariaLabel } : {})} |
| 59 | {...(ariaLabelledBy ? { "aria-labelledby": ariaLabelledBy } : {})} |
| 60 | key={index} |
| 61 | className="block size-4 shrink-0 rounded-4xl border border-primary bg-white shadow-sm ring-ring/50 transition-colors select-none hover:ring-4 focus-visible:outline-hidden disabled:pointer-events-none disabled:opacity-50 dark:bg-foreground" |
| 62 | /> |
| 63 | ))} |
| 64 | </SliderPrimitive.Root> |
| 65 | ) |
| 66 | } |
| 67 |
| 68 | export { Slider } |
| 69 |