fix(theme): nav-state.js tracks wired sections in a WeakSet so enhanced navigation cannot stack toggle listeners; bump 0.5.2 (scadaproj#48)

Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
This commit is contained in:
Joseph Doherty
2026-11-09 14:56:33 -05:00
parent ab3fa9ca83
commit 5bf2f2caf1
7 changed files with 158 additions and 14 deletions
@@ -14,8 +14,21 @@
if (summary) summary.setAttribute("aria-expanded", el.open ? "true" : "false");
}
// JS-side record of wired elements. The INIT_ATTR DOM attribute is NOT a reliable guard: Blazor's
// enhanced-navigation DOM sync keeps the <details> element but strips attributes the server did
// not render, so the marker disappears while our toggle listener stays attached - and a second
// wire() would stack another listener (Theme-008). A WeakSet (or expando fallback) is untouched
// by DOM sync.
var wired = typeof WeakSet === "function" ? new WeakSet() : null;
function isWired(el) { return wired ? wired.has(el) : el.__zbnavWired === true; }
function markWired(el) {
if (wired) wired.add(el);
else el.__zbnavWired = true;
}
function wire(el) {
el.setAttribute(INIT_ATTR, "");
markWired(el);
el.setAttribute(INIT_ATTR, ""); // informational only; never read as a guard
var key = PREFIX + el.getAttribute("data-nav-key");
var saved = null;
try { saved = window.localStorage.getItem(key); } catch (e) { saved = null; }
@@ -36,7 +49,7 @@
function apply() {
document.querySelectorAll("details.rail-section[data-nav-key]").forEach(function (el) {
if (!el.hasAttribute(INIT_ATTR)) wire(el); // wire once — avoid duplicate listeners
if (!isWired(el)) wire(el); // wire once — avoid duplicate listeners
syncAria(el); // re-sync aria on every pass
});
@@ -60,7 +73,7 @@
// Re-run after Blazor static-SSR enhanced navigation (or any re-render that
// replaces the rail nodes) so freshly inserted sections are wired, restored,
// and active-revealed (issue #3). The per-element INIT_ATTR guard keeps this
// and active-revealed (issue #3). The per-element wired guard keeps this
// idempotent for nodes that survived the navigation.
if (window.Blazor && typeof window.Blazor.addEventListener === "function") {
window.Blazor.addEventListener("enhancedload", apply);
@@ -72,7 +85,7 @@
// runtime adopts the page, and `enhancedload` does NOT fire — so without this
// the live sections are never wired (no persistence, no aria sync, no
// active-reveal). A MutationObserver is the render-mode-agnostic backstop;
// the per-element INIT_ATTR guard keeps re-applies idempotent, and the
// the per-element wired guard keeps re-applies idempotent, and the
// childList-only filter (plus the active-reveal's `if (!sec.open)` guard)
// avoids any observe→mutate→observe loop (issue #6).
if (typeof MutationObserver === "function") {