Skip to content

Commit c5c2696

Browse files
authored
Enhance two-phase transition explainer with new examples
Updated the example code to demonstrate the use of deferPageSwap for cross-document navigation and added details about the historyChange option.
1 parent 73ca7e1 commit c5c2696

1 file changed

Lines changed: 33 additions & 8 deletions

File tree

css-view-transitions-2/two-phase-transition-explainer.md

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,45 @@ To achieve that, proposing two provide the following:
4242

4343
```js
4444
// Returns a boolean if the page is prerendered/BFCached and not render-blocked.
45-
navigateEvent.nextDocumentReadyToRender
45+
navigateEvent.destination.ready
4646

47-
// Allows delaying the commit *without* intercepting as a same-document navigation.
48-
navigateEvent.deferCommit(promise)
49-
```
47+
// Allows delaying the cross-document commit (aka page-swap) *without* intercepting this as a same-document navigation.
48+
navigateEvent.deferPageSwap({
49+
// "immediate", which is also the default, means that the history is swapped immediately, like a `pushState`,
50+
// and the navigation becomes a `replace`.
51+
historyChange: "immediate" | "after-transition",
52+
53+
// When the returned promise resovles, the navigation can proceed.
54+
// The handler can register a "restore" callback, to be called if the navigation is aborted
55+
// or if the page is restored from BFCache.
56+
handler: (controller) => Promise
57+
});```
5058
5159
Possible usage:
5260
```js
53-
if (!navigateEvent.nextDocumentReadyToRender) {
54-
const transition = document.startViewTransition(show_preview);
55-
navigateEvent.deferCommit(transition.finished);
56-
}
61+
navigation.addEventListener("navigate", event => {
62+
if (!event.destination.ready) {
63+
event.deferPageSwap({
64+
// This means that the history is swapped immediately, like a `pushState`,
65+
// and the navigation becomes a `replace`.
66+
historyChange: "immediate",
67+
async handler(controller) {
68+
const transition = document.startViewTransition(() => show_preview());
69+
70+
// The restore callback will be called if the navigation is aborted, or if this document is restored from BFCache.
71+
controller.addRestoreCallback(() => hide_preview());
72+
return transition.finished;
73+
}
74+
});
75+
}
76+
});
5777
```
5878

79+
### Some more details for deferred commit/page-swap
80+
- By default, the new history entry applies immediately, like a `pushState` or `replaceState`.
81+
This makes it so that a quick press on "back" or so doesn't go too far back. The `historyChange` option can opt out of this behavior.
82+
- Only same-origin navigations without cross-origin redirects are deferrable.
83+
5984

6085
## Allowing animations to defer commit for a short period
6186

0 commit comments

Comments
 (0)