Skip to content
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

propagate dark/light theme to algolia search bar #5231

Merged
merged 4 commits into from
Apr 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 46 additions & 2 deletions docs/_static/custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,50 @@ function scrollToActive() {
window.addEventListener("beforeunload", () => {
sessionStorage.setItem("sidebar-scroll-top", sidebar.scrollTop);
});
}
}


function setHtmlDataTheme() {
// Set theme at the root html element
setTimeout(() => {
const theme = document.body.dataset.theme;
const prefersDark = window.matchMedia("(prefers-color-scheme: dark)").matches;

if (theme === "auto") {
document.documentElement.dataset.theme = prefersDark ? "dark" : "light";
} else {
document.documentElement.dataset.theme = theme;
}
}, 10)
}


document.addEventListener('DOMContentLoaded', scrollToActive);
function setupAlgoliaTheme() {
// To get darkmode in the algolia search modal, we need to set the theme in
// the root html element. This function propagates the theme set by furo
// that's set in the body element.
const buttons = document.getElementsByClassName("theme-toggle");

// set for initial document load
setHtmlDataTheme();

// listen for when theme button is clicked.
Array.from(buttons).forEach((btn) => {
btn.addEventListener("click", setHtmlDataTheme);
});
}


function main() {
scrollToActive()
setupAlgoliaTheme()
}

document.addEventListener('DOMContentLoaded', main);
window.addEventListener('keydown', (event) => {
if (event.code === "Escape") {
// make sure to prevent default behavior with escape key so that algolia
// modal can be closed properly.
event.preventDefault();
}
});
Loading