-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
f85c533
commit 765a5b7
Showing
4 changed files
with
105 additions
and
119 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,8 +28,7 @@ | |
<script src="https://unpkg.com/[email protected]" integrity="sha384-ujb1lZYygJmzgSwoxRggbCHcjc0rB2XoQrxeTUQyRjrOnlCoYta87iKBWq3EsdM2" crossorigin="anonymous"></script> | ||
<!-- TODO bring this local if we like it --> | ||
<script src="https://cdn.jsdelivr.net/npm/apexcharts"></script> | ||
<!-- detect dark or light mode --> | ||
<script src="{% static 'js/DetectMode.js' %}"></script> | ||
<script src="{% static 'js/theme_handling.js' %}"></script> | ||
<script src="{% static 'js/scroll-to-link.js' %}" defer></script> | ||
<script src="{% static 'js/boost-gecko/main.062e4862.js' %}" defer></script> | ||
{% block extra_head %} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters