Skip to content

Commit b7f9de3

Browse files
committed
clean up
1 parent 4ec60c9 commit b7f9de3

File tree

1 file changed

+23
-30
lines changed

1 file changed

+23
-30
lines changed

src/pydata_sphinx_theme/toctree.py

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -355,8 +355,6 @@ def add_collapse_checkboxes(soup: BeautifulSoup) -> None:
355355
"""Add checkboxes to collapse children in a toctree."""
356356
# based on https://github.com/pradyunsg/furo
357357

358-
toctree_subtree_count = 0
359-
360358
for element in soup.find_all("li", recursive=True):
361359
# We check all "li" elements, to add a "current-page" to the correct li.
362360
classes = element.get("class", [])
@@ -365,7 +363,7 @@ def add_collapse_checkboxes(soup: BeautifulSoup) -> None:
365363
if "current" in classes:
366364
parentli = element.find_parent("li", class_="toctree-l0")
367365
if parentli:
368-
parentli.select("p.caption ~ input")[0].attrs["checked"] = ""
366+
parentli.find("details")["open"] = True
369367

370368
# Nothing more to do, unless this has "children"
371369
subtree = element.find("ul")
@@ -375,50 +373,45 @@ def add_collapse_checkboxes(soup: BeautifulSoup) -> None:
375373
# Add a class to indicate that this has children.
376374
element["class"] = classes + ["has-children"]
377375

378-
# Put everything inside a <details> tag
376+
if soup.new_tag is None:
377+
continue
378+
379+
# Modify the tree so that it looks like
380+
#
381+
# li.has-children
382+
# > details
383+
# > summary
384+
# > a.reference ~ span.toctree-toggle
385+
# > ul
386+
387+
# Create <details> and move everything under this TOC <li> entry inside it
379388
details = soup.new_tag("details")
380389
details.extend(element.contents)
381390
element.append(details)
382391

383-
# Put the <a> in the <summary>
392+
# Create <summary> and move the TOC <a> entry into it
384393
summary = soup.new_tag("summary")
385394
summary.append(details.a)
386395
details.insert(0, summary)
387396

388-
# We're gonna add a checkbox.
389-
toctree_subtree_count += 1
390-
subtree_name = f"toctree-subtree-{toctree_subtree_count}"
391-
subtree["id"] = subtree_name
392-
# checkbox_name = f"toctree-checkbox-{toctree_subtree_count}"
393-
394-
# Add the "label" for the checkbox which will get filled.
395-
if soup.new_tag is None:
396-
continue
397-
398-
section_name = element.a.get_text()
399-
label = soup.new_tag(
397+
# Create chevron icon and append to <summary>
398+
span = soup.new_tag(
400399
"span",
401400
attrs={
402-
# "role": "button",
403-
# "for": checkbox_name,
404401
"class": "toctree-toggle",
405-
# "tabindex": "0",
406-
"aria-expanded": "false",
407-
"aria-controls": subtree_name,
408-
"aria-label": f"Expand section {section_name}",
402+
"role": "presentation", # This element and the chevron it contains are purely decorative; the actual expand/collapse functionality is delegated to the <summary> tag
409403
},
410404
)
411-
label.append(soup.new_tag("i", attrs={"class": "fa-solid fa-chevron-down"}))
405+
span.append(soup.new_tag("i", attrs={"class": "fa-solid fa-chevron-down"}))
412406
if "toctree-l0" in classes:
413407
# making label cover the whole caption text with css
414-
label["class"] = "label-parts"
415-
summary.append(label)
408+
span["class"] = "label-parts"
409+
summary.append(span)
416410

417-
# if this has a "current" class, be expanded by default
418-
# (by checking the checkbox)
411+
# If this has a "current" class, be expanded by default
412+
# (by opening the details/summary disclosure widget)
419413
if "current" in classes:
420-
label.attrs["aria-expanded"] = "true"
421-
label.attrs["aria-label"] = f"Collapse section {section_name}"
414+
details["open"] = True
422415

423416

424417
def get_local_toctree_for(

0 commit comments

Comments
 (0)