From 07e416b5aba0826b2ae8b10dd84838c74b7c9394 Mon Sep 17 00:00:00 2001 From: 12rambau Date: Wed, 5 Jan 2022 13:29:39 +0000 Subject: [PATCH 01/53] docs: exclude files created by jupyter --- docs/conf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/conf.py b/docs/conf.py index cdbcf7aaa..3e75c3f28 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -62,7 +62,7 @@ # List of patterns, relative to source directory, that match files and # directories to ignore when looking for source files. # This pattern also affects html_static_path and html_extra_path. -exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"] +exclude_patterns = ["_build", "Thumbs.db", ".DS_Store", "**.ipynb_checkpoints"] html_sidebars = { "contributing": ["search-field", "custom-template"], From 932844e894702a0209df626529ebf1db6b02f015 Mon Sep 17 00:00:00 2001 From: 12rambau Date: Wed, 5 Jan 2022 15:36:11 +0000 Subject: [PATCH 02/53] set up js for theme change --- .../assets/scripts/index.js | 72 +++++++++++++++++++ .../_templates/navbar-nav.html | 5 ++ 2 files changed, 77 insertions(+) diff --git a/src/pydata_sphinx_theme/assets/scripts/index.js b/src/pydata_sphinx_theme/assets/scripts/index.js index 6eff70ce7..47e340b9c 100644 --- a/src/pydata_sphinx_theme/assets/scripts/index.js +++ b/src/pydata_sphinx_theme/assets/scripts/index.js @@ -9,6 +9,10 @@ import "bootstrap"; import "../styles/index.scss"; +//////////////////////////////////////////////////////////////////////////////// +// TOC interactivity +//////////////////////////////////////////////////////////////////////////////// + function addTOCInteractivity() { // TOC sidebar - add "active" class to parent list // @@ -31,6 +35,10 @@ function addTOCInteractivity() { }); } +//////////////////////////////////////////////////////////////////////////////// +// Scroll +//////////////////////////////////////////////////////////////////////////////// + // Navigation sidebar scrolling to active page function scrollToActive() { var sidebar = document.getElementById("bd-docs-nav"); @@ -68,7 +76,71 @@ function scrollToActive() { }); } +//////////////////////////////////////////////////////////////////////////////// +// Theme interaction +//////////////////////////////////////////////////////////////////////////////// + +function setTheme(mode) { + const prefersDark = window.matchMedia("(prefers-color-scheme: dark)").matches; + + if (mode !== "light" && mode !== "dark" && mode !== "auto") { + console.error(`Got invalid theme mode: ${mode}. Resetting to auto.`); + mode = "auto"; + } + + // change mode + var theme = mode; + if (mode == "auto") { + theme = prefersDark ? "dark" : "light"; + } + document.body.dataset.theme = theme; + + // save mode + localStorage.setItem("theme", mode); + console.log(`Changed to ${mode} mode.`); + + // change btn visibillity + const btnList = document.getElementsByClassName("theme-switch"); + Array.from(btnList).forEach((btn) => { + btn.style.display = btn.dataset.mode == mode ? "block" : "none"; + }); +} + +function cycleThemeOnce() { + const currentTheme = localStorage.getItem("theme") || "auto"; + const prefersDark = window.matchMedia("(prefers-color-scheme: dark)").matches; + + if (prefersDark) { + // Auto (dark) -> Light -> Dark + if (currentTheme === "auto") { + setTheme("light"); + } else if (currentTheme == "light") { + setTheme("dark"); + } else { + setTheme("auto"); + } + } else { + // Auto (light) -> Dark -> Light + if (currentTheme === "auto") { + setTheme("dark"); + } else if (currentTheme == "dark") { + setTheme("light"); + } else { + setTheme("auto"); + } + } +} + +function setupTheme() { + // Attach event handlers for toggling themes + const btnList = document.getElementsByClassName("theme-switch"); + Array.from(btnList).forEach((btn) => { + btn.addEventListener("click", cycleThemeOnce); + }); +} + $(document).ready(() => { + setupTheme(); scrollToActive(); addTOCInteractivity(); }); diff --git a/src/pydata_sphinx_theme/theme/pydata_sphinx_theme/_templates/navbar-nav.html b/src/pydata_sphinx_theme/theme/pydata_sphinx_theme/_templates/navbar-nav.html index 790c9b713..f89d563c0 100644 --- a/src/pydata_sphinx_theme/theme/pydata_sphinx_theme/_templates/navbar-nav.html +++ b/src/pydata_sphinx_theme/theme/pydata_sphinx_theme/_templates/navbar-nav.html @@ -1,4 +1,9 @@