UNPKG

2.22 kBJavaScriptView Raw
1import useCustomEffect from './useCustomEffect';
2import { dequal } from 'dequal';
3import useImmediateUpdateEffect from './useImmediateUpdateEffect';
4import useEventCallback from './useEventCallback';
5import { useState } from 'react';
6function isDepsEqual([nextElement, nextConfig], [prevElement, prevConfig]) {
7 return nextElement === prevElement && dequal(nextConfig, prevConfig);
8}
9
10/**
11 * Observe mutations on a DOM node or tree of DOM nodes.
12 * Depends on the `MutationObserver` api.
13 *
14 * ```tsx
15 * const [element, attachRef] = useCallbackRef(null);
16 *
17 * useMutationObserver(element, { subtree: true }, (records) => {
18 *
19 * });
20 *
21 * return (
22 * <div ref={attachRef} />
23 * )
24 * ```
25 *
26 * @param element The DOM element to observe
27 * @param config The observer configuration
28 * @param callback A callback fired when a mutation occurs
29 */
30
31/**
32 * Observe mutations on a DOM node or tree of DOM nodes.
33 * use a `MutationObserver` and return records as the are received.
34 *
35 * ```tsx
36 * const [element, attachRef] = useCallbackRef(null);
37 *
38 * const records = useMutationObserver(element, { subtree: true });
39 *
40 * return (
41 * <div ref={attachRef} />
42 * )
43 * ```
44 *
45 * @param element The DOM element to observe
46 * @param config The observer configuration
47 */
48
49function useMutationObserver(element, config, callback) {
50 const [records, setRecords] = useState(null);
51 const handler = useEventCallback(callback || setRecords);
52 useCustomEffect(() => {
53 if (!element) return;
54
55 // The behavior around reusing mutation observers is confusing
56 // observing again _should_ disable the last listener but doesn't
57 // seem to always be the case, maybe just in JSDOM? In any case the cost
58 // to redeclaring it is gonna be fairly low anyway, so make it simple
59 const observer = new MutationObserver(handler);
60 observer.observe(element, config);
61 return () => {
62 observer.disconnect();
63 };
64 }, [element, config], {
65 isEqual: isDepsEqual,
66 // Intentionally done in render, otherwise observer will miss any
67 // changes made to the DOM during this update
68 effectHook: useImmediateUpdateEffect
69 });
70 return callback ? void 0 : records || [];
71}
72export default useMutationObserver;
\No newline at end of file