|
| 1 | +import React, {useState, useRef, useEffect} from 'react'; |
| 2 | +import {motion, useMotionValue, animate} from 'framer-motion'; |
| 3 | + |
| 4 | +const RotaryDial = ({onDial}) => { |
| 5 | + const [isDragging, setIsDragging] = useState(false); |
| 6 | + const [activeDigit, setActiveDigit] = useState(null); |
| 7 | + const rotation = useMotionValue(0); |
| 8 | + const containerRef = useRef(null); // Ref for the static container |
| 9 | + const animationControls = useRef(null); |
| 10 | + |
| 11 | + // Configuration |
| 12 | + const STOP_ANGLE = 60; // Angle of the finger stop (in degrees, 0 is 3 o'clock) |
| 13 | + const GAP = 30; // Degrees between numbers |
| 14 | + // Numbers 1-9, 0. '1' is closest to stop. |
| 15 | + const DIGITS = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]; |
| 16 | + |
| 17 | + const getDigitAngle = (digit) => { |
| 18 | + const index = DIGITS.indexOf(digit); |
| 19 | + return STOP_ANGLE - (index + 1) * GAP; |
| 20 | + }; |
| 21 | + |
| 22 | + const getAngle = (event, center) => { |
| 23 | + const clientX = event.touches ? event.touches[0].clientX : event.clientX; |
| 24 | + const clientY = event.touches ? event.touches[0].clientY : event.clientY; |
| 25 | + |
| 26 | + const dx = clientX - center.x; |
| 27 | + const dy = clientY - center.y; |
| 28 | + let theta = Math.atan2(dy, dx) * (180 / Math.PI); |
| 29 | + return theta; |
| 30 | + }; |
| 31 | + |
| 32 | + const handleStart = (event, digit) => { |
| 33 | + // Only prevent default on touch to stop scrolling |
| 34 | + if (event.touches && event.cancelable) event.preventDefault(); |
| 35 | + |
| 36 | + // Stop any ongoing spring-back animation |
| 37 | + if (animationControls.current) { |
| 38 | + animationControls.current.stop(); |
| 39 | + } |
| 40 | + |
| 41 | + setIsDragging(true); |
| 42 | + setActiveDigit(digit); |
| 43 | + }; |
| 44 | + |
| 45 | + const handleMove = (event) => { |
| 46 | + if (!isDragging || activeDigit === null || !containerRef.current) return; |
| 47 | + const rect = containerRef.current.getBoundingClientRect(); |
| 48 | + const center = { |
| 49 | + x: rect.left + rect.width / 2, |
| 50 | + y: rect.top + rect.height / 2 |
| 51 | + }; |
| 52 | + |
| 53 | + const currentMouseAngle = getAngle(event, center); |
| 54 | + const startAngle = getDigitAngle(activeDigit); |
| 55 | + let newRotation = currentMouseAngle - startAngle; |
| 56 | + |
| 57 | + const normalizeDiff = (diff) => { |
| 58 | + while (diff <= -180) diff += 360; |
| 59 | + while (diff > 180) diff -= 360; |
| 60 | + return diff; |
| 61 | + }; |
| 62 | + |
| 63 | + newRotation = normalizeDiff(newRotation); |
| 64 | + |
| 65 | + const maxRot = STOP_ANGLE - startAngle; |
| 66 | + |
| 67 | + // If rotation is negative, it might be because we've crossed the 180 boundary |
| 68 | + // for a high number digit (like 9 or 0). |
| 69 | + // Check if adding 360 brings us into a valid positive range close to maxRot. |
| 70 | + // We allow a small buffer over maxRot for elasticity. |
| 71 | + if (newRotation < 0 && (newRotation + 360) <= maxRot + 30) { |
| 72 | + newRotation += 360; |
| 73 | + } |
| 74 | + |
| 75 | + // Clamp |
| 76 | + if (newRotation < -10) newRotation = -10; |
| 77 | + if (newRotation > maxRot) newRotation = maxRot; |
| 78 | + |
| 79 | + rotation.set(newRotation); |
| 80 | + }; |
| 81 | + const handleEnd = () => { |
| 82 | + if (!isDragging) return; |
| 83 | + setIsDragging(false); |
| 84 | + |
| 85 | + const maxRot = STOP_ANGLE - getDigitAngle(activeDigit); |
| 86 | + const threshold = 15; |
| 87 | + |
| 88 | + const currentRot = rotation.get(); |
| 89 | + |
| 90 | + if (Math.abs(currentRot - maxRot) < threshold) { |
| 91 | + if (onDial) onDial(activeDigit); |
| 92 | + } |
| 93 | + |
| 94 | + setActiveDigit(null); |
| 95 | + |
| 96 | + // Animate back to 0 |
| 97 | + animationControls.current = animate(rotation, 0, { |
| 98 | + type: "spring", |
| 99 | + stiffness: 200, |
| 100 | + damping: 20, |
| 101 | + mass: 1 |
| 102 | + }); |
| 103 | + }; |
| 104 | + |
| 105 | + useEffect(() => { |
| 106 | + const onMove = (e) => handleMove(e); |
| 107 | + const onUp = () => handleEnd(); |
| 108 | + |
| 109 | + if (isDragging) { |
| 110 | + window.addEventListener('mousemove', onMove); |
| 111 | + window.addEventListener('mouseup', onUp); |
| 112 | + window.addEventListener('touchmove', onMove, {passive: false}); |
| 113 | + window.addEventListener('touchend', onUp); |
| 114 | + } |
| 115 | + |
| 116 | + return () => { |
| 117 | + window.removeEventListener('mousemove', onMove); |
| 118 | + window.removeEventListener('mouseup', onUp); |
| 119 | + window.removeEventListener('touchmove', onMove); |
| 120 | + window.removeEventListener('touchend', onUp); |
| 121 | + }; |
| 122 | + }, [isDragging, activeDigit]); |
| 123 | + |
| 124 | + return ( |
| 125 | + <div |
| 126 | + ref={containerRef} |
| 127 | + className="relative w-80 h-80 sm:w-96 sm:h-96 select-none" |
| 128 | + > |
| 129 | + <div className="absolute inset-0 rounded-full bg-gray-900 border-4 border-gray-700 shadow-2xl"> |
| 130 | + {DIGITS.map((digit) => { |
| 131 | + const angle = getDigitAngle(digit); |
| 132 | + return ( |
| 133 | + <div |
| 134 | + key={`num-${digit}`} |
| 135 | + className="absolute inset-0 flex items-center justify-center pointer-events-none" |
| 136 | + style={{transform: `rotate(${angle}deg)`}} |
| 137 | + > |
| 138 | + <div |
| 139 | + className="text-3xl font-bold text-white font-mono" |
| 140 | + style={{transform: `translate(${140}%) rotate(${-angle}deg)`}} |
| 141 | + > |
| 142 | + {digit} |
| 143 | + </div> |
| 144 | + </div> |
| 145 | + ); |
| 146 | + })} |
| 147 | + </div> |
| 148 | + |
| 149 | + <motion.div |
| 150 | + className="absolute inset-0 rounded-full border-4 border-transparent" |
| 151 | + style={{rotate: rotation}} |
| 152 | + > |
| 153 | + <div |
| 154 | + className="absolute inset-2 rounded-full bg-gradient-to-br from-gray-800/90 to-gray-900/90 shadow-inner backdrop-blur-sm border border-gray-600"> |
| 155 | + <div |
| 156 | + className="absolute inset-0 m-auto w-1/3 h-1/3 rounded-full bg-gray-800 border-2 border-gray-600 flex items-center justify-center shadow-lg"> |
| 157 | + <div className="text-gray-500 text-xs font-mono text-center opacity-50"> |
| 158 | + FEZ<br/>CODE |
| 159 | + </div> |
| 160 | + </div> |
| 161 | + |
| 162 | + {DIGITS.map((digit) => { |
| 163 | + const angle = getDigitAngle(digit); |
| 164 | + return ( |
| 165 | + <div |
| 166 | + key={`hole-${digit}`} |
| 167 | + className="absolute inset-0 flex items-center justify-center pointer-events-none" |
| 168 | + style={{transform: `rotate(${angle}deg)`}} |
| 169 | + > |
| 170 | + <div |
| 171 | + className={`w-14 h-14 sm:w-16 sm:h-16 rounded-full border-2 transition-colors duration-200 cursor-pointer pointer-events-auto |
| 172 | + ${activeDigit === digit ? 'bg-primary-500/20 border-primary-400 shadow-[0_0_15px_rgba(59,130,246,0.5)]' : 'bg-gray-950/50 border-gray-600 hover:border-gray-400'} |
| 173 | + flex items-center justify-center shadow-inner`} |
| 174 | + style={{transform: `translate(${140}%)`}} |
| 175 | + onMouseDown={(e) => handleStart(e, digit)} |
| 176 | + onTouchStart={(e) => handleStart(e, digit)} |
| 177 | + > |
| 178 | + </div> |
| 179 | + </div> |
| 180 | + ); |
| 181 | + })} |
| 182 | + </div> |
| 183 | + </motion.div> |
| 184 | + |
| 185 | + <div |
| 186 | + className="absolute inset-0 pointer-events-none z-10" |
| 187 | + style={{transform: `rotate(${STOP_ANGLE}deg)`}} |
| 188 | + > |
| 189 | + <div |
| 190 | + className="absolute top-1/2 right-0 w-16 h-4 bg-gradient-to-r from-gray-400 to-gray-200 rounded-l-lg shadow-lg origin-right translate-x-2 -translate-y-1/2"/> |
| 191 | + </div> |
| 192 | + </div> |
| 193 | + ); |
| 194 | +}; |
| 195 | + |
| 196 | +export default RotaryDial; |
0 commit comments