infoalloggi-monorepo/apps/infoalloggi/src/components/ui/slider.tsx

69 lines
2.1 KiB
TypeScript
Raw Normal View History

2025-08-04 17:45:44 +02:00
import { Slider as SliderPrimitive } from "radix-ui";
2025-08-28 18:27:07 +02:00
import * as React from "react";
2025-08-04 17:45:44 +02:00
import { cn } from "~/lib/utils";
interface SliderProps
2025-08-28 18:27:07 +02:00
extends React.ComponentPropsWithoutRef<typeof SliderPrimitive.Root> {
rangeClassName?: string;
thumbClassName?: string;
2025-08-04 17:45:44 +02:00
}
function Slider({
2025-08-28 18:27:07 +02:00
className,
thumbClassName,
rangeClassName,
defaultValue,
value,
min = 0,
max = 100,
...props
2025-08-04 17:45:44 +02:00
}: React.ComponentProps<typeof SliderPrimitive.Root> & SliderProps) {
const _values = React.useMemo(() => {
if (Array.isArray(value)) return value;
if (Array.isArray(defaultValue)) return defaultValue;
return [min, max];
}, [value, defaultValue, min, max]);
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
return (
<SliderPrimitive.Root
className={cn(
"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",
className,
)}
2025-08-29 16:18:32 +02:00
data-slot="slider"
defaultValue={defaultValue}
max={max}
min={min}
value={value}
2025-08-28 18:27:07 +02:00
{...props}
>
<SliderPrimitive.Track
className={cn(
"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",
)}
2025-08-29 16:18:32 +02:00
data-slot="slider-track"
2025-08-28 18:27:07 +02:00
>
<SliderPrimitive.Range
className={cn(
"bg-primary absolute data-[orientation=horizontal]:h-full data-[orientation=vertical]:w-full",
rangeClassName,
)}
2025-08-29 16:18:32 +02:00
data-slot="slider-range"
2025-08-28 18:27:07 +02:00
/>
</SliderPrimitive.Track>
{Array.from({ length: _values.length }, (_, index) => (
<SliderPrimitive.Thumb
className={cn(
"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",
thumbClassName,
)}
2025-08-29 16:18:32 +02:00
data-slot="slider-thumb"
key={index}
2025-08-28 18:27:07 +02:00
/>
))}
</SliderPrimitive.Root>
);
2025-08-04 17:45:44 +02:00
}
export { Slider };