diff --git a/packages/main/src/components/ObjectPage/index.tsx b/packages/main/src/components/ObjectPage/index.tsx index 6ffbbd74593..82cd7d74728 100644 --- a/packages/main/src/components/ObjectPage/index.tsx +++ b/packages/main/src/components/ObjectPage/index.tsx @@ -746,6 +746,10 @@ const ObjectPage = forwardRef((props, ref opNode.style.scrollPaddingBlock = '0px'; // Fallback: Some (ui5-table) ui5wc components don't implement `relatedTarget` as expected. } else if (!e.relatedTarget) { + // a window/tab blur also fires focusout with no relatedTarget; don't re-scroll to focus on tab return + if (!document.hasFocus()) { + return; + } const currentTarget = e.currentTarget; opNode.style.scrollPaddingBlock = '0px'; requestAnimationFrame(() => { diff --git a/packages/main/src/components/ObjectPage/test/ObjectPage.gallery.tsx b/packages/main/src/components/ObjectPage/test/ObjectPage.gallery.tsx index c413a5cc17c..28054228b8c 100644 --- a/packages/main/src/components/ObjectPage/test/ObjectPage.gallery.tsx +++ b/packages/main/src/components/ObjectPage/test/ObjectPage.gallery.tsx @@ -1,4 +1,5 @@ import { ObjectPageMode } from '../../../enums/ObjectPageMode.js'; +import { Button } from '../../../webComponents/Button/index.js'; import { Link } from '../../../webComponents/Link/index.js'; import { MessageStrip } from '../../../webComponents/MessageStrip/index.js'; import { Title } from '../../../webComponents/Title/index.js'; @@ -203,3 +204,24 @@ export const ObjectPageSingleSectionIconTabBarTestComp = () => { ); }; + +// Focusable element in the first section + enough content to scroll it out of view (tab-return scroll test). +export const ObjectPageTabReturnScrollTestComp = () => { + return ( + Denise Smith} snappedHeader={Denise Smith} />} + > + + +
+ + +
+ + +
+ + + ); +}; diff --git a/packages/main/src/components/ObjectPage/test/ObjectPage.spec.tsx b/packages/main/src/components/ObjectPage/test/ObjectPage.spec.tsx index a08cb97ae15..0c563bf0b81 100644 --- a/packages/main/src/components/ObjectPage/test/ObjectPage.spec.tsx +++ b/packages/main/src/components/ObjectPage/test/ObjectPage.spec.tsx @@ -189,6 +189,42 @@ test.describe('ObjectPage', () => { ); }); + test('preserves scroll position when the tab/window loses and regains focus', async ({ mount, page }) => { + await page.setViewportSize({ width: 950, height: 700 }); + await mount('ObjectPage/ObjectPageTabReturnScrollTestComp'); + + const op = page.locator('[data-component-name="ObjectPage"]'); + + // activeElement (not toBeFocused) so the assert survives a backgrounded window in parallel runs + await op.evaluate(() => document.getElementById('focus-target').focus()); + await expect.poll(() => op.evaluate(() => document.activeElement?.id)).toBe('focus-target'); + // instant scroll so the baseline isn't captured mid-animation under CSS smooth scrolling + await op.evaluate((el) => el.scrollTo({ top: 600, behavior: 'instant' })); + const scrollBefore = await op.evaluate((el) => el.scrollTop); + expect(scrollBefore).toBeGreaterThan(400); + + // The two signals a tab switch emits: focusout with no relatedTarget while document.hasFocus() is false. + await op.evaluate(() => { + const target = document.getElementById('focus-target'); + const realHasFocus = document.hasFocus.bind(document); + Object.defineProperty(document, 'hasFocus', { configurable: true, value: () => false }); + target.dispatchEvent(new FocusEvent('focusout', { bubbles: true, relatedTarget: null })); + Object.defineProperty(document, 'hasFocus', { configurable: true, value: realHasFocus }); + }); + + // On the bug the focused element gets (smooth-)scrolled back into view; assert the position held once settled. + await expect.poll(() => op.evaluate(() => document.activeElement?.id)).toBe('focus-target'); + let prev = -1; + await expect + .poll(async () => { + const current = await op.evaluate((el) => el.scrollTop); + const stable = current === prev; + prev = current; + return stable ? current : NaN; + }) + .toBeCloseTo(scrollBefore, -1); + }); + test('keeps only a single section out of the tab order', async ({ mount, page }) => { await mount('ObjectPage/ObjectPageSingleSectionTestComp');