You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
1.4 KiB
JavaScript

// Get the theme switcher button element.
const themeSwitcher = document.querySelector(".theme-switcher");
// Retrieve theme from localStorage.
let currentTheme = localStorage.getItem("theme");
2 years ago
// Function to switch between dark and light themes.
function switchTheme() {
// Set the new theme based on the current theme.
currentTheme = currentTheme === "dark" ? "light" : "dark";
document.documentElement.setAttribute("data-theme", currentTheme);
localStorage.setItem("theme", currentTheme);
// Send a message to the Utterances iframe to change its theme.
document.querySelectorAll(".utterances-frame").forEach((frame) => {
frame.contentWindow.postMessage(
{ type: 'set-theme', theme: `github-${currentTheme}` },
'*'
);
});
2 years ago
}
// Initialize the theme switcher button.
themeSwitcher.addEventListener("click", switchTheme, false);
// Update the theme based on system preference if the user hasn't manually changed the theme.
window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change", e => {
if (!localStorage.getItem("theme")) {
currentTheme = e.matches ? "dark" : "light";
document.documentElement.setAttribute("data-theme", currentTheme);
}
});
// Fix the theme switcher button not working on the first click by updating currentTheme.
if (!currentTheme) {
currentTheme = document.documentElement.getAttribute("data-theme");
}