diff --git a/integration/tests/component-relayout-page.test.js b/integration/tests/component-relayout-page.test.js new file mode 100644 index 0000000000..591e853adb --- /dev/null +++ b/integration/tests/component-relayout-page.test.js @@ -0,0 +1,40 @@ +import { expect } from '@esm-bundle/chai'; +import { fixtureSync, nextRender } from '@vaadin/testing-helpers'; +import { ContextMenu } from '@vaadin/context-menu'; + +[{ tagName: ContextMenu.is }].forEach(({ tagName }) => { + describe(`${tagName} re-layout`, () => { + let wrapper; + + beforeEach(() => { + wrapper = fixtureSync('
'); + }); + + function renderChildren() { + if (wrapper.children.length) { + [...wrapper.children].forEach((child) => child.remove()); + } + + wrapper.appendChild(document.createElement(tagName)); + + for (let i = 0; i < 100; i++) { + const btn = document.createElement('button'); + btn.textContent = `Button ${i}`; + wrapper.appendChild(btn); + wrapper.appendChild(document.createElement('br')); + } + } + + it(`should not reset scroll to top when creating a ${tagName}`, async () => { + renderChildren(); + await nextRender(); + + document.documentElement.scrollTop = 1000; + + renderChildren(); + await nextRender(); + + expect(document.documentElement.scrollTop).to.equal(1000); + }); + }); +}); diff --git a/packages/context-menu/src/vaadin-context-menu-mixin.js b/packages/context-menu/src/vaadin-context-menu-mixin.js index 719da372d9..cae633fbf2 100644 --- a/packages/context-menu/src/vaadin-context-menu-mixin.js +++ b/packages/context-menu/src/vaadin-context-menu-mixin.js @@ -209,8 +209,11 @@ export const ContextMenuMixin = (superClass) => * @private */ _onOverlayOpened(e) { - this._setOpened(e.detail.value); - this.__alignOverlayPosition(); + const opened = e.detail.value; + this._setOpened(opened); + if (opened) { + this.__alignOverlayPosition(); + } } /** diff --git a/packages/context-menu/test/dom/__snapshots__/context-menu.test.snap.js b/packages/context-menu/test/dom/__snapshots__/context-menu.test.snap.js index 63aba91b02..eeeeb9fc42 100644 --- a/packages/context-menu/test/dom/__snapshots__/context-menu.test.snap.js +++ b/packages/context-menu/test/dom/__snapshots__/context-menu.test.snap.js @@ -65,7 +65,6 @@ snapshots["context-menu items nested"] = dir="ltr" modeless="" opened="" - right-aligned="" start-aligned="" top-aligned="" > @@ -166,7 +165,6 @@ snapshots["context-menu items overlay class nested"] = dir="ltr" modeless="" opened="" - right-aligned="" start-aligned="" top-aligned="" > diff --git a/packages/menu-bar/test/dom/__snapshots__/menu-bar.test.snap.js b/packages/menu-bar/test/dom/__snapshots__/menu-bar.test.snap.js index 0deae25b2a..16b15eb52f 100644 --- a/packages/menu-bar/test/dom/__snapshots__/menu-bar.test.snap.js +++ b/packages/menu-bar/test/dom/__snapshots__/menu-bar.test.snap.js @@ -58,7 +58,6 @@ snapshots["menu-bar overlay"] = `