@esri/calcite-components
Version:
Web Components for Esri's Calcite Design System.
38 lines (37 loc) • 1.34 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";
import { createObserver } from "./observers";
const observed = new Set();
let mutationObserver;
const observerOptions = { childList: true };
/**
* Helper to set up a conditional slot component on connectedCallback.
*/
export function connectConditionalSlotComponent(component) {
if (!mutationObserver) {
mutationObserver = createObserver("mutation", processMutations);
}
mutationObserver.observe(component.el, observerOptions);
}
/**
* Helper to tear down a conditional slot component on disconnectedCallback.
*/
export 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);
});
}