mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
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.
224 lines
6.3 KiB
JavaScript
224 lines
6.3 KiB
JavaScript
/**
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
* @flow
|
|
*/
|
|
|
|
import type {ReactNodeList, ReactFormState} from 'shared/ReactTypes';
|
|
import type {
|
|
FiberRoot,
|
|
SuspenseHydrationCallbacks,
|
|
TransitionTracingCallbacks,
|
|
} from './ReactInternalTypes';
|
|
import type {RootTag} from './ReactRootTags';
|
|
import type {Cache} from './ReactFiberCacheComponent';
|
|
import type {Container} from './ReactFiberConfig';
|
|
|
|
import {noTimeout} from './ReactFiberConfig';
|
|
import {createHostRootFiber} from './ReactFiber';
|
|
import {
|
|
NoLane,
|
|
NoLanes,
|
|
NoTimestamp,
|
|
TotalLanes,
|
|
createLaneMap,
|
|
} from './ReactFiberLane';
|
|
import {
|
|
enableSuspenseCallback,
|
|
enableProfilerCommitHooks,
|
|
enableProfilerTimer,
|
|
enableUpdaterTracking,
|
|
enableTransitionTracing,
|
|
disableLegacyMode,
|
|
enableSwipeTransition,
|
|
} from 'shared/ReactFeatureFlags';
|
|
import {initializeUpdateQueue} from './ReactFiberClassUpdateQueue';
|
|
import {LegacyRoot, ConcurrentRoot} from './ReactRootTags';
|
|
import {createCache, retainCache} from './ReactFiberCacheComponent';
|
|
|
|
export type RootState = {
|
|
element: any,
|
|
isDehydrated: boolean,
|
|
cache: Cache,
|
|
};
|
|
|
|
function FiberRootNode(
|
|
this: $FlowFixMe,
|
|
containerInfo: any,
|
|
// $FlowFixMe[missing-local-annot]
|
|
tag,
|
|
hydrate: any,
|
|
identifierPrefix: any,
|
|
onUncaughtError: any,
|
|
onCaughtError: any,
|
|
onRecoverableError: any,
|
|
formState: ReactFormState<any, any> | null,
|
|
) {
|
|
this.tag = disableLegacyMode ? ConcurrentRoot : tag;
|
|
this.containerInfo = containerInfo;
|
|
this.pendingChildren = null;
|
|
this.current = null;
|
|
this.pingCache = null;
|
|
this.timeoutHandle = noTimeout;
|
|
this.cancelPendingCommit = null;
|
|
this.context = null;
|
|
this.pendingContext = null;
|
|
this.next = null;
|
|
this.callbackNode = null;
|
|
this.callbackPriority = NoLane;
|
|
this.expirationTimes = createLaneMap(NoTimestamp);
|
|
|
|
this.pendingLanes = NoLanes;
|
|
this.suspendedLanes = NoLanes;
|
|
this.pingedLanes = NoLanes;
|
|
this.warmLanes = NoLanes;
|
|
this.expiredLanes = NoLanes;
|
|
this.errorRecoveryDisabledLanes = NoLanes;
|
|
this.shellSuspendCounter = 0;
|
|
|
|
this.entangledLanes = NoLanes;
|
|
this.entanglements = createLaneMap(NoLanes);
|
|
|
|
this.hiddenUpdates = createLaneMap(null);
|
|
|
|
this.identifierPrefix = identifierPrefix;
|
|
this.onUncaughtError = onUncaughtError;
|
|
this.onCaughtError = onCaughtError;
|
|
this.onRecoverableError = onRecoverableError;
|
|
|
|
this.pooledCache = null;
|
|
this.pooledCacheLanes = NoLanes;
|
|
|
|
if (enableSuspenseCallback) {
|
|
this.hydrationCallbacks = null;
|
|
}
|
|
|
|
this.formState = formState;
|
|
|
|
if (enableSwipeTransition) {
|
|
this.pendingGestures = null;
|
|
this.stoppingGestures = null;
|
|
this.gestureClone = null;
|
|
}
|
|
|
|
this.incompleteTransitions = new Map();
|
|
if (enableTransitionTracing) {
|
|
this.transitionCallbacks = null;
|
|
const transitionLanesMap = (this.transitionLanes = []);
|
|
for (let i = 0; i < TotalLanes; i++) {
|
|
transitionLanesMap.push(null);
|
|
}
|
|
}
|
|
|
|
if (enableProfilerTimer && enableProfilerCommitHooks) {
|
|
this.effectDuration = -0;
|
|
this.passiveEffectDuration = -0;
|
|
}
|
|
|
|
if (enableUpdaterTracking) {
|
|
this.memoizedUpdaters = new Set();
|
|
const pendingUpdatersLaneMap = (this.pendingUpdatersLaneMap = []);
|
|
for (let i = 0; i < TotalLanes; i++) {
|
|
pendingUpdatersLaneMap.push(new Set());
|
|
}
|
|
}
|
|
|
|
if (__DEV__) {
|
|
if (disableLegacyMode) {
|
|
// TODO: This varies by each renderer.
|
|
this._debugRootType = hydrate ? 'hydrateRoot()' : 'createRoot()';
|
|
} else {
|
|
switch (tag) {
|
|
case ConcurrentRoot:
|
|
this._debugRootType = hydrate ? 'hydrateRoot()' : 'createRoot()';
|
|
break;
|
|
case LegacyRoot:
|
|
this._debugRootType = hydrate ? 'hydrate()' : 'render()';
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
export function createFiberRoot(
|
|
containerInfo: Container,
|
|
tag: RootTag,
|
|
hydrate: boolean,
|
|
initialChildren: ReactNodeList,
|
|
hydrationCallbacks: null | SuspenseHydrationCallbacks,
|
|
isStrictMode: boolean,
|
|
// TODO: We have several of these arguments that are conceptually part of the
|
|
// host config, but because they are passed in at runtime, we have to thread
|
|
// them through the root constructor. Perhaps we should put them all into a
|
|
// single type, like a DynamicHostConfig that is defined by the renderer.
|
|
identifierPrefix: string,
|
|
onUncaughtError: (
|
|
error: mixed,
|
|
errorInfo: {+componentStack?: ?string},
|
|
) => void,
|
|
onCaughtError: (
|
|
error: mixed,
|
|
errorInfo: {
|
|
+componentStack?: ?string,
|
|
+errorBoundary?: ?React$Component<any, any>,
|
|
},
|
|
) => void,
|
|
onRecoverableError: (
|
|
error: mixed,
|
|
errorInfo: {+componentStack?: ?string},
|
|
) => void,
|
|
transitionCallbacks: null | TransitionTracingCallbacks,
|
|
formState: ReactFormState<any, any> | null,
|
|
): FiberRoot {
|
|
// $FlowFixMe[invalid-constructor] Flow no longer supports calling new on functions
|
|
const root: FiberRoot = (new FiberRootNode(
|
|
containerInfo,
|
|
tag,
|
|
hydrate,
|
|
identifierPrefix,
|
|
onUncaughtError,
|
|
onCaughtError,
|
|
onRecoverableError,
|
|
formState,
|
|
): any);
|
|
if (enableSuspenseCallback) {
|
|
root.hydrationCallbacks = hydrationCallbacks;
|
|
}
|
|
|
|
if (enableTransitionTracing) {
|
|
root.transitionCallbacks = transitionCallbacks;
|
|
}
|
|
|
|
// Cyclic construction. This cheats the type system right now because
|
|
// stateNode is any.
|
|
const uninitializedFiber = createHostRootFiber(tag, isStrictMode);
|
|
root.current = uninitializedFiber;
|
|
uninitializedFiber.stateNode = root;
|
|
|
|
const initialCache = createCache();
|
|
retainCache(initialCache);
|
|
|
|
// The pooledCache is a fresh cache instance that is used temporarily
|
|
// for newly mounted boundaries during a render. In general, the
|
|
// pooledCache is always cleared from the root at the end of a render:
|
|
// it is either released when render commits, or moved to an Offscreen
|
|
// component if rendering suspends. Because the lifetime of the pooled
|
|
// cache is distinct from the main memoizedState.cache, it must be
|
|
// retained separately.
|
|
root.pooledCache = initialCache;
|
|
retainCache(initialCache);
|
|
const initialState: RootState = {
|
|
element: initialChildren,
|
|
isDehydrated: hydrate,
|
|
cache: initialCache,
|
|
};
|
|
uninitializedFiber.memoizedState = initialState;
|
|
|
|
initializeUpdateQueue(uninitializedFiber);
|
|
|
|
return root;
|
|
}
|