({ hsv, onChange })
| 7 | } |
| 8 | |
| 9 | export const SatValBox: React.FC<SatValBoxProps> = ({ hsv, onChange }) => { |
| 10 | const boxRef = useRef<HTMLDivElement>(null); |
| 11 | const [isDragging, setIsDragging] = useState(false); |
| 12 | |
| 13 | const handleMove = useCallback((e: MouseEvent) => { |
| 14 | if (!boxRef.current) return; |
| 15 | const rect = boxRef.current.getBoundingClientRect(); |
| 16 | const x = Math.max(0, Math.min(1, (e.clientX - rect.left) / rect.width)); |
| 17 | const y = Math.max(0, Math.min(1, (e.clientY - rect.top) / rect.height)); |
| 18 | |
| 19 | // x is saturation (0..100), y is value (100..0) |
| 20 | onChange({ ...hsv, s: x * 100, v: (1 - y) * 100 }); |
| 21 | }, [hsv, onChange]); |
| 22 | |
| 23 | useEffect(() => { |
| 24 | const up = () => setIsDragging(false); |
| 25 | const move = (e: MouseEvent) => { if (isDragging) handleMove(e); }; |
| 26 | |
| 27 | if (isDragging) { |
| 28 | window.addEventListener('mouseup', up); |
| 29 | window.addEventListener('mousemove', move); |
| 30 | } |
| 31 | |
| 32 | return () => { |
| 33 | window.removeEventListener('mouseup', up); |
| 34 | window.removeEventListener('mousemove', move); |
| 35 | }; |
| 36 | }, [isDragging, handleMove]); |
| 37 | |
| 38 | return ( |
| 39 | <div |
| 40 | ref={boxRef} |
| 41 | className="w-full h-32 relative cursor-crosshair border border-[var(--color-border)] mb-2" |
| 42 | style={{ |
| 43 | backgroundColor: `hsl(${hsv.h}, 100%, 50%)`, |
| 44 | backgroundImage: 'linear-gradient(to right, #fff, transparent), linear-gradient(to top, #000, transparent)' |
| 45 | }} |
| 46 | onMouseDown={(e) => { |
| 47 | setIsDragging(true); |
| 48 | handleMove(e.nativeEvent); |
| 49 | }} |
| 50 | > |
| 51 | <div |
| 52 | className="absolute w-3 h-3 border-2 border-black bg-white rounded-full -ml-1.5 -mt-1.5 shadow-sm pointer-events-none mix-blend-difference" |
| 53 | style={{ |
| 54 | left: `${hsv.s}%`, |
| 55 | top: `${100 - hsv.v}%` |
| 56 | }} |
| 57 | /> |
| 58 | </div> |
| 59 | ); |
| 60 | }; |
| 61 | |
| 62 | export interface HueSliderProps { |
| 63 | hsv: { h: number, s: number, v: number }; |
nothing calls this directly
no outgoing calls
no test coverage detected