mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-12 15:19:58 +08:00
7e821bc8f6
* chore: code style optimization * fix lint * add size-limit
30 lines
753 B
TypeScript
30 lines
753 B
TypeScript
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 };
|
|
}
|