|
| 1 | +import React from "react"; |
| 2 | + |
| 3 | +// NOTE: This hook was generated entirely with Claude then lightly touched up, |
| 4 | +// and the behavior works correctly from testing it manually in the browser. |
| 5 | + |
| 6 | +type Position = { top: number; right: number }; |
| 7 | + |
| 8 | +type UseDraggableOptions = { |
| 9 | + elementRef: React.RefObject<HTMLElement | null>; |
| 10 | + initialPosition?: Position; |
| 11 | + reclampDeps?: React.DependencyList; |
| 12 | + /** Extra space to reserve beyond the element's right edge (px). */ |
| 13 | + rightPadding?: number; |
| 14 | +}; |
| 15 | + |
| 16 | +type UseDraggableReturn = { |
| 17 | + position: Position; |
| 18 | + isDragging: boolean; |
| 19 | + handlePointerDown: (e: React.PointerEvent) => void; |
| 20 | +}; |
| 21 | + |
| 22 | +const DEFAULT_POSITION: Position = { top: 16, right: 16 }; |
| 23 | + |
| 24 | +/** |
| 25 | + * @param rightPadding Extra space to reserve on the right edge (e.g. for a |
| 26 | + * drag handle that protrudes beyond the element). The element's left edge |
| 27 | + * is clamped so that `elementWidth + rightPadding` stays within the viewport. |
| 28 | + */ |
| 29 | +export function clampPosition( |
| 30 | + pos: Position, |
| 31 | + elementWidth: number, |
| 32 | + elementHeight: number, |
| 33 | + rightPadding = 0, |
| 34 | +): Position { |
| 35 | + const viewportWidth = window.innerWidth; |
| 36 | + const viewportHeight = window.innerHeight; |
| 37 | + |
| 38 | + const totalWidth = elementWidth + rightPadding; |
| 39 | + const left = viewportWidth - pos.right - elementWidth; |
| 40 | + const clampedLeft = Math.max(0, Math.min(left, viewportWidth - totalWidth)); |
| 41 | + const clampedTop = Math.max( |
| 42 | + 0, |
| 43 | + Math.min(pos.top, viewportHeight - elementHeight), |
| 44 | + ); |
| 45 | + const clampedRight = viewportWidth - clampedLeft - elementWidth; |
| 46 | + |
| 47 | + return { top: clampedTop, right: clampedRight }; |
| 48 | +} |
| 49 | + |
| 50 | +export function useDraggable({ |
| 51 | + elementRef, |
| 52 | + initialPosition = DEFAULT_POSITION, |
| 53 | + reclampDeps = [], |
| 54 | + rightPadding = 0, |
| 55 | +}: UseDraggableOptions): UseDraggableReturn { |
| 56 | + const [position, setPosition] = React.useState<Position>(initialPosition); |
| 57 | + const [isDragging, setIsDragging] = React.useState(false); |
| 58 | + |
| 59 | + const positionRef = React.useRef(position); |
| 60 | + positionRef.current = position; |
| 61 | + |
| 62 | + const startPointerRef = React.useRef({ x: 0, y: 0 }); |
| 63 | + const startPositionRef = React.useRef<Position>({ top: 0, right: 0 }); |
| 64 | + const rafIdRef = React.useRef<number | null>(null); |
| 65 | + const isDraggingRef = React.useRef(false); |
| 66 | + const cleanupListenersRef = React.useRef<(() => void) | null>(null); |
| 67 | + |
| 68 | + const reclamp = React.useCallback(() => { |
| 69 | + const el = elementRef.current; |
| 70 | + if (!el) return; |
| 71 | + const rect = el.getBoundingClientRect(); |
| 72 | + setPosition((prev) => |
| 73 | + clampPosition(prev, rect.width, rect.height, rightPadding), |
| 74 | + ); |
| 75 | + }, [elementRef, rightPadding]); |
| 76 | + |
| 77 | + // Stable pointerdown handler |
| 78 | + const handlePointerDown = React.useCallback( |
| 79 | + (e: React.PointerEvent) => { |
| 80 | + e.preventDefault(); |
| 81 | + startPointerRef.current = { x: e.clientX, y: e.clientY }; |
| 82 | + startPositionRef.current = { ...positionRef.current }; |
| 83 | + isDraggingRef.current = true; |
| 84 | + setIsDragging(true); |
| 85 | + |
| 86 | + const onPointerMove = (moveEvent: PointerEvent) => { |
| 87 | + if (!isDraggingRef.current) return; |
| 88 | + |
| 89 | + if (rafIdRef.current !== null) return; |
| 90 | + |
| 91 | + rafIdRef.current = requestAnimationFrame(() => { |
| 92 | + rafIdRef.current = null; |
| 93 | + if (!isDraggingRef.current) return; |
| 94 | + |
| 95 | + const dx = moveEvent.clientX - startPointerRef.current.x; |
| 96 | + const dy = moveEvent.clientY - startPointerRef.current.y; |
| 97 | + |
| 98 | + const newPos: Position = { |
| 99 | + top: startPositionRef.current.top + dy, |
| 100 | + right: startPositionRef.current.right - dx, |
| 101 | + }; |
| 102 | + |
| 103 | + const el = elementRef.current; |
| 104 | + if (!el) return; |
| 105 | + const rect = el.getBoundingClientRect(); |
| 106 | + const clamped = clampPosition( |
| 107 | + newPos, |
| 108 | + rect.width, |
| 109 | + rect.height, |
| 110 | + rightPadding, |
| 111 | + ); |
| 112 | + setPosition(clamped); |
| 113 | + }); |
| 114 | + }; |
| 115 | + |
| 116 | + const cleanup = () => { |
| 117 | + isDraggingRef.current = false; |
| 118 | + setIsDragging(false); |
| 119 | + if (rafIdRef.current !== null) { |
| 120 | + cancelAnimationFrame(rafIdRef.current); |
| 121 | + rafIdRef.current = null; |
| 122 | + } |
| 123 | + document.removeEventListener("pointermove", onPointerMove); |
| 124 | + document.removeEventListener("pointerup", onPointerUp); |
| 125 | + cleanupListenersRef.current = null; |
| 126 | + }; |
| 127 | + |
| 128 | + const onPointerUp = () => cleanup(); |
| 129 | + |
| 130 | + document.addEventListener("pointermove", onPointerMove); |
| 131 | + document.addEventListener("pointerup", onPointerUp); |
| 132 | + cleanupListenersRef.current = cleanup; |
| 133 | + }, |
| 134 | + [elementRef, rightPadding], |
| 135 | + ); |
| 136 | + |
| 137 | + // Cleanup on unmount |
| 138 | + React.useEffect(() => { |
| 139 | + return () => { |
| 140 | + cleanupListenersRef.current?.(); |
| 141 | + }; |
| 142 | + }, []); |
| 143 | + |
| 144 | + // Re-clamp on window resize |
| 145 | + React.useEffect(() => { |
| 146 | + const onResize = () => reclamp(); |
| 147 | + window.addEventListener("resize", onResize); |
| 148 | + return () => window.removeEventListener("resize", onResize); |
| 149 | + }, [reclamp]); |
| 150 | + |
| 151 | + // Re-clamp when deps change (e.g. collapse toggle) |
| 152 | + React.useEffect(() => { |
| 153 | + const id = requestAnimationFrame(() => reclamp()); |
| 154 | + return () => cancelAnimationFrame(id); |
| 155 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 156 | + }, reclampDeps); |
| 157 | + |
| 158 | + return { position, isDragging, handlePointerDown }; |
| 159 | +} |
0 commit comments