Skip to content

Commit 7210f2f

Browse files
tomivirkkiclaude
andauthored
fix: menu-bar overflow detection in flex/grid layouts (#11358)
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent b7ed99f commit 7210f2f

13 files changed

Lines changed: 184 additions & 0 deletions

File tree

‎packages/menu-bar/src/styles/vaadin-menu-bar-base-styles.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { css } from 'lit';
88
export const menuBarStyles = css`
99
:host {
1010
display: block;
11+
min-width: 0;
1112
}
1213
1314
:host([hidden]) {

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,13 @@ export const MenuBarMixin = (superClass) =>
471471
__setOverflowItems(buttons, overflow) {
472472
const container = this._container;
473473

474+
// Prevent the container from shrinking while buttons are being hidden.
475+
// The host has min-width: 0 so it can shrink inside flex/grid layouts.
476+
// Without this lock, hiding a button reduces the host width, which
477+
// shrinks the container (width: 100%), shifting all button positions
478+
// and causing a cascading collapse where every button appears to overflow.
479+
container.style.minWidth = `${container.offsetWidth}px`;
480+
474481
if (container.offsetWidth < container.scrollWidth) {
475482
this._hasOverflow = true;
476483

@@ -501,6 +508,8 @@ export const MenuBarMixin = (superClass) =>
501508
const items = buttons.filter((b) => !remaining.includes(b)).map((b) => b.item);
502509
this.__updateOverflow(items);
503510
}
511+
512+
container.style.minWidth = '';
504513
}
505514

506515
/** @private */

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

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,125 @@ describe('overflow', () => {
337337
});
338338
});
339339

340+
describe('layout combinations', () => {
341+
const items = [{ text: 'Item 1' }, { text: 'Item 2' }, { text: 'Item 3' }, { text: 'Item 4' }, { text: 'Item 5' }];
342+
343+
async function initMenuBar(container) {
344+
const menu = container.querySelector('vaadin-menu-bar');
345+
menu.items = items;
346+
await nextResize(menu);
347+
const buttons = menu._buttons;
348+
const overflow = buttons[buttons.length - 1];
349+
return { menu, buttons, overflow };
350+
}
351+
352+
describe('flex row with sibling', () => {
353+
let container, overflow;
354+
355+
beforeEach(async () => {
356+
container = fixtureSync(`
357+
<div style="display: flex; width: ${BUTTON_WIDTH * 5}px">
358+
<vaadin-menu-bar style="width: 100%"></vaadin-menu-bar>
359+
<div style="min-width: ${BUTTON_WIDTH * 2}px">Sibling</div>
360+
</div>
361+
`);
362+
overflow = (await initMenuBar(container)).overflow;
363+
});
364+
365+
it('should collapse items into overflow when sibling takes space', () => {
366+
expect(overflow.hasAttribute('hidden')).to.be.false;
367+
expect(overflow.item.children.length).to.be.greaterThan(0);
368+
expect(overflow.item.children.length).to.be.lessThan(items.length);
369+
});
370+
});
371+
372+
describe('css grid with sibling', () => {
373+
let container, overflow;
374+
375+
beforeEach(async () => {
376+
container = fixtureSync(`
377+
<div style="display: grid; grid-template-columns: 1fr ${BUTTON_WIDTH * 2}px; width: ${BUTTON_WIDTH * 5}px">
378+
<vaadin-menu-bar></vaadin-menu-bar>
379+
<div>Sibling</div>
380+
</div>
381+
`);
382+
overflow = (await initMenuBar(container)).overflow;
383+
});
384+
385+
it('should collapse items into overflow', () => {
386+
expect(overflow.hasAttribute('hidden')).to.be.false;
387+
expect(overflow.item.children.length).to.be.greaterThan(0);
388+
expect(overflow.item.children.length).to.be.lessThan(items.length);
389+
});
390+
});
391+
392+
describe('flex row-reverse with no width on menu-bar', () => {
393+
let container, menu, buttons, overflow;
394+
395+
beforeEach(async () => {
396+
container = fixtureSync(`
397+
<div style="display: flex; flex-direction: row-reverse; width: ${BUTTON_WIDTH * 5}px">
398+
<div style="min-width: ${BUTTON_WIDTH * 2}px">Sibling</div>
399+
<vaadin-menu-bar></vaadin-menu-bar>
400+
</div>
401+
`);
402+
({ menu, buttons, overflow } = await initMenuBar(container));
403+
});
404+
405+
it('should collapse items into overflow', () => {
406+
expect(overflow.hasAttribute('hidden')).to.be.false;
407+
expect(overflow.item.children.length).to.be.greaterThan(0);
408+
expect(overflow.item.children.length).to.be.lessThan(items.length);
409+
});
410+
411+
it('should restore items when container width increases', async () => {
412+
container.style.width = `${BUTTON_WIDTH * 8}px`;
413+
await nextResize(menu);
414+
buttons.slice(0, -1).forEach((btn) => assertVisible(btn));
415+
expect(overflow.hasAttribute('hidden')).to.be.true;
416+
});
417+
});
418+
419+
describe('gradual collapse in flex container', () => {
420+
let container, menu, buttons, overflow;
421+
422+
beforeEach(async () => {
423+
// Reproduces the layout from https://github.com/vaadin/web-components/issues/11269:
424+
// a flex container with a menu-bar (no explicit width) and a sibling.
425+
// Without the containerWidth snapshot fix, hiding a button causes
426+
// the host to shrink (min-width: 0), which shrinks the container,
427+
// causing cascading collapse where ALL items end up in overflow.
428+
container = fixtureSync(`
429+
<div style="display: flex; width: ${BUTTON_WIDTH * 3.5}px">
430+
<vaadin-menu-bar></vaadin-menu-bar>
431+
<button>Sibling</button>
432+
</div>
433+
`);
434+
menu = container.querySelector('vaadin-menu-bar');
435+
menu.items = [
436+
{ text: 'Item 1' },
437+
{ text: 'Item 2' },
438+
{ text: 'Item 3' },
439+
{ text: 'Item 4' },
440+
{ text: 'Item 5' },
441+
{ text: 'Item 6' },
442+
];
443+
await nextResize(menu);
444+
buttons = menu._buttons;
445+
overflow = buttons.at(-1);
446+
});
447+
448+
it('should not collapse all items at once', () => {
449+
const visibleButtons = buttons.filter(
450+
(btn) => btn !== overflow && getComputedStyle(btn).visibility !== 'hidden',
451+
);
452+
expect(visibleButtons.length).to.be.greaterThan(0);
453+
expect(overflow.item.children.length).to.be.greaterThan(0);
454+
expect(overflow.item.children.length).to.be.lessThan(menu.items.length);
455+
});
456+
});
457+
});
458+
340459
describe('parent resize', () => {
341460
let container, text, menu, buttons;
342461

‎packages/menu-bar/test/visual/aura/menu-bar.test.js‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,22 @@ describe('menu-bar', () => {
124124
await visualDiff(div, 'tertiary');
125125
});
126126
});
127+
128+
describe('overflow in flex layout', () => {
129+
beforeEach(async () => {
130+
div = fixtureSync(`
131+
<div style="display: flex; width: 300px; padding: 10px">
132+
<vaadin-menu-bar style="width: 100%"></vaadin-menu-bar>
133+
<div style="min-width: 120px">Sibling</div>
134+
</div>
135+
`);
136+
element = div.querySelector('vaadin-menu-bar');
137+
element.items = [{ text: 'Home' }, { text: 'Reports' }, { text: 'Dashboard' }, { text: 'Help' }];
138+
await nextResize(element);
139+
});
140+
141+
it('overflow in flex layout', async () => {
142+
await visualDiff(div, 'overflow-flex-layout');
143+
});
144+
});
127145
});
7.36 KB
Loading
7.99 KB
Loading

‎packages/menu-bar/test/visual/base/menu-bar.test.js‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,24 @@ describe('menu-bar', () => {
159159
await visualDiff(div, `${dir}-end-aligned-overflow-button`);
160160
});
161161
});
162+
163+
describe('overflow in flex layout', () => {
164+
beforeEach(async () => {
165+
div = fixtureSync(`
166+
<div style="display: flex; width: 300px; padding: 10px">
167+
<vaadin-menu-bar style="width: 100%"></vaadin-menu-bar>
168+
<div style="min-width: 120px">Sibling</div>
169+
</div>
170+
`);
171+
element = div.querySelector('vaadin-menu-bar');
172+
element.items = [{ text: 'Home' }, { text: 'Reports' }, { text: 'Dashboard' }, { text: 'Help' }];
173+
await nextResize(element);
174+
});
175+
176+
it('overflow in flex layout', async () => {
177+
await visualDiff(div, `${dir}-overflow-flex-layout`);
178+
});
179+
});
162180
});
163181
});
164182
});
3.12 KB
Loading
3.12 KB
Loading

‎packages/menu-bar/test/visual/lumo/menu-bar.test.js‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,24 @@ describe('menu-bar', () => {
257257
});
258258
});
259259
});
260+
261+
describe(`overflow in flex layout (${dir})`, () => {
262+
beforeEach(async () => {
263+
div = fixtureSync(`
264+
<div style="display: flex; width: 300px; padding: 10px">
265+
<vaadin-menu-bar style="width: 100%"></vaadin-menu-bar>
266+
<div style="min-width: 120px">Sibling</div>
267+
</div>
268+
`);
269+
element = div.querySelector('vaadin-menu-bar');
270+
element.items = [{ text: 'Home' }, { text: 'Reports' }, { text: 'Dashboard' }, { text: 'Help' }];
271+
await nextResize(element);
272+
});
273+
274+
it('overflow in flex layout', async () => {
275+
await visualDiff(div, `${dir}-overflow-flex-layout`);
276+
});
277+
});
260278
});
261279

262280
describe('dark', () => {

0 commit comments

Comments
 (0)