Skip to content

Commit e6ff683

Browse files
committed
Use the same flags as Should/MaySuspendCommit to track named view transitions
1 parent 71c99a0 commit e6ff683

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

packages/react-reconciler/src/ReactFiberBeginWork.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ import {
9797
Passive,
9898
DidDefer,
9999
ViewTransitionNamedStatic,
100+
ViewTransitionNamedMount,
100101
LayoutStatic,
101102
} from './ReactFiberFlags';
102103
import {
@@ -3242,7 +3243,10 @@ function updateViewTransition(
32423243
if (pendingProps.name != null && pendingProps.name !== 'auto') {
32433244
// Explicitly named boundary. We track it so that we can pair it up with another explicit
32443245
// boundary if we get deleted.
3245-
workInProgress.flags |= ViewTransitionNamedStatic;
3246+
workInProgress.flags |=
3247+
current === null
3248+
? ViewTransitionNamedMount | ViewTransitionNamedStatic
3249+
: ViewTransitionNamedStatic;
32463250
} else {
32473251
// Assign an auto generated name using the useId algorthim if an explicit one is not provided.
32483252
// We don't need the name yet but we do it here to allow hydration state to be used.

packages/react-reconciler/src/ReactFiberCommitWork.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4808,6 +4808,10 @@ export function commitPassiveUnmountEffects(finishedWork: Fiber): void {
48084808
// already in the "current" tree. Because their visibility has changed, the
48094809
// browser may not have prerendered them yet. So we check the MaySuspendCommit
48104810
// flag instead.
4811+
//
4812+
// Note that MaySuspendCommit and ShouldSuspendCommit also includes named
4813+
// ViewTransitions so that we know to also visit those to collect appearing
4814+
// pairs.
48114815
let suspenseyCommitFlag = ShouldSuspendCommit;
48124816
export function accumulateSuspenseyCommit(finishedWork: Fiber): void {
48134817
appearingViewTransitions = null;
@@ -4889,6 +4893,29 @@ function accumulateSuspenseyCommitOnFiber(fiber: Fiber) {
48894893
}
48904894
break;
48914895
}
4896+
case ViewTransitionComponent: {
4897+
if (enableViewTransition) {
4898+
if ((fiber.flags & suspenseyCommitFlag) !== NoFlags) {
4899+
const props: ViewTransitionProps = fiber.memoizedProps;
4900+
const name: ?string | 'auto' = props.name;
4901+
if (name != null && name !== 'auto') {
4902+
// This is a named ViewTransition being mounted or reappearing. Let's add it to
4903+
// the map so we can match it with deletions later.
4904+
if (appearingViewTransitions === null) {
4905+
appearingViewTransitions = new Map();
4906+
}
4907+
// Reset the pair in case we didn't end up restoring the instance in previous commits.
4908+
// This shouldn't really happen anymore but just in case. We could maybe add an invariant.
4909+
const instance: ViewTransitionState = fiber.stateNode;
4910+
instance.paired = null;
4911+
appearingViewTransitions.set(name, instance);
4912+
}
4913+
}
4914+
recursivelyAccumulateSuspenseyCommit(fiber);
4915+
break;
4916+
}
4917+
// Fallthrough
4918+
}
48924919
default: {
48934920
recursivelyAccumulateSuspenseyCommit(fiber);
48944921
}

packages/react-reconciler/src/ReactFiberFlags.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export const StoreConsistency = /* */ 0b0000000000000000100000000000
4444
// possible, because we're about to run out of bits.
4545
export const ScheduleRetry = StoreConsistency;
4646
export const ShouldSuspendCommit = Visibility;
47+
export const ViewTransitionNamedMount = ShouldSuspendCommit;
4748
export const DidDefer = ContentReset;
4849
export const FormReset = Snapshot;
4950
export const AffectedParentLayout = ContentReset;
@@ -74,8 +75,10 @@ export const PassiveStatic = /* */ 0b0000000100000000000000000000
7475
export const MaySuspendCommit = /* */ 0b0000001000000000000000000000000;
7576
// ViewTransitionNamedStatic tracks explicitly name ViewTransition components deeply
7677
// that might need to be visited during clean up. This is similar to SnapshotStatic
77-
// if there was any other use for it.
78-
export const ViewTransitionNamedStatic = /* */ SnapshotStatic;
78+
// if there was any other use for it. It also needs to run in the same phase as
79+
// MaySuspendCommit tracking.
80+
export const ViewTransitionNamedStatic =
81+
/* */ SnapshotStatic | MaySuspendCommit;
7982
// ViewTransitionStatic tracks whether there are an ViewTransition components from
8083
// the nearest HostComponent down. It resets at every HostComponent level.
8184
export const ViewTransitionStatic = /* */ 0b0000010000000000000000000000000;

0 commit comments

Comments
 (0)