aboutsummaryrefslogtreecommitdiff
path: root/174bg/handbook/source/nav.js
diff options
context:
space:
mode:
authorAlex Pooley (@zuedev) <zuedev@gmail.com>2026-05-30 04:08:47 +0100
committerAlex Pooley (@zuedev) <zuedev@gmail.com>2026-05-30 04:08:47 +0100
commit224234307100cec2e4d816441bd59083780d605d (patch)
treefaf494502fef887a7ded677e6c4002cc0306d884 /174bg/handbook/source/nav.js
parent1221056f0d041b240b23dffedfeea20caec17cdb (diff)
downloadunnamed-group-224234307100cec2e4d816441bd59083780d605d.tar
unnamed-group-224234307100cec2e4d816441bd59083780d605d.tar.gz
unnamed-group-224234307100cec2e4d816441bd59083780d605d.tar.bz2
unnamed-group-224234307100cec2e4d816441bd59083780d605d.tar.xz
unnamed-group-224234307100cec2e4d816441bd59083780d605d.zip
make it responsive
Diffstat (limited to '174bg/handbook/source/nav.js')
-rw-r--r--174bg/handbook/source/nav.js53
1 files changed, 53 insertions, 0 deletions
diff --git a/174bg/handbook/source/nav.js b/174bg/handbook/source/nav.js
new file mode 100644
index 0000000..d9a2ab2
--- /dev/null
+++ b/174bg/handbook/source/nav.js
@@ -0,0 +1,53 @@
+/**
+ * nav.js
+ *
+ * Toggles the off-canvas navigation drawer on small screens.
+ *
+ * How it works:
+ * 1. On small screens the sidebar (`#nav`) is positioned off-canvas and
+ * revealed by toggling its transform via the hamburger button
+ * (`#nav-toggle`).
+ * 2. A backdrop (`#nav-backdrop`) is shown while the drawer is open and
+ * closes it when clicked.
+ * 3. Following any link inside the drawer closes it on mobile so the
+ * target section is visible.
+ *
+ * On medium screens and up the drawer is always visible and these handlers
+ * have no visible effect.
+ */
+document.addEventListener("DOMContentLoaded", () => {
+ const toggle = document.getElementById("nav-toggle");
+ const nav = document.getElementById("nav");
+ const backdrop = document.getElementById("nav-backdrop");
+ if (!toggle || !nav || !backdrop) return;
+
+ const open = () => {
+ nav.classList.remove("-translate-x-full");
+ nav.classList.add("translate-x-0");
+ backdrop.classList.remove("hidden");
+ toggle.setAttribute("aria-expanded", "true");
+ };
+
+ const close = () => {
+ nav.classList.add("-translate-x-full");
+ nav.classList.remove("translate-x-0");
+ backdrop.classList.add("hidden");
+ toggle.setAttribute("aria-expanded", "false");
+ };
+
+ toggle.addEventListener("click", () => {
+ if (nav.classList.contains("-translate-x-full")) open();
+ else close();
+ });
+
+ backdrop.addEventListener("click", close);
+
+ nav.addEventListener("click", (event) => {
+ if (
+ event.target.closest("a") &&
+ window.matchMedia("(max-width: 767px)").matches
+ ) {
+ close();
+ }
+ });
+});