|
| 1 | +import type {RefObject} from 'react'; |
| 2 | +import {useCallback, useEffect, useRef, useState} from 'react'; |
| 3 | + |
| 4 | +export const RESIZABLE_DEFAULT_WIDTH = 200; |
| 5 | +export const RESIZABLE_MIN_WIDTH = 100; |
| 6 | +export const RESIZABLE_MAX_WIDTH = Infinity; |
| 7 | + |
| 8 | +interface UseResizableOptions { |
| 9 | + /** |
| 10 | + * The ref to the element to be resized. |
| 11 | + */ |
| 12 | + ref: RefObject<HTMLElement | null>; |
| 13 | + |
| 14 | + /** |
| 15 | + * The starting size of the container, and the size that is set in the onDoubleClick handler. |
| 16 | + * |
| 17 | + * If `sizeStorageKey` is provided and exists in local storage, |
| 18 | + * then this will be ignored in favor of the size stored in local storage. |
| 19 | + */ |
| 20 | + initialSize?: number; |
| 21 | + |
| 22 | + /** |
| 23 | + * The maximum width the container can be resized to. Defaults to Infinity. |
| 24 | + */ |
| 25 | + maxWidth?: number; |
| 26 | + |
| 27 | + /** |
| 28 | + * The minimum width the container can be resized to. Defaults to 100. |
| 29 | + */ |
| 30 | + minWidth?: number; |
| 31 | + |
| 32 | + /** |
| 33 | + * Triggered when the user finishes dragging the resize handle. |
| 34 | + */ |
| 35 | + onResizeEnd?: (newWidth: number) => void; |
| 36 | + |
| 37 | + /** |
| 38 | + * Triggered when the user starts dragging the resize handle. |
| 39 | + */ |
| 40 | + onResizeStart?: () => void; |
| 41 | + |
| 42 | + /** |
| 43 | + * The local storage key used to persist the size of the container. If not provided, |
| 44 | + * the size will not be persisted and the defaultWidth will be used. |
| 45 | + */ |
| 46 | + sizeStorageKey?: string; |
| 47 | +} |
| 48 | + |
| 49 | +/** |
| 50 | + * Performant hook to support draggable container resizing. |
| 51 | + * |
| 52 | + * Currently only supports resizing width and not height. |
| 53 | + */ |
| 54 | +export const useResizable = ({ |
| 55 | + ref, |
| 56 | + initialSize = RESIZABLE_DEFAULT_WIDTH, |
| 57 | + maxWidth = RESIZABLE_MAX_WIDTH, |
| 58 | + minWidth = RESIZABLE_MIN_WIDTH, |
| 59 | + onResizeEnd, |
| 60 | + onResizeStart, |
| 61 | + sizeStorageKey, |
| 62 | +}: UseResizableOptions): { |
| 63 | + /** |
| 64 | + * Whether the drag handle is held. |
| 65 | + */ |
| 66 | + isHeld: boolean; |
| 67 | + /** |
| 68 | + * Apply this to the drag handle element to include 'reset' functionality. |
| 69 | + */ |
| 70 | + onDoubleClick: () => void; |
| 71 | + /** |
| 72 | + * Attach this to the drag handle element's onMouseDown handler. |
| 73 | + */ |
| 74 | + onMouseDown: (e: React.MouseEvent) => void; |
| 75 | + /** |
| 76 | + * The current size of the container. This is NOT updated during the drag |
| 77 | + * event, only after the user finishes dragging. |
| 78 | + */ |
| 79 | + size: number; |
| 80 | +} => { |
| 81 | + const [isHeld, setIsHeld] = useState(false); |
| 82 | + |
| 83 | + const isDraggingRef = useRef<boolean>(false); |
| 84 | + const startXRef = useRef<number>(0); |
| 85 | + const startWidthRef = useRef<number>(0); |
| 86 | + |
| 87 | + useEffect(() => { |
| 88 | + if (ref.current) { |
| 89 | + const storedSize = sizeStorageKey |
| 90 | + ? parseInt(localStorage.getItem(sizeStorageKey) ?? '', 10) |
| 91 | + : undefined; |
| 92 | + |
| 93 | + ref.current.style.width = `${storedSize ?? initialSize}px`; |
| 94 | + } |
| 95 | + }, [ref, initialSize, sizeStorageKey]); |
| 96 | + |
| 97 | + const handleMouseDown = useCallback( |
| 98 | + (e: React.MouseEvent) => { |
| 99 | + setIsHeld(true); |
| 100 | + e.preventDefault(); |
| 101 | + |
| 102 | + const currentWidth = ref.current |
| 103 | + ? parseInt(getComputedStyle(ref.current).width, 10) |
| 104 | + : 0; |
| 105 | + |
| 106 | + isDraggingRef.current = true; |
| 107 | + startXRef.current = e.clientX; |
| 108 | + startWidthRef.current = currentWidth; |
| 109 | + |
| 110 | + document.body.style.cursor = 'ew-resize'; |
| 111 | + document.body.style.userSelect = 'none'; |
| 112 | + onResizeStart?.(); |
| 113 | + }, |
| 114 | + [ref, onResizeStart] |
| 115 | + ); |
| 116 | + |
| 117 | + const handleMouseMove = useCallback( |
| 118 | + (e: MouseEvent) => { |
| 119 | + if (!isDraggingRef.current) return; |
| 120 | + |
| 121 | + const deltaX = e.clientX - startXRef.current; |
| 122 | + const newWidth = Math.max( |
| 123 | + minWidth, |
| 124 | + Math.min(maxWidth, startWidthRef.current + deltaX) |
| 125 | + ); |
| 126 | + |
| 127 | + if (ref.current) { |
| 128 | + ref.current.style.width = `${newWidth}px`; |
| 129 | + } |
| 130 | + }, |
| 131 | + [ref, minWidth, maxWidth] |
| 132 | + ); |
| 133 | + |
| 134 | + const handleMouseUp = useCallback(() => { |
| 135 | + setIsHeld(false); |
| 136 | + const newSize = ref.current?.offsetWidth ?? initialSize; |
| 137 | + isDraggingRef.current = false; |
| 138 | + document.body.style.cursor = ''; |
| 139 | + document.body.style.userSelect = ''; |
| 140 | + onResizeEnd?.(newSize); |
| 141 | + if (sizeStorageKey) { |
| 142 | + localStorage.setItem(sizeStorageKey, newSize.toString()); |
| 143 | + } |
| 144 | + }, [onResizeEnd, ref, sizeStorageKey, initialSize]); |
| 145 | + |
| 146 | + useEffect(() => { |
| 147 | + document.addEventListener('mousemove', handleMouseMove); |
| 148 | + document.addEventListener('mouseup', handleMouseUp); |
| 149 | + |
| 150 | + return () => { |
| 151 | + document.removeEventListener('mousemove', handleMouseMove); |
| 152 | + document.removeEventListener('mouseup', handleMouseUp); |
| 153 | + }; |
| 154 | + }, [handleMouseMove, handleMouseUp]); |
| 155 | + |
| 156 | + const onDoubleClick = useCallback(() => { |
| 157 | + if (ref.current) { |
| 158 | + ref.current.style.width = `${initialSize}px`; |
| 159 | + } |
| 160 | + }, [ref, initialSize]); |
| 161 | + |
| 162 | + return { |
| 163 | + isHeld, |
| 164 | + size: ref.current?.offsetWidth ?? initialSize, |
| 165 | + onMouseDown: handleMouseDown, |
| 166 | + onDoubleClick, |
| 167 | + }; |
| 168 | +}; |
| 169 | + |
| 170 | +export default useResizable; |
0 commit comments