Skip to content
Open
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
4 changes: 4 additions & 0 deletions packages/main/src/components/ObjectPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,10 @@ const ObjectPage = forwardRef<ObjectPageDomRef, ObjectPagePropTypes>((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(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -203,3 +204,24 @@ export const ObjectPageSingleSectionIconTabBarTestComp = () => {
</ObjectPage>
);
};

// Focusable element in the first section + enough content to scroll it out of view (tab-return scroll test).
export const ObjectPageTabReturnScrollTestComp = () => {
return (
<ObjectPage
style={{ height: '500px' }}
titleArea={<ObjectPageTitle header={<Title>Denise Smith</Title>} snappedHeader={<Title>Denise Smith</Title>} />}
>
<ObjectPageSection titleText="First" id="first" aria-label="First">
<Button id="focus-target">Focus me</Button>
<div style={{ height: '120px', width: '100%', background: 'lightblue' }} />
</ObjectPageSection>
<ObjectPageSection titleText="Second" id="second" aria-label="Second">
<div style={{ height: '1200px', width: '100%', background: 'lightyellow' }} />
</ObjectPageSection>
<ObjectPageSection titleText="Third" id="third" aria-label="Third">
<div style={{ height: '1200px', width: '100%', background: 'orange' }} />
</ObjectPageSection>
</ObjectPage>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
Loading