useMutateObserver.js 1017 B

12345678910111213141516171819202122232425262728
  1. import canUseDom from "rc-util/es/Dom/canUseDom";
  2. import * as React from 'react';
  3. var defaultOptions = {
  4. subtree: true,
  5. childList: true,
  6. attributeFilter: ['style', 'class']
  7. };
  8. export default function useMutateObserver(nodeOrList, callback) {
  9. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : defaultOptions;
  10. React.useEffect(function () {
  11. if (!canUseDom() || !nodeOrList) {
  12. return;
  13. }
  14. var instance;
  15. var nodeList = Array.isArray(nodeOrList) ? nodeOrList : [nodeOrList];
  16. if ('MutationObserver' in window) {
  17. instance = new MutationObserver(callback);
  18. nodeList.forEach(function (element) {
  19. instance.observe(element, options);
  20. });
  21. }
  22. return function () {
  23. var _instance, _instance2;
  24. (_instance = instance) === null || _instance === void 0 ? void 0 : _instance.takeRecords();
  25. (_instance2 = instance) === null || _instance2 === void 0 ? void 0 : _instance2.disconnect();
  26. };
  27. }, [options, nodeOrList]);
  28. }