import React, { useEffect, useRef, useState } from 'react'; interface SliderProps { min?: number; max?: number; step?: number; initialValue?: number; heightPx?: number; minLabel?: string; maxLabel?: string; bubbleElement?: React.ReactNode; bubbleLabel?: string; className?: string; minClassName?: string; maxClassName?: string; bubbleClassName?: string; onChange?: (value: number) => void; } const Slider: React.FC = ({ min = 0, max = 100, step = 1, initialValue = 50, heightPx = 40, minLabel = '', maxLabel = '', bubbleElement, bubbleLabel = '', className = '', minClassName = '', maxClassName = '', bubbleClassName = '', onChange, }) => { const [value, setValue] = useState(initialValue); const [isRtl, setIsRtl] = useState(false); const sliderRef = useRef(null); const handleChange = (e: React.ChangeEvent) => { const newValue = parseInt((e.target as HTMLInputElement).value, 10); setValue(newValue); if (onChange) { onChange(newValue); } }; useEffect(() => { let node: HTMLElement | null = sliderRef.current; while (node) { if (node.getAttribute('dir') === 'rtl') { setIsRtl(true); break; } node = node.parentElement; } }, []); useEffect(() => { setValue(initialValue); }, [initialValue]); const percentage = ((value - min) / (max - min)) * 100; return (
{/* Background track */}
{/* Filled portion */}
0 ? `max(calc(${percentage}% + ${heightPx / 2}px), ${heightPx}px)` : '0px', [isRtl ? 'right' : 'left']: 0, }} >
{/* Min/Max labels */}
{minLabel} {maxLabel}
{/* Thumb bubble */}
{bubbleElement || bubbleLabel}
); }; export default Slider;