Skip to content

Commit

Permalink
fix: reset overlay position target to avoid closing after items change (
Browse files Browse the repository at this point in the history
#7702) (#7705)

Co-authored-by: Serhii Kulykov <[email protected]>
  • Loading branch information
vaadin-bot and web-padawan authored Aug 29, 2024
1 parent 3ae3d5b commit 595e499
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/menu-bar/src/vaadin-menu-bar-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,13 @@ export const MenuBarMixin = (superClass) =>
this._expandedButton = button;

requestAnimationFrame(async () => {
// After changing items, buttons are recreated so the old button is
// no longer in the DOM. Reset position target to null to prevent
// overlay from closing due to target width / height equal to 0.
if (overlay.positionTarget && !overlay.positionTarget.isConnected) {
overlay.positionTarget = null;
}

button.dispatchEvent(
new CustomEvent('opensubmenu', {
detail: {
Expand Down
16 changes: 16 additions & 0 deletions packages/menu-bar/test/sub-menu.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,22 @@ describe('sub-menu', () => {
expect(subMenu.opened).to.be.true;
});

it('should reopen sub-menu after updating items', async () => {
buttons[0].click();
await nextRender();

document.body.click();
await nextRender();

menu.items = [menu.items[0]];
await nextRender();

buttons = menu._buttons;
buttons[0].click();
await nextRender();
expect(subMenu.opened).to.be.true;
});

it('should dispatch item-selected event on leaf button click', () => {
const spy = sinon.spy();
menu.addEventListener('item-selected', spy);
Expand Down

0 comments on commit 595e499

Please sign in to comment.