Repository navigation
chore(NavigationStack): preview the draft record (not for merge) #12002
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
vjeux
wants to merge
15
commits into
meta:main
Choose a base branch
from
vjeux:preview/navigation-stack-spec-demos
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+4,032
−68
Draft
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
f15247e
chore(NavigationStack): preview the draft record (not for merge)
vjeux c98148b
chore(NavigationStack): evictions hold, and cancelled presses clean up
vjeux efbf122
chore(NavigationStack): a change from rest starts at its commit
vjeux 609f313
chore(NavigationStack): pathTransition, and laid-out retained screens
vjeux b0ca12a
chore(NavigationStack): links track their press with Core's tracker
vjeux cca4469
chore(NavigationStack): a changeset for Core's press-tracker re-export
vjeux 96a66b3
Merge branch 'main' into preview/navigation-stack-spec-demos
vjeux 439b4ac
chore(sandbox): NavigationStack pages for its draft record (preview)
vjeux da92db7
chore(sandbox): time each push's commit to its first frame
vjeux dd76ddc
fix(NavigationStack): a bar-hidden screen's header takes taps (preview)
vjeux 21c0f02
chore(sandbox): a page for how a tap-started motion takes its start
vjeux c424e72
chore(sandbox): share the ruler and the first-move watch
vjeux a8bee94
chore(sandbox): hold a motion's first value for a while
vjeux a423fc7
chore(sandbox): compare a motion with and without a hold
vjeux dde2ab8
chore(sandbox): time one inert flip where a slide-out settles
vjeux File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| '@astryxdesign/core': patch | ||
| --- | ||
|
|
||
| [chore] Internal: the internal touch entry re-exports `usePressTracker` for Lab components, as it re-exports `useActionGroupTracking` for Lab's TabBar. Not exported from the package entry point. | ||
|
|
||
| @vjeux |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,367 @@ | ||
| // Copyright (c) Meta Platforms, Inc. and affiliates. | ||
|
|
||
| 'use client'; | ||
|
|
||
| /** | ||
| * @file navigationStackCells.tsx | ||
| * @input Lab's NavigationStack and its opt-in options | ||
| * @output What the NavigationStack phone pages share: a busy loop, the x a | ||
| * screen's card shows, a watch for a change's first moved frame, a readout | ||
| * fixed at the page's bottom, a heavy screen and a windowed list in a | ||
| * screen's own scroller | ||
| * @position Sandbox; imported by the raw navstack-* pages, which a physical | ||
| * phone loads from a pull request's preview | ||
| */ | ||
|
|
||
| import { | ||
| useCallback, | ||
| useLayoutEffect, | ||
| useRef, | ||
| useState, | ||
| type ReactNode, | ||
| } from 'react'; | ||
| import * as stylex from '@stylexjs/stylex'; | ||
| import {useNavigationScroller} from '@astryxdesign/lab'; | ||
| import {List, ListItem} from '@astryxdesign/core/List'; | ||
|
|
||
| /** Holds the main thread for `ms`, as a heavy commit or task does. */ | ||
| export function busyFor(ms: number): void { | ||
| const end = performance.now() + ms; | ||
| while (performance.now() < end) { | ||
| // Spins: nothing else runs on the main thread meanwhile. | ||
| } | ||
| } | ||
|
|
||
| /** A number from the query, or `fallback` when it is missing or not one. */ | ||
| export function queryNumber( | ||
| params: URLSearchParams, | ||
| name: string, | ||
| fallback: number, | ||
| ): number { | ||
| const value = Number(params.get(name)); | ||
| return params.has(name) && Number.isFinite(value) ? value : fallback; | ||
| } | ||
|
|
||
| /** | ||
| * The card of the screen whose entry has `key` (its `getKey`), or the root's | ||
| * card for null. The stack names an entry's card `screen-<key>`. | ||
| */ | ||
| export function cardOf(key: string | null): HTMLElement | null { | ||
| return document.querySelector<HTMLElement>( | ||
| `[data-navigation-screen="${CSS.escape(key == null ? 'root' : `screen-${key}`)}"]`, | ||
| ); | ||
| } | ||
|
|
||
| /** The x the card of `key` shows now, in px, or null with no card. */ | ||
| /** A stack card's key, or the moving element itself. */ | ||
| export type MovingTarget = string | HTMLElement; | ||
|
|
||
| function elementOf(target: MovingTarget): HTMLElement | null { | ||
| return typeof target === 'string' ? cardOf(target) : target; | ||
| } | ||
|
|
||
| export function shownX(target: MovingTarget): number | null { | ||
| const card = elementOf(target); | ||
| if (card == null) { | ||
| return null; | ||
| } | ||
| const translate = getComputedStyle(card).translate; | ||
| return translate === 'none' ? 0 : Number.parseFloat(translate); | ||
| } | ||
|
|
||
| /** A change's first moved frame on the page clock. */ | ||
| export interface FirstMove { | ||
| /** Milliseconds from `since` to the frame's callback time. */ | ||
| ms: number; | ||
| /** How far the card had travelled at that frame: 0 at rest, 1 at its end. */ | ||
| travel: number; | ||
| /** The frame's callback time on the page clock. */ | ||
| at: number; | ||
| } | ||
|
|
||
| /** | ||
| * Calls `onMove` at the first animation frame after `since` in which the | ||
| * card of `key` shows away from `fromX`, with its travel toward `toX`. | ||
| * The page clock counts frame callbacks, which stop while the main thread is | ||
| * held, so a film of the screen stays the measure. | ||
| */ | ||
| export function watchFirstMove( | ||
| target: MovingTarget, | ||
| since: number, | ||
| fromX: (width: number) => number, | ||
| toX: (width: number) => number, | ||
| onMove: (move: FirstMove) => void, | ||
| ): () => void { | ||
| let frame = 0; | ||
| let stopped = false; | ||
| const deadline = since + 3000; | ||
| const tick = (time: number) => { | ||
| if (stopped) { | ||
| return; | ||
| } | ||
| const card = elementOf(target); | ||
| const x = shownX(target); | ||
| if (card != null && x != null) { | ||
| const width = card.parentElement?.clientWidth ?? card.clientWidth; | ||
| const from = fromX(width); | ||
| const to = toX(width); | ||
| if (Math.abs(x - from) > 0.5) { | ||
| onMove({ | ||
| ms: Math.round((time - since) * 10) / 10, | ||
| at: time, | ||
| travel: Math.round(((x - from) / (to - from)) * 1000) / 1000, | ||
| }); | ||
| return; | ||
| } | ||
| } | ||
| if (time < deadline) { | ||
| frame = requestAnimationFrame(tick); | ||
| } | ||
| }; | ||
| frame = requestAnimationFrame(tick); | ||
| return () => { | ||
| stopped = true; | ||
| cancelAnimationFrame(frame); | ||
| }; | ||
| } | ||
|
|
||
| const MARKS = Array.from({length: 11}, (_, index) => index * 10); | ||
|
|
||
| const styles = stylex.create({ | ||
| ruler: { | ||
| position: 'fixed', | ||
| insetInline: 0, | ||
| top: 0, | ||
| height: 14, | ||
| zIndex: 11, | ||
| pointerEvents: 'none', | ||
| }, | ||
| mark: (percent: number) => ({ | ||
| position: 'absolute', | ||
| top: 0, | ||
| insetInlineStart: `${percent}%`, | ||
| width: 1, | ||
| height: percent % 50 === 0 ? 14 : 8, | ||
| backgroundColor: 'rgba(220, 0, 0, 0.8)', | ||
| }), | ||
| readout: { | ||
| position: 'fixed', | ||
| insetInline: 0, | ||
| bottom: 0, | ||
| zIndex: 10, | ||
| maxHeight: '30dvh', | ||
| overflowY: 'auto', | ||
| paddingBlock: 6, | ||
| paddingInline: 10, | ||
| paddingBottom: 'max(6px, env(safe-area-inset-bottom))', | ||
| backgroundColor: 'rgba(0, 0, 0, 0.82)', | ||
| color: '#fff', | ||
| fontFamily: 'ui-monospace, SFMono-Regular, Menlo, monospace', | ||
| fontSize: 12, | ||
| lineHeight: 1.35, | ||
| }, | ||
| line: { | ||
| margin: 0, | ||
| whiteSpace: 'pre-wrap', | ||
| }, | ||
| links: { | ||
| display: 'flex', | ||
| flexWrap: 'wrap', | ||
| gap: 10, | ||
| marginBottom: 4, | ||
| }, | ||
| link: { | ||
| color: '#9cf', | ||
| }, | ||
| stage: { | ||
| position: 'fixed', | ||
| insetInline: 0, | ||
| top: 0, | ||
| bottom: 'var(--navstack-readout-height, 120px)', | ||
| }, | ||
| scroller: { | ||
| height: '100%', | ||
| overflowY: 'auto', | ||
| overscrollBehavior: 'contain', | ||
| }, | ||
| spacer: (height: number) => ({ | ||
| height, | ||
| }), | ||
| inset: (height: number) => ({ | ||
| paddingTop: height, | ||
| }), | ||
| }); | ||
|
|
||
| /** The stage the stack fills: the page above the readout. */ | ||
| export function Stage({children}: {children: ReactNode}) { | ||
| return <div {...stylex.props(styles.stage)}>{children}</div>; | ||
| } | ||
|
|
||
| /** | ||
| * A readout fixed at the page's bottom: links to the page's other arms, then | ||
| * one line per measurement, newest first. It sets the stage's bottom edge. | ||
| */ | ||
| export function Readout({ | ||
| links, | ||
| lines, | ||
| children, | ||
| }: { | ||
| links: ReadonlyArray<{label: string; href: string}>; | ||
| lines: ReadonlyArray<string>; | ||
| children?: ReactNode; | ||
| }) { | ||
| const ref = useRef<HTMLDivElement | null>(null); | ||
| useLayoutEffect(() => { | ||
| const element = ref.current; | ||
| if (element == null) { | ||
| return undefined; | ||
| } | ||
| const measure = () => | ||
| document.documentElement.style.setProperty( | ||
| '--navstack-readout-height', | ||
| `${element.offsetHeight}px`, | ||
| ); | ||
| measure(); | ||
| const observer = new ResizeObserver(measure); | ||
| observer.observe(element); | ||
| return () => observer.disconnect(); | ||
| }, []); | ||
| return ( | ||
| <div ref={ref} data-readout {...stylex.props(styles.readout)}> | ||
| <div {...stylex.props(styles.links)}> | ||
| {links.map(link => ( | ||
| <a key={link.href} href={link.href} {...stylex.props(styles.link)}> | ||
| {link.label} | ||
| </a> | ||
| ))} | ||
| </div> | ||
| {children} | ||
| {lines.map((line, index) => ( | ||
| <p key={lines.length - index} {...stylex.props(styles.line)}> | ||
| {line} | ||
| </p> | ||
| ))} | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| /** Appends a line to a readout, newest first, keeping the last 12. */ | ||
| export function useReadoutLines(): [ | ||
| ReadonlyArray<string>, | ||
| (line: string) => void, | ||
| ] { | ||
| const [lines, setLines] = useState<ReadonlyArray<string>>([]); | ||
| const add = useCallback( | ||
| (line: string) => setLines(current => [line, ...current].slice(0, 12)), | ||
| [], | ||
| ); | ||
| return [lines, add]; | ||
| } | ||
|
|
||
| /** | ||
| * A screen whose mount is heavy: `rows` list rows, and `layoutMs` of work in a | ||
| * layout effect when it mounts, as a large screen's measuring code does. | ||
| */ | ||
| export function HeavyScreen({ | ||
| name, | ||
| rows, | ||
| layoutMs, | ||
| }: { | ||
| name: string; | ||
| rows: number; | ||
| layoutMs: number; | ||
| }) { | ||
| useLayoutEffect(() => { | ||
| busyFor(layoutMs); | ||
| }, [layoutMs]); | ||
| return ( | ||
| <List> | ||
| {Array.from({length: rows}, (_, index) => ( | ||
| <ListItem | ||
| key={index} | ||
| label={`${name}, message ${index + 1}`} | ||
| description="A long screen, so its mount takes time." | ||
| /> | ||
| ))} | ||
| </List> | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * A long list in the screen's own scroller (`scroll="content"`) that renders | ||
| * only the rows near the viewport, as a windowed list does. | ||
| */ | ||
| export function WindowedList({ | ||
| count, | ||
| renderRow, | ||
| overscan = 8, | ||
| }: { | ||
| count: number; | ||
| renderRow: (index: number) => ReactNode; | ||
| overscan?: number; | ||
| }) { | ||
| const scroller = useNavigationScroller(); | ||
| const boxRef = useRef<HTMLDivElement | null>(null); | ||
| const [rowHeight, setRowHeight] = useState(56); | ||
| const [range, setRange] = useState({first: 0, last: 30}); | ||
| const update = useCallback(() => { | ||
| const box = boxRef.current; | ||
| if (box == null) { | ||
| return; | ||
| } | ||
| const first = Math.max(0, Math.floor(box.scrollTop / rowHeight) - overscan); | ||
| const last = Math.min( | ||
| count, | ||
| Math.ceil((box.scrollTop + box.clientHeight) / rowHeight) + overscan, | ||
| ); | ||
| setRange(current => | ||
| current.first === first && current.last === last | ||
| ? current | ||
| : {first, last}, | ||
| ); | ||
| }, [count, overscan, rowHeight]); | ||
| const attach = useCallback( | ||
| (element: HTMLDivElement | null) => { | ||
| boxRef.current = element; | ||
| scroller.ref(element); | ||
| }, | ||
| [scroller], | ||
| ); | ||
| useLayoutEffect(() => { | ||
| const row = boxRef.current?.querySelector<HTMLElement>('[data-row]'); | ||
| if (row != null && row.offsetHeight > 0) { | ||
| setRowHeight(row.offsetHeight); | ||
| } | ||
| update(); | ||
| }, [update]); | ||
| const rows: ReactNode[] = []; | ||
| for (let index = range.first; index < range.last; index += 1) { | ||
| rows.push( | ||
| <div key={index} data-row> | ||
| {renderRow(index)} | ||
| </div>, | ||
| ); | ||
| } | ||
| return ( | ||
| <div | ||
| ref={attach} | ||
| onScroll={update} | ||
| {...stylex.props(styles.scroller, styles.inset(scroller.barInset))}> | ||
| <div {...scroller.sentinelProps} /> | ||
| <div {...stylex.props(styles.spacer(range.first * rowHeight))} /> | ||
| <List>{rows}</List> | ||
| <div {...stylex.props(styles.spacer((count - range.last) * rowHeight))} /> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| /** Marks every tenth of the screen's width, for reading a film. */ | ||
| export function Ruler() { | ||
| return ( | ||
| <div aria-hidden {...stylex.props(styles.ruler)}> | ||
| {MARKS.map(percent => ( | ||
| <span key={percent} {...stylex.props(styles.mark(percent))} /> | ||
| ))} | ||
| </div> | ||
| ); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Non-blocking: these two doc comments now both sit on
MovingTarget, soshownX(:63) has none, and the first still names "the card ofkey". Move it ontoshownXwith "the target" in place of "the card ofkey", and make the same change inwatchFirstMove's doc (:83-84).