|
| 1 | +import { expect } from '@vaadin/chai-plugins'; |
| 2 | +import { fixtureSync, nextResize } from '@vaadin/testing-helpers'; |
| 3 | +import '../src/vaadin-menu-bar.js'; |
| 4 | +import { |
| 5 | + assertHidden, |
| 6 | + assertVisible, |
| 7 | + BUTTON_WIDTH, |
| 8 | + createItems, |
| 9 | + expectCollapsed, |
| 10 | + expectOverflowInside, |
| 11 | +} from './helpers.js'; |
| 12 | + |
| 13 | +async function fixtureMenuBar(html, itemsCount = 5) { |
| 14 | + const container = fixtureSync(html); |
| 15 | + const menu = container.querySelector('vaadin-menu-bar') ?? container; |
| 16 | + menu.items = createItems(itemsCount); |
| 17 | + await nextResize(menu); |
| 18 | + const buttons = menu._buttons; |
| 19 | + return { container, menu, buttons, overflow: buttons.at(-1) }; |
| 20 | +} |
| 21 | + |
| 22 | +describe('overflow in layouts', () => { |
| 23 | + beforeEach(() => { |
| 24 | + fixtureSync(` |
| 25 | + <style> |
| 26 | + vaadin-menu-bar-button { |
| 27 | + width: ${BUTTON_WIDTH}px; |
| 28 | + } |
| 29 | + </style> |
| 30 | + `); |
| 31 | + }); |
| 32 | + |
| 33 | + describe('responsive behavior in container', () => { |
| 34 | + it('should hide overflow button and reset its items when all buttons fit', async () => { |
| 35 | + const { container, menu, buttons, overflow } = await fixtureMenuBar(` |
| 36 | + <div style="display: flex; width: ${BUTTON_WIDTH * 2.5}px"> |
| 37 | + <vaadin-menu-bar style="min-width: 100%"></vaadin-menu-bar> |
| 38 | + </div> |
| 39 | + `); |
| 40 | + |
| 41 | + // Must work even if menu-bar won't automatically resize to a larger size |
| 42 | + // when more space becomes available |
| 43 | + // see https://github.com/vaadin/vaadin-menu-bar/issues/130 |
| 44 | + menu.style.minWidth = '0'; |
| 45 | + container.style.width = `${BUTTON_WIDTH * 1.5}px`; |
| 46 | + await nextResize(menu); |
| 47 | + assertHidden(buttons[2]); |
| 48 | + assertHidden(buttons[3]); |
| 49 | + |
| 50 | + container.style.width = `${BUTTON_WIDTH * 5}px`; |
| 51 | + await nextResize(menu); |
| 52 | + assertVisible(buttons[2]); |
| 53 | + assertVisible(buttons[3]); |
| 54 | + assertVisible(buttons[4]); |
| 55 | + expect(overflow.hasAttribute('hidden')).to.be.true; |
| 56 | + expect(overflow.item.children.length).to.equal(0); |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + describe('layout combinations', () => { |
| 61 | + it('should collapse items into overflow in a flex row with a sibling', async () => { |
| 62 | + const { menu } = await fixtureMenuBar(` |
| 63 | + <div style="display: flex; width: ${BUTTON_WIDTH * 5}px"> |
| 64 | + <vaadin-menu-bar style="width: 100%"></vaadin-menu-bar> |
| 65 | + <div style="min-width: ${BUTTON_WIDTH * 2}px">Sibling</div> |
| 66 | + </div> |
| 67 | + `); |
| 68 | + expectCollapsed(menu, [2, 3, 4]); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should collapse items into overflow in a css grid with a sibling', async () => { |
| 72 | + const { menu } = await fixtureMenuBar(` |
| 73 | + <div style="display: grid; grid-template-columns: 1fr ${BUTTON_WIDTH * 2}px; width: ${BUTTON_WIDTH * 5}px"> |
| 74 | + <vaadin-menu-bar></vaadin-menu-bar> |
| 75 | + <div>Sibling</div> |
| 76 | + </div> |
| 77 | + `); |
| 78 | + expectCollapsed(menu, [2, 3, 4]); |
| 79 | + }); |
| 80 | + |
| 81 | + describe('flex row-reverse with no width on menu-bar', () => { |
| 82 | + let container, menu; |
| 83 | + |
| 84 | + beforeEach(async () => { |
| 85 | + ({ container, menu } = await fixtureMenuBar(` |
| 86 | + <div style="display: flex; flex-direction: row-reverse; width: ${BUTTON_WIDTH * 5}px"> |
| 87 | + <div style="min-width: ${BUTTON_WIDTH * 2}px">Sibling</div> |
| 88 | + <vaadin-menu-bar></vaadin-menu-bar> |
| 89 | + </div> |
| 90 | + `)); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should collapse items into overflow', () => { |
| 94 | + expectCollapsed(menu, [2, 3, 4]); |
| 95 | + }); |
| 96 | + |
| 97 | + it('should restore items when container width increases', async () => { |
| 98 | + container.style.width = `${BUTTON_WIDTH * 8}px`; |
| 99 | + await nextResize(menu); |
| 100 | + expectCollapsed(menu, []); |
| 101 | + }); |
| 102 | + }); |
| 103 | + |
| 104 | + it('should not collapse all items at once in a flex container with a sibling', async () => { |
| 105 | + // Reproduces the layout from https://github.com/vaadin/web-components/issues/11269: |
| 106 | + // a flex container with a menu-bar (no explicit width) and a sibling. |
| 107 | + // Without the containerWidth snapshot fix, hiding a button causes |
| 108 | + // the host to shrink (min-width: 0), which shrinks the container, |
| 109 | + // causing cascading collapse where ALL items end up in overflow. |
| 110 | + const { menu, buttons, overflow } = await fixtureMenuBar( |
| 111 | + ` |
| 112 | + <div style="display: flex; width: ${BUTTON_WIDTH * 3.5}px"> |
| 113 | + <vaadin-menu-bar></vaadin-menu-bar> |
| 114 | + <button>Sibling</button> |
| 115 | + </div> |
| 116 | + `, |
| 117 | + 6, |
| 118 | + ); |
| 119 | + const visibleButtons = buttons.filter((btn) => btn !== overflow && getComputedStyle(btn).visibility !== 'hidden'); |
| 120 | + expect(visibleButtons.length).to.be.greaterThan(0); |
| 121 | + expect(overflow.item.children.length).to.be.greaterThan(0); |
| 122 | + expect(overflow.item.children.length).to.be.lessThan(menu.items.length); |
| 123 | + }); |
| 124 | + |
| 125 | + it('should keep the overflow button inside the menu bar in a grid column sized by content', async () => { |
| 126 | + const { menu } = await fixtureMenuBar(` |
| 127 | + <div style="display: grid; grid-template-columns: auto 1fr; width: ${BUTTON_WIDTH * 5}px"> |
| 128 | + <vaadin-menu-bar></vaadin-menu-bar> |
| 129 | + <div style="min-width: ${BUTTON_WIDTH * 2}px">Sibling</div> |
| 130 | + </div> |
| 131 | + `); |
| 132 | + expectCollapsed(menu, [2, 3, 4]); |
| 133 | + expectOverflowInside(menu); |
| 134 | + }); |
| 135 | + |
| 136 | + it('should keep the overflow button inside the menu bar in a toolbar with centered items', async () => { |
| 137 | + const { menu } = await fixtureMenuBar(` |
| 138 | + <div style="display: flex; align-items: center; width: ${BUTTON_WIDTH * 5}px"> |
| 139 | + <vaadin-menu-bar></vaadin-menu-bar> |
| 140 | + <div style="min-width: ${BUTTON_WIDTH * 2}px">Sibling</div> |
| 141 | + </div> |
| 142 | + `); |
| 143 | + expectCollapsed(menu, [2, 3, 4]); |
| 144 | + expectOverflowInside(menu); |
| 145 | + }); |
| 146 | + |
| 147 | + it('should keep the overflow button inside a shrink-to-fit menu bar limited by max-width', async () => { |
| 148 | + const { menu } = await fixtureMenuBar(` |
| 149 | + <div style="width: ${BUTTON_WIDTH * 3}px"> |
| 150 | + <vaadin-menu-bar style="display: inline-block; max-width: 100%"></vaadin-menu-bar> |
| 151 | + </div> |
| 152 | + `); |
| 153 | + expectCollapsed(menu, [2, 3, 4]); |
| 154 | + expectOverflowInside(menu); |
| 155 | + }); |
| 156 | + }); |
| 157 | + |
| 158 | + describe('parent resize', () => { |
| 159 | + let container, menu, buttons; |
| 160 | + |
| 161 | + beforeEach(async () => { |
| 162 | + ({ container, menu, buttons } = await fixtureMenuBar( |
| 163 | + ` |
| 164 | + <div style="display: flex; max-width: ${BUTTON_WIDTH * 3}px"> |
| 165 | + <div>Sibling</div> |
| 166 | + <vaadin-menu-bar style="min-width: ${BUTTON_WIDTH * 1.5}px"></vaadin-menu-bar> |
| 167 | + </div> |
| 168 | + `, |
| 169 | + 4, |
| 170 | + )); |
| 171 | + assertHidden(buttons[2]); |
| 172 | + assertHidden(buttons[3]); |
| 173 | + }); |
| 174 | + |
| 175 | + it('should show buttons when container width increases and menu-bar width stays the same', async () => { |
| 176 | + container.style.maxWidth = `${BUTTON_WIDTH * 5}px`; |
| 177 | + await nextResize(menu); |
| 178 | + |
| 179 | + assertVisible(buttons[2]); |
| 180 | + assertVisible(buttons[3]); |
| 181 | + }); |
| 182 | + |
| 183 | + it('should show buttons after attaching another container and increasing its width', async () => { |
| 184 | + const other = fixtureSync(`<div style="display: flex; max-width: ${BUTTON_WIDTH * 4}px"></div>`); |
| 185 | + |
| 186 | + other.append(...container.children); |
| 187 | + other.style.maxWidth = `${BUTTON_WIDTH * 5}px`; |
| 188 | + await nextResize(menu); |
| 189 | + |
| 190 | + assertVisible(buttons[2]); |
| 191 | + assertVisible(buttons[3]); |
| 192 | + }); |
| 193 | + |
| 194 | + it('should show buttons when shadow host width increases and menu-bar width stays the same', async () => { |
| 195 | + const host = fixtureSync(`<div style="display: flex; max-width: ${BUTTON_WIDTH * 3}px"></div>`); |
| 196 | + host.attachShadow({ mode: 'open' }); |
| 197 | + |
| 198 | + host.shadowRoot.append(...container.children); |
| 199 | + await nextResize(menu); |
| 200 | + assertHidden(buttons[2]); |
| 201 | + assertHidden(buttons[3]); |
| 202 | + |
| 203 | + host.style.maxWidth = `${BUTTON_WIDTH * 6}px`; |
| 204 | + await nextResize(menu); |
| 205 | + |
| 206 | + assertVisible(buttons[2]); |
| 207 | + assertVisible(buttons[3]); |
| 208 | + }); |
| 209 | + }); |
| 210 | +}); |
0 commit comments