Skip to content

Commit

Permalink
fix: postpone menu-bar initial render to prevent re-layout (#7312)
Browse files Browse the repository at this point in the history
  • Loading branch information
web-padawan authored and vaadin-bot committed Apr 11, 2024
1 parent f86e0ec commit 184e277
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 9 deletions.
16 changes: 14 additions & 2 deletions integration/tests/component-relayout-page.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import { expect } from '@esm-bundle/chai';
import { fixtureSync, nextRender } from '@vaadin/testing-helpers';
import { ContextMenu } from '@vaadin/context-menu';

[{ tagName: ContextMenu.is }].forEach(({ tagName }) => {
import { MenuBar } from '@vaadin/menu-bar';

[
{ tagName: ContextMenu.is },
{
tagName: MenuBar.is,
callback: (el) => {
el.items = [{ text: 'Item' }];
},
},
].forEach(({ tagName, callback }) => {
describe(`${tagName} re-layout`, () => {
let wrapper;

Expand All @@ -16,6 +25,9 @@ import { ContextMenu } from '@vaadin/context-menu';
}

wrapper.appendChild(document.createElement(tagName));
if (callback) {
callback(wrapper.firstElementChild);
}

for (let i = 0; i < 100; i++) {
const btn = document.createElement('button');
Expand Down
11 changes: 10 additions & 1 deletion packages/menu-bar/src/vaadin-menu-bar-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,12 @@ export const MenuBarMixin = (superClass) =>
container.addEventListener('click', this.__onButtonClick.bind(this));
container.addEventListener('mouseover', (e) => this._onMouseOver(e));

this._container = container;
// Delay setting container to avoid rendering buttons immediately,
// which would also trigger detecting overflow and force re-layout
// See https://github.com/vaadin/web-components/issues/7271
queueMicrotask(() => {
this._container = container;
});
}

/**
Expand Down Expand Up @@ -444,6 +449,10 @@ export const MenuBarMixin = (superClass) =>

/** @private */
__detectOverflow() {
if (!this._container) {
return;
}

const overflow = this._overflow;
const buttons = this._buttons.filter((btn) => btn !== overflow);
const oldOverflowCount = this.__getOverflowCount(overflow);
Expand Down
12 changes: 9 additions & 3 deletions packages/menu-bar/test/visual/lumo/menu-bar.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ describe('menu-bar', () => {
});

describe('basic', () => {
beforeEach(() => {
beforeEach(async () => {
div = document.createElement('div');
div.style.padding = '10px';

element = fixtureSync('<vaadin-menu-bar></vaadin-menu-bar>', div);
await nextRender();

element.items = [
{ text: 'Home' },
{
Expand All @@ -47,11 +49,13 @@ describe('menu-bar', () => {
});

describe('single button', () => {
beforeEach(() => {
beforeEach(async () => {
div = document.createElement('div');
div.style.padding = '10px';

element = fixtureSync('<vaadin-menu-bar></vaadin-menu-bar>', div);
await nextRender();

element.items = [{ text: 'Actions' }];
});

Expand All @@ -77,11 +81,13 @@ describe('menu-bar', () => {
return item;
}

beforeEach(() => {
beforeEach(async () => {
div = document.createElement('div');
div.style.padding = '10px';

element = fixtureSync('<vaadin-menu-bar></vaadin-menu-bar>', div);
await nextRender();

element.items = [
{ component: 'u', text: 'Home' },
{
Expand Down
12 changes: 9 additions & 3 deletions packages/menu-bar/test/visual/material/menu-bar.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ describe('menu-bar', () => {
});

describe('basic', () => {
beforeEach(() => {
beforeEach(async () => {
div = document.createElement('div');
div.style.padding = '10px';

element = fixtureSync('<vaadin-menu-bar></vaadin-menu-bar>', div);
await nextRender();

element.items = [
{ text: 'Home' },
{
Expand All @@ -47,11 +49,13 @@ describe('menu-bar', () => {
});

describe('single button', () => {
beforeEach(() => {
beforeEach(async () => {
div = document.createElement('div');
div.style.padding = '10px';

element = fixtureSync('<vaadin-menu-bar></vaadin-menu-bar>', div);
await nextRender();

element.items = [{ text: 'Actions' }];
element.setAttribute('theme', 'outlined');
});
Expand All @@ -78,11 +82,13 @@ describe('menu-bar', () => {
return item;
}

beforeEach(() => {
beforeEach(async () => {
div = document.createElement('div');
div.style.padding = '10px';

element = fixtureSync('<vaadin-menu-bar></vaadin-menu-bar>', div);
await nextRender();

element.items = [
{ component: 'u', text: 'Home' },
{
Expand Down

0 comments on commit 184e277

Please sign in to comment.