Skip to content

Commit 366d60a

Browse files
committed
fix: add the new node to every menu
1 parent 4dc7816 commit 366d60a

File tree

1 file changed

+38
-30
lines changed

1 file changed

+38
-30
lines changed

src/pydata_sphinx_theme/assets/scripts/pydata-sphinx-theme.js

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -353,36 +353,44 @@ if (themeSwitchBtns.length) {
353353
if (!("name" in entry)) {
354354
entry.name = entry.version;
355355
}
356-
// create the node
357-
const span = document.createElement("span");
358-
span.textContent = `${entry.name}`;
359-
360-
const node = document.createElement("a");
361-
node.setAttribute("class", "list-group-item list-group-item-action py-1");
362-
node.setAttribute("href", `${entry.url}${currentFilePath}`);
363-
node.appendChild(span);
364-
365-
// on click, AJAX calls will check if the linked page exists before
366-
// trying to redirect, and if not, will redirect to the homepage
367-
// for that version of the docs.
368-
node.onclick = checkPageExistsAndRedirect;
369-
// Add dataset values for the version and name in case people want
370-
// to apply CSS styling based on this information.
371-
node.dataset["versionName"] = entry.name;
372-
node.dataset["version"] = entry.version;
373-
374-
document.querySelector(".version-switcher__menu").append(node);
375-
// replace dropdown button text with the preferred display name of
376-
// this version, rather than using sphinx's {{ version }} variable.
377-
// also highlight the dropdown entry for the currently-viewed
378-
// version's entry
379-
if (entry.version == DOCUMENTATION_OPTIONS.version_switcher_version_match) {
380-
node.classList.add("active");
381-
themeSwitchBtns.forEach((btn) => {
382-
btn.innerText = btn.dataset["activeVersionName"] = entry.name;
383-
btn.dataset["activeVersion"] = entry.version;
384-
});
385-
}
356+
357+
document.querySelectorAll(".version-switcher__menu").forEach((menu) => {
358+
// There may be multiple version-switcher elements, e.g. one
359+
// in a slide-over panel displayed on smaller screens.
360+
// create the node
361+
const span = document.createElement("span");
362+
span.textContent = `${entry.name}`;
363+
364+
const node = document.createElement("a");
365+
node.setAttribute("class", "list-group-item list-group-item-action py-1");
366+
node.setAttribute("href", `${entry.url}${currentFilePath}`);
367+
node.appendChild(span);
368+
369+
// on click, AJAX calls will check if the linked page exists before
370+
// trying to redirect, and if not, will redirect to the homepage
371+
// for that version of the docs.
372+
node.onclick = checkPageExistsAndRedirect;
373+
// Add dataset values for the version and name in case people want
374+
// to apply CSS styling based on this information.
375+
node.dataset["versionName"] = entry.name;
376+
node.dataset["version"] = entry.version;
377+
378+
// replace dropdown button text with the preferred display name of
379+
// this version, rather than using sphinx's {{ version }} variable.
380+
// also highlight the dropdown entry for the currently-viewed
381+
// version's entry
382+
if (
383+
entry.version == DOCUMENTATION_OPTIONS.version_switcher_version_match
384+
) {
385+
node.classList.add("active");
386+
themeSwitchBtns.forEach((btn) => {
387+
btn.innerText = btn.dataset["activeVersionName"] = entry.name;
388+
btn.dataset["activeVersion"] = entry.version;
389+
});
390+
}
391+
392+
menu.append(node);
393+
});
386394
});
387395
}
388396

0 commit comments

Comments
 (0)