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

fix(menu): allow keyboard events to propagate from opened menu #4793

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
8 changes: 7 additions & 1 deletion packages/menu/src/Menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -590,21 +590,27 @@ export class Menu extends SizedMixin(SpectrumElement, { noDefaultSize: true }) {

protected navigateBetweenRelatedMenus(event: KeyboardEvent): void {
const { key } = event;
event.stopPropagation();

if (key === 'ArrowRight' || key === 'ArrowLeft') {
event.preventDefault();
}

const shouldOpenSubmenu =
(this.isLTR && key === 'ArrowRight') ||
(!this.isLTR && key === 'ArrowLeft');
const shouldCloseSelfAsSubmenu =
(this.isLTR && key === 'ArrowLeft') ||
(!this.isLTR && key === 'ArrowRight');
if (shouldOpenSubmenu) {
event.stopPropagation();
const lastFocusedItem = this.childItems[this.focusedItemIndex];
if (lastFocusedItem?.hasSubmenu) {
// Remove focus while opening overlay from keyboard or the visible focus
// will slip back to the first item in the menu.
lastFocusedItem.openOverlay();
}
} else if (shouldCloseSelfAsSubmenu && this.isSubmenu) {
event.stopPropagation();
this.dispatchEvent(new Event('close', { bubbles: true }));
this.updateSelectedItemIndex();
}
Expand Down
32 changes: 32 additions & 0 deletions packages/menu/test/menu.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,38 @@ describe('Menu', () => {
el.dispatchEvent(arrowDownEvent());
expect(secondItem.focused, 'second').to.be.true;
});

it('allows keyboard events to bubble up', async () => {
const el = await fixture<Menu>(html`
<sp-menu id="test">
<sp-menu-item class="first">Deselect</sp-menu-item>
<sp-menu-item>Invert Selection</sp-menu-item>
<sp-menu-item>Feather...</sp-menu-item>
<sp-menu-item>Select and Mask...</sp-menu-item>
<sp-menu-item selected class="selected">
Save Selection
</sp-menu-item>
</sp-menu>
`);

await elementUpdated(el);
await nextFrame();
el.focus();

expect(document.activeElement).to.equal(el);

// Add a listener to the document to check if the event bubbles up
const eventSpy = spy();
document.addEventListener('keydown', eventSpy);

// press a key
await sendKeys({
press: 'S',
});

expect(eventSpy.callCount).to.equal(1);
});

it('handles focus across focused MenuItem removals', async () => {
const el = await fixture<Menu>(html`
<sp-menu id="test">
Expand Down
Loading