Skip to content
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

feat(menu): improve focus loop - FRONT-4402 #3445

Merged
merged 2 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/implementations/vanilla/components/mega-menu/mega-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -1049,6 +1049,12 @@ export class MegaMenu {
this.toggleLabel.innerHTML = closeLabel;
}
this.positionMenuOverlay();

// Focus first element
if (this.links.length > 0) {
this.links[0].focus();
}

this.trigger('onOpen', e);
}
}
Expand Down
58 changes: 16 additions & 42 deletions src/implementations/vanilla/components/menu/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ export class Menu {
this.handleHoverOnItem = this.handleHoverOnItem.bind(this);
this.handleHoverOffItem = this.handleHoverOffItem.bind(this);
this.handleFocusIn = this.handleFocusIn.bind(this);
this.handleFocusOut = this.handleFocusOut.bind(this);
this.handleKeyboard = this.handleKeyboard.bind(this);
this.handleKeyboardGlobal = this.handleKeyboardGlobal.bind(this);
this.handleResize = this.handleResize.bind(this);
Expand Down Expand Up @@ -250,7 +249,6 @@ export class Menu {
if (this.attachFocusListener) {
link.addEventListener('focusin', this.closeOpenDropdown);
link.addEventListener('focusin', this.handleFocusIn);
link.addEventListener('focusout', this.handleFocusOut);
}
if (this.attachKeyListener) {
link.addEventListener('keyup', this.handleKeyboard);
Expand All @@ -263,7 +261,6 @@ export class Menu {
this.carets.forEach((caret) => {
if (this.attachFocusListener) {
caret.addEventListener('focusin', this.handleFocusIn);
caret.addEventListener('focusout', this.handleFocusOut);
}
if (this.attachKeyListener) {
caret.addEventListener('keyup', this.handleKeyboard);
Expand All @@ -281,9 +278,6 @@ export class Menu {
if (this.attachKeyListener && subLink) {
subLink.addEventListener('keyup', this.handleKeyboard);
}
if (this.attachFocusListener && subLink) {
subLink.addEventListener('focusout', this.handleFocusOut);
}
});
}

Expand Down Expand Up @@ -441,7 +435,6 @@ export class Menu {
if (this.attachFocusListener) {
link.removeEventListener('focusin', this.closeOpenDropdown);
link.removeEventListener('focusin', this.handleFocusIn);
link.removeEventListener('focusout', this.handleFocusOut);
}
if (this.attachKeyListener) {
link.removeEventListener('keyup', this.handleKeyboard);
Expand All @@ -453,7 +446,6 @@ export class Menu {
this.carets.forEach((caret) => {
if (this.attachFocusListener) {
caret.removeEventListener('focusin', this.handleFocusIn);
caret.removeEventListener('focusout', this.handleFocusOut);
}
if (this.attachKeyListener) {
caret.removeEventListener('keyup', this.handleKeyboard);
Expand All @@ -470,9 +462,6 @@ export class Menu {
if (this.attachKeyListener && subLink) {
subLink.removeEventListener('keyup', this.handleKeyboard);
}
if (this.attachFocusListener && subLink) {
subLink.removeEventListener('focusout', this.handleFocusOut);
}
});
}

Expand Down Expand Up @@ -983,13 +972,19 @@ export class Menu {
this.inner.setAttribute('aria-hidden', 'false');
this.disableScroll();
this.isOpen = true;
this.focusTrap.activate();

// Update label
const closeLabel = this.element.getAttribute(this.labelCloseAttribute);
if (this.toggleLabel && closeLabel) {
this.toggleLabel.innerHTML = closeLabel;
}

// Focus first element
if (this.links.length > 0) {
this.links[0].focus();
}

this.trigger('onOpen', e);

return this;
Expand Down Expand Up @@ -1168,6 +1163,16 @@ export class Menu {
}
});
this.checkMegaMenu(menuItem);

// Focus first item
console.log(menuItem);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

forgotten debug here

const firstItem = queryOne(
'.ecl-menu__subitem:first-of-type .ecl-menu__sublink',
menuItem,
);
if (firstItem) {
firstItem.focus();
}
}

/**
Expand Down Expand Up @@ -1263,37 +1268,6 @@ export class Menu {
}
}

/**
* Focus out of a menu link
* @param {Event} e
*/
handleFocusOut(e) {
const element = e.target;
const menuExpanded = this.element.getAttribute('aria-expanded');

// Specific focus action for mobile menu
// Loop through the items and go back to close button
if (menuExpanded === 'true') {
const nextItem = element.parentElement.nextSibling;

if (!nextItem) {
// There are no next menu item, but maybe there is a carret button
const caretButton = queryOne(
'.ecl-menu__button-caret',
element.parentElement,
);
if (caretButton && element !== caretButton) {
return;
}
const focusedEl = document.activeElement;
const isStillMenu = this.element.contains(focusedEl);
if (!isStillMenu) {
this.focusTrap.activate();
}
}
}
}

/**
* Handles global click events, triggered outside of the menu.
*
Expand Down
Loading