Skip to content
Draft
Show file tree
Hide file tree
Changes from 4 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
521 changes: 521 additions & 0 deletions apps/storybook/stories/NavigationStackSpecDemos.stories.tsx

Large diffs are not rendered by default.

432 changes: 432 additions & 0 deletions packages/lab/src/NavigationStack/NavigationStack.keep.test.tsx

Large diffs are not rendered by default.

670 changes: 601 additions & 69 deletions packages/lab/src/NavigationStack/NavigationStack.tsx

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// Copyright (c) Meta Platforms, Inc. and affiliates.

/**
* @file NavigationStackHeavyCommit.interaction.browser.spec.ts
* @input The built Storybook's NavigationStack Settings story, in Chromium
* and WebKit on the phone profile
* @output Proof that a push or a pop from rest starts at its first presented
* frame: a block before that frame delays the motion, and skips none of it
* @position High-fidelity lane for NavigationStack's draft record. A frame's
* timing needs a real engine
*
* SYNC: The story this reads is Settings in
* apps/storybook/stories/NavigationStack.stories.tsx.
*/

import {expect, test, type Page} from '@astryxdesign/a11y-spec/playwright';
import {openStory, withStorybook} from '@astryxdesign/interaction-harness';

const storybook = withStorybook(test);

/** How long the first frame after the change blocks the main thread. */
const BLOCK_MS = 300;

/** Where a card is in the first frame after a blocked commit, and its travel. */
interface FirstFrame {
left: number;
width: number;
}

/**
* Blocks the main thread for BLOCK_MS in the first frame after the next
* click, before that frame paints, and resolves with where the card
* `selector` names is then. Call it before the click and await it after.
*/
function blockFirstFrameAfterClick(
page: Page,
selector: string,
): Promise<FirstFrame | null> {
return page.evaluate(
([cardSelector, ms]) =>
new Promise<FirstFrame | null>(resolve => {
document.addEventListener(
'click',
() => {
requestAnimationFrame(() => {
const end = performance.now() + ms;
while (performance.now() < end) {
// The commit's heavy work, before the frame paints.
}
const card = document.querySelector(cardSelector);
const cards = card?.parentElement;
resolve(
card == null || cards == null
? null
: {
left: card.getBoundingClientRect().left,
width: cards.clientWidth,
},
);
});
},
{capture: true, once: true},
);
}),
[selector, BLOCK_MS] as const,
);
}

const SCREEN = '[data-navigation-screen="screen-General"]';

/** Waits until no animation runs on the root's and General's cards. */
async function settled(page: Page) {
await expect
.poll(() =>
page.evaluate(() =>
Array.from(
document.querySelectorAll('[data-navigation-screen]'),
).reduce((count, card) => count + card.getAnimations().length, 0),
),
)
.toBe(0);
}

test('a push from rest starts at its first presented frame, when its commit blocks the main thread', async ({
page,
}) => {
await openStory(page, storybook(), 'lab-navigationstack--settings');
const first = blockFirstFrameAfterClick(page, SCREEN);
await page.getByRole('button', {name: 'General', exact: true}).tap();
const frame = await first;
if (frame == null) {
throw new Error('The pushed card was not in the first frame.');
}
// The pushed card enters from the trailing edge: in its first frame, most
// of its travel is still ahead.
expect(frame.left).toBeGreaterThan(frame.width * 0.5);
});

test('a pop from rest starts at its first presented frame, when its commit blocks the main thread', async ({
page,
}) => {
await openStory(page, storybook(), 'lab-navigationstack--settings');
await page.getByRole('button', {name: 'General', exact: true}).tap();
await expect(page.locator(SCREEN)).toHaveAttribute('data-navigation-top');
await settled(page);
const first = blockFirstFrameAfterClick(page, SCREEN);
await page.locator('[data-navigation-back]').tap();
const frame = await first;
if (frame == null) {
throw new Error('The popped card was not in the first frame.');
}
// The popped card leaves toward the trailing edge: in its first frame,
// most of its travel is still ahead.
expect(frame.left).toBeLessThan(frame.width * 0.5);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
// Copyright (c) Meta Platforms, Inc. and affiliates.

/**
* @file NavigationStackHidden.interaction.browser.spec.ts
* @input The built Storybook's NavigationStack spec demos, with real touch
* input in Chromium and WebKit on the phone profile
* @output Proof that a retained screen takes no input, whatever its place in
* the DOM, and that after a re-push the screen on view's controls act on
* its own state while focus stays in it
* @position High-fidelity lane for NavigationStack's draft record (FR4, FR9).
* Hit testing and focus need a real engine; jsdom has neither
*
* SYNC: The stories this reads are HiddenScreensTakeNoInput and
* RetainedScreens in apps/storybook/stories/NavigationStackSpecDemos.stories.tsx.
*/

import {expect, test, type Page} from '@astryxdesign/a11y-spec/playwright';
import {
drag,
openStory,
touchDriver,
withStorybook,
} from '@astryxdesign/interaction-harness';

const storybook = withStorybook(test);

const card = (page: Page, key: string) =>
page.locator(`[data-navigation-screen="${key}"]`);

/** Taps a row and waits for its screen to come to rest on top. */
async function open(page: Page, chat: string) {
await page.getByRole('button', {name: chat, exact: true}).first().tap();
await expect(card(page, `screen-${chat}`)).toHaveAttribute(
'data-navigation-screen-state',
'top',
);
await expect(card(page, `screen-${chat}`)).toHaveAttribute(
'data-navigation-top',
);
}

/** Taps Back and waits for the pop to end and the screen to be retained. */
async function back(page: Page, chat: string) {
await page.locator('[data-navigation-back]').tap();
await expect(card(page, `screen-${chat}`)).toHaveAttribute(
'data-navigation-screen-state',
'hidden',
);
}

test('a retained screen after the screen on view in the DOM takes no tap, key or hit test', async ({
page,
}) => {
await openStory(
page,
storybook(),
'lab-navigationstack-spec-demos--hidden-screens-take-no-input',
);
await open(page, 'Chat 1');
await back(page, 'Chat 1');

// The retained screen comes after the root in the DOM.
const after = await page.evaluate(() => {
const root = document.querySelector('[data-navigation-screen="root"]');
const kept = document.querySelector(
'[data-navigation-screen="screen-Chat 1"]',
);
return root != null && kept != null
? Boolean(
root.compareDocumentPosition(kept) & Node.DOCUMENT_POSITION_FOLLOWING,
)
: null;
});
expect(after).toBe(true);

// The retained screen stays laid out, so it shows again with no layout
// work, and it does not paint.
const kept = card(page, 'screen-Chat 1');
expect(
await kept.evaluate(element => {
const style = getComputedStyle(element);
return [style.contentVisibility, style.visibility];
}),
).toEqual(['visible', 'hidden']);

// A hit test over the root's probe names the root.
const probe = card(page, 'root').locator('[data-hit-probe]');
const box = await probe.boundingBox();
if (box == null) {
throw new Error('The probe has no box');
}
const x = Math.round(box.x + box.width / 2);
const y = Math.round(box.y + box.height / 2);
const hit = await page.evaluate(
([px, py]) =>
document
.elementFromPoint(px, py)
?.closest('[data-navigation-screen]')
?.getAttribute('data-navigation-screen') ?? null,
[x, y] as const,
);
expect(hit).toBe('root');

// A tap over it reaches the root, and the retained screen counts none.
await page.touchscreen.tap(x, y);
await expect(card(page, 'root').locator('[data-clicks]')).toHaveText('1');
await expect(card(page, 'screen-Chat 1').locator('[data-clicks]')).toHaveText(
'0',
);

// Tab never lands in the retained screen.
for (let press = 0; press < 6; press++) {
await page.keyboard.press('Tab');
const inKept = await page.evaluate(
() =>
document.activeElement?.closest(
'[data-navigation-screen="screen-Chat 1"]',
) != null,
);
expect(inKept).toBe(false);
}
});

test('a finger scrolling over the screen on view scrolls it, with a retained screen after it in the DOM', async ({
page,
browserName,
}) => {
test.skip(
browserName !== 'chromium',
'native scrolling from injected touches is Chromium-only (harness README)',
);
await openStory(
page,
storybook(),
'lab-navigationstack-spec-demos--hidden-screens-take-no-input',
);
await open(page, 'Chat 1');
await back(page, 'Chat 1');
const probe = card(page, 'root').locator('[data-hit-probe]');
const box = await probe.boundingBox();
if (box == null) {
throw new Error('The probe has no box');
}
const x = Math.round(box.x + box.width / 2);
const y = Math.round(box.y + box.height / 2);
const scrolled = (key: string) =>
page.evaluate(
selector => document.querySelector(selector)?.scrollTop ?? 0,
`[data-navigation-screen="${key}"] > div`,
);
const before = await scrolled('root');
const keptBefore = await scrolled('screen-Chat 1');
const driver = await touchDriver(page);
await driver.play(drag({x, y: y + 120}, {x, y: y - 120}, {durationMs: 300}), {
pacing: 'spacing',
});
await expect.poll(() => scrolled('root')).toBeGreaterThan(before);
expect(await scrolled('screen-Chat 1')).toBe(keptBefore);
});

test('after a re-push, the screen on view’s controls act on its own state, and focus stays in it', async ({
page,
}) => {
await openStory(
page,
storybook(),
'lab-navigationstack-spec-demos--retained-screens',
);
await open(page, 'Chat 1');
await back(page, 'Chat 1');
await open(page, 'Chat 2');
await back(page, 'Chat 2');
await open(page, 'Chat 1');

const chat1 = card(page, 'screen-Chat 1');
const draft = chat1.getByRole('textbox', {name: 'Draft for Chat 1'});
await draft.tap();
await page.keyboard.type('follow-up');
await chat1.getByRole('button', {name: 'Send'}).tap();

await expect(chat1.getByRole('list', {name: 'Sent in Chat 1'})).toHaveText(
'follow-up',
);
await expect(
card(page, 'screen-Chat 2').locator('[data-sent] li'),
).toHaveCount(0);
const focusIn = await page.evaluate(
() =>
document.activeElement
?.closest('[data-navigation-screen]')
?.getAttribute('data-navigation-screen') ?? null,
);
expect(focusIn).toBe('screen-Chat 1');
});
8 changes: 8 additions & 0 deletions packages/lab/src/NavigationStack/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,18 @@
export {
NavigationScreen,
NavigationStack,
useNavigationScreen,
useNavigationScroller,
useNavigationStack,
} from './NavigationStack';
export {useNavigationLink} from './useNavigationLink';
export type {NavigationLinkProps} from './useNavigationLink';
export type {
NavigationChangeOptions,
NavigationScreenProps,
NavigationScreenState,
NavigationScroller,
NavigationStackApi,
NavigationStackProps,
NavigationTransition,
} from './NavigationStack';
Loading