conditionalSlot.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /*!
  2. * All material copyright ESRI, All Rights Reserved, unless otherwise specified.
  3. * See https://github.com/Esri/calcite-components/blob/master/LICENSE.md for details.
  4. * v1.0.0-beta.97
  5. */
  6. import { forceUpdate } from '@stencil/core/internal/client/index.js';
  7. import { c as createObserver } from './observers.js';
  8. const observed = new Set();
  9. let mutationObserver;
  10. const observerOptions = { childList: true };
  11. /**
  12. * Helper to set up a conditional slot component on connectedCallback.
  13. *
  14. * @param component
  15. */
  16. function connectConditionalSlotComponent(component) {
  17. if (!mutationObserver) {
  18. mutationObserver = createObserver("mutation", processMutations);
  19. }
  20. mutationObserver.observe(component.el, observerOptions);
  21. }
  22. /**
  23. * Helper to tear down a conditional slot component on disconnectedCallback.
  24. *
  25. * @param component
  26. */
  27. function disconnectConditionalSlotComponent(component) {
  28. observed.delete(component.el);
  29. // we explicitly process queued mutations and disconnect and reconnect
  30. // the observer until MutationObserver gets an `unobserve` method
  31. // see https://github.com/whatwg/dom/issues/126
  32. processMutations(mutationObserver.takeRecords());
  33. mutationObserver.disconnect();
  34. for (const [element] of observed.entries()) {
  35. mutationObserver.observe(element, observerOptions);
  36. }
  37. }
  38. function processMutations(mutations) {
  39. mutations.forEach(({ target }) => {
  40. forceUpdate(target);
  41. });
  42. }
  43. export { connectConditionalSlotComponent as c, disconnectConditionalSlotComponent as d };