|
| 1 | +import { useEffect, useRef, useState, type RefObject } from "react"; |
| 2 | + |
| 3 | +interface ElementSize { |
| 4 | + width: number; |
| 5 | + height: number; |
| 6 | +} |
| 7 | + |
| 8 | +interface UseElementSizeOptions { |
| 9 | + /** |
| 10 | + * Debounce interval in milliseconds. When set, the returned size only |
| 11 | + * updates at most once per interval, batching rapid resize events. |
| 12 | + * Useful for expensive downstream work (e.g. chart recreation). |
| 13 | + * Defaults to 0 (no debounce — updates on every ResizeObserver callback). |
| 14 | + */ |
| 15 | + debounce?: number; |
| 16 | +} |
| 17 | + |
| 18 | +/** |
| 19 | + * Returns the content-box size of a DOM element, kept in sync via ResizeObserver. |
| 20 | + * |
| 21 | + * Returns `null` until the element is mounted and the first observation fires. |
| 22 | + * Supports an optional `debounce` interval to throttle updates. |
| 23 | + * |
| 24 | + * @example |
| 25 | + * ```tsx |
| 26 | + * const ref = useRef<HTMLDivElement>(null); |
| 27 | + * const size = useElementSize(ref, { debounce: 100 }); |
| 28 | + * |
| 29 | + * return <div ref={ref}>{size && `${size.width} × ${size.height}`}</div>; |
| 30 | + * ``` |
| 31 | + */ |
| 32 | +export function useElementSize( |
| 33 | + ref: RefObject<HTMLElement | null>, |
| 34 | + options?: UseElementSizeOptions, |
| 35 | +): ElementSize | null { |
| 36 | + "use no memo"; // imperative observer + timer management |
| 37 | + const [size, setSize] = useState<ElementSize | null>(null); |
| 38 | + const debounceMs = options?.debounce ?? 0; |
| 39 | + const timerRef = useRef<ReturnType<typeof setTimeout> | null>(null); |
| 40 | + |
| 41 | + useEffect(() => { |
| 42 | + const el = ref.current; |
| 43 | + if (!el) { |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + const update = (width: number, height: number) => { |
| 48 | + setSize((prev) => { |
| 49 | + if (prev && prev.width === width && prev.height === height) { |
| 50 | + return prev; // avoid spurious re-renders |
| 51 | + } |
| 52 | + return { width, height }; |
| 53 | + }); |
| 54 | + }; |
| 55 | + |
| 56 | + const ro = new ResizeObserver((entries) => { |
| 57 | + const entry = entries[0]; |
| 58 | + if (!entry) { |
| 59 | + return; |
| 60 | + } |
| 61 | + const { width, height } = entry.contentRect; |
| 62 | + if (debounceMs > 0) { |
| 63 | + if (timerRef.current != null) { |
| 64 | + clearTimeout(timerRef.current); |
| 65 | + } |
| 66 | + timerRef.current = setTimeout(() => { |
| 67 | + update(width, height); |
| 68 | + timerRef.current = null; |
| 69 | + }, debounceMs); |
| 70 | + } else { |
| 71 | + update(width, height); |
| 72 | + } |
| 73 | + }); |
| 74 | + |
| 75 | + ro.observe(el); |
| 76 | + |
| 77 | + return () => { |
| 78 | + ro.disconnect(); |
| 79 | + if (timerRef.current != null) { |
| 80 | + clearTimeout(timerRef.current); |
| 81 | + timerRef.current = null; |
| 82 | + } |
| 83 | + }; |
| 84 | + }, [ref, debounceMs]); |
| 85 | + |
| 86 | + return size; |
| 87 | +} |
0 commit comments