From 57cdf8b0b16bf5f3c5eebecf2c7da6ede3b0701d Mon Sep 17 00:00:00 2001 From: Jey Burrows <175012+jeyrb@users.noreply.github.com> Date: Wed, 25 Nov 2026 15:43:14 +0000 Subject: [PATCH] docs: pin the left nav from 960px wide, with a button to hide it The theme only pins the navigation sidebar from 1220px, leaving it behind the hamburger menu on narrower windows, and has no setting for that or for collapsing it. A build hook moves the breakpoint to 60em in the theme's stylesheet and JavaScript, and a header button hides the sidebar, remembered between pages and visits. Co-Authored-By: Claude Opus 5.5 --- docgen/sidebar_breakpoint.py | 32 +++++++++++++++++++++++++++++++ docs/assets/stylesheets/extra.css | 23 +++++++++++++++++++--- docs/javascripts/nav_toggle.js | 29 ++++++++++++++++++++++++++++ docs/overrides/main.html | 6 ++++++ properdocs.yml | 2 ++ 5 files changed, 89 insertions(+), 3 deletions(-) create mode 100644 docgen/sidebar_breakpoint.py create mode 100644 docs/javascripts/nav_toggle.js diff --git a/docgen/sidebar_breakpoint.py b/docgen/sidebar_breakpoint.py new file mode 100644 index 0000000000..f0c0ed9bf0 --- /dev/null +++ b/docgen/sidebar_breakpoint.py @@ -0,0 +1,32 @@ +"""Pin the navigation sidebar from 60em (960px) wide, rather than the theme's 76.25em (1220px). + +The theme has no setting for this, and uses the breakpoint in its JavaScript, to position the +sidebar, as well as in its stylesheet, so both are rewritten after the build to keep them in step. +docs/javascripts/nav_toggle.js adds a button to hide the sidebar. +""" + +import logging +from pathlib import Path +from typing import Any + +_LOGGER = logging.getLogger(f"properdocs.hooks.{__name__}") + +THEME_ASSETS = ("**/assets/stylesheets/main.*.min.css", "**/assets/javascripts/bundle.*.min.js") +# the theme's last width below the breakpoint, then the breakpoint itself +REPLACEMENTS = (("76.234375em", "59.984375em"), ("76.25em", "60em")) + + +def on_post_build(config: Any, **kwargs: Any) -> None: # ruff: ignore[any-type] + site = Path(config["site_dir"]) + for pattern in THEME_ASSETS: + paths = list(site.glob(pattern)) + if not paths: + _LOGGER.warning("No theme asset matching %s, so the sidebar breakpoint is unchanged", pattern) + for path in paths: + text = path.read_text(encoding="utf-8") + if not any(old in text for old, _new in REPLACEMENTS): + _LOGGER.warning("No sidebar breakpoint found in %s, so it is unchanged", path) + continue + for old, new in REPLACEMENTS: + text = text.replace(old, new) + path.write_text(text, encoding="utf-8") diff --git a/docs/assets/stylesheets/extra.css b/docs/assets/stylesheets/extra.css index 007220c471..d3c643ac9c 100644 --- a/docs/assets/stylesheets/extra.css +++ b/docs/assets/stylesheets/extra.css @@ -1,7 +1,24 @@ -/* The theme hides the top tabs below 76.25em, leaving the other pages only behind the - hamburger menu. Keep them visible down to tablet width; the tab list scrolls sideways. */ -@media screen and (min-width: 45em) and (max-width: 76.234375em) { +/* The theme hides the top tabs below 60em (moved from 76.25em by docgen/sidebar_breakpoint.py), leaving + the other pages only behind the hamburger menu. Keep them visible down to tablet width; the tab list + scrolls sideways. */ +@media screen and (min-width: 45em) and (max-width: 59.984375em) { .md-tabs { display: block; } } + +/* The button from javascripts/nav_toggle.js, shown only where the navigation sidebar is pinned, + with the theme's class as well to outrank its rule showing every header button */ +.md-header__button.sn-nav-toggle { + display: none; +} + +@media screen and (min-width: 60em) { + .md-header__button.sn-nav-toggle { + display: inline-block; + } + + .sn-nav-collapsed .md-sidebar--primary { + display: none; + } +} diff --git a/docs/javascripts/nav_toggle.js b/docs/javascripts/nav_toggle.js new file mode 100644 index 0000000000..fa0758ff69 --- /dev/null +++ b/docs/javascripts/nav_toggle.js @@ -0,0 +1,29 @@ +// A header button to hide the navigation sidebar, pinned from 60em wide by docgen/sidebar_breakpoint.py. +// The choice is remembered between pages and visits, and applied before the page is drawn by overrides/main.html. +const NAV_COLLAPSED = "sn-nav-collapsed" + +const navToggle = document.createElement("button") +navToggle.type = "button" +navToggle.className = "md-header__button md-icon sn-nav-toggle" +navToggle.innerHTML = '' + +function labelNavToggle() { + const collapsed = document.documentElement.classList.contains(NAV_COLLAPSED) + const label = collapsed ? "Show navigation" : "Hide navigation" + navToggle.title = label + navToggle.setAttribute("aria-label", label) + navToggle.setAttribute("aria-expanded", String(!collapsed)) +} + +navToggle.addEventListener("click", () => { + const collapsed = document.documentElement.classList.toggle(NAV_COLLAPSED) + try { + localStorage.setItem(NAV_COLLAPSED, collapsed ? "1" : "0") + } catch { + // storage blocked, so the choice lasts only until the page is reloaded + } + labelNavToggle() +}) + +labelNavToggle() +document.querySelector('.md-header__inner label[for="__drawer"]')?.after(navToggle) diff --git a/docs/overrides/main.html b/docs/overrides/main.html index 6da9138e9d..4bf1803b67 100644 --- a/docs/overrides/main.html +++ b/docs/overrides/main.html @@ -14,6 +14,12 @@ {% endblock %} {% block extrahead %} + {#- Hide the navigation sidebar before the page is drawn, if hidden before, see javascripts/nav_toggle.js -#} + {#- One static social card per language, in place of the social plugin's card per page -#} {% if page %} {% set card_title = config.site_name if page.is_homepage else (page.meta.title or page.title) | striptags ~ " - " ~ config.site_name %} diff --git a/properdocs.yml b/properdocs.yml index 52c68df056..69930ece0e 100644 --- a/properdocs.yml +++ b/properdocs.yml @@ -218,11 +218,13 @@ extra_css: extra_javascript: - javascripts/markdown_actions.js - javascripts/nav_collapse.js + - javascripts/nav_toggle.js - javascripts/tag_filter.js hooks: - docgen/nav_order.py - docgen/tagged_index.py + - docgen/sidebar_breakpoint.py watch: - custom_components/supernotify