Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix - focusable button for toc expand toggle #197

Merged
merged 2 commits into from
Aug 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 28 additions & 23 deletions js/theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,32 +63,37 @@ function updateDarkMode(event, { isInitial = false } = {}) {
*/
updateDarkMode(null, { isInitial: true });

// Inject collapsible menu
function toggleCurrent(event) {
event.preventDefault();
var link = event.currentTarget.parentElement;
var element = link.parentElement;
var siblings =
element.parentElement.parentElement.querySelectorAll("li.current");
for (var i = 0; i < siblings.length; i++) {
if (siblings[i] !== element) {
siblings[i].classList.remove("current");
}
/**
* Inject collapsible menu
*/
function makeMenuExpandable() {
function toggleCurrent(event) {
const expandButton = event.currentTarget;
const element = expandButton.parentElement;
const siblings =
element.parentElement.parentElement.querySelectorAll("li.current");

siblings.forEach((sibling) => {
if (sibling !== element) {
sibling.classList.remove("current");
}
});

element.classList.toggle("current");
}
element.classList.toggle("current");
}

function makeMenuExpandable() {
var toc = document.querySelector(".toc");
var links = toc.getElementsByTagName("a");
for (var i = 0; i < links.length; i++) {
if (links[i].nextSibling) {
var expand = document.createElement("span");
expand.classList.add("toctree-expand");
expand.addEventListener("click", toggleCurrent, true);
links[i].prepend(expand);
const toc = document.querySelector(".toc");
const links = Array.from(toc.getElementsByTagName("a"));
const template = document.querySelector("[data-toggle-item-template]");
const templateChild = template.content.firstElementChild;

links.forEach((link) => {
if (link.nextSibling) {
const expandButton = templateChild.cloneNode(true);
expandButton.addEventListener("click", toggleCurrent, true);
link.before(expandButton);
}
}
});
}

makeMenuExpandable();
Expand Down
4 changes: 0 additions & 4 deletions sass/_component_header.scss
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,4 @@ header {
.navbar-dark .navbar-nav .nav-link {
color: $link-alternate-color;
}

.navbar-toggler-text {
@include sr-only;
}
}
8 changes: 6 additions & 2 deletions sass/_component_toc.scss
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
.toc {
a {
position: relative;
display: inline-block;
line-height: 1.25;
padding: ($spacer / 4) 0;
}

li {
margin: $size-1 0;

&[class^="toctree-"] {
// ensure the toctree-expand button can be positioned
lb- marked this conversation as resolved.
Show resolved Hide resolved
position: relative;
}
}

li:not(.toctree-l1) a {
Expand All @@ -23,7 +27,7 @@
position: absolute;
display: block;
top: (($spacer / 4) + ($font-size-base * 1.25 / 2));
left: -$size-6;
left: -$size-5;
transform: translate(0, -50%);
height: 1em;
width: 1em;
Expand Down
7 changes: 6 additions & 1 deletion sphinx_wagtail_theme/globaltoc.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
<div class="site-toc">
<nav class="toc mt-3" aria-label="Main menu">
<nav class="toc mt-3" aria-label="{{ _('Main menu') }}">
{{ toctree(includehidden=False, collapse=False, maxdepth=8, titles_only=True) }}
</nav>
<template data-toggle-item-template>
<button class="btn btn-sm btn-link toctree-expand" type="button">
<span class="sr-only">{{ _('Toggle menu contents') }}</span>
</button>
</template>
</div>
2 changes: 1 addition & 1 deletion sphinx_wagtail_theme/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
{% endif %}
<button class="navbar-toggler btn btn-primary d-lg-none" type="button" data-toggle="collapse" data-target="#collapseSidebar" aria-expanded="false" aria-controls="collapseExample">
<span class="navbar-toggler-icon"></span>
<span class="navbar-toggler-text">Menu</span>
<span class="sr-only">Menu</span>
</button>
</div>
</div>
Expand Down