Skip to content

Commit 62e4b8b

Browse files
brabojclaude
andcommitted
fix: defer mobile ToC population to DOMContentLoaded
The inline script ran before the sidebar-toc element was in the DOM, so querySelector returned null and no links were added. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent c855fb9 commit 62e4b8b

1 file changed

Lines changed: 24 additions & 23 deletions

File tree

astro-site/src/components/Header.astro

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -92,29 +92,30 @@ const base = import.meta.env.BASE_URL;
9292
});
9393

9494
// Populate "On this page" section from the sidebar ToC
95-
const sidebarToc = document.querySelector(".sidebar-toc");
96-
if (sidebarToc) {
95+
document.addEventListener("DOMContentLoaded", () => {
96+
const sidebarToc = document.querySelector(".sidebar-toc");
97+
if (!sidebarToc) return;
9798
const topLinks = sidebarToc.querySelectorAll(":scope > ul > li");
98-
if (topLinks.length > 0) {
99-
const label = document.createElement("div");
100-
label.className = "nav-toc-label";
101-
label.textContent = "On this page";
102-
siteTabs.appendChild(label);
99+
if (topLinks.length === 0) return;
103100

104-
topLinks.forEach((li) => {
105-
const source = li.querySelector(":scope > a, :scope > details > summary > a");
106-
if (source) {
107-
const link = document.createElement("a");
108-
link.href = source.getAttribute("href");
109-
link.textContent = source.textContent;
110-
link.className = "nav-toc-link";
111-
link.addEventListener("click", () => {
112-
navToggle.setAttribute("aria-expanded", "false");
113-
siteTabs.classList.remove("open");
114-
});
115-
siteTabs.appendChild(link);
116-
}
117-
});
118-
}
119-
}
101+
const label = document.createElement("div");
102+
label.className = "nav-toc-label";
103+
label.textContent = "On this page";
104+
siteTabs.appendChild(label);
105+
106+
topLinks.forEach((li) => {
107+
const source = li.querySelector(":scope > a, :scope > details > summary > a");
108+
if (source) {
109+
const link = document.createElement("a");
110+
link.href = source.getAttribute("href");
111+
link.textContent = source.textContent;
112+
link.className = "nav-toc-link";
113+
link.addEventListener("click", () => {
114+
navToggle.setAttribute("aria-expanded", "false");
115+
siteTabs.classList.remove("open");
116+
});
117+
siteTabs.appendChild(link);
118+
}
119+
});
120+
});
120121
</script>

0 commit comments

Comments
 (0)