-
-
Notifications
You must be signed in to change notification settings - Fork 17
Add ability to do nested dropdowns in navbar #291
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
Merged
Merged
Changes from 13 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
a92dec5
feat: add nested submenus in docs dropdown
alasdairwilson d2f9f94
l.i.n.t.
alasdairwilson 3484b15
moved js to static folder
alasdairwilson bba9036
fix: prevent submenu openings from persisting when dropdown reopened
alasdairwilson 90bb56f
l.i.n.t. again
alasdairwilson aabd3ca
swapped to concertina rather than infinite dropdowns
alasdairwilson a9c61b0
make dropdown nav menus scrollable
alasdairwilson 2e396da
ofc
alasdairwilson d6c691e
Explicitly include js
Cadair 7b72603
Apply suggestion from @Cadair
Cadair 317a09c
fix: moved demcmc out of tools, moved icons in from edge of dropdown
alasdairwilson 964f001
Update docs/conf.py
Cadair 06e207c
Merge branch 'main' into add-submenu-js
nabobalis 43c61d2
Apply suggestion from @nabobalis
nabobalis eb4f48f
Apply suggestion from @nabobalis
nabobalis c898e42
Update __init__.py
nabobalis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 66 additions & 29 deletions
95
src/sunpy_sphinx_theme/theme/sunpy/components/navbar_center.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,75 @@ | ||
{# When we are rendering the top nav bar (wide screens) we use bootstrap dropdowns, #} | ||
{# when we render the sidebar (narrow screens) use collapse instead. #} | ||
{# TODO: If you have a section uncollapsed and then expand the width of your screen, weird shit happens #} | ||
{% if in_header %} | ||
{% set toggle="dropdown" %} | ||
{% set list_class="dropdown-menu" %} | ||
{% else %} | ||
{% set toggle="collapse" %} | ||
{% set list_class="collapse" %} | ||
{% endif %} | ||
{% macro render_nav_item(item, depth=0) %} | ||
{# Determine item type and a unique, context-prefixed submenu ID #} | ||
{% set is_leaf = item[1] is string %} | ||
{% set is_nested = item[1] is iterable and (item[1]|length > 0) and (item[1][0] is sequence or item[1][0] is mapping) %} | ||
{% set context_prefix = 'header' if in_header else 'sidebar' %} | ||
{% set base_id = item[0]|replace(" ", "_")|lower %} | ||
{% set submenu_id = context_prefix ~ '_' ~ base_id ~ '_' ~ depth %} | ||
|
||
{# ----------------------------- LEAF LINK ----------------------------- #} | ||
{% if is_leaf %} | ||
<li class="nav-item{% if depth > 0 %} dropdown-submenu{% endif %} {% if depth == 0 %}ms-2{% endif %}"> | ||
<a class="{% if depth == 0 %}nav-link{% else %}dropdown-item{% endif %}{% if depth > 1 %} concertina-subitem{% endif %}" href="{{ sst_pathto(*item[1:]) }}"> | ||
{{ item[0] }} | ||
</a> | ||
</li> | ||
|
||
{# ----------------------------- TOP LEVEL DROPDOWN ----------------------------- #} | ||
{% elif depth == 0 and is_nested %} | ||
<li class="nav-item dropdown ms-2"> | ||
{% if in_header %} | ||
<a href="#" class="dropdown-toggle nav-link" id="dropdownMenu_{{ submenu_id }}" | ||
data-bs-toggle="dropdown" role="button" aria-expanded="false"> | ||
{{ item[0] }} | ||
</a> | ||
<ul class="dropdown-menu" aria-labelledby="dropdownMenu_{{ submenu_id }}"> | ||
{% else %} | ||
<a href="#" class="nav-link d-flex justify-content-between align-items-center" | ||
data-bs-toggle="collapse" data-bs-target="#collapse_{{ submenu_id }}" | ||
role="button" aria-expanded="false" aria-controls="collapse_{{ submenu_id }}"> | ||
{{ item[0] }} | ||
<span class="caret" aria-hidden="true">▾</span> | ||
</a> | ||
<ul class="collapse" id="collapse_{{ submenu_id }}"> | ||
{% endif %} | ||
{% for subitem in item[1] %} | ||
{{ render_nav_item(subitem, depth + 1) }} | ||
{% endfor %} | ||
</ul> | ||
</li> | ||
|
||
{# ----------------------------- NESTED CONCERTINA ----------------------------- #} | ||
{% elif depth > 0 and is_nested %} | ||
<li> | ||
<button class="dropdown-item concertina-toggle d-flex align-items-center px-0 position-relative" | ||
aria-expanded="false" | ||
aria-controls="submenu_{{ submenu_id }}"> | ||
<span class="concertina-label ps-4">{{ item[0] }}</span> | ||
<span class="concertina-icon position-absolute start-0" aria-hidden="true">▸</span> | ||
</button> | ||
<ul id="submenu_{{ submenu_id }}" class="concertina-submenu collapse"> | ||
{% for subitem in item[1] %} | ||
{{ render_nav_item(subitem, depth + 1) }} | ||
{% endfor %} | ||
</ul> | ||
</li> | ||
{% endif %} | ||
{% endmacro %} | ||
|
||
<nav class="navbar-nav"> | ||
<ul class="bd-navbar-elements navbar-nav"> | ||
{% if theme_navbar_links %} | ||
{%- for navlink in theme_navbar_links %} | ||
{% if navlink[1] is not string %} | ||
<li class="nav-item dropdown ms-2 has-children"> | ||
<a class="nav-link dropdown-toggle" href="#" role="button" id="dropdownMenuLink" data-bs-toggle="{{ toggle }}" data-bs-target="#{{ navlink[0] }}" aria-haspopup="true" aria-expanded="false">{{ navlink[0] }}<b class="caret"></b></a> | ||
<ul class="{{ list_class }}" id="{{ navlink[0] }}" aria-labelledby="dropdownMenuLink"> | ||
{%- for link in navlink[1] %} | ||
<li class="nav-item"> | ||
<a class="nav-link" href="{{ sst_pathto(*link[1:]) }}">{{ link[0] }}</a> | ||
</li> | ||
{%- endfor %} | ||
</ul> | ||
</li> | ||
{% else %} | ||
<li class="nav-item ms-2"><a class="nav-link" role="button" href="{{ sst_pathto(*navlink[1:]) }}">{{ navlink[0] }}</a></li> | ||
{% endif %} | ||
{%- endfor %} | ||
{% for navlink in theme_navbar_links %} | ||
{{ render_nav_item(navlink) }} | ||
{% endfor %} | ||
{% endif %} | ||
|
||
{% if theme_external_links %} | ||
{%- for external_link in theme_external_links %} | ||
<li class="nav-item ms-2"><a class="nav-link nav-external" role="button" href="{{ external_link['url'] }}">{{ external_link['name'] }}</a></li> | ||
{%- endfor %} | ||
{% for ext in theme_external_links %} | ||
<li class="nav-item ms-2"> | ||
<a class="nav-link nav-external" href="{{ ext['url'] }}">{{ ext['name'] }}</a> | ||
</li> | ||
{% endfor %} | ||
{% endif %} | ||
</ul> | ||
</nav> |
46 changes: 46 additions & 0 deletions
46
src/sunpy_sphinx_theme/theme/sunpy/static/js/submenu-concertina-toggle.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
document.addEventListener("DOMContentLoaded", function () { | ||
// Handle concertina toggle clicks inside dropdown | ||
document.querySelectorAll(".concertina-toggle").forEach(function (toggle) { | ||
toggle.addEventListener("click", function (e) { | ||
e.preventDefault(); | ||
e.stopPropagation(); | ||
|
||
const submenuId = toggle.getAttribute("aria-controls"); | ||
const submenu = document.getElementById(submenuId); | ||
if (!submenu) return; | ||
submenu.classList.add("collapse"); // Remove bootstraps control to ensure we can toggle | ||
const expanded = submenu.classList.contains("show"); | ||
submenu.classList.toggle("show", !expanded); | ||
submenu.classList.toggle("collapse", !expanded); | ||
toggle.setAttribute("aria-expanded", String(!expanded)); | ||
}); | ||
}); | ||
|
||
// Prevent dropdown from closing when clicking inside the concertina | ||
document.querySelectorAll(".dropdown-menu").forEach(function (menu) { | ||
menu.addEventListener("click", function (e) { | ||
if ( | ||
e.target.closest(".concertina-toggle") || | ||
e.target.closest(".concertina-submenu") | ||
) { | ||
e.stopPropagation(); | ||
} | ||
}); | ||
}); | ||
|
||
// Reset all concertinas when dropdown is hidden | ||
document.querySelectorAll(".dropdown").forEach(function (dropdown) { | ||
dropdown.addEventListener("hide.bs.dropdown", function () { | ||
dropdown | ||
.querySelectorAll(".concertina-submenu.show") | ||
.forEach(function (submenu) { | ||
submenu.classList.remove("show"); | ||
}); | ||
dropdown | ||
.querySelectorAll(".concertina-toggle") | ||
.forEach(function (toggle) { | ||
toggle.setAttribute("aria-expanded", "false"); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.