@elastic/eui
Version:
Elastic UI Component Library
62 lines (61 loc) • 3.57 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/defineProperty";
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import { useCallback, useEffect, useRef } from 'react';
import { useObserver } from '../use_observer';
export var EuiMutationObserver = function EuiMutationObserver(_ref) {
var children = _ref.children,
onMutation = _ref.onMutation,
observerOptions = _ref.observerOptions;
// Store onMutation and observerOptions in refs so the observer callback
// and setup always use the latest prop values without needing to
// re-subscribe (which would cause the ref callback to cycle)
var onMutationRef = useRef(onMutation);
onMutationRef.current = onMutation;
var observerOptionsRef = useRef(observerOptions);
observerOptionsRef.current = observerOptions;
var mutationCallback = useCallback(function (records, observer) {
onMutationRef.current(records, observer);
}, []);
var beginObserve = useCallback(function (node) {
return makeMutationObserver(node, observerOptionsRef.current, mutationCallback);
}, [mutationCallback]);
var updateChildNode = useObserver(beginObserve, 'EuiMutationObserver');
return children(updateChildNode);
};
EuiMutationObserver.displayName = 'EuiMutationObserver';
var makeMutationObserver = function makeMutationObserver(node, _observerOptions, callback) {
// The MutationObserver polyfill used in Kibana (for Jest) implements
// an older spec in which specifying `attributeOldValue` or `attributeFilter`
// without specifying `attributes` results in a `SyntaxError`.
// The following logic patches the newer spec in which `attributes: true` can be
// implied when appropriate (`attributeOldValue` or `attributeFilter` is specified).
var observerOptions = _objectSpread({}, _observerOptions);
var needsAttributes = observerOptions.hasOwnProperty('attributeOldValue') || observerOptions.hasOwnProperty('attributeFilter');
if (needsAttributes && !observerOptions.hasOwnProperty('attributes')) {
observerOptions.attributes = true;
}
var observer = new MutationObserver(callback);
observer.observe(node, observerOptions);
return observer;
};
export var useMutationObserver = function useMutationObserver(container, callback, observerOptions) {
useEffect(function () {
if (container != null) {
var observer = makeMutationObserver(container, observerOptions, callback);
return function () {
return observer.disconnect();
};
}
},
// ignore changing observerOptions
// eslint-disable-next-line
[container, callback]);
};