GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [Fabric] Use container node to toggle the visibility of Offscreen and… · react/react@392253a

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • packages
    • react-native-renderer/src
      • ReactFabricHostConfig.js
    • react-noop-renderer/src
      • createReactNoop.js
    • react-reconciler/src
      • ReactFiber.new.js
      • ReactFiber.old.js
      • ReactFiberBeginWork.new.js
      • ReactFiberBeginWork.old.js
      • ReactFiberCompleteWork.new.js
      • ReactFiberCompleteWork.old.js
      • ReactFiberHostConfigWithNoPersistence.js
      • ReactFiberOffscreenComponent.js
      • forks
        • ReactFiberHostConfig.custom.js
    • shared
      • ReactTypes.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎packages/react-native-renderer/src/ReactFabricHostConfig.js‎

Copy file name to clipboardExpand all lines: packages/react-native-renderer/src/ReactFabricHostConfig.js
+28-22Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* @flow
88
*/
99

10+
import type {ReactNodeList, OffscreenMode} from 'shared/ReactTypes';
1011
import type {ElementRef} from 'react';
1112
import type {
1213
HostComponent,
@@ -301,6 +302,9 @@ export function getChildHostContext(
301302
type === 'RCTText' ||
302303
type === 'RCTVirtualText';
303304

305+
// TODO: If this is an offscreen host container, we should reuse the
306+
// parent context.
307+
304308
if (prevIsInAParentText !== isInAParentText) {
305309
return {isInAParentText};
306310
} else {
@@ -413,30 +417,32 @@ export function cloneInstance(
413417
};
414418
}
415419

416-
export function cloneHiddenInstance(
417-
instance: Instance,
418-
type: string,
419-
props: Props,
420-
internalInstanceHandle: Object,
421-
): Instance {
422-
const viewConfig = instance.canonical.viewConfig;
423-
const node = instance.node;
424-
const updatePayload = create(
425-
{style: {display: 'none'}},
426-
viewConfig.validAttributes,
427-
);
428-
return {
429-
node: cloneNodeWithNewProps(node, updatePayload),
430-
canonical: instance.canonical,
431-
};
420+
// TODO: These two methods should be replaced with `createOffscreenInstance` and
421+
// `cloneOffscreenInstance`. I did it this way for now because the offscreen
422+
// instance is stored on an extra HostComponent fiber instead of the
423+
// OffscreenComponent fiber, and I didn't want to add an extra check to the
424+
// generic HostComponent path. Instead we should use the OffscreenComponent
425+
// fiber, but currently Fabric expects a 1:1 correspondence between Fabric
426+
// instances and host fibers, so I'm leaving this optimization for later once
427+
// we can confirm this won't break any downstream expectations.
428+
export function getOffscreenContainerType(): string {
429+
return 'RCTView';
432430
}
433431

434-
export function cloneHiddenTextInstance(
435-
instance: Instance,
436-
text: string,
437-
internalInstanceHandle: Object,
438-
): TextInstance {
439-
throw new Error('Not yet implemented.');
432+
export function getOffscreenContainerProps(
433+
mode: OffscreenMode,
434+
children: ReactNodeList,
435+
): Props {
436+
if (mode === 'hidden') {
437+
return {
438+
children,
439+
style: {display: 'none'},
440+
};
441+
} else {
442+
return {
443+
children,
444+
};
445+
}
440446
}
441447

442448
export function createContainerChildSet(container: Container): ChildSet {
Collapse file

‎packages/react-noop-renderer/src/createReactNoop.js‎

Copy file name to clipboardExpand all lines: packages/react-noop-renderer/src/createReactNoop.js
+159-41Lines changed: 159 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import type {Fiber} from 'react-reconciler/src/ReactInternalTypes';
1818
import type {UpdateQueue} from 'react-reconciler/src/ReactUpdateQueue';
19-
import type {ReactNodeList} from 'shared/ReactTypes';
19+
import type {ReactNodeList, OffscreenMode} from 'shared/ReactTypes';
2020
import type {RootTag} from 'react-reconciler/src/ReactRootTags';
2121

2222
import * as Scheduler from 'scheduler/unstable_mock';
@@ -258,6 +258,9 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {
258258
type: string,
259259
rootcontainerInstance: Container,
260260
) {
261+
if (type === 'offscreen') {
262+
return parentHostContext;
263+
}
261264
if (type === 'uppercase') {
262265
return UPPERCASE_CONTEXT;
263266
}
@@ -539,47 +542,18 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {
539542
container.children = newChildren;
540543
},
541544

542-
cloneHiddenInstance(
543-
instance: Instance,
544-
type: string,
545-
props: Props,
546-
internalInstanceHandle: Object,
547-
): Instance {
548-
const clone = cloneInstance(
549-
instance,
550-
null,
551-
type,
552-
props,
553-
props,
554-
internalInstanceHandle,
555-
true,
556-
null,
557-
);
558-
clone.hidden = true;
559-
return clone;
545+
getOffscreenContainerType(): string {
546+
return 'offscreen';
560547
},
561548

562-
cloneHiddenTextInstance(
563-
instance: TextInstance,
564-
text: string,
565-
internalInstanceHandle: Object,
566-
): TextInstance {
567-
const clone = {
568-
text: instance.text,
569-
id: instanceCounter++,
570-
hidden: true,
571-
context: instance.context,
549+
getOffscreenContainerProps(
550+
mode: OffscreenMode,
551+
children: ReactNodeList,
552+
): Props {
553+
return {
554+
hidden: mode === 'hidden',
555+
children,
572556
};
573-
// Hide from unit tests
574-
Object.defineProperty(clone, 'id', {
575-
value: clone.id,
576-
enumerable: false,
577-
});
578-
Object.defineProperty(clone, 'context', {
579-
value: clone.context,
580-
enumerable: false,
581-
});
582-
return clone;
583557
},
584558
};
585559

@@ -646,20 +620,164 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {
646620

647621
function getChildren(root) {
648622
if (root) {
649-
return root.children;
623+
return useMutation
624+
? root.children
625+
: removeOffscreenContainersFromChildren(root.children, false);
650626
} else {
651627
return null;
652628
}
653629
}
654630

655631
function getPendingChildren(root) {
656632
if (root) {
657-
return root.pendingChildren;
633+
return useMutation
634+
? root.children
635+
: removeOffscreenContainersFromChildren(root.pendingChildren, false);
658636
} else {
659637
return null;
660638
}
661639
}
662640

641+
function removeOffscreenContainersFromChildren(children, hideNearestNode) {
642+
// Mutation mode and persistent mode have different outputs for Offscreen
643+
// and Suspense trees. Persistent mode adds an additional host node wrapper,
644+
// whereas mutation mode does not.
645+
//
646+
// This function removes the offscreen host wrappers so that the output is
647+
// consistent. If the offscreen node is hidden, it transfers the hiddenness
648+
// to the child nodes, to mimic how it works in mutation mode. That way our
649+
// tests don't have to fork tree assertions.
650+
//
651+
// So, it takes a tree that looks like this:
652+
//
653+
// <offscreen hidden={true}>
654+
// <span>A</span>
655+
// <span>B</span>
656+
// </offscren>
657+
//
658+
// And turns it into this:
659+
//
660+
// <span hidden={true}>A</span>
661+
// <span hidden={true}>B</span>
662+
//
663+
// We don't mutate the original tree, but instead return a copy.
664+
//
665+
// This function is only used by our test assertions, via the `getChildren`
666+
// and `getChildrenAsJSX` methods.
667+
let didClone = false;
668+
const newChildren = [];
669+
for (let i = 0; i < children.length; i++) {
670+
const child = children[i];
671+
const innerChildren = child.children;
672+
if (innerChildren !== undefined) {
673+
// This is a host instance instance
674+
const instance: Instance = (child: any);
675+
if (instance.type === 'offscreen') {
676+
// This is an offscreen wrapper instance. Remove it from the tree
677+
// and recursively return its children, as if it were a fragment.
678+
didClone = true;
679+
if (instance.text !== null) {
680+
// If this offscreen tree contains only text, we replace it with
681+
// a text child. Related to `shouldReplaceTextContent` feature.
682+
const offscreenTextInstance: TextInstance = {
683+
text: instance.text,
684+
id: instanceCounter++,
685+
hidden: hideNearestNode || instance.hidden,
686+
context: instance.context,
687+
};
688+
// Hide from unit tests
689+
Object.defineProperty(offscreenTextInstance, 'id', {
690+
value: offscreenTextInstance.id,
691+
enumerable: false,
692+
});
693+
Object.defineProperty(offscreenTextInstance, 'context', {
694+
value: offscreenTextInstance.context,
695+
enumerable: false,
696+
});
697+
newChildren.push(offscreenTextInstance);
698+
} else {
699+
// Skip the offscreen node and replace it with its children
700+
const offscreenChildren = removeOffscreenContainersFromChildren(
701+
innerChildren,
702+
hideNearestNode || instance.hidden,
703+
);
704+
newChildren.push.apply(newChildren, offscreenChildren);
705+
}
706+
} else {
707+
// This is a regular (non-offscreen) instance. If the nearest
708+
// offscreen boundary is hidden, hide this node.
709+
const hidden = hideNearestNode ? true : instance.hidden;
710+
const clonedChildren = removeOffscreenContainersFromChildren(
711+
instance.children,
712+
// We never need to hide the children of this node, since if we're
713+
// inside a hidden tree, then the hidden style will be applied to
714+
// this node.
715+
false,
716+
);
717+
if (
718+
clonedChildren === instance.children &&
719+
hidden === instance.hidden
720+
) {
721+
// No changes. Reuse the original instance without cloning.
722+
newChildren.push(instance);
723+
} else {
724+
didClone = true;
725+
const clone: Instance = {
726+
id: instance.id,
727+
type: instance.type,
728+
children: clonedChildren,
729+
text: instance.text,
730+
prop: instance.prop,
731+
hidden: hideNearestNode ? true : instance.hidden,
732+
context: instance.context,
733+
};
734+
Object.defineProperty(clone, 'id', {
735+
value: clone.id,
736+
enumerable: false,
737+
});
738+
Object.defineProperty(clone, 'text', {
739+
value: clone.text,
740+
enumerable: false,
741+
});
742+
Object.defineProperty(clone, 'context', {
743+
value: clone.context,
744+
enumerable: false,
745+
});
746+
newChildren.push(clone);
747+
}
748+
}
749+
} else {
750+
// This is a text instance
751+
const textInstance: TextInstance = (child: any);
752+
if (hideNearestNode) {
753+
didClone = true;
754+
const clone = {
755+
text: textInstance.text,
756+
id: textInstance.id,
757+
hidden: textInstance.hidden || hideNearestNode,
758+
context: textInstance.context,
759+
};
760+
Object.defineProperty(clone, 'id', {
761+
value: clone.id,
762+
enumerable: false,
763+
});
764+
Object.defineProperty(clone, 'context', {
765+
value: clone.context,
766+
enumerable: false,
767+
});
768+
769+
newChildren.push(clone);
770+
} else {
771+
newChildren.push(textInstance);
772+
}
773+
}
774+
}
775+
// There are some tests that assume reference equality, so preserve it
776+
// when possible. Alternatively, we could update the tests to compare the
777+
// ids instead.
778+
return didClone ? newChildren : children;
779+
}
780+
663781
function getChildrenAsJSX(root) {
664782
const children = childToJSX(getChildren(root), null);
665783
if (children === null) {
Collapse file

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

Copy file name to clipboardExpand all lines: packages/react-reconciler/src/ReactFiber.new.js
+24-1Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import type {RootTag} from './ReactRootTags';
1414
import type {WorkTag} from './ReactWorkTags';
1515
import type {TypeOfMode} from './ReactTypeOfMode';
1616
import type {Lanes} from './ReactFiberLane.new';
17-
import type {SuspenseInstance} from './ReactFiberHostConfig';
17+
import type {SuspenseInstance, Props} from './ReactFiberHostConfig';
1818
import type {OffscreenProps} from './ReactFiberOffscreenComponent';
1919

2020
import invariant from 'shared/invariant';
@@ -27,6 +27,10 @@ import {
2727
enableSyncDefaultUpdates,
2828
allowConcurrentByDefault,
2929
} from 'shared/ReactFeatureFlags';
30+
import {
31+
supportsPersistence,
32+
getOffscreenContainerType,
33+
} from './ReactFiberHostConfig';
3034
import {NoFlags, Placement, StaticMask} from './ReactFiberFlags';
3135
import {ConcurrentRoot} from './ReactRootTags';
3236
import {
@@ -585,6 +589,25 @@ export function createFiberFromTypeAndProps(
585589
return fiber;
586590
}
587591

592+
export function createOffscreenHostContainerFiber(
593+
props: Props,
594+
fiberMode: TypeOfMode,
595+
lanes: Lanes,
596+
key: null | string,
597+
): Fiber {
598+
if (supportsPersistence) {
599+
const type = getOffscreenContainerType();
600+
const fiber = createFiber(HostComponent, props, key, fiberMode);
601+
fiber.elementType = type;
602+
fiber.type = type;
603+
fiber.lanes = lanes;
604+
return fiber;
605+
} else {
606+
// Only implemented in persistent mode
607+
invariant(false, 'Not implemented.');
608+
}
609+
}
610+
588611
export function createFiberFromElement(
589612
element: ReactElement,
590613
mode: TypeOfMode,

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.