8000 Toggle Gurubase widget theme by bshelkhonov · Pull Request #2117 · ag2ai/faststream · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Toggle Gurubase widget theme #2117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions docs/docs/javascripts/theme-switch.js
Original file line number Diff line number Diff line change
@@ -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);
});
1 change: 1 addition & 0 deletions docs/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ extra_css:
extra_javascript:
- javascripts/extra.js
- javascripts/gurubase-widget.js
- javascripts/theme-switch.js

plugins:
- search:
Expand Down
Loading
0