@esri/calcite-components
Version:
Web Components for Esri's Calcite Design System.
41 lines (38 loc) • 1.44 kB
JavaScript
/*!
* All material copyright ESRI, All Rights Reserved, unless otherwise specified.
* See https://github.com/Esri/calcite-components/blob/master/LICENSE.md for details.
*/
import { forceUpdate } from '@stencil/core/internal/client';
import { c as createObserver } from './observers.js';
const observed = new Set();
let mutationObserver;
const observerOptions = { childList: true };
/**
* Helper to set up a conditional slot component on connectedCallback.
*/
function connectConditionalSlotComponent(component) {
if (!mutationObserver) {
mutationObserver = createObserver("mutation", processMutations);
}
mutationObserver.observe(component.el, observerOptions);
}
/**
* Helper to tear down a conditional slot component on disconnectedCallback.
*/
function disconnectConditionalSlotComponent(component) {
observed.delete(component.el);
// we explicitly process queued mutations and disconnect and reconnect
// the observer until MutationObserver gets an `unobserve` method
// see https://github.com/whatwg/dom/issues/126
processMutations(mutationObserver.takeRecords());
mutationObserver.disconnect();
for (const [element] of observed.entries()) {
mutationObserver.observe(element, observerOptions);
}
}
function processMutations(mutations) {
mutations.forEach(({ target }) => {
forceUpdate(target);
});
}
export { connectConditionalSlotComponent as c, disconnectConditionalSlotComponent as d };