Is there a way to have nav links auto collapsed on page load? #6952
-
I'd like for categories to be collapsed by default. Preferably I would be able to specify which tree of links to auto collapse and which to not. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 3 replies
-
This should be doable with customization. A bit of extra JS could simply click on the "categories" element to open it. |
Beta Was this translation helpful? Give feedback.
-
I decided to do just that... document.addEventListener('DOMContentLoaded', function() {
const itemsToClick = ['categories'];
for (const item of itemsToClick) {
const elem = [...document.querySelectorAll('nav > ul > li > label')]
.find(label => label.children[0].textContent.trim() === item);
if (elem) {
elem.click();
}
}
}); |
Beta Was this translation helpful? Give feedback.
-
Unfortunately this approach is pretty disorienting in the mobile view which makes it not worth using this imo. The preferable way to do this for me would be to just not have dropdown menus at all in desktop view. Of course, that's another question... |
Beta Was this translation helpful? Give feedback.
-
This is probably a bit jank but whatever document.addEventListener('DOMContentLoaded', function() {
const desktopViewportBreakpoint = 1219;
const itemsToExpand = ['categories'];
function expandNavItems(itemsToExpand) {
const navLabels = [...document.querySelectorAll('nav > ul > li > label')];
for (const item of itemsToExpand) {
const label = navLabels.find(l => l.children[0].textContent.trim() === item);
const expands = label.nextElementSibling;
if (label && expands && expands.getAttribute('aria-expanded') === 'false') {
label.click();
}
}
}
function handleViewportChange() {
if (window.innerWidth >= desktopViewportBreakpoint) {
expandNavItems(itemsToExpand);
}
}
handleViewportChange();
window.addEventListener('resize', handleViewportChange);
}); |
Beta Was this translation helpful? Give feedback.
This is probably a bit jank but whatever