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

refactor(menu): focus before animation #5663

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
24 changes: 12 additions & 12 deletions menu/internal/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -596,41 +596,41 @@ export abstract class Menu extends LitElement {

let animationAborted = !this.quick;

if (this.quick) {
this.dispatchEvent(new Event('opening'));
} else {
animationAborted = !!(await this.animateOpen());
}

// This must come after the opening animation or else it may focus one of
// the items before the animation has begun and causes the list to slide
// (block-padding-of-the-menu)px at the end of the animation
// Focusing one of the items before the animation has begun causes the list
// to slide (block-padding-of-the-menu)px at the end of the animation.
// Setting preventScroll avoids this.
switch (this.defaultFocus) {
case FocusState.FIRST_ITEM:
const first = getFirstActivatableItem(items);
if (first) {
first.tabIndex = 0;
first.focus();
first.focus({ preventScroll: true });
await (first as LitElement & MenuItem).updateComplete;
}
break;
case FocusState.LAST_ITEM:
const last = getLastActivatableItem(items);
if (last) {
last.tabIndex = 0;
last.focus();
last.focus({ preventScroll: true });
await (last as LitElement & MenuItem).updateComplete;
}
break;
case FocusState.LIST_ROOT:
this.focus();
this.focus({ preventScroll: true });
break;
default:
case FocusState.NONE:
// Do nothing.
break;
}

if (this.quick) {
this.dispatchEvent(new Event('opening'));
} else {
animationAborted = !!(await this.animateOpen());
}

if (!animationAborted) {
this.dispatchEvent(new Event('opened'));
}
Expand Down
4 changes: 2 additions & 2 deletions menu/internal/menuitem/menu-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,9 @@ export class MenuItemEl extends LitElement implements MenuItem {
`;
}

override focus() {
override focus(options?: FocusOptions) {
// TODO(b/300334509): needed for some cases where delegatesFocus doesn't
// work programmatically like in FF and select-option
this.listItemRoot?.focus();
this.listItemRoot?.focus(options);
}
}
Loading