Skip to content

Commit

Permalink
Add isHidden to OffscreenInstance
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
acdlite committed Jun 7, 2022
1 parent 7e8a020 commit dc5ed00
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 3 deletions.
4 changes: 3 additions & 1 deletion packages/react-reconciler/src/ReactFiber.new.js
Expand Up @@ -715,7 +715,9 @@ export function createFiberFromOffscreen(
const fiber = createFiber(OffscreenComponent, pendingProps, key, mode);
fiber.elementType = REACT_OFFSCREEN_TYPE;
fiber.lanes = lanes;
const primaryChildInstance: OffscreenInstance = {};
const primaryChildInstance: OffscreenInstance = {
isHidden: false,
};
fiber.stateNode = primaryChildInstance;
return fiber;
}
Expand Down
4 changes: 3 additions & 1 deletion packages/react-reconciler/src/ReactFiber.old.js
Expand Up @@ -715,7 +715,9 @@ export function createFiberFromOffscreen(
const fiber = createFiber(OffscreenComponent, pendingProps, key, mode);
fiber.elementType = REACT_OFFSCREEN_TYPE;
fiber.lanes = lanes;
const primaryChildInstance: OffscreenInstance = {};
const primaryChildInstance: OffscreenInstance = {
isHidden: false,
};
fiber.stateNode = primaryChildInstance;
return fiber;
}
Expand Down
11 changes: 11 additions & 0 deletions packages/react-reconciler/src/ReactFiberCommitWork.new.js
Expand Up @@ -2309,8 +2309,14 @@ function commitMutationEffectsOnFiber(
const offscreenFiber: Fiber = (finishedWork.child: any);

if (offscreenFiber.flags & Visibility) {
const offscreenInstance: OffscreenInstance = offscreenFiber.stateNode;
const newState: OffscreenState | null = offscreenFiber.memoizedState;
const isHidden = newState !== null;

// Track the current state on the Offscreen instance so we can
// read it during an event
offscreenInstance.isHidden = isHidden;

if (isHidden) {
const wasHidden =
offscreenFiber.alternate !== null &&
Expand Down Expand Up @@ -2354,10 +2360,15 @@ function commitMutationEffectsOnFiber(
commitReconciliationEffects(finishedWork);

if (flags & Visibility) {
const offscreenInstance: OffscreenInstance = finishedWork.stateNode;
const newState: OffscreenState | null = finishedWork.memoizedState;
const isHidden = newState !== null;
const offscreenBoundary: Fiber = finishedWork;

// Track the current state on the Offscreen instance so we can
// read it during an event
offscreenInstance.isHidden = isHidden;

if (enableSuspenseLayoutEffectSemantics) {
if (isHidden) {
if (!wasHidden) {
Expand Down
11 changes: 11 additions & 0 deletions packages/react-reconciler/src/ReactFiberCommitWork.old.js
Expand Up @@ -2309,8 +2309,14 @@ function commitMutationEffectsOnFiber(
const offscreenFiber: Fiber = (finishedWork.child: any);

if (offscreenFiber.flags & Visibility) {
const offscreenInstance: OffscreenInstance = offscreenFiber.stateNode;
const newState: OffscreenState | null = offscreenFiber.memoizedState;
const isHidden = newState !== null;

// Track the current state on the Offscreen instance so we can
// read it during an event
offscreenInstance.isHidden = isHidden;

if (isHidden) {
const wasHidden =
offscreenFiber.alternate !== null &&
Expand Down Expand Up @@ -2354,10 +2360,15 @@ function commitMutationEffectsOnFiber(
commitReconciliationEffects(finishedWork);

if (flags & Visibility) {
const offscreenInstance: OffscreenInstance = finishedWork.stateNode;
const newState: OffscreenState | null = finishedWork.memoizedState;
const isHidden = newState !== null;
const offscreenBoundary: Fiber = finishedWork;

// Track the current state on the Offscreen instance so we can
// read it during an event
offscreenInstance.isHidden = isHidden;

if (enableSuspenseLayoutEffectSemantics) {
if (isHidden) {
if (!wasHidden) {
Expand Down
Expand Up @@ -38,4 +38,6 @@ export type OffscreenQueue = {|
transitions: Array<Transition> | null,
|} | null;

export type OffscreenInstance = {};
export type OffscreenInstance = {|
isHidden: boolean,
|};

0 comments on commit dc5ed00

Please sign in to comment.