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: close menu-bar programmatically #7102

Merged
merged 1 commit into from
Jan 24, 2024
Merged
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
5 changes: 5 additions & 0 deletions packages/menu-bar/src/vaadin-menu-bar-mixin.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ export declare class MenuBarMixinClass {
*/
openOnHover: boolean | null | undefined;

/**
* Closes the current submenu.
*/
close(): void;

protected readonly _buttons: HTMLElement[];

protected readonly _container: HTMLElement;
Expand Down
9 changes: 8 additions & 1 deletion packages/menu-bar/src/vaadin-menu-bar-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -908,11 +908,18 @@ export const MenuBarMixin = (superClass) =>
* @param {boolean} restoreFocus
* @protected
*/
_close(restoreFocus) {
_close(restoreFocus = false) {
this.style.pointerEvents = '';
this.__deactivateButton(restoreFocus);
if (this._subMenu.opened) {
this._subMenu.close();
}
}

/**
* Closes the current submenu.
*/
close() {
this._close();
}
};
9 changes: 9 additions & 0 deletions packages/menu-bar/test/overflow.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,15 @@ describe('overflow', () => {
expect(subMenu.opened).to.be.false;
});

it('should close the overflow sub-menu programmatically', async () => {
overflow.click();
await nextRender(subMenu);
expect(subMenu.opened).to.be.true;

menu.close();
expect(subMenu.opened).to.be.false;
});

it('should teleport the same component to overflow sub-menu and back', async () => {
overflow.click();
await nextRender(subMenu);
Expand Down
9 changes: 9 additions & 0 deletions packages/menu-bar/test/sub-menu.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,15 @@ describe('sub-menu', () => {
expect(spy.firstCall.args[0].detail.value).to.deep.equal({ text: 'Menu Item 1 1' });
});

it('should close sub-menu programmatically', async () => {
buttons[0].click();
await nextRender(subMenu);
expect(subMenu.opened).to.be.true;

menu.close();
expect(subMenu.opened).to.be.false;
});

it('should not close submenu on item contextmenu event', async () => {
buttons[0].click();
await nextRender(subMenu);
Expand Down