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: postpone menu-bar initial render to prevent re-layout #7312

Merged
merged 2 commits into from
Apr 11, 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
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 @@ -262,7 +262,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 @@ -468,6 +473,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 Down Expand Up @@ -57,11 +59,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 @@ -87,11 +91,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 Down Expand Up @@ -58,11 +60,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 @@ -89,11 +93,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
Loading