diff --git a/docs/_static/js/detect_theme.js b/docs/_static/js/detect_theme.js index 484b2bb8b..712565ef7 100644 --- a/docs/_static/js/detect_theme.js +++ b/docs/_static/js/detect_theme.js @@ -1,9 +1,32 @@ document.addEventListener("DOMContentLoaded", function () { const prefersDark = window.matchMedia("(prefers-color-scheme: dark)").matches; 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 { - 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 }); });