mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-03 16:39:41 +08:00
6f83c63d74
* feat: New Component Watermark * docs: add watermark docs * docs: add watermark demo * test: add watermark test * test: add watermark snapshot * chore: add jest-canvas-mock * feat: Watermark calculates the width and height of content by default * docs: update docs * docs: update demo * test: update snapshot * docs: update docs * chore: update bundlesize * chore: Optimize code logic * chore: update size-limit * test: update watermark snapshot
33 lines
772 B
TypeScript
33 lines
772 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,
|
|
};
|
|
}
|