({ defaultValue, startingValue, maxValue, isStepped, stepSize, leftIcon, rightIcon })
| 33 | } |
| 34 | |
| 35 | function Slider({ defaultValue, startingValue, maxValue, isStepped, stepSize, leftIcon, rightIcon }) { |
| 36 | const [value, setValue] = useState(defaultValue); |
| 37 | const sliderRef = useRef(null); |
| 38 | const [region, setRegion] = useState('middle'); |
| 39 | const clientX = useMotionValue(0); |
| 40 | const overflow = useMotionValue(0); |
| 41 | const scale = useMotionValue(1); |
| 42 | |
| 43 | useEffect(() => { |
| 44 | setValue(defaultValue); |
| 45 | }, [defaultValue]); |
| 46 | |
| 47 | useMotionValueEvent(clientX, 'change', latest => { |
| 48 | if (sliderRef.current) { |
| 49 | const { left, right } = sliderRef.current.getBoundingClientRect(); |
| 50 | let newValue; |
| 51 | |
| 52 | if (latest < left) { |
| 53 | setRegion('left'); |
| 54 | newValue = left - latest; |
| 55 | } else if (latest > right) { |
| 56 | setRegion('right'); |
| 57 | newValue = latest - right; |
| 58 | } else { |
| 59 | setRegion('middle'); |
| 60 | newValue = 0; |
| 61 | } |
| 62 | |
| 63 | overflow.jump(decay(newValue, MAX_OVERFLOW)); |
| 64 | } |
| 65 | }); |
| 66 | |
| 67 | const handlePointerMove = e => { |
| 68 | if (e.buttons > 0 && sliderRef.current) { |
| 69 | const { left, width } = sliderRef.current.getBoundingClientRect(); |
| 70 | let newValue = startingValue + ((e.clientX - left) / width) * (maxValue - startingValue); |
| 71 | |
| 72 | if (isStepped) { |
| 73 | newValue = Math.round(newValue / stepSize) * stepSize; |
| 74 | } |
| 75 | |
| 76 | newValue = Math.min(Math.max(newValue, startingValue), maxValue); |
| 77 | setValue(newValue); |
| 78 | clientX.jump(e.clientX); |
| 79 | } |
| 80 | }; |
| 81 | |
| 82 | const handlePointerDown = e => { |
| 83 | handlePointerMove(e); |
| 84 | e.currentTarget.setPointerCapture(e.pointerId); |
| 85 | }; |
| 86 | |
| 87 | const handlePointerUp = () => { |
| 88 | animate(overflow, 0, { type: 'spring', bounce: 0.5 }); |
| 89 | }; |
| 90 | |
| 91 | const getRangePercentage = () => { |
| 92 | const totalRange = maxValue - startingValue; |
nothing calls this directly
no test coverage detected