diff --git a/static/js/DetectMode.js b/static/js/DetectMode.js deleted file mode 100644 index 9a3716e0..00000000 --- a/static/js/DetectMode.js +++ /dev/null @@ -1,16 +0,0 @@ -let explicitelyPreferScheme = false; - -if (window.localStorage) { - if (localStorage.getItem('colorMode') === 'dark') { - document.documentElement.classList.add('dark'); - explicitelyPreferScheme = 'dark'; - } else if (localStorage.getItem('colorMode') === 'light') { - document.documentElement.classList.remove('dark'); - explicitelyPreferScheme = 'light'; - } -} - -if (explicitelyPreferScheme !== 'light' && window.matchMedia('(prefers-color-scheme:dark)').matches) { - localStorage.colorMode = 'dark'; // explicitly set `colorMode` to system if there is no preference. this fixes charts and search default mode in Firefox. - document.documentElement.classList.add('dark'); -} diff --git a/static/js/theme_handling.js b/static/js/theme_handling.js new file mode 100644 index 00000000..aa7bf6ee --- /dev/null +++ b/static/js/theme_handling.js @@ -0,0 +1,64 @@ +/* +this file contains any theme related code that can be removed from the header/base, +they should only be placed there when performance is needed. +*/ + +function changeTheme() { + const storedColorMode = localStorage.getItem("colorMode"); + const browserColorMode = getBrowserColorMode(window); + let newColorMode = null; + if (storedColorMode) { + newColorMode = storedColorMode === "dark" ? "light" : "dark"; + } else { + newColorMode = browserColorMode === "dark" ? "light" : "dark"; + } + saveColorMode(newColorMode); // triggers theme change via storage event +} + +function saveColorMode(colorMode) { + const oldColorMode = localStorage.getItem("colorMode"); + const allowedModes = ['light', 'dark']; + if (!allowedModes.includes(colorMode)) { + if (console) { + console.warn(`Invalid color mode: ${colorMode}`); + } + return; + } + localStorage.setItem("colorMode", colorMode); + // this is dumb but only on pages that AREN'T the current page do normal + // localStorage events get picked up, so they need to be triggered manually here. + // To prevent errors from triggering twice actions should be idempotent. + window.dispatchEvent( + new StorageEvent('storage', { + key: 'colorMode', + oldValue: oldColorMode, + newValue: colorMode, + }) + ); +} + +// here we handle the picking up of the change in color mode from the storage event +// which is then handled differently for iframes with very slight delay for the +// parent so there's minimal disparity between iframe and parent changing +window.addEventListener('storage', function (e) { + if (e.key === 'colorMode' && e.newValue) { + let isIframe = window.location.pathname === "srcdoc"; + if (isIframe) { + setColorElements(e.newValue); + } + else { + setTimeout(() => setColorElements(e.newValue), 1); + } + document.getElementById("gecko-search-button").setAttribute( + 'data-theme-mode', + e.newValue + ); + } +}); + +document.addEventListener("alpine:init", function() { + document.getElementById("gecko-search-button").setAttribute( + 'data-theme-mode', + localStorage.getItem("colorMode") || getBrowserColorMode(window) + ); +}); diff --git a/templates/base.html b/templates/base.html index 3e397057..9503eb53 100644 --- a/templates/base.html +++ b/templates/base.html @@ -28,8 +28,7 @@ - - + {% block extra_head %} diff --git a/templates/includes/_header.html b/templates/includes/_header.html index b85cbd3d..940a55d5 100644 --- a/templates/includes/_header.html +++ b/templates/includes/_header.html @@ -391,10 +391,6 @@