You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
import { useEffect, useRef } from 'react';
|
|
|
|
|
|
|
|
export default function useMutationObserver() {
|
|
|
|
const instance = useRef<MutationObserver>();
|
|
|
|
|
|
|
|
const destroyObserver = () => {
|
|
|
|
if (instance.current) {
|
|
|
|
instance.current.takeRecords();
|
|
|
|
instance.current.disconnect();
|
|
|
|
instance.current = undefined;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const createObserver = (target: Node, callback: MutationCallback) => {
|
|
|
|
if (MutationObserver) {
|
|
|
|
destroyObserver();
|
|
|
|
instance.current = new MutationObserver(callback);
|
|
|
|
instance.current.observe(target, {
|
|
|
|
childList: true,
|
|
|
|
subtree: true,
|
|
|
|
attributeFilter: ['style', 'class'],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => destroyObserver, []);
|
|
|
|
|
|
|
|
return { createObserver, destroyObserver };
|
|
|
|
}
|