From 76624ffdadb77d7aed179663d61c8c3d3a85194a Mon Sep 17 00:00:00 2001 From: Bulat Shelkhonov Date: Fri, 14 Mar 2025 23:30:43 +0300 Subject: [PATCH 1/2] Toggle Gurubase widget theme --- docs/docs/javascripts/theme-switch.js | 60 +++++++++++++++++++++++++++ docs/mkdocs.yml | 1 + 2 files changed, 61 insertions(+) create mode 100644 docs/docs/javascripts/theme-switch.js diff --git a/docs/docs/javascripts/theme-switch.js b/docs/docs/javascripts/theme-switch.js new file mode 100644 index 0000000000..596a9d5f2c --- /dev/null +++ b/docs/docs/javascripts/theme-switch.js @@ -0,0 +1,60 @@ +document.addEventListener("DOMContentLoaded", function() { + // Wait for widget to be initialized + const waitForWidget = setInterval(() => { + if (window.chatWidget) { + clearInterval(waitForWidget); + + let lastThemeChange = Date.now(); + + // Set up theme change detection + const observer = new MutationObserver((mutations) => { + + // Find the mutation where a label becomes visible (hidden attribute is removed) + const themeChange = mutations.find(mutation => + mutation.target.classList.contains("md-header__button") && + mutation.attributeName === "hidden" && + !mutation.target.hasAttribute("hidden") // Only process the label becoming visible + ); + + if (themeChange && Date.now() - lastThemeChange > 100) { + lastThemeChange = Date.now(); + window.chatWidget.switchTheme(); + } + }); + + // Find and observe both theme toggle labels + const themeToggles = document.querySelectorAll("label.md-header__button.md-icon"); + + if (themeToggles.length > 0) { + themeToggles.forEach(toggle => { + observer.observe(toggle, { + attributes: true, + attributeOldValue: true, + attributeFilter: ["hidden", "title"] + }); + }); + + // Set initial theme based on current MkDocs theme + const htmlElement = document.querySelector("body"); + const currentTheme = htmlElement.getAttribute("data-md-color-scheme"); + const widgetShouldBeDark = currentTheme === "slate"; + const widgetIsDark = !window.chatWidget.lightMode; + + // Only switch if there"s a mismatch + if (widgetShouldBeDark !== widgetIsDark) { + window.chatWidget.switchTheme(); + } + } else { + console.error("Theme toggle labels not found!"); + } + } + }, 1000); + + // Add a timeout to stop checking after 30 seconds + setTimeout(() => { + if (!window.chatWidget) { + console.error("Widget not found after 30 seconds"); + clearInterval(waitForWidget); + } + }, 30000); +}); \ No newline at end of file diff --git a/docs/mkdocs.yml b/docs/mkdocs.yml index 653b607b5a..5dd213d1df 100644 --- a/docs/mkdocs.yml +++ b/docs/mkdocs.yml @@ -68,6 +68,7 @@ extra_css: extra_javascript: - javascripts/extra.js - javascripts/gurubase-widget.js + - javascripts/theme-switch.js plugins: - search: From b6699d16c64dcbc96b9fed51d604148ba4e2a61c Mon Sep 17 00:00:00 2001 From: bshelkhonov <52068583+bshelkhonov@users.noreply.github.com> Date: Fri, 14 Mar 2025 20:41:46 +0000 Subject: [PATCH 2/2] docs: generate API References --- docs/docs/javascripts/theme-switch.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/docs/javascripts/theme-switch.js b/docs/docs/javascripts/theme-switch.js index 596a9d5f2c..22c16d1886 100644 --- a/docs/docs/javascripts/theme-switch.js +++ b/docs/docs/javascripts/theme-switch.js @@ -57,4 +57,4 @@ document.addEventListener("DOMContentLoaded", function() { clearInterval(waitForWidget); } }, 30000); -}); \ No newline at end of file +});