Skip to content

Commit

Permalink
Apply these colors based on light/dark mode (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
glenn-jocher authored Nov 21, 2024
1 parent 82eca5c commit a78ee83
Showing 1 changed file with 16 additions and 30 deletions.
46 changes: 16 additions & 30 deletions docs/overrides/javascript/extra.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,25 @@
// Light/Dark Mode -----------------------------------------------------------------------------------------------------
const applyTheme = (isDark) => {
document.body.setAttribute(
"data-md-color-scheme",
isDark ? "slate" : "default",
);
document.body.setAttribute(
"data-md-color-primary",
isDark ? "black" : "indigo",
);
// Apply theme colors based on dark/light mode -------------------------------------------------------------------------
document.body.setAttribute('data-md-color-scheme', isDark ? 'slate' : 'default');
document.body.setAttribute('data-md-color-primary', isDark ? 'black' : 'indigo');
};

// Check and apply auto theme
const checkAutoTheme = () => {
const palette = JSON.parse(localStorage.getItem(".__palette") || "{}");

if (palette.index === 0) {
applyTheme(window.matchMedia("(prefers-color-scheme: dark)").matches);
// Check and apply appropriate theme based on system/user preference
const checkTheme = () => {
const palette = JSON.parse(localStorage.getItem('.__palette') || '{}');
if (palette.index === 0) { // Auto mode is selected
applyTheme(window.matchMedia('(prefers-color-scheme: dark)').matches);
}
};

// Event listeners for theme changes
const mediaQueryList = window.matchMedia("(prefers-color-scheme: dark)");
mediaQueryList.addListener(checkAutoTheme);

// Initial theme check
checkAutoTheme();
// Watch for system theme changes
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', checkTheme);

// Auto theme input listener
document.addEventListener("DOMContentLoaded", () => {
const autoThemeInput = document.getElementById("__palette_1");
autoThemeInput?.addEventListener("click", () => {
if (autoThemeInput.checked) {
setTimeout(checkAutoTheme);
}
});
// Initialize theme handling on page load
document.addEventListener('DOMContentLoaded', () => {
document.getElementById('__palette_1')?.addEventListener('change', e =>
e.target.checked && setTimeout(checkTheme)
);
checkTheme();
});

// Inkeep --------------------------------------------------------------------------------------------------------------
Expand Down

0 comments on commit a78ee83

Please sign in to comment.