Skip to content

Commit 58b6461

Browse files
authored
[6.0] Fix default menu layout - submenu stay open on page load (#46436)
* Toggle all dropdowns for active menu-item in submenu on load * Ensure aria-expanded attribute is safely set * Prevent submenu open on load for active menuitem only with class `nav-active-open` * fix: 🐛 Correct typo in preventSubmenuOpenOnload setting
1 parent d85d75c commit 58b6461

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

build/media_source/mod_menu/js/menu.es6.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
// Default settings for the Nav class
3131
static defaultSettings = {
3232
menuHoverClass: 'show-menu',
33-
dir: 'ltr'
33+
dir: 'ltr',
34+
preventSubmenuOpenOnload: 'nav-active-open'
3435
};
3536

3637
constructor(nav, settings = {}) {
@@ -79,6 +80,10 @@
7980

8081
nav.addEventListener('keydown', this.onMenuKeyDown.bind(this));
8182
nav.addEventListener('click', this.onClick.bind(this));
83+
84+
if (this.nav.classList.contains(this.settings.preventSubmenuOpenOnload)) {
85+
this.toggleAllForCurrentActive();
86+
}
8287
}
8388

8489
onMenuKeyDown(event) {
@@ -203,7 +208,7 @@
203208
ulChild.setAttribute('aria-hidden', open ? 'false' : 'true');
204209
ulChild.classList.toggle(this.settings.menuHoverClass, open);
205210
});
206-
target.querySelector(':scope > [aria-expanded]').setAttribute('aria-expanded', open ? 'true' : 'false');
211+
target.querySelector(':scope > [aria-expanded]')?.setAttribute('aria-expanded', open ? 'true' : 'false');
207212
}
208213

209214
focusTabbable(direction = 1) {
@@ -232,6 +237,22 @@
232237
}
233238
return currentLi; // top-level li or null if not found, or the
234239
}
240+
241+
toggleAllForCurrentActive() {
242+
const active = this.nav.querySelector('.current.active');
243+
if (active) {
244+
const $parentTopLevel = this.getTopLevelParentLi(active);
245+
let currentLi = active;
246+
while (currentLi && !Array.from(this.topLevelNodes).includes(currentLi)) {
247+
const parentUl = currentLi.parentElement.closest('ul');
248+
currentLi = parentUl ? parentUl.closest('li') : null;
249+
if (currentLi) {
250+
const subLists = currentLi.querySelectorAll('ul');
251+
this.toggleSubMenu(currentLi, subLists, subLists[0]?.getAttribute('aria-hidden') === 'true');
252+
}
253+
}
254+
}
255+
}
235256
}
236257

237258
// static idCounter for unique id generation of submenus

0 commit comments

Comments
 (0)