Skip to content

Commit dc5ed00

Browse files
committed
Add isHidden to OffscreenInstance
We need to be able to read whether an offscreen tree is hidden from an imperative event. We can store this on its OffscreenInstance. We were already scheduling a commit effect whenever the visibility changes, in order to toggle the inner effects. So we can reuse that.
1 parent 7e8a020 commit dc5ed00

File tree

5 files changed

+31
-3
lines changed

5 files changed

+31
-3
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,9 @@ export function createFiberFromOffscreen(
715715
const fiber = createFiber(OffscreenComponent, pendingProps, key, mode);
716716
fiber.elementType = REACT_OFFSCREEN_TYPE;
717717
fiber.lanes = lanes;
718-
const primaryChildInstance: OffscreenInstance = {};
718+
const primaryChildInstance: OffscreenInstance = {
719+
isHidden: false,
720+
};
719721
fiber.stateNode = primaryChildInstance;
720722
return fiber;
721723
}

packages/react-reconciler/src/ReactFiber.old.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,9 @@ export function createFiberFromOffscreen(
715715
const fiber = createFiber(OffscreenComponent, pendingProps, key, mode);
716716
fiber.elementType = REACT_OFFSCREEN_TYPE;
717717
fiber.lanes = lanes;
718-
const primaryChildInstance: OffscreenInstance = {};
718+
const primaryChildInstance: OffscreenInstance = {
719+
isHidden: false,
720+
};
719721
fiber.stateNode = primaryChildInstance;
720722
return fiber;
721723
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2309,8 +2309,14 @@ function commitMutationEffectsOnFiber(
23092309
const offscreenFiber: Fiber = (finishedWork.child: any);
23102310

23112311
if (offscreenFiber.flags & Visibility) {
2312+
const offscreenInstance: OffscreenInstance = offscreenFiber.stateNode;
23122313
const newState: OffscreenState | null = offscreenFiber.memoizedState;
23132314
const isHidden = newState !== null;
2315+
2316+
// Track the current state on the Offscreen instance so we can
2317+
// read it during an event
2318+
offscreenInstance.isHidden = isHidden;
2319+
23142320
if (isHidden) {
23152321
const wasHidden =
23162322
offscreenFiber.alternate !== null &&
@@ -2354,10 +2360,15 @@ function commitMutationEffectsOnFiber(
23542360
commitReconciliationEffects(finishedWork);
23552361

23562362
if (flags & Visibility) {
2363+
const offscreenInstance: OffscreenInstance = finishedWork.stateNode;
23572364
const newState: OffscreenState | null = finishedWork.memoizedState;
23582365
const isHidden = newState !== null;
23592366
const offscreenBoundary: Fiber = finishedWork;
23602367

2368+
// Track the current state on the Offscreen instance so we can
2369+
// read it during an event
2370+
offscreenInstance.isHidden = isHidden;
2371+
23612372
if (enableSuspenseLayoutEffectSemantics) {
23622373
if (isHidden) {
23632374
if (!wasHidden) {

packages/react-reconciler/src/ReactFiberCommitWork.old.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2309,8 +2309,14 @@ function commitMutationEffectsOnFiber(
23092309
const offscreenFiber: Fiber = (finishedWork.child: any);
23102310

23112311
if (offscreenFiber.flags & Visibility) {
2312+
const offscreenInstance: OffscreenInstance = offscreenFiber.stateNode;
23122313
const newState: OffscreenState | null = offscreenFiber.memoizedState;
23132314
const isHidden = newState !== null;
2315+
2316+
// Track the current state on the Offscreen instance so we can
2317+
// read it during an event
2318+
offscreenInstance.isHidden = isHidden;
2319+
23142320
if (isHidden) {
23152321
const wasHidden =
23162322
offscreenFiber.alternate !== null &&
@@ -2354,10 +2360,15 @@ function commitMutationEffectsOnFiber(
23542360
commitReconciliationEffects(finishedWork);
23552361

23562362
if (flags & Visibility) {
2363+
const offscreenInstance: OffscreenInstance = finishedWork.stateNode;
23572364
const newState: OffscreenState | null = finishedWork.memoizedState;
23582365
const isHidden = newState !== null;
23592366
const offscreenBoundary: Fiber = finishedWork;
23602367

2368+
// Track the current state on the Offscreen instance so we can
2369+
// read it during an event
2370+
offscreenInstance.isHidden = isHidden;
2371+
23612372
if (enableSuspenseLayoutEffectSemantics) {
23622373
if (isHidden) {
23632374
if (!wasHidden) {

packages/react-reconciler/src/ReactFiberOffscreenComponent.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,6 @@ export type OffscreenQueue = {|
3838
transitions: Array<Transition> | null,
3939
|} | null;
4040

41-
export type OffscreenInstance = {};
41+
export type OffscreenInstance = {|
42+
isHidden: boolean,
43+
|};

0 commit comments

Comments
 (0)