|
| 1 | +// @copy https://github.com/pmndrs/react-use-measure/blob/master/src/web/index.ts |
| 2 | + |
| 3 | +import { debounce } from "lodash-es" |
| 4 | +import { useEffect, useMemo, useRef, useState } from "react" |
| 5 | + |
| 6 | +const createDebounce = debounce |
| 7 | +declare type ResizeObserverCallback = ( |
| 8 | + entries: any[], |
| 9 | + observer: ResizeObserver |
| 10 | +) => void |
| 11 | +declare class ResizeObserver { |
| 12 | + constructor(callback: ResizeObserverCallback) |
| 13 | + observe(target: Element, options?: any): void |
| 14 | + unobserve(target: Element): void |
| 15 | + disconnect(): void |
| 16 | + static toString(): string |
| 17 | +} |
| 18 | + |
| 19 | +export interface RectReadOnly { |
| 20 | + readonly x: number |
| 21 | + readonly y: number |
| 22 | + readonly width: number |
| 23 | + readonly height: number |
| 24 | + readonly top: number |
| 25 | + readonly right: number |
| 26 | + readonly bottom: number |
| 27 | + readonly left: number |
| 28 | + [key: string]: number |
| 29 | +} |
| 30 | + |
| 31 | +type HTMLOrSVGElement = HTMLElement | SVGElement |
| 32 | + |
| 33 | +type Result = [ |
| 34 | + (element: HTMLOrSVGElement | null) => void, |
| 35 | + RectReadOnly, |
| 36 | + () => void, |
| 37 | +] |
| 38 | + |
| 39 | +type State = { |
| 40 | + element: HTMLOrSVGElement | null |
| 41 | + scrollContainers: HTMLOrSVGElement[] | null |
| 42 | + resizeObserver: ResizeObserver | null |
| 43 | + lastBounds: RectReadOnly |
| 44 | +} |
| 45 | + |
| 46 | +export type Options = { |
| 47 | + debounce?: number | { scroll: number, resize: number } |
| 48 | + scroll?: boolean |
| 49 | + offsetSize?: boolean |
| 50 | +} |
| 51 | + |
| 52 | +const defaultOptions: Options = { |
| 53 | + debounce: 0, |
| 54 | + scroll: false, |
| 55 | + offsetSize: false, |
| 56 | +} |
| 57 | +export function useMeasure({ |
| 58 | + debounce, |
| 59 | + scroll, |
| 60 | + offsetSize, |
| 61 | +}: Options = defaultOptions): Result { |
| 62 | + const [bounds, set] = useState<RectReadOnly>({ |
| 63 | + left: 0, |
| 64 | + top: 0, |
| 65 | + width: 0, |
| 66 | + height: 0, |
| 67 | + bottom: 0, |
| 68 | + right: 0, |
| 69 | + x: 0, |
| 70 | + y: 0, |
| 71 | + }) |
| 72 | + |
| 73 | + // keep all state in a ref |
| 74 | + const state = useRef<State>({ |
| 75 | + element: null, |
| 76 | + scrollContainers: null, |
| 77 | + resizeObserver: null, |
| 78 | + lastBounds: bounds, |
| 79 | + }) |
| 80 | + |
| 81 | + // set actual debounce values early, so effects know if they should react accordingly |
| 82 | + const scrollDebounce = debounce ? |
| 83 | + typeof debounce === "number" ? |
| 84 | + debounce : |
| 85 | + debounce.scroll : |
| 86 | + null |
| 87 | + const resizeDebounce = debounce ? |
| 88 | + typeof debounce === "number" ? |
| 89 | + debounce : |
| 90 | + debounce.resize : |
| 91 | + null |
| 92 | + |
| 93 | + // make sure to update state only as long as the component is truly mounted |
| 94 | + const mounted = useRef(false) |
| 95 | + useEffect(() => { |
| 96 | + mounted.current = true |
| 97 | + return () => void (mounted.current = false) |
| 98 | + }) |
| 99 | + |
| 100 | + // memoize handlers, so event-listeners know when they should update |
| 101 | + const [forceRefresh, resizeChange, scrollChange] = useMemo(() => { |
| 102 | + const callback = () => { |
| 103 | + if (!state.current.element) return |
| 104 | + const { left, top, width, height, bottom, right, x, y } = |
| 105 | + state.current.element.getBoundingClientRect() as unknown as RectReadOnly |
| 106 | + |
| 107 | + const size = { |
| 108 | + left, |
| 109 | + top, |
| 110 | + width, |
| 111 | + height, |
| 112 | + bottom, |
| 113 | + right, |
| 114 | + x, |
| 115 | + y, |
| 116 | + } |
| 117 | + |
| 118 | + if (state.current.element instanceof HTMLElement && offsetSize) { |
| 119 | + size.height = state.current.element.offsetHeight |
| 120 | + size.width = state.current.element.offsetWidth |
| 121 | + } |
| 122 | + |
| 123 | + Object.freeze(size) |
| 124 | + if (mounted.current && !areBoundsEqual(state.current.lastBounds, size)) { set((state.current.lastBounds = size)) } |
| 125 | + } |
| 126 | + return [ |
| 127 | + callback, |
| 128 | + resizeDebounce ? createDebounce(callback, resizeDebounce) : callback, |
| 129 | + scrollDebounce ? createDebounce(callback, scrollDebounce) : callback, |
| 130 | + ] |
| 131 | + }, [set, offsetSize, scrollDebounce, resizeDebounce]) |
| 132 | + |
| 133 | + // cleanup current scroll-listeners / observers |
| 134 | + function removeListeners() { |
| 135 | + if (state.current.scrollContainers) { |
| 136 | + state.current.scrollContainers.forEach((element) => |
| 137 | + element.removeEventListener("scroll", scrollChange, true), |
| 138 | + ) |
| 139 | + state.current.scrollContainers = null |
| 140 | + } |
| 141 | + |
| 142 | + if (state.current.resizeObserver) { |
| 143 | + state.current.resizeObserver.disconnect() |
| 144 | + state.current.resizeObserver = null |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + // add scroll-listeners / observers |
| 149 | + function addListeners() { |
| 150 | + if (!state.current.element) return |
| 151 | + state.current.resizeObserver = new ResizeObserver(scrollChange) |
| 152 | + state.current.resizeObserver!.observe(state.current.element) |
| 153 | + if (scroll && state.current.scrollContainers) { |
| 154 | + state.current.scrollContainers.forEach((scrollContainer) => |
| 155 | + scrollContainer.addEventListener("scroll", scrollChange, { |
| 156 | + capture: true, |
| 157 | + passive: true, |
| 158 | + }), |
| 159 | + ) |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + // the ref we expose to the user |
| 164 | + const ref = (node: HTMLOrSVGElement | null) => { |
| 165 | + if (!node || node === state.current.element) return |
| 166 | + removeListeners() |
| 167 | + state.current.element = node |
| 168 | + state.current.scrollContainers = findScrollContainers(node) |
| 169 | + addListeners() |
| 170 | + } |
| 171 | + |
| 172 | + // add general event listeners |
| 173 | + useOnWindowScroll(scrollChange, Boolean(scroll)) |
| 174 | + useOnWindowResize(resizeChange) |
| 175 | + |
| 176 | + // respond to changes that are relevant for the listeners |
| 177 | + useEffect(() => { |
| 178 | + removeListeners() |
| 179 | + addListeners() |
| 180 | + }, [scroll, scrollChange, resizeChange]) |
| 181 | + |
| 182 | + // remove all listeners when the components unmounts |
| 183 | + useEffect(() => removeListeners, []) |
| 184 | + return [ref, bounds, forceRefresh] |
| 185 | +} |
| 186 | + |
| 187 | +// Adds native resize listener to window |
| 188 | +function useOnWindowResize(onWindowResize: (event: Event) => void) { |
| 189 | + useEffect(() => { |
| 190 | + const cb = onWindowResize |
| 191 | + window.addEventListener("resize", cb) |
| 192 | + return () => void window.removeEventListener("resize", cb) |
| 193 | + }, [onWindowResize]) |
| 194 | +} |
| 195 | +function useOnWindowScroll(onScroll: () => void, enabled: boolean) { |
| 196 | + useEffect(() => { |
| 197 | + if (enabled) { |
| 198 | + const cb = onScroll |
| 199 | + window.addEventListener("scroll", cb, { capture: true, passive: true }) |
| 200 | + return () => void window.removeEventListener("scroll", cb, true) |
| 201 | + } |
| 202 | + }, [onScroll, enabled]) |
| 203 | +} |
| 204 | + |
| 205 | +// Returns a list of scroll offsets |
| 206 | +function findScrollContainers( |
| 207 | + element: HTMLOrSVGElement | null, |
| 208 | +): HTMLOrSVGElement[] { |
| 209 | + const result: HTMLOrSVGElement[] = [] |
| 210 | + if (!element || element === document.body) return result |
| 211 | + const { overflow, overflowX, overflowY } = window.getComputedStyle(element) |
| 212 | + if ( |
| 213 | + [overflow, overflowX, overflowY].some( |
| 214 | + (prop) => prop === "auto" || prop === "scroll", |
| 215 | + ) |
| 216 | + ) { result.push(element) } |
| 217 | + return [...result, ...findScrollContainers(element.parentElement)] |
| 218 | +} |
| 219 | + |
| 220 | +// Checks if element boundaries are equal |
| 221 | +const keys: (keyof RectReadOnly)[] = [ |
| 222 | + "x", |
| 223 | + "y", |
| 224 | + "top", |
| 225 | + "bottom", |
| 226 | + "left", |
| 227 | + "right", |
| 228 | + "width", |
| 229 | + "height", |
| 230 | +] |
| 231 | +const areBoundsEqual = (a: RectReadOnly, b: RectReadOnly): boolean => |
| 232 | + keys.every((key) => a[key] === b[key]) |
0 commit comments