Skip to content

Commit e9252bc

Browse files
authored
During a Swipe Gesture Render a Clone Offscreen and Animate it Onscreen (#32500)
This is really the essence mechanism of the `useSwipeTransition` feature. We don't want to immediately switch to the destination state when starting a gesture. The effects remain mounted on the current state. We want the current state to be "live". This is important to for example allow a video to keeping playing while starting a swipe (think TikTok/Reels) and not stop until you've committed the action. The only thing that can be live is the "new" state. Therefore we treat the destination as the "old" state and perform a reverse animation from there. Ideally we could apply the old state to the DOM tree, take a snapshot and then revert it back in the mutation of `startViewTransition`. Unfortunately, the way `startViewTransition` was designed it always paints one frame of the "old" state which would lead this to cause a flicker. To work around this, we need to create a clone of any View Transition boundary that might be mutated and then render that offscreen. That way we can render the "current" state on screen and the "destination" state offscreen for the screenshots. Being mutated can be either due to React doing a DOM mutation or if a child boundary resizes that causes the parent to relayout. We don't have to do this for insertions or deletions since they only appear on one side. The worst case scenario is that we have to clone the whole root. That's what this first PR implements. We clone the container and if it's not absolutely positioned, we position it on top of the current one. If the container is `document` or `<html>` we instead clone the `<body>` tag since it's the only one we can insert a duplicate of. If the container is deep in the tree we clone just that even though technically we should probably clone the whole document in that case. We just keep the impact smaller. Ideally though we'd never hit this case. In fact, if we clone the document we issue a warning (always for now) since you probably should optimize this. In the future I intend to add optimizations when affected View Transition boundaries are absolutely positioned since they cannot possibly relayout the parent. This would be the ideal way to use this feature most efficiently but it still works without it. Since we render the "old" state outside the viewport, we need to then adjust the animation to put it back into the viewport. This is the trickiest part to get right while still preserving any customization of the View Transitions done using CSS. This current approach reapplies all the animations with adjusted keyframes. In the case of an "exit" the pseudo-element itself is positioned outside the viewport but since we can't programmatically update the style of the pseudo-element itself we instead adjust all the keyframes to put it back into the viewport. If there is no animation on the group we add one. In the case of an "update" the pseudo-element is positioned on the new state which is already inside the viewport. However, the auto-generated animation of the group has a starting keyframe that starts outside the viewport. In this case we need to adjust that keyframe. In the future I might explore a technique that inserts stylesheets instead of mutating the animations. It might be simpler. But whatever hacks work to maximize the compatibility is best.
1 parent e0fe347 commit e9252bc

File tree

14 files changed

+981
-17
lines changed

14 files changed

+981
-17
lines changed

fixtures/view-transition/src/components/Page.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import React, {
22
unstable_ViewTransition as ViewTransition,
33
unstable_Activity as Activity,
44
unstable_useSwipeTransition as useSwipeTransition,
5+
useEffect,
6+
useState,
57
} from 'react';
68

79
import SwipeRecognizer from './SwipeRecognizer';
@@ -53,6 +55,13 @@ export default function Page({url, navigate}) {
5355
navigate(show ? '/?a' : '/?b');
5456
}
5557

58+
const [counter, setCounter] = useState(0);
59+
60+
useEffect(() => {
61+
const timer = setInterval(() => setCounter(c => c + 1), 1000);
62+
return () => clearInterval(timer);
63+
}, []);
64+
5665
const exclamation = (
5766
<ViewTransition name="exclamation" onShare={onTransition}>
5867
<span>!</span>
@@ -76,7 +85,7 @@ export default function Page({url, navigate}) {
7685
'navigation-back': transitions['slide-right'],
7786
'navigation-forward': transitions['slide-left'],
7887
}}>
79-
<h1>{!show ? 'A' : 'B'}</h1>
88+
<h1>{!show ? 'A' + counter : 'B' + counter}</h1>
8089
</ViewTransition>
8190
{show ? (
8291
<div>

packages/react-art/src/ReactFiberConfigART.js

+16
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,10 @@ export function createInstance(type, props, internalInstanceHandle) {
302302
return instance;
303303
}
304304

305+
export function cloneMutableInstance(instance, keepChildren) {
306+
return instance;
307+
}
308+
305309
export function createTextInstance(
306310
text,
307311
rootContainerInstance,
@@ -310,6 +314,10 @@ export function createTextInstance(
310314
return text;
311315
}
312316

317+
export function cloneMutableTextInstance(textInstance) {
318+
return textInstance;
319+
}
320+
313321
export function finalizeInitialChildren(domElement, type, props) {
314322
return false;
315323
}
@@ -475,6 +483,14 @@ export function restoreRootViewTransitionName(rootContainer) {
475483
// Noop
476484
}
477485

486+
export function cloneRootViewTransitionContainer(rootContainer) {
487+
throw new Error('Not implemented.');
488+
}
489+
490+
export function removeRootViewTransitionClone(rootContainer, clone) {
491+
throw new Error('Not implemented.');
492+
}
493+
478494
export type InstanceMeasurement = null;
479495

480496
export function measureInstance(instance) {

0 commit comments

Comments
 (0)