(options: UseResizerOptions)
| 48 | * @returns Handlers and state for resize interactions |
| 49 | */ |
| 50 | export function useResizer(options: UseResizerOptions): UseResizerResult { |
| 51 | const { |
| 52 | direction, |
| 53 | sizes, |
| 54 | minSizes, |
| 55 | maxSizes, |
| 56 | snapPoints = [], |
| 57 | snapTolerance = 10, |
| 58 | step, |
| 59 | onResizeStart, |
| 60 | onResize, |
| 61 | onResizeEnd, |
| 62 | } = options; |
| 63 | |
| 64 | const [isDragging, setIsDragging] = useState(false); |
| 65 | const [currentSizes, setCurrentSizes] = useState(sizes); |
| 66 | |
| 67 | const dragStateRef = useRef<{ |
| 68 | dividerIndex: number; |
| 69 | startPosition: number; |
| 70 | startSizes: number[]; |
| 71 | pointerId: number; |
| 72 | pointerType: 'mouse' | 'touch' | 'pen'; |
| 73 | element: HTMLElement | null; |
| 74 | } | null>(null); |
| 75 | |
| 76 | const rafRef = useRef<number | null>(null); |
| 77 | const mountedRef = useRef(true); |
| 78 | const lastPositionRef = useRef<{ x: number; y: number } | null>(null); |
| 79 | |
| 80 | // Use refs to avoid stale closures in event handlers |
| 81 | const currentSizesRef = useRef(currentSizes); |
| 82 | currentSizesRef.current = currentSizes; |
| 83 | |
| 84 | const onResizeEndRef = useRef(onResizeEnd); |
| 85 | onResizeEndRef.current = onResizeEnd; |
| 86 | |
| 87 | // Sync sizes from props when not dragging (React 19 compatible) |
| 88 | const sizesRef = useRef(sizes); |
| 89 | useEffect(() => { |
| 90 | if ( |
| 91 | !isDragging && |
| 92 | JSON.stringify(sizes) !== JSON.stringify(sizesRef.current) |
| 93 | ) { |
| 94 | sizesRef.current = sizes; |
| 95 | setCurrentSizes(sizes); |
| 96 | } |
| 97 | }, [sizes, isDragging]); |
| 98 | |
| 99 | // Track mounted state for RAF cleanup |
| 100 | useEffect(() => { |
| 101 | mountedRef.current = true; |
| 102 | return () => { |
| 103 | mountedRef.current = false; |
| 104 | if (rafRef.current) { |
| 105 | cancelAnimationFrame(rafRef.current); |
| 106 | rafRef.current = null; |
| 107 | } |
no test coverage detected
searching dependent graphs…