-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Color theme switcher and automatic dark mode based on browser prefers…
…-color-scheme (#83) * Add color mode switcher * Add darkmode to syntax highlighter * yarn build
- Loading branch information
Showing
8 changed files
with
98 additions
and
5 deletions.
There are no files selected for viewing
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,50 @@ | ||
"use strict"; | ||
|
||
/** | ||
* Get user preferred theme from their past choice or browser | ||
* @returns {String} User preferred theme | ||
*/ | ||
function getPreferredTheme() { | ||
const storedTheme = localStorage.getItem("theme"); | ||
if (storedTheme) { | ||
return storedTheme; | ||
} | ||
// Firefox with 'resistFingerprint' activated always returns light | ||
return window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light"; | ||
} | ||
|
||
/** | ||
* Update navbar icon to match given theme. | ||
* @param {String} theme - 'dark' or 'light' | ||
*/ | ||
function showActiveTheme(theme) { | ||
const activeThemeIcon = document.querySelector(".theme-switch i.fas"); | ||
activeThemeIcon.classList.toggle("fa-moon", theme === "dark"); | ||
activeThemeIcon.classList.toggle("fa-sun", theme !== "dark"); | ||
} | ||
|
||
// Change body theme early to prevent flash | ||
let currentTheme = getPreferredTheme(); | ||
document.documentElement.setAttribute("data-bs-theme", currentTheme); | ||
|
||
// On browser color-scheme change, update | ||
window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change", () => { | ||
currentTheme = getPreferredTheme(); | ||
document.documentElement.setAttribute("data-bs-theme", currentTheme); | ||
showActiveTheme(currentTheme); | ||
}); | ||
|
||
window.addEventListener("load", () => { | ||
showActiveTheme(currentTheme); | ||
|
||
// On button click, switch | ||
document.querySelectorAll(".theme-switch").forEach(e => { | ||
e.addEventListener("click", ev => { | ||
currentTheme = currentTheme === "light" ? "dark" : "light"; | ||
document.documentElement.setAttribute("data-bs-theme", currentTheme); | ||
localStorage.setItem("theme", currentTheme); | ||
showActiveTheme(currentTheme); | ||
ev.preventDefault(); | ||
}); | ||
}); | ||
}); |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
static/assets/main.dad26364.css → static/assets/main.ec6ebc81.css
Large diffs are not rendered by default.
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
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
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
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