2025-11-17 15:23:00 +01:00
|
|
|
'use client';
|
|
|
|
|
|
|
|
|
|
import * as React from 'react';
|
|
|
|
|
import { cn } from '@/lib/utils/cn';
|
|
|
|
|
|
|
|
|
|
export interface SliderProps
|
2025-11-17 20:03:40 +01:00
|
|
|
extends Omit<React.InputHTMLAttributes<HTMLInputElement>, 'onChange' | 'value' | 'onValueChange'> {
|
|
|
|
|
value?: number | number[];
|
2025-11-17 15:23:00 +01:00
|
|
|
onChange?: (value: number) => void;
|
2025-11-17 20:03:40 +01:00
|
|
|
onValueChange?: (value: number[]) => void;
|
2025-11-17 15:23:00 +01:00
|
|
|
min?: number;
|
|
|
|
|
max?: number;
|
|
|
|
|
step?: number;
|
|
|
|
|
label?: string;
|
|
|
|
|
showValue?: boolean;
|
2025-11-18 23:29:18 +01:00
|
|
|
onTouchStart?: () => void;
|
|
|
|
|
onTouchEnd?: () => void;
|
2025-11-17 15:23:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const Slider = React.forwardRef<HTMLInputElement, SliderProps>(
|
|
|
|
|
(
|
|
|
|
|
{
|
|
|
|
|
className,
|
|
|
|
|
value = 0,
|
|
|
|
|
onChange,
|
2025-11-17 20:03:40 +01:00
|
|
|
onValueChange,
|
2025-11-17 15:23:00 +01:00
|
|
|
min = 0,
|
|
|
|
|
max = 100,
|
|
|
|
|
step = 1,
|
|
|
|
|
label,
|
|
|
|
|
showValue = false,
|
|
|
|
|
disabled,
|
2025-11-18 23:29:18 +01:00
|
|
|
onTouchStart,
|
|
|
|
|
onTouchEnd,
|
2025-11-17 15:23:00 +01:00
|
|
|
...props
|
|
|
|
|
},
|
|
|
|
|
ref
|
|
|
|
|
) => {
|
2025-11-17 20:03:40 +01:00
|
|
|
// Support both value formats (number or number[])
|
|
|
|
|
const currentValue = Array.isArray(value) ? value[0] : value;
|
|
|
|
|
|
2025-11-17 15:23:00 +01:00
|
|
|
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
2025-11-17 20:03:40 +01:00
|
|
|
const numValue = parseFloat(e.target.value);
|
|
|
|
|
onChange?.(numValue);
|
|
|
|
|
onValueChange?.([numValue]);
|
2025-11-17 15:23:00 +01:00
|
|
|
};
|
|
|
|
|
|
2025-11-18 23:29:18 +01:00
|
|
|
const handleMouseDown = () => {
|
|
|
|
|
onTouchStart?.();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleMouseUp = () => {
|
|
|
|
|
onTouchEnd?.();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
|
if (onTouchEnd) {
|
|
|
|
|
window.addEventListener('mouseup', handleMouseUp);
|
|
|
|
|
return () => window.removeEventListener('mouseup', handleMouseUp);
|
|
|
|
|
}
|
|
|
|
|
}, [onTouchEnd]);
|
|
|
|
|
|
2025-11-17 15:23:00 +01:00
|
|
|
return (
|
|
|
|
|
<div className={cn('w-full', className)}>
|
|
|
|
|
{(label || showValue) && (
|
|
|
|
|
<div className="flex items-center justify-between mb-2">
|
|
|
|
|
{label && (
|
|
|
|
|
<label className="text-sm font-medium text-foreground">
|
|
|
|
|
{label}
|
|
|
|
|
</label>
|
|
|
|
|
)}
|
|
|
|
|
{showValue && (
|
2025-11-17 20:03:40 +01:00
|
|
|
<span className="text-sm text-muted-foreground">{currentValue}</span>
|
2025-11-17 15:23:00 +01:00
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
<input
|
|
|
|
|
ref={ref}
|
|
|
|
|
type="range"
|
|
|
|
|
min={min}
|
|
|
|
|
max={max}
|
|
|
|
|
step={step}
|
2025-11-17 20:03:40 +01:00
|
|
|
value={currentValue}
|
2025-11-17 15:23:00 +01:00
|
|
|
onChange={handleChange}
|
2025-11-18 23:29:18 +01:00
|
|
|
onMouseDown={handleMouseDown}
|
2025-11-17 15:23:00 +01:00
|
|
|
disabled={disabled}
|
|
|
|
|
className={cn(
|
|
|
|
|
'w-full h-2 bg-secondary rounded-lg appearance-none cursor-pointer',
|
|
|
|
|
'focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2',
|
|
|
|
|
'disabled:opacity-50 disabled:cursor-not-allowed',
|
|
|
|
|
'[&::-webkit-slider-thumb]:appearance-none [&::-webkit-slider-thumb]:w-4 [&::-webkit-slider-thumb]:h-4',
|
|
|
|
|
'[&::-webkit-slider-thumb]:rounded-full [&::-webkit-slider-thumb]:bg-primary',
|
|
|
|
|
'[&::-webkit-slider-thumb]:cursor-pointer [&::-webkit-slider-thumb]:transition-colors',
|
|
|
|
|
'[&::-webkit-slider-thumb]:hover:bg-primary/90',
|
|
|
|
|
'[&::-moz-range-thumb]:w-4 [&::-moz-range-thumb]:h-4 [&::-moz-range-thumb]:rounded-full',
|
|
|
|
|
'[&::-moz-range-thumb]:bg-primary [&::-moz-range-thumb]:border-0 [&::-moz-range-thumb]:cursor-pointer',
|
|
|
|
|
'[&::-moz-range-thumb]:transition-colors [&::-moz-range-thumb]:hover:bg-primary/90'
|
|
|
|
|
)}
|
|
|
|
|
{...props}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
Slider.displayName = 'Slider';
|
|
|
|
|
|
|
|
|
|
export { Slider };
|