Repository navigation
fix(NavigationStack): a platform-animated history back or forward plays no second animation #12004
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
5566d32
test(sandbox): a raw page that logs whether the platform animated a h…
cixzhang 497288d
fix(NavigationStack): a platform-animated history back or forward pla…
cixzhang ad6b37f
fix(NavigationStack): the traversal listeners attach with the container
cixzhang 534e5c1
test(sandbox): the probe stack follows a back to the page's first entry
cixzhang b40e1dd
Merge main into PR #12004's branch
cixzhang 3363e4b
Merge main into PR #12004's branch
cixzhang 34ab1a5
Merge main into fix/navigationstack-ios-platform-back
cixzhang e91f402
chore(sandbox): drop the history-back probe page from this change
cixzhang 6d60a4b
test(NavigationStack): a traversal that hides the stack before the ma…
cixzhang 3d4e0fa
fix(NavigationStack): detaching the traversal listener clears the pla…
cixzhang d8684f5
Merge main into fix/navigationstack-ios-platform-back
cixzhang 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
183 changes: 183 additions & 0 deletions
183
apps/sandbox/src/app/(raw)/pages/history-back-probe/page.tsx
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,183 @@ | ||
| // Copyright (c) Meta Platforms, Inc. and affiliates. | ||
|
|
||
| 'use client'; | ||
|
|
||
| /** | ||
| * @file page.tsx | ||
| * @input A finger on a phone: the buttons that push two history entries and | ||
| * go back, and the platform's own back gesture (Safari's edge swipe, an | ||
| * iOS Home Screen app's edge swipe) | ||
| * @output A bare page that logs, on screen, each popstate and Navigation API | ||
| * navigate event with its hasUAVisualTransition, and the display mode the | ||
| * page runs in. Below the log, a NavigationStack pushes one history entry | ||
| * per push and pops when the history goes back, so the platform's back | ||
| * swipe has history to run on | ||
| * @position A raw page, with no sandbox chrome, for checking on a real phone | ||
| * whether the platform animated a history back (NavigationStack skips its | ||
| * own pop animation after one) | ||
| */ | ||
|
|
||
| import {useEffect, useState} from 'react'; | ||
| import {Button} from '@astryxdesign/core/Button'; | ||
| import {HStack, VStack} from '@astryxdesign/core/Layout'; | ||
| import {List, ListItem} from '@astryxdesign/core/List'; | ||
| import { | ||
| NavigationScreen, | ||
| NavigationStack, | ||
| useNavigationStack, | ||
| } from '@astryxdesign/lab'; | ||
| import {Heading, Text} from '@astryxdesign/core/Text'; | ||
|
|
||
| interface LogEntry { | ||
| time: string; | ||
| event: string; | ||
| detail: string; | ||
| } | ||
|
|
||
| /** The Navigation API's navigate event fields this page reads. */ | ||
| interface NavigateEventLike extends Event { | ||
| navigationType?: string; | ||
| hasUAVisualTransition?: boolean; | ||
| destination?: {url: string}; | ||
| } | ||
|
|
||
| function now(): string { | ||
| return new Date().toISOString().slice(11, 23); | ||
| } | ||
|
|
||
| const ROWS = ['General', 'About', 'Storage']; | ||
|
|
||
| function Rows() { | ||
| const nav = useNavigationStack<string>(); | ||
| return ( | ||
| <List> | ||
| {ROWS.map(row => ( | ||
| <ListItem key={row} label={row} onClick={() => nav.push(row)} /> | ||
| ))} | ||
| </List> | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * A NavigationStack whose every push adds a history entry, and whose path | ||
| * follows the history back and forward. | ||
| */ | ||
| function HistoryStack() { | ||
| const [path, setPath] = useState<string[]>([]); | ||
| useEffect(() => { | ||
| const onPopState = (event: PopStateEvent) => { | ||
| // The probe's own two entries leave the stack alone. Any other entry | ||
| // without a path (the page's first, which the router marks) is the root. | ||
| const state = event.state as {path?: string[]; step?: number} | null; | ||
| if (state?.step != null) { | ||
| return; | ||
| } | ||
| setPath(state?.path ?? []); | ||
| }; | ||
| window.addEventListener('popstate', onPopState); | ||
| return () => window.removeEventListener('popstate', onPopState); | ||
| }, []); | ||
| return ( | ||
| <div style={{height: 420}}> | ||
| <NavigationStack | ||
| label="Probe" | ||
| root={ | ||
| <NavigationScreen title="Probe"> | ||
| <Rows /> | ||
| </NavigationScreen> | ||
| } | ||
| path={path} | ||
| onPathChange={(next: string[]) => { | ||
| if (next.length > path.length) { | ||
| history.pushState({path: next}, '', `?depth=${next.length}`); | ||
| setPath(next); | ||
| } else { | ||
| // The stack's swipe checks the path a frame after its release, so | ||
| // the pop is set now; the popstate that follows sets an equal path. | ||
| setPath(next); | ||
| history.go(next.length - path.length); | ||
|
astracat-bot[bot] marked this conversation as resolved.
Resolved
|
||
| } | ||
| }} | ||
| getKey={(entry: string, index: number) => `${index}-${entry}`} | ||
| renderScreen={(entry: string) => ( | ||
| <NavigationScreen title={entry}> | ||
| <Rows /> | ||
| </NavigationScreen> | ||
| )} | ||
| data-testid="navigation-stack" | ||
| /> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| export default function HistoryBackProbePage() { | ||
| const [log, setLog] = useState<LogEntry[]>([]); | ||
| const [mode, setMode] = useState(''); | ||
|
|
||
| useEffect(() => { | ||
| const add = (event: string, detail: string) => | ||
| setLog(current => | ||
| [{time: now(), event, detail}, ...current].slice(0, 40), | ||
| ); | ||
| const standalone = | ||
| (navigator as Navigator & {standalone?: boolean}).standalone === true; | ||
| setMode( | ||
| `standalone=${standalone} display-mode:standalone=${matchMedia('(display-mode: standalone)').matches} ` + | ||
| `navigation=${'navigation' in window} ${navigator.userAgent}`, | ||
| ); | ||
| const onPopState = (event: PopStateEvent) => | ||
| add( | ||
| 'popstate', | ||
| `hasUAVisualTransition=${String((event as PopStateEvent & {hasUAVisualTransition?: boolean}).hasUAVisualTransition)} ` + | ||
| `state=${JSON.stringify(event.state)} url=${location.search || '/'}`, | ||
| ); | ||
| window.addEventListener('popstate', onPopState); | ||
| const navigation = (window as Window & {navigation?: EventTarget}) | ||
| .navigation; | ||
| const onNavigate = (event: Event) => { | ||
| const navigate = event as NavigateEventLike; | ||
| add( | ||
| 'navigate', | ||
| `type=${navigate.navigationType} hasUAVisualTransition=${String(navigate.hasUAVisualTransition)} ` + | ||
| `to=${navigate.destination?.url.split('?')[1] ?? '/'}`, | ||
| ); | ||
| }; | ||
| navigation?.addEventListener('navigate', onNavigate); | ||
| return () => { | ||
| window.removeEventListener('popstate', onPopState); | ||
| navigation?.removeEventListener('navigate', onNavigate); | ||
| }; | ||
| }, []); | ||
|
|
||
| const pushTwo = () => { | ||
| history.pushState({step: 1}, '', '?step=1'); | ||
| history.pushState({step: 2}, '', '?step=2'); | ||
| setLog(current => [ | ||
| {time: now(), event: 'pushState', detail: 'step=1, step=2'}, | ||
| ...current, | ||
| ]); | ||
| }; | ||
|
|
||
| return ( | ||
| <VStack gap={3} padding={4}> | ||
| <Heading level={1}>History back probe</Heading> | ||
| <Text color="secondary" data-testid="probe-mode"> | ||
| {mode} | ||
| </Text> | ||
| <HStack gap={2}> | ||
| <Button label="Push two entries" onClick={pushTwo} /> | ||
| <Button label="history.back()" onClick={() => history.back()} /> | ||
| </HStack> | ||
| <List data-testid="probe-log"> | ||
| {log.map((entry, index) => ( | ||
| <ListItem | ||
| key={`${entry.time}-${index}`} | ||
| label={`${entry.event} ${entry.time}`} | ||
| description={entry.detail} | ||
| /> | ||
| ))} | ||
| </List> | ||
| <HistoryStack /> | ||
| </VStack> | ||
| ); | ||
| } | ||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.