Skip to content
Closed
79 changes: 64 additions & 15 deletions packages/lab/src/NavigationStack/NavigationStack.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,40 @@ function writingDirection(element: HTMLElement | null): 'ltr' | 'rtl' {
return isRtlElement(element) ? 'rtl' : 'ltr';
}

/** When a transition's animations start, as the animator's call options. */
type TransitionClock = {at?: number; start?: 'next-frame'};

/**
* The clock of a transition. A finger's release keeps the time of its
* release, so the spring continues from the frame the finger left. A
* transition from rest leaves the start to the animator: it starts when the
* browser commits it, so a heavy commit or a later listener's block delays
* the motion instead of skipping into it. Started in an input event's own
Comment thread
astracat-bot[bot] marked this conversation as resolved.
Resolved
* task (a tap on a row, a key), it starts at the first frame after that
* task, however long the task runs.
*/
function transitionClock(
releasedAt: number | undefined,
isInputTask: boolean,
): TransitionClock {
if (releasedAt != null) {
return {at: releasedAt};
}
return isInputTask ? {start: 'next-frame'} : {};
}

/**
* The page is dispatching an input event (a pointer, a key, a touch) now.
* The fallback for a push the app starts outside the stack's own handlers.
*/
function isDispatchingInput(view: Window | null | undefined): boolean {
if (view == null) {
return false;
}
const event: unknown = Reflect.get(view, 'event');
return event instanceof view.UIEvent;
Comment thread
astracat-bot[bot] marked this conversation as resolved.
Resolved
}

/**
* A push/pop navigator: UINavigationController's transitions, back button
* and back swipes, on the compositor.
Expand Down Expand Up @@ -963,9 +997,12 @@ export function NavigationStack<T>({
// ---------------------------------------------------------------------------

const runningRef = useRef<{id: number; done: boolean} | null>(null);
// The clock the last transition's screens run on (their start time), so
// the back chevron keeps time with them.
const startedRef = useRef(0);
// The clock the last transition's screens run on (their start), so the
// back chevron keeps time with them.
const startedRef = useRef<TransitionClock>({});
// A click or key press in the stack is being handled in this task
// (rememberActivation).
const inputTaskRef = useRef(false);
// A pop's focus return, waiting for the commit that drops the leaving
// screen (until then every screen is `inert` and cannot take focus).
const pendingFocusRef = useRef<{popped: string; revealed: string} | null>(
Expand Down Expand Up @@ -1101,6 +1138,8 @@ export function NavigationStack<T>({
/**
* Springs the layers to progress `to`. `dp` is the progress velocity to
* start from (omit it to keep the velocity the layers have on screen).
* `releasedAt` is a finger's release time; without it the springs start on
* the animator's clock (transitionClock).
*/
const animateProgress = useCallback(
(
Expand All @@ -1111,14 +1150,18 @@ export function NavigationStack<T>({
dp: number | undefined,
release: boolean,
underBar: 'rest' | 'ride',
startAt?: number,
releasedAt?: number,
): Promise<boolean> => {
const width = cardsRef.current?.clientWidth ?? 0;
const direction = writingDirection(containerRef.current);
const values = layersAt(to, width, direction);
const velocity =
dp == null ? null : layerVelocities(dp, width, direction);
const at = startAt ?? performance.now();
const clock = transitionClock(
releasedAt,
inputTaskRef.current ||
isDispatchingInput(containerRef.current?.ownerDocument.defaultView),
);
const topLayers = layersOf(top);
const underLayers = layersOf(under);
const handles = [];
Expand All @@ -1131,13 +1174,13 @@ export function NavigationStack<T>({
{x: values.topX},
{spring, velocity: velocity ? {x: velocity.topX} : undefined},
{
at,
...clock,
touchRelease: release,
crossfadeAs: to === 0 ? 'entrance' : 'exit',
},
),
);
startedRef.current = at;
startedRef.current = clock;
}
// The bars move with their cards (each screen's bar is part of it in
// UIKit): the incoming one covers the outgoing title.
Expand All @@ -1146,7 +1189,7 @@ export function NavigationStack<T>({
topLayers.bar.animate(
{x: values.topX},
{spring, velocity: velocity ? {x: velocity.topX} : undefined},
{at, touchRelease: release},
{...clock, touchRelease: release},
),
);
}
Expand All @@ -1155,7 +1198,7 @@ export function NavigationStack<T>({
underLayers.bar.animate(
{x: values.underX},
{spring, velocity: velocity ? {x: velocity.underX} : undefined},
{at, touchRelease: release},
{...clock, touchRelease: release},
),
);
}
Expand All @@ -1165,7 +1208,7 @@ export function NavigationStack<T>({
{x: values.underX},
{spring, velocity: velocity ? {x: velocity.underX} : undefined},
{
at,
...clock,
touchRelease: release,
crossfadeAs: to === 1 ? 'entrance' : 'exit',
},
Expand Down Expand Up @@ -1205,15 +1248,15 @@ export function NavigationStack<T>({
spring,
velocity: clipVelocity == null ? undefined : {x: clipVelocity},
},
{at, touchRelease: release},
{...clock, touchRelease: release},
),
underLayers.barClipContent.animate(
{x: -clipTo},
{
spring,
velocity: clipVelocity == null ? undefined : {x: -clipVelocity},
},
{at, touchRelease: release},
{...clock, touchRelease: release},
),
);
}
Expand All @@ -1239,7 +1282,7 @@ export function NavigationStack<T>({
spring,
velocity: velocity ? {opacity: velocity.dim} : undefined,
},
{at, touchRelease: release},
{...clock, touchRelease: release},
),
);
}
Expand Down Expand Up @@ -1294,7 +1337,7 @@ export function NavigationStack<T>({
velocity: t => (t < delayS ? 0 : fade.velocity(t - delayS)),
},
},
{at: startedRef.current, touchRelease: release},
{...startedRef.current, touchRelease: release},
);
},
[view.models, backControl.animator, backChevron.animator],
Expand Down Expand Up @@ -1449,7 +1492,6 @@ export function NavigationStack<T>({
wasRunning ? undefined : programmaticVelocity('pop'),
false,
'ride',
at,
);
fadeBack('pop', under);
}
Expand Down Expand Up @@ -1558,6 +1600,13 @@ export function NavigationStack<T>({
// from the container's own capture handlers. A press on a control's label or
// icon remembers the control itself.
const rememberActivation = useCallback((event: SyntheticEvent) => {
// A transition this click or key starts runs in its task: React commits
// it before the task ends, and the next task clears the mark.
inputTaskRef.current = true;
Comment thread
astracat-bot[bot] marked this conversation as resolved.
Resolved
const view = containerRef.current?.ownerDocument.defaultView;
view?.setTimeout(() => {
inputTaskRef.current = false;
}, 0);
const {target} = event;
if (isElement(target)) {
// The scroller is focusable while it overflows, but it is not a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1150,6 +1150,52 @@ function blockDuringPush(page: Page, ms: number) {
});
}

test('a push whose commit blocks the main thread starts from its first frame', async ({
page,
}) => {
// A heavy screen blocks the main thread in the task that pushes it (here
// for 300 ms, right after the stack mounts it). The push starts when that
// task ends, so its first frames show it near where it starts. Dated
// before the block, it would show almost all of its travel at once.
const {driver, width} = await openStack(page, 'settings');
await page.evaluate(() => {
const observer = new MutationObserver(() => {
if (
document.querySelector('[data-navigation-screen="screen-General"]') ==
null
) {
return;
}
observer.disconnect();
const until = performance.now() + 300;
while (performance.now() < until) {
// Hold the main thread, as a heavy commit does.
}
});
observer.observe(document.body, {childList: true, subtree: true});
});
await driver.tap(await rowCenter(page, 'General'));
// The second frame after the block: the push has run for about a frame.
const x = await page.evaluate(
() =>
new Promise<number>(resolve => {
requestAnimationFrame(() =>
requestAnimationFrame(() => {
const pushed = document.querySelector(
'[data-navigation-screen="screen-General"]',
);
const value =
pushed == null ? 'none' : getComputedStyle(pushed).translate;
resolve(value === 'none' ? 0 : parseFloat(value));
}),
);
}),
);
// Critically damped from the trailing edge: about 70 % of the width is
// still to travel after 33 ms; under 1 % after the 300 ms block.
expect(Math.abs(x) / width).toBeGreaterThan(0.5);
Comment thread
astracat-bot[bot] marked this conversation as resolved.
Resolved
});

test('a push keeps advancing every frame while the main thread is blocked', async ({
page,
browserName,
Expand Down