forked from phoenix-oss/llama-stack-mirror
chore: Fix to persist the theme preference across page navigation. (#1974)
# What does this PR do? This PR persists the theme preference across page navigation. Currently, if the default theme is detected, it is used. But if a user flips **_the default theme_** and goes to a new page, the theme will switch back to the default. This resolves that issue. ## Test Plan [Describe the tests you ran to verify your changes with result summaries. *Provide clear instructions so the plan can be easily re-executed.*] [//]: # (## Documentation) Signed-off-by: Francisco Javier Arceo <farceo@redhat.com>
This commit is contained in:
parent
b5a9ef4c6d
commit
00b232c282
1 changed files with 26 additions and 3 deletions
29
docs/_static/js/detect_theme.js
vendored
29
docs/_static/js/detect_theme.js
vendored
|
@ -1,9 +1,32 @@
|
||||||
document.addEventListener("DOMContentLoaded", function () {
|
document.addEventListener("DOMContentLoaded", function () {
|
||||||
const prefersDark = window.matchMedia("(prefers-color-scheme: dark)").matches;
|
const prefersDark = window.matchMedia("(prefers-color-scheme: dark)").matches;
|
||||||
const htmlElement = document.documentElement;
|
const htmlElement = document.documentElement;
|
||||||
if (prefersDark) {
|
|
||||||
htmlElement.setAttribute("data-theme", "dark");
|
// Check if theme is saved in localStorage
|
||||||
|
const savedTheme = localStorage.getItem("sphinx-rtd-theme");
|
||||||
|
|
||||||
|
if (savedTheme) {
|
||||||
|
// Use the saved theme preference
|
||||||
|
htmlElement.setAttribute("data-theme", savedTheme);
|
||||||
|
document.body.classList.toggle("dark", savedTheme === "dark");
|
||||||
} else {
|
} else {
|
||||||
htmlElement.setAttribute("data-theme", "light");
|
// Fall back to system preference
|
||||||
|
const theme = prefersDark ? "dark" : "light";
|
||||||
|
htmlElement.setAttribute("data-theme", theme);
|
||||||
|
document.body.classList.toggle("dark", theme === "dark");
|
||||||
|
// Save initial preference
|
||||||
|
localStorage.setItem("sphinx-rtd-theme", theme);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Listen for theme changes from the existing toggle
|
||||||
|
const observer = new MutationObserver(function(mutations) {
|
||||||
|
mutations.forEach(function(mutation) {
|
||||||
|
if (mutation.attributeName === "data-theme") {
|
||||||
|
const currentTheme = htmlElement.getAttribute("data-theme");
|
||||||
|
localStorage.setItem("sphinx-rtd-theme", currentTheme);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
observer.observe(htmlElement, { attributes: true });
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue