Skip to content
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
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions docgen/sidebar_breakpoint.py
Original file line number Diff line number Diff line change
@@ -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")
23 changes: 20 additions & 3 deletions docs/assets/stylesheets/extra.css
Original file line number Diff line number Diff line change
@@ -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;
}
}
29 changes: 29 additions & 0 deletions docs/javascripts/nav_toggle.js
Original file line number Diff line number Diff line change
@@ -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 = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M3 6h18v2H3zm0 5h18v2H3zm0 5h18v2H3z"/></svg>'

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)
6 changes: 6 additions & 0 deletions docs/overrides/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
{% endblock %}

{% block extrahead %}
{#- Hide the navigation sidebar before the page is drawn, if hidden before, see javascripts/nav_toggle.js -#}
<script>
try {
if (localStorage.getItem("sn-nav-collapsed") === "1") document.documentElement.classList.add("sn-nav-collapsed")
} catch {}
</script>
{#- 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 %}
Expand Down
2 changes: 2 additions & 0 deletions properdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading