mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
28 lines
517 B
TypeScript
28 lines
517 B
TypeScript
import { useEffect } from "react";
|
|
|
|
const config: MutationObserverInit = {
|
|
subtree: true,
|
|
childList: true,
|
|
attributes: false,
|
|
characterData: false
|
|
};
|
|
|
|
export function useMutationObserver(
|
|
root: Element,
|
|
callback: MutationCallback,
|
|
options: MutationObserverInit = config
|
|
) {
|
|
|
|
useEffect(() => {
|
|
if (root) {
|
|
const observer = new MutationObserver(callback);
|
|
|
|
observer.observe(root, options);
|
|
|
|
return () => {
|
|
observer.disconnect();
|
|
};
|
|
}
|
|
}, [callback, options]);
|
|
}
|