amelioration(menu): extraction des menu dans un composant ruby pour ne pas dupliquer les changements aria partout ds la codebase
This commit is contained in:
parent
7b5aad5f47
commit
2be0f5aa99
5 changed files with 102 additions and 63 deletions
|
@ -21,38 +21,17 @@ export class MenuButtonController extends ApplicationController {
|
|||
}
|
||||
|
||||
private get isMenu() {
|
||||
return !(this.element as HTMLElement).dataset.popover;
|
||||
return this.menuTarget.getAttribute('role') == 'menu';
|
||||
}
|
||||
|
||||
private setup() {
|
||||
this.buttonTarget.setAttribute(
|
||||
'aria-haspopup',
|
||||
this.isMenu ? 'menu' : 'true'
|
||||
);
|
||||
this.buttonTarget.setAttribute('aria-controls', this.menuTarget.id);
|
||||
if (!this.buttonTarget.id) {
|
||||
this.buttonTarget.id = `${this.menuTarget.id}_button`;
|
||||
}
|
||||
|
||||
this.menuTarget.setAttribute('aria-labelledby', this.buttonTarget.id);
|
||||
this.menuTarget.setAttribute('role', this.isMenu ? 'menu' : 'region');
|
||||
// see:
|
||||
// To progressively enhance this navigation widget that is by default accessible,
|
||||
// the class to hide the menu and the inclusion of tabindex="-1" on the interactive menuitem
|
||||
// content should be added with JavaScript on load.
|
||||
this.menuTarget.classList.add('fade-in-down');
|
||||
this.menuTarget.setAttribute('tab-index', '-1');
|
||||
|
||||
if (this.isMenu) {
|
||||
for (const menuItem of this.menuTarget.querySelectorAll('a')) {
|
||||
menuItem.setAttribute('role', 'menuitem');
|
||||
}
|
||||
for (const dropdownItems of this.menuTarget.querySelectorAll(
|
||||
'.dropdown-items'
|
||||
)) {
|
||||
dropdownItems.setAttribute('role', 'none');
|
||||
}
|
||||
for (const dropdownItems of this.menuTarget.querySelectorAll(
|
||||
'.dropdown-items > li'
|
||||
)) {
|
||||
dropdownItems.setAttribute('role', 'none');
|
||||
}
|
||||
this.menuItems.map((menuItem) => menuItem.setAttribute('tabindex', '-1'));
|
||||
}
|
||||
|
||||
this.on('click', (event) => {
|
||||
|
@ -106,7 +85,7 @@ export class MenuButtonController extends ApplicationController {
|
|||
}
|
||||
|
||||
private close() {
|
||||
this.buttonTarget.removeAttribute('aria-expanded');
|
||||
this.buttonTarget.setAttribute('aria-expanded', 'false');
|
||||
this.menuTarget.parentElement?.classList.remove('open');
|
||||
this.#teardown?.();
|
||||
this.setFocusToMenuitem(null);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue