MCPcopy
hub / github.com/DavidHDev/react-bits / PreviewSlider

Function PreviewSlider

src/components/common/Preview/PreviewSlider.jsx:16–151  ·  view source on GitHub ↗
({
  title = '',
  min = 0,
  max = 100,
  step = 1,
  value = 0,
  valueUnit = '',
  isDisabled = false,
  displayValue,
  onChange
})

Source from the content-addressed store, hash-verified

14};
15
16const PreviewSlider = ({
17 title = '',
18 min = 0,
19 max = 100,
20 step = 1,
21 value = 0,
22 valueUnit = '',
23 isDisabled = false,
24 displayValue,
25 onChange
26}) => {
27 const trackRef = useRef(null);
28 const [isDragging, setIsDragging] = useState(false);
29 const [isHovering, setIsHovering] = useState(false);
30 const [isHoverDevice, setIsHoverDevice] = useState(false);
31
32 const range = max - min;
33 const percentage = range > 0 ? ((value - min) / range) * 100 : 0;
34 const isActive = isDragging || (isHoverDevice && isHovering);
35
36 useEffect(() => {
37 const mq = window.matchMedia('(hover: hover) and (pointer: fine)');
38 setIsHoverDevice(mq.matches);
39 const handleChange = (e) => setIsHoverDevice(e.matches);
40 mq.addEventListener('change', handleChange);
41 return () => mq.removeEventListener('change', handleChange);
42 }, []);
43
44 const computeValue = useCallback(
45 (clientX) => {
46 const track = trackRef.current;
47 if (!track) return value;
48 const rect = track.getBoundingClientRect();
49 const ratio = clamp((clientX - rect.left) / rect.width, 0, 1);
50 const raw = min + ratio * range;
51 return clamp(roundToStep(raw, step, min), min, max);
52 },
53 [min, max, step, range, value]
54 );
55
56 const handlePointerDown = useCallback(
57 (e) => {
58 if (isDisabled) return;
59 e.preventDefault();
60 trackRef.current?.setPointerCapture(e.pointerId);
61 setIsDragging(true);
62 onChange?.(computeValue(e.clientX));
63 },
64 [computeValue, onChange, isDisabled]
65 );
66
67 const handlePointerMove = useCallback(
68 (e) => {
69 if (!isDragging) return;
70 onChange?.(computeValue(e.clientX));
71 },
72 [isDragging, computeValue, onChange]
73 );

Callers

nothing calls this directly

Calls 4

clampFunction · 0.70
roundToStepFunction · 0.70
stepDecimalsFunction · 0.70
NumberFunction · 0.50

Tested by

no test coverage detected