UNPKG

@elastic/eui

Version:

Elastic UI Component Library

74 lines (73 loc) 4.68 kB
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } 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; } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } /* * 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 PropTypes from "prop-types"; 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.propTypes = { /** * ReactNode to render as this component's content */ children: PropTypes.func.isRequired, onMutation: PropTypes.any.isRequired, observerOptions: PropTypes.any }; 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]); };