Skip to content

Commit eb3181e

Browse files
authored
Add Visibility flag for hiding/unhiding trees (#20043)
* Add Visibility flag for hiding/unhiding trees There's `beforeblur` logic in the snapshot phase that needs to visit every Suspense boundary whose visibility is toggled. Right now it does that by visiting Placement and Deletion effects. That includes many unrelated nodes. By adding a new flag specifically for toggling Visibility, we will only visit the relevant Suspense (and Offscreen) boundaries, instead of all nodes that have a Placement. Potential follow-ups (not urgent): - The `beforeblur` logic also has a check to see whether the visibility was toggled on or off. It only cares about things being hidden. As a follow up, I can split the Visibility flag into separate Hide/Show flags, and only visit Hide. - Now that this is separate from Update, we can move the rest of the Suspense's layout effects (like attaching retry listeners) to the passive phase. * Gate behind createEventHandle feature flag Only need to visit deleted and hidden trees during the snapshot phase if the experimental `createEventHandle` flag is enabled. Currently, it's only used internally at Facebook, not open source.
1 parent 0dd809b commit eb3181e

File tree

3 files changed

+52
-37
lines changed

3 files changed

+52
-37
lines changed

packages/react-reconciler/src/ReactFiberCompleteWork.new.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ import {
7373
NoFlags,
7474
DidCapture,
7575
Snapshot,
76+
Visibility,
7677
MutationMask,
7778
LayoutMask,
7879
PassiveMask,
@@ -1154,8 +1155,8 @@ function completeWork(
11541155
// TODO: Only schedule updates if not prevDidTimeout.
11551156
if (nextDidTimeout) {
11561157
// If this boundary just timed out, schedule an effect to attach a
1157-
// retry listener to the promise. This flag is also used to hide the
1158-
// primary children.
1158+
// retry listener to the promise.
1159+
// TODO: Move to passive phase
11591160
workInProgress.flags |= Update;
11601161
}
11611162
}
@@ -1167,7 +1168,7 @@ function completeWork(
11671168
// primary children. In mutation mode, we also need the flag to
11681169
// *unhide* children that were previously hidden, so check if this
11691170
// is currently timed out, too.
1170-
workInProgress.flags |= Update;
1171+
workInProgress.flags |= Update | Visibility;
11711172
}
11721173
}
11731174
if (
@@ -1176,6 +1177,7 @@ function completeWork(
11761177
workInProgress.memoizedProps.suspenseCallback != null
11771178
) {
11781179
// Always notify the callback
1180+
// TODO: Move to passive phase
11791181
workInProgress.flags |= Update;
11801182
}
11811183
bubbleProperties(workInProgress);
@@ -1523,7 +1525,7 @@ function completeWork(
15231525
prevIsHidden !== nextIsHidden &&
15241526
newProps.mode !== 'unstable-defer-without-hiding'
15251527
) {
1526-
workInProgress.flags |= Update;
1528+
workInProgress.flags |= Update | Visibility;
15271529
}
15281530
}
15291531

packages/react-reconciler/src/ReactFiberFlags.js

+42-32
Original file line numberDiff line numberDiff line change
@@ -7,62 +7,72 @@
77
* @flow
88
*/
99

10+
import {enableCreateEventHandleAPI} from 'shared/ReactFeatureFlags';
11+
1012
export type Flags = number;
1113

1214
// Don't change these two values. They're used by React Dev Tools.
13-
export const NoFlags = /* */ 0b000000000000000000;
14-
export const PerformedWork = /* */ 0b000000000000000001;
15+
export const NoFlags = /* */ 0b0000000000000000000;
16+
export const PerformedWork = /* */ 0b0000000000000000001;
1517

1618
// You can change the rest (and add more).
17-
export const Placement = /* */ 0b000000000000000010;
18-
export const Update = /* */ 0b000000000000000100;
19-
export const PlacementAndUpdate = /* */ 0b000000000000000110;
20-
export const Deletion = /* */ 0b000000000000001000;
21-
export const ContentReset = /* */ 0b000000000000010000;
22-
export const Callback = /* */ 0b000000000000100000;
23-
export const DidCapture = /* */ 0b000000000001000000;
24-
export const Ref = /* */ 0b000000000010000000;
25-
export const Snapshot = /* */ 0b000000000100000000;
26-
export const Passive = /* */ 0b000000001000000000;
27-
// TODO (effects) Remove this bit once the new reconciler is synced to the old.
28-
export const PassiveUnmountPendingDev = /* */ 0b000010000000000000;
29-
export const Hydrating = /* */ 0b000000010000000000;
30-
export const HydratingAndUpdate = /* */ 0b000000010000000100;
19+
export const Placement = /* */ 0b0000000000000000010;
20+
export const Update = /* */ 0b0000000000000000100;
21+
export const PlacementAndUpdate = /* */ 0b0000000000000000110;
22+
export const Deletion = /* */ 0b0000000000000001000;
23+
export const ContentReset = /* */ 0b0000000000000010000;
24+
export const Callback = /* */ 0b0000000000000100000;
25+
export const DidCapture = /* */ 0b0000000000001000000;
26+
export const Ref = /* */ 0b0000000000010000000;
27+
export const Snapshot = /* */ 0b0000000000100000000;
28+
export const Passive = /* */ 0b0000000001000000000;
29+
export const Hydrating = /* */ 0b0000000010000000000;
30+
export const HydratingAndUpdate = /* */ 0b0000000010000000100;
31+
export const Visibility = /* */ 0b0000000100000000000;
3132

3233
// Passive & Update & Callback & Ref & Snapshot
33-
export const LifecycleEffectMask = /* */ 0b000000001110100100;
34+
export const LifecycleEffectMask = /* */ 0b0000000001110100100;
3435

3536
// Union of all host effects
36-
export const HostEffectMask = /* */ 0b000000011111111111;
37+
export const HostEffectMask = /* */ 0b0000000111111111111;
3738

3839
// These are not really side effects, but we still reuse this field.
39-
export const Incomplete = /* */ 0b000000100000000000;
40-
export const ShouldCapture = /* */ 0b000001000000000000;
41-
export const ForceUpdateForLegacySuspense = /* */ 0b000100000000000000;
40+
export const Incomplete = /* */ 0b0000001000000000000;
41+
export const ShouldCapture = /* */ 0b0000010000000000000;
42+
// TODO (effects) Remove this bit once the new reconciler is synced to the old.
43+
export const PassiveUnmountPendingDev = /* */ 0b0000100000000000000;
44+
export const ForceUpdateForLegacySuspense = /* */ 0b0001000000000000000;
4245

4346
// Static tags describe aspects of a fiber that are not specific to a render,
4447
// e.g. a fiber uses a passive effect (even if there are no updates on this particular render).
4548
// This enables us to defer more work in the unmount case,
4649
// since we can defer traversing the tree during layout to look for Passive effects,
4750
// and instead rely on the static flag as a signal that there may be cleanup work.
48-
export const PassiveStatic = /* */ 0b001000000000000000;
51+
export const PassiveStatic = /* */ 0b0010000000000000000;
4952

5053
// Union of side effect groupings as pertains to subtreeFlags
51-
// TODO: Don't need to visit Placement during BeforeMutation phase
52-
// TODO: Only need to visit Deletions during BeforeMutation phase if an element
53-
// is focused.
54-
export const BeforeMutationMask = /* */ 0b000000000100001010;
55-
export const MutationMask = /* */ 0b000000010010011110;
56-
export const LayoutMask = /* */ 0b000000000010100100;
57-
export const PassiveMask = /* */ 0b000000001000001000;
54+
55+
export const BeforeMutationMask =
56+
Snapshot |
57+
(enableCreateEventHandleAPI
58+
? // createEventHandle needs to visit deleted and hidden trees to
59+
// fire beforeblur
60+
// TODO: Only need to visit Deletions during BeforeMutation phase if an
61+
// element is focused.
62+
Deletion | Visibility
63+
: 0);
64+
65+
export const MutationMask = /* */ 0b0000000110010011110;
66+
export const LayoutMask = /* */ 0b0000000000010100100;
67+
export const PassiveMask = /* */ 0b0000000001000001000;
5868

5969
// Union of tags that don't get reset on clones.
6070
// This allows certain concepts to persist without recalculting them,
6171
// e.g. whether a subtree contains passive effects or portals.
62-
export const StaticMask = /* */ 0b001000000000000000;
72+
export const StaticMask = /* */ 0b0010000000000000000;
6373

6474
// These flags allow us to traverse to fibers that have effects on mount
6575
// without traversing the entire tree after every commit for
6676
// double invoking
67-
export const MountLayoutDev = /* */ 0b010000000000000000;
68-
export const MountPassiveDev = /* */ 0b100000000000000000;
77+
export const MountLayoutDev = /* */ 0b0100000000000000000;
78+
export const MountPassiveDev = /* */ 0b1000000000000000000;

packages/react-reconciler/src/ReactFiberWorkLoop.new.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ import {
134134
HostEffectMask,
135135
Hydrating,
136136
HydratingAndUpdate,
137+
Visibility,
137138
BeforeMutationMask,
138139
MutationMask,
139140
LayoutMask,
@@ -2155,8 +2156,10 @@ function commitBeforeMutationEffectsImpl(fiber: Fiber) {
21552156

21562157
if (!shouldFireAfterActiveInstanceBlur && focusedInstanceHandle !== null) {
21572158
// Check to see if the focused element was inside of a hidden (Suspense) subtree.
2158-
// TODO: Move this out of the hot path using a dedicated effect tag.
21592159
if (
2160+
// TODO: Can optimize this further with separate Hide and Show flags. We
2161+
// only care about Hide here.
2162+
(flags & Visibility) !== NoFlags &&
21602163
fiber.tag === SuspenseComponent &&
21612164
isSuspenseBoundaryBeingHidden(current, fiber) &&
21622165
doesFiberContain(fiber, focusedInstanceHandle)

0 commit comments

Comments
 (0)