Design Notes
For contributors and adapter authors: the rules the core follows. The source marks each rule where it applies (packages/route-stack/src/core/core.ts).
The core as a state machine
- State: the committed screens (newest first), a pending new screen being rendered, the screen shown, the saved scroll of each screen, and at most one running slide.
- Inputs: a render and its commit, history events, a finger, a slide played to its end, the page being hidden.
- Outputs: slide commands, the window scroll, and a re-render when screens are dropped.
A running slide is in one phase:
| Phase | Meaning |
|---|---|
drag | A finger moves it |
play | It plays to one end |
rest | It reached the end, but the back a swipe sent has not committed yet |
await marks a slide waiting for the commit of the back its swipe sent.
Rules
| Rule | When | What |
|---|---|---|
| R1 resolve | Render | Pick the screen for this path: ① the current entry's screen at the same path (a hidden one at another path absorbs the entry when the render is current and the screen can show the path: the same pathname, or as sameScreen keeps it) ② the shown screen at the same path ③ the shown screen when it is the current entry's screen and can show the path; ahead of history ({ ahead }), only when sameScreen keeps the move ④ else a new pending screen. Same input, same result. |
| R2 commit | Commit | Ignore a render that is not "current" (except the first). A pending screen joins the list and takes the current entry's screen. A shown screen drawn for an entry no screen shows yet absorbs that entry (a guard sending a back on to the screen left). |
| R3 same screen | Commit | The shown screen did not change: nothing. If an entry was absorbed, a swipe's back goes to rest (R10). |
| R4 change | Commit | Direction from where both screens were last in history. A screen without a saved scroll starts at the #hash element or 0. animate gives a slide, or settles at once. A traversal the browser drew itself (hasUAVisualTransition) takes that slide with nothing moving. |
| R5 retarget | Commit | The screen sliding in was replaced (a redirect): the slide goes on to the new screen, no new slide. Not a slide with nothing moving: the new screen gets a slide of its own. |
| R6 cut | Commit | Another navigation ends the running slide where React already is, then starts its own. A finger still down is ignored. |
| R7 drag | Finger moves | Seek to the progress, and scroll the window to the nearer screen (Scrolling ahead). |
| R8 release | Finger leaves | Back: play forward and await, history.back() on the next frame. Stay: play back to rest. |
| R9 swipe commit | Commit | The awaited back committed: keep the slide, show both screens again. If it already reached the end, settle. |
| R10 back to rest | Frame after a traversal, or an absorbing commit | Awaiting, and the current entry is the shown screen, or view() is "held": play back to rest. After any traversal, the shown screen not the current entry's: render the stack again (a traversal between two entries of one URL renders nothing in Next). |
| R11 settle | Slide ended | The one exit: drop the layers on the shown screen (if the window is not there yet, scroll it under the paused layers and drop a frame after its last move), restore its scroll, drop screens no entry at or behind the current one shows, re-render only if one was dropped. |
| R12 hidden | pagehide, visibilitychange | Settle any slide; a back not sent is not sent. Frames stop while hidden. |
Why these choices
- Screens are decided by history, not by renders. A router may not render for an entry (a query push in Next), or render it late (loaders). Deciding when the entry becomes current never misses one.
- One commit point. The core only acts on committed renders. React may render twice or throw a render away; the result is the same.
- One exit. Every slide ends in settle, whether it played, was cancelled, or had no animation.
displayis never saved and restored. React may hide or show a screen while it is lifted; restoring an old value would swap screens.- No negative
playbackRate, noreverse(). iOS WebKit ends such animations at once. Playing to the other end builds a new animation from the values on screen.