Skip to content

Commit 4ec60c9

Browse files
committed
details/summary, no checkbox
1 parent a5bef7c commit 4ec60c9

File tree

4 files changed

+43
-110
lines changed

4 files changed

+43
-110
lines changed

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

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -564,58 +564,6 @@ if (hasVersionsJSON && (hasSwitcherMenu || wantsWarningBanner)) {
564564
}
565565
}
566566

567-
/*******************************************************************************
568-
* Table of contents expand/collapse chevron aka toctree-toggle
569-
*/
570-
function setupToctreeToggle() {
571-
const handleKey = (event) => {
572-
if (
573-
!event.target.classList.contains("toctree-toggle") ||
574-
(event.key !== "Enter" && event.key !== " ")
575-
) {
576-
return;
577-
}
578-
579-
// Prevent default in any case
580-
event.preventDefault();
581-
582-
if (
583-
!(
584-
// Enter key triggers button action on keydown
585-
(
586-
(event.type === "keydown" && event.key === "Enter") ||
587-
// Space key triggers button action on keyup
588-
(event.type === "keyup" && event.key === " ")
589-
)
590-
)
591-
) {
592-
return;
593-
}
594-
595-
event.stopPropagation();
596-
const label = event.target;
597-
const forId = label.getAttribute("for");
598-
const invisibleCheckbox = document.getElementById(forId);
599-
invisibleCheckbox.checked = !invisibleCheckbox.checked;
600-
if (label.getAttribute("aria-expanded") === "true") {
601-
label.setAttribute(
602-
"aria-label",
603-
label.ariaLabel.replace("Collapse", "Expand")
604-
);
605-
label.setAttribute("aria-expanded", "false");
606-
} else {
607-
label.setAttribute(
608-
"aria-label",
609-
label.ariaLabel.replace("Expand", "Collapse")
610-
);
611-
label.setAttribute("aria-expanded", "true");
612-
}
613-
};
614-
615-
window.addEventListener("keydown", handleKey);
616-
window.addEventListener("keyup", handleKey);
617-
}
618-
619567
/*******************************************************************************
620568
* Call functions after document loading.
621569
*/
@@ -624,5 +572,4 @@ documentReady(addModeListener);
624572
documentReady(scrollToActive);
625573
documentReady(addTOCInteractivity);
626574
documentReady(setupSearchButtons);
627-
documentReady(setupToctreeToggle);
628575
documentReady(initRTDObserver);

src/pydata_sphinx_theme/assets/styles/components/_navbar-links.scss

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,32 +26,6 @@
2626
@include link-style-text;
2727
}
2828
}
29-
30-
/**
31-
* Togglable expand/collapse
32-
* This is only applicable to the primary sidebar which has these checkboxes
33-
*/
34-
.toctree-checkbox {
35-
position: absolute;
36-
display: none;
37-
}
38-
39-
.toctree-checkbox {
40-
~ ul {
41-
display: none;
42-
}
43-
~ label i {
44-
transform: rotate(0deg);
45-
}
46-
}
47-
.toctree-checkbox:checked {
48-
~ ul {
49-
display: block;
50-
}
51-
~ label i {
52-
transform: rotate(180deg);
53-
}
54-
}
5529
}
5630

5731
// Don't display the `site navigation` in the header menu

src/pydata_sphinx_theme/assets/styles/sections/_sidebar-primary.scss

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -149,19 +149,36 @@
149149
> .reference {
150150
padding-right: 30px;
151151
}
152+
153+
details {
154+
// Get rid of the default toggle icon
155+
> summary {
156+
display: flex;
157+
justify-content: space-between;
158+
list-style: none;
159+
&::-webkit-details-marker {
160+
display: none;
161+
}
162+
}
163+
164+
// The section is open, rotate the toggle icon (chevron) so it points down instead of to the right
165+
&[open] {
166+
.fa-chevron-down {
167+
transform: rotate(180deg);
168+
}
169+
}
170+
}
171+
text-align: initial;
152172
}
153173
}
154174
// Navigation item chevrons
155-
label.toctree-toggle {
156-
position: absolute;
157-
top: 0;
158-
right: 0;
175+
span.toctree-toggle {
159176
height: 30px;
160177
width: 30px;
161178

162179
cursor: pointer;
163180

164-
display: flex;
181+
display: inline-flex;
165182
justify-content: center;
166183
align-items: center;
167184

@@ -208,7 +225,8 @@ nav.bd-links {
208225
}
209226
}
210227

211-
li > a {
228+
li > a,
229+
li summary a {
212230
display: block;
213231
padding: 0.25rem 0.65rem;
214232
@include link-sidebar;
@@ -224,11 +242,9 @@ nav.bd-links {
224242
}
225243
}
226244

227-
.current {
228-
> a {
229-
@include link-sidebar-current;
230-
background-color: transparent;
231-
}
245+
a.current {
246+
@include link-sidebar-current;
247+
background-color: transparent;
232248
}
233249

234250
// Title

src/pydata_sphinx_theme/toctree.py

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -375,24 +375,34 @@ def add_collapse_checkboxes(soup: BeautifulSoup) -> None:
375375
# Add a class to indicate that this has children.
376376
element["class"] = classes + ["has-children"]
377377

378+
# Put everything inside a <details> tag
379+
details = soup.new_tag("details")
380+
details.extend(element.contents)
381+
element.append(details)
382+
383+
# Put the <a> in the <summary>
384+
summary = soup.new_tag("summary")
385+
summary.append(details.a)
386+
details.insert(0, summary)
387+
378388
# We're gonna add a checkbox.
379389
toctree_subtree_count += 1
380390
subtree_name = f"toctree-subtree-{toctree_subtree_count}"
381391
subtree["id"] = subtree_name
382-
checkbox_name = f"toctree-checkbox-{toctree_subtree_count}"
392+
# checkbox_name = f"toctree-checkbox-{toctree_subtree_count}"
383393

384394
# Add the "label" for the checkbox which will get filled.
385395
if soup.new_tag is None:
386396
continue
387397

388398
section_name = element.a.get_text()
389399
label = soup.new_tag(
390-
"label",
400+
"span",
391401
attrs={
392-
"role": "button",
393-
"for": checkbox_name,
402+
# "role": "button",
403+
# "for": checkbox_name,
394404
"class": "toctree-toggle",
395-
"tabindex": "0",
405+
# "tabindex": "0",
396406
"aria-expanded": "false",
397407
"aria-controls": subtree_name,
398408
"aria-label": f"Expand section {section_name}",
@@ -402,28 +412,14 @@ def add_collapse_checkboxes(soup: BeautifulSoup) -> None:
402412
if "toctree-l0" in classes:
403413
# making label cover the whole caption text with css
404414
label["class"] = "label-parts"
405-
element.insert(1, label)
406-
407-
# Add the checkbox that's used to store expanded/collapsed state.
408-
checkbox = soup.new_tag(
409-
"input",
410-
attrs={
411-
"type": "checkbox",
412-
"class": ["toctree-checkbox"],
413-
"id": checkbox_name,
414-
"name": checkbox_name,
415-
},
416-
)
415+
summary.append(label)
417416

418417
# if this has a "current" class, be expanded by default
419418
# (by checking the checkbox)
420419
if "current" in classes:
421-
checkbox.attrs["checked"] = ""
422420
label.attrs["aria-expanded"] = "true"
423421
label.attrs["aria-label"] = f"Collapse section {section_name}"
424422

425-
element.insert(1, checkbox)
426-
427423

428424
def get_local_toctree_for(
429425
self: TocTree, indexname: str, docname: str, builder, collapse: bool, **kwargs

0 commit comments

Comments
 (0)