Skip to content

Commit c9361ee

Browse files
fix: update menu-bar state attributes before measuring overflow (#12844) (#12845)
Co-authored-by: Serhii Kulykov <iamkulykov@gmail.com>
1 parent 728f9e6 commit c9361ee

2 files changed

Lines changed: 32 additions & 3 deletions

File tree

‎packages/menu-bar/src/vaadin-menu-bar-mixin.js‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,20 @@ export const MenuBarMixin = (superClass) =>
472472
});
473473
}
474474

475+
/**
476+
* Restores all buttons, the overflow button and the state attributes, so that
477+
* the measurement does not use the button margins of the previous state.
478+
*
479+
* @param {!Array<!HTMLElement>} buttons
480+
* @private
481+
*/
482+
__restoreState(buttons) {
483+
this.__restoreButtons(buttons);
484+
this.__updateOverflow([]);
485+
this.toggleAttribute('has-single-button', buttons.length === 1);
486+
this.__updateVisibleAttributes(buttons);
487+
}
488+
475489
/** @private */
476490
__restoreItem(button, item) {
477491
button.appendChild(item);
@@ -596,9 +610,7 @@ export const MenuBarMixin = (superClass) =>
596610
const buttons = this._buttons.filter((btn) => btn !== overflow);
597611
const oldOverflowCount = this.__getOverflowCount(overflow);
598612

599-
// Reset all buttons in the menu bar and the overflow button
600-
this.__restoreButtons(buttons);
601-
this.__updateOverflow([]);
613+
this.__restoreState(buttons);
602614

603615
// Hide any overflowing buttons and put them in the 'overflow' button
604616
this.__setOverflowItems(buttons, overflow);
@@ -627,6 +639,11 @@ export const MenuBarMixin = (superClass) =>
627639
this._setTabindex(visibleButtons[visibleButtons.length - 1], true);
628640
}
629641

642+
this.__updateVisibleAttributes(visibleButtons);
643+
}
644+
645+
/** @private */
646+
__updateVisibleAttributes(visibleButtons) {
630647
visibleButtons.forEach((btn, index) => {
631648
btn.toggleAttribute('first-visible', index === 0);
632649
btn.toggleAttribute('last-visible', !this._hasOverflow && index === visibleButtons.length - 1);

‎packages/menu-bar/test/overflow.test.js‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,18 @@ describe('overflow', () => {
297297
expect(menu.hasAttribute('has-single-button')).to.be.false;
298298
});
299299

300+
it('should keep the buttons that fit when the width increases after all buttons were collapsed', async () => {
301+
menu.style.width = `${BUTTON_WIDTH * 0.5}px`;
302+
await nextResize(menu);
303+
304+
// Two buttons and the overflow button, each button after the first overlaps
305+
// the previous one by its 1px border. Without the attribute reset before
306+
// measuring, the buttons keep the single button margins and one more collapses.
307+
menu.style.width = `${BUTTON_WIDTH * 3 - 2}px`;
308+
await nextResize(menu);
309+
expectCollapsed(menu, [2, 3]);
310+
});
311+
300312
it('should set when theme attribute makes other buttons not fit', async () => {
301313
menu.style.width = `${BUTTON_WIDTH * 2}px`;
302314
await nextResize(menu);

0 commit comments

Comments
 (0)