Skip to content
Merged
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
137 changes: 136 additions & 1 deletion apps/storybook/stories/NavigationStack.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (c) Meta Platforms, Inc. and affiliates.

import {useState} from 'react';
import {useLayoutEffect, useState} from 'react';
import type {Meta, StoryObj} from '@storybook/react';
import {
NavigationScreen,
Expand Down Expand Up @@ -97,6 +97,141 @@ export const Settings: Story = {
render: () => <SettingsStack />,
};

// =============================================================================
// Settings Heavy Commit — a push whose own commit blocks the main thread
// =============================================================================

type HeavyMode = 'commit' | 'after';

/** Holds the main thread for `ms`, as a heavy commit does. */
function holdMainThread(ms: number) {
const until = performance.now() + ms;
while (performance.now() < until) {
// Busy on purpose.
}
}

/** A pushed screen whose mount blocks for 300 ms inside the push's commit. */
function HeavyScreen({entry, mode}: {entry: string; mode: HeavyMode}) {
useLayoutEffect(() => {
if (mode === 'commit') {
holdMainThread(300);
}
}, [mode]);
return <Rows title={entry} />;
}

/**
* Reads the pushed card's travelled share in each of the first eight frames
* after a push. In `after` mode, the first moving frame blocks for 300 ms.
*/
function traceFrames(
entry: string,
mode: HeavyMode,
done: (lines: string[]) => void,
) {
const start = performance.now();
const lines: string[] = [];
let hasHeld = false;
const step = (t: number) => {
const card = document.querySelector<HTMLElement>(
`[data-navigation-screen="screen-${entry}"]`,
);
const stack = document.querySelector<HTMLElement>(
'[data-testid="navigation-stack"]',
);
const width = stack?.getBoundingClientRect().width ?? 1;
const value = card == null ? 'none' : getComputedStyle(card).translate;
const offset = value === 'none' ? 0 : Math.abs(parseFloat(value));
const share = card == null ? 0 : 1 - offset / width;
lines.push(`${Math.round(t - start)} ms ${(share * 100).toFixed(1)}%`);
if (mode === 'after' && !hasHeld && share > 0.005) {
hasHeld = true;
holdMainThread(300);
}
if (lines.length < 8) {
requestAnimationFrame(step);
} else {
done(lines);
}
};
requestAnimationFrame(step);
}

function HeavyCommitStack() {
const [path, setPath] = useState<string[]>([]);
const [mode, setMode] = useState<HeavyMode>('commit');
const [readout, setReadout] = useState<string[]>([]);
const standalone =
typeof navigator !== 'undefined' &&
(navigator as Navigator & {standalone?: boolean}).standalone === true;
// With Reduce Motion on, the push cross-fades, so every frame reads 100%.
const reducedMotion =
typeof window !== 'undefined' &&
window.matchMedia('(prefers-reduced-motion: reduce)').matches;
const push = (next: HeavyMode) => {
setMode(next);
setReadout([]);
traceFrames('General', next, setReadout);
setPath(['General']);
};
return (
<div style={{height: '100vh', position: 'relative'}}>
<NavigationStack
label="Settings"
root={
<NavigationScreen title="Settings">
<List>
<ListItem
label="General (heavy commit)"
onClick={() => push('commit')}
/>
<ListItem
label="General (heavy after)"
onClick={() => push('after')}
/>
</List>
</NavigationScreen>
}
path={path}
onPathChange={setPath}
getKey={entry => entry}
renderScreen={entry => (
<NavigationScreen title={entry}>
<HeavyScreen entry={entry} mode={mode} />
</NavigationScreen>
)}
data-testid="navigation-stack"
/>
<pre
style={{
position: 'fixed',
insetInlineStart: 8,
bottom: 8,
margin: 0,
padding: 6,
font: '11px/1.3 monospace',
background: 'rgba(0, 0, 0, 0.75)',
color: 'white',
pointerEvents: 'none',
zIndex: 10,
}}>
{`standalone ${String(standalone)} reduce-motion ${String(reducedMotion)} mode ${mode}\n${readout.join('\n')}`}
</pre>
</div>
);
}

/**
* Evidence story: a push whose own commit blocks the main thread for 300 ms
* (`heavy commit`), or whose first moving frame does (`heavy after`). The
* readout lists the pushed card's travelled share in the first eight frames,
* and whether the page runs as a Home Screen app.
*/
export const SettingsHeavyCommit: Story = {
render: () => <HeavyCommitStack />,
};

/** The root screen's bar holds an Edit button at its end (`endContent`). */
function EndContentStack() {
const [path, setPath] = useState<string[]>([]);
Expand Down
Loading