app-globals-59bc5b85.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 { d as darkTheme, a as autoTheme } from './resources-9c476cb6.js';
  7. /**
  8. * Emits when the theme is dynamically toggled between light and dark on <body> or in OS preferences.
  9. */
  10. function initThemeChangeEvent() {
  11. const { classList } = document.body;
  12. const prefersDark = window.matchMedia("(prefers-color-scheme: dark)").matches;
  13. const getTheme = () => classList.contains(darkTheme) || (classList.contains(autoTheme) && prefersDark) ? "dark" : "light";
  14. const emitThemeChange = (theme) => document.body.dispatchEvent(new CustomEvent("calciteThemeChange", { bubbles: true, detail: { theme } }));
  15. const themeChangeHandler = (newTheme) => {
  16. currentTheme !== newTheme && emitThemeChange(newTheme);
  17. currentTheme = newTheme;
  18. };
  19. let currentTheme = getTheme();
  20. // emits event on page load
  21. emitThemeChange(currentTheme);
  22. // emits event when changing OS theme preferences
  23. window
  24. .matchMedia("(prefers-color-scheme: dark)")
  25. .addEventListener("change", (event) => themeChangeHandler(event.matches ? "dark" : "light"));
  26. // emits event when toggling between theme classes on <body>
  27. new MutationObserver(() => themeChangeHandler(getTheme())).observe(document.body, {
  28. attributes: true,
  29. attributeFilter: ["class"]
  30. });
  31. }
  32. /**
  33. * This file is imported in Stencil's `globalScript` config option.
  34. *
  35. * @see {@link https://stenciljs.com/docs/config#globalscript}
  36. */
  37. function appGlobalScript () {
  38. const isBrowser = typeof window !== "undefined" &&
  39. typeof location !== "undefined" &&
  40. typeof document !== "undefined" &&
  41. window.location === location &&
  42. window.document === document;
  43. if (isBrowser) {
  44. if (document.readyState === "interactive") {
  45. initThemeChangeEvent();
  46. }
  47. else {
  48. document.addEventListener("DOMContentLoaded", () => initThemeChangeEvent(), { once: true });
  49. }
  50. }
  51. }
  52. const globalScripts = appGlobalScript;
  53. export { globalScripts as g };