From 9afbf3553b89fb11bddd56d5c675f1cc4029dc56 Mon Sep 17 00:00:00 2001 From: cosmicBboy Date: Sat, 13 Apr 2024 22:18:09 -0400 Subject: [PATCH 1/4] propagate dark/light theme to algolia search bar Signed-off-by: cosmicBboy --- docs/_static/custom.js | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/docs/_static/custom.js b/docs/_static/custom.js index be7fef0314..201653b4ad 100644 --- a/docs/_static/custom.js +++ b/docs/_static/custom.js @@ -62,6 +62,43 @@ function scrollToActive() { window.addEventListener("beforeunload", () => { sessionStorage.setItem("sidebar-scroll-top", sidebar.scrollTop); }); - } +} -document.addEventListener('DOMContentLoaded', scrollToActive); + +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) +} + + +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); From dc092aae359ff61062c789b7a971a8e66e370121 Mon Sep 17 00:00:00 2001 From: cosmicBboy Date: Sun, 14 Apr 2024 11:31:06 -0400 Subject: [PATCH 2/4] make sure escape key closes algolia searchbar Signed-off-by: cosmicBboy --- docs/_static/custom.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/docs/_static/custom.js b/docs/_static/custom.js index 201653b4ad..7b974aa3bb 100644 --- a/docs/_static/custom.js +++ b/docs/_static/custom.js @@ -102,3 +102,10 @@ function main() { } 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(); + } +}); From d9baabebab92f5d075e3802685a7690c4c578034 Mon Sep 17 00:00:00 2001 From: cosmicBboy Date: Sun, 14 Apr 2024 11:54:07 -0400 Subject: [PATCH 3/4] debug Signed-off-by: cosmicBboy --- docs/_static/custom.js | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/_static/custom.js b/docs/_static/custom.js index 7b974aa3bb..9eb6cedd5d 100644 --- a/docs/_static/custom.js +++ b/docs/_static/custom.js @@ -103,6 +103,7 @@ function main() { document.addEventListener('DOMContentLoaded', main); window.addEventListener('keydown', (event) => { + console.log(event); if (event.code === "Escape") { // make sure to prevent default behavior with escape key so that algolia // modal can be closed properly. From 3b3fedccdf1b9b220e98179d3d086067f1b00d67 Mon Sep 17 00:00:00 2001 From: cosmicBboy Date: Sun, 14 Apr 2024 12:14:41 -0400 Subject: [PATCH 4/4] remove log Signed-off-by: cosmicBboy --- docs/_static/custom.js | 1 - 1 file changed, 1 deletion(-) diff --git a/docs/_static/custom.js b/docs/_static/custom.js index 9eb6cedd5d..7b974aa3bb 100644 --- a/docs/_static/custom.js +++ b/docs/_static/custom.js @@ -103,7 +103,6 @@ function main() { document.addEventListener('DOMContentLoaded', main); window.addEventListener('keydown', (event) => { - console.log(event); if (event.code === "Escape") { // make sure to prevent default behavior with escape key so that algolia // modal can be closed properly.