sidebar-toggle-fix.js (1725B)
1 /* Loaded only for the affected theme bundles by _exts/sidebar_toggle.py. */ 2 document.addEventListener("DOMContentLoaded", () => { 3 const sidebar = document.getElementById("pst-primary-sidebar"); 4 const drawer = document.getElementById("pst-primary-sidebar-modal"); 5 const buttons = document.querySelectorAll(".primary-toggle"); 6 7 // Leave unfamiliar layouts and the older checkbox-based theme alone. 8 if (!sidebar || !drawer || drawer.tagName !== "DIALOG" || buttons.length !== 1) { 9 return; 10 } 11 12 const button = buttons[0]; 13 const desktop = window.matchMedia("(min-width: 960px)"); 14 const collapsedClass = "taler-primary-sidebar-collapsed"; 15 16 function updateControls() { 17 const expanded = desktop.matches 18 ? !document.body.classList.contains(collapsedClass) 19 : drawer.open; 20 button.setAttribute("aria-expanded", String(expanded)); 21 button.setAttribute("aria-controls", desktop.matches ? sidebar.id : drawer.id); 22 } 23 24 // Intercept desktop clicks before the theme's mobile drawer handler. 25 document.addEventListener("click", (event) => { 26 if (!desktop.matches || !button.contains(event.target)) { 27 return; 28 } 29 event.preventDefault(); 30 event.stopPropagation(); 31 document.body.classList.toggle(collapsedClass); 32 updateControls(); 33 }, true); 34 35 desktop.addEventListener("change", () => { 36 // Let the theme restore the navigation to its desktop container. 37 if (desktop.matches && drawer.open) { 38 drawer.close(); 39 } 40 updateControls(); 41 }); 42 43 // Opening, Escape and backdrop dismissal remain handled by the theme. 44 new MutationObserver(updateControls).observe(drawer, { 45 attributes: true, 46 attributeFilter: ["open"], 47 }); 48 updateControls(); 49 });