|
| 1 | +import type { RefObject } from 'react'; |
| 2 | +import { useEffect, useRef, useState } from 'react'; |
| 3 | + |
| 4 | +import { getElement } from '@/utils/helpers'; |
| 5 | + |
| 6 | +/** The mutation observer target element type */ |
| 7 | +export type UseMutationObserverTarget = |
| 8 | + | RefObject<Element | null | undefined> |
| 9 | + | (() => Element) |
| 10 | + | Element |
| 11 | + | Window |
| 12 | + | Document; |
| 13 | + |
| 14 | +/** The mutation observer return type */ |
| 15 | +export interface UseMutationObserverReturn { |
| 16 | + /** The mutation observer entries */ |
| 17 | + stop: () => void; |
| 18 | + /** The mutation observer instance */ |
| 19 | + observer: MutationObserver; |
| 20 | +} |
| 21 | + |
| 22 | +/** The mutation observer options type */ |
| 23 | +export interface UseMutationObserverOptions extends MutationObserverInit { |
| 24 | + /** The enabled state of the mutation observer */ |
| 25 | + enabled?: boolean; |
| 26 | +} |
| 27 | + |
| 28 | +export type UseResizeObserver = { |
| 29 | + <Target extends UseMutationObserverTarget | UseMutationObserverTarget[]>( |
| 30 | + target: Target, |
| 31 | + callback: MutationCallback, |
| 32 | + options?: UseMutationObserverOptions |
| 33 | + ): UseMutationObserverReturn; |
| 34 | + |
| 35 | + <Target extends UseMutationObserverTarget | UseMutationObserverTarget[]>( |
| 36 | + callback: MutationCallback, |
| 37 | + options?: UseMutationObserverOptions, |
| 38 | + target?: never |
| 39 | + ): UseMutationObserverReturn & { ref: (node: Target) => void }; |
| 40 | +}; |
| 41 | + |
| 42 | +/** |
| 43 | + * @name useMutationObserver |
| 44 | + * @description - Hook that gives you mutation observer state |
| 45 | + * @category Browser |
| 46 | + * |
| 47 | + * @overload |
| 48 | + * @template Target The target element |
| 49 | + * @param {MutationCallback} callback The callback to execute when mutation is detected |
| 50 | + * @param {boolean} [options.enabled=true] The enabled state of the mutation observer |
| 51 | + * @param {boolean} [options.attributes] Set to true if mutations to target's attributes are to be observed |
| 52 | + * @param {boolean} [options.characterData] Set to true if mutations to target's data are to be observed |
| 53 | + * @param {boolean} [options.childList] Set to true if mutations to target's children are to be observed |
| 54 | + * @param {boolean} [options.subtree] Set to true if mutations to not just target, but also target's descendants are to be observed |
| 55 | + * @param {boolean} [options.characterDataOldValue] Set to true if characterData is set to true or omitted and target's data before the mutation needs to be recorded |
| 56 | + * @param {boolean} [options.attributeOldValue] Set to a list of attribute local names (without namespace) if not all attribute mutations need to be observed and attributes is true or omitted |
| 57 | + * @param {string[]} [options.attributeFilter] Set to a list of attribute local names (without namespace) if not all attribute mutations need to be observed and attributes is true or omitted |
| 58 | + * @returns {UseMutationObserverReturn & { ref: (node: Target) => void }} An object containing the mutation observer state |
| 59 | + * |
| 60 | + * @example |
| 61 | + * const { ref, observer, stop } = useMutationObserver(() => console.log('callback')) |
| 62 | + * |
| 63 | + * @overload |
| 64 | + * @template Target The target element |
| 65 | + * @param {Target} target The target element to observe |
| 66 | + * @param {MutationCallback} callback The callback to execute when mutation is detected |
| 67 | + * @param {boolean} [options.enabled=true] The enabled state of the mutation observer |
| 68 | + * @param {boolean} [options.attributes] Set to true if mutations to target's attributes are to be observed |
| 69 | + * @param {boolean} [options.characterData] Set to true if mutations to target's data are to be observed |
| 70 | + * @param {boolean} [options.childList] Set to true if mutations to target's children are to be observed |
| 71 | + * @param {boolean} [options.subtree] Set to true if mutations to not just target, but also target's descendants are to be observed |
| 72 | + * @param {boolean} [options.characterDataOldValue] Set to true if characterData is set to true or omitted and target's data before the mutation needs to be recorded |
| 73 | + * @param {boolean} [options.attributeOldValue] Set to a list of attribute local names (without namespace) if not all attribute mutations need to be observed and attributes is true or omitted |
| 74 | + * @param {string[]} [options.attributeFilter] Set to a list of attribute local names (without namespace) if not all attribute mutations need to be observed and attributes is true or omitted |
| 75 | + * @returns {UseMutationObserverReturn} An object containing the mutation observer state |
| 76 | + * |
| 77 | + * @example |
| 78 | + * const { observer, stop } = useMutationObserver(ref, () => console.log('callback')) |
| 79 | + */ |
| 80 | +export const useMutationObserver = ((...params: any[]) => { |
| 81 | + const target = ( |
| 82 | + typeof params[0] === 'object' && !('current' in params[0]) ? undefined : params[0] |
| 83 | + ) as UseMutationObserverTarget | UseMutationObserverTarget[] | undefined; |
| 84 | + const callback = (target ? params[1] : params[0]) as MutationCallback; |
| 85 | + const options = (target ? params[2] : params[1]) as UseMutationObserverOptions | undefined; |
| 86 | + |
| 87 | + const [observer, setObserver] = useState<MutationObserver>(); |
| 88 | + const enabled = options?.enabled ?? true; |
| 89 | + |
| 90 | + const [internalRef, setInternalRef] = useState<Element>(); |
| 91 | + const internalCallbackRef = useRef<MutationCallback>(callback); |
| 92 | + internalCallbackRef.current = callback; |
| 93 | + |
| 94 | + useEffect(() => { |
| 95 | + if (!enabled && !target && !internalRef) return; |
| 96 | + |
| 97 | + if (Array.isArray(target)) { |
| 98 | + if (!target.length) return; |
| 99 | + const observer = new MutationObserver(internalCallbackRef.current); |
| 100 | + setObserver(observer); |
| 101 | + target.forEach((target) => { |
| 102 | + const element = getElement(target); |
| 103 | + if (!element) return; |
| 104 | + observer.observe(element as Element, options); |
| 105 | + }); |
| 106 | + |
| 107 | + return () => { |
| 108 | + observer.disconnect(); |
| 109 | + }; |
| 110 | + } |
| 111 | + |
| 112 | + const element = target ? getElement(target) : internalRef; |
| 113 | + if (!element) return; |
| 114 | + |
| 115 | + const observer = new MutationObserver(internalCallbackRef.current); |
| 116 | + setObserver(observer); |
| 117 | + observer.observe(element as Element, options); |
| 118 | + |
| 119 | + return () => { |
| 120 | + observer.disconnect(); |
| 121 | + }; |
| 122 | + }, [internalRef, target, ...Object.values(options ?? {})]); |
| 123 | + |
| 124 | + const stop = () => observer?.disconnect(); |
| 125 | + |
| 126 | + if (target) return { stop, observer }; |
| 127 | + return { |
| 128 | + ref: setInternalRef, |
| 129 | + stop, |
| 130 | + observer |
| 131 | + }; |
| 132 | +}) as UseResizeObserver; |
0 commit comments