Skip to content
Snippets Groups Projects

Resolve #3480037 "Nested menus with"

Merged Jennifer Dust requested to merge issue/prototype-3480037:3480037-nested-menus-with into 5.x
Files
7
(function (Drupal) {
(function (Drupal) {
 
/**
 
* Attaches ARIA controls and data attributes to given elements.
 
*
 
* This function iterates over each element in the provided array and sets the appropriate
 
* ARIA attributes based on the element type (button or other).
 
*
 
* For buttons, it sets `aria-haspopup`, `aria-controls`, `data-menu-controls`, and `aria-label`.
 
* For other elements it sets `data-menu-controls`.
 
*
 
* `data-menu-controls` is an attribute used to support arrow keys in the MenuControl class.
 
*
 
* @param {HTMLElement[]} elements - An array of HTML elements to which ARIA controls will be attached.
 
*/
 
function attachControls(elements) {
 
elements.forEach(function (element) {
 
const id = element.getAttribute('data-plugin-id');
 
const submenu = element.nextElementSibling;
 
 
if (element.tagName.toLowerCase() === 'button') {
 
element.setAttribute('aria-haspopup', 'true');
 
if (submenu) {
 
const submenuId = `panel-${id}`;
 
submenu.setAttribute('id', submenuId);
 
element.setAttribute('aria-controls', submenuId);
 
element.setAttribute('data-menu-controls', submenuId);
 
element.setAttribute('aria-label', element.textContent.trim());
 
}
 
} else if (submenu) {
 
const submenuId = `panel-${id}`;
 
submenu.setAttribute('id', submenuId);
 
element.setAttribute('data-menu-controls', submenuId);
 
}
 
});
 
}
 
/*------------------------------------*\
/*------------------------------------*\
- 01 - Drupal Aria Controls
- 01 - Drupal Aria & Data Controls
Attach aria controls to menu items
Attach aria and data controls to menu items
\*------------------------------------*/
\*------------------------------------*/
Drupal.behaviors.ariaControls = {
Drupal.behaviors.ariaControls = {
attach: function (context) {
attach: function (context) {
@@ -9,19 +44,12 @@
@@ -9,19 +44,12 @@
// Find top level menu buttons
// Find top level menu buttons
const buttons = menu.querySelectorAll(':scope button.menu__link');
const buttons = menu.querySelectorAll(':scope button.menu__link');
// Add aria controls for buttons & related submenus
// Find menu spans for megamenu items
buttons.forEach(function (button) {
const spans = menu.querySelectorAll(':scope span.menu__link');
const id = button.getAttribute('data-plugin-id');
button.setAttribute('aria-haspopup', 'true');
const submenu = button.nextElementSibling;
// Attach controls to buttons and spans
if (submenu) {
attachControls(buttons);
const submenuId = `panel-${id}`;
attachControls(spans);
submenu.setAttribute('id', submenuId);
button.setAttribute('aria-controls', submenuId);
button.setAttribute('aria-label', button.textContent.trim());
}
});
});
});
},
},
};
};
Loading