Skip to content

Move traverseFragmentInstanceChildren to internal ReactFiberTreeReflection #32613

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 7 additions & 44 deletions packages/react-dom-bindings/src/client/ReactFiberConfigDOM.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import {getCurrentRootHostContainer} from 'react-reconciler/src/ReactFiberHostCo
import hasOwnProperty from 'shared/hasOwnProperty';
import {checkAttributeStringCoercion} from 'shared/CheckStringCoercion';
import {REACT_CONTEXT_TYPE} from 'shared/ReactSymbols';
import {OffscreenComponent} from 'react-reconciler/src/ReactWorkTags';

export {
setCurrentUpdatePriority,
Expand All @@ -54,6 +53,8 @@ import {
markNodeAsHoistable,
isOwnedInstance,
} from './ReactDOMComponentTree';
import {traverseFragmentInstance} from 'react-reconciler/src/ReactFiberTreeReflection';

export {detachDeletedInstance};
import {hasRole} from './DOMAccessibilityRoles';
import {
Expand Down Expand Up @@ -2221,9 +2222,8 @@ FragmentInstance.prototype.addEventListener = function (
indexOfEventListener(listeners, type, listener, optionsOrUseCapture) === -1;
if (isNewEventListener) {
listeners.push({type, listener, optionsOrUseCapture});
traverseFragmentInstanceChildren(
this,
this._fragmentFiber.child,
traverseFragmentInstance(
this._fragmentFiber,
addEventListenerToChild,
type,
listener,
Expand Down Expand Up @@ -2253,9 +2253,8 @@ FragmentInstance.prototype.removeEventListener = function (
return;
}
if (typeof listeners !== 'undefined' && listeners.length > 0) {
traverseFragmentInstanceChildren(
this,
this._fragmentFiber.child,
traverseFragmentInstance(
this._fragmentFiber,
removeEventListenerFromChild,
type,
listener,
Expand Down Expand Up @@ -2283,45 +2282,9 @@ function removeEventListenerFromChild(
}
// $FlowFixMe[prop-missing]
FragmentInstance.prototype.focus = function (this: FragmentInstanceType) {
traverseFragmentInstanceChildren(
this,
this._fragmentFiber.child,
setFocusIfFocusable,
);
traverseFragmentInstance(this._fragmentFiber, setFocusIfFocusable);
};

function traverseFragmentInstanceChildren<A, B, C>(
fragmentInstance: FragmentInstanceType,
child: Fiber | null,
fn: (Instance, A, B, C) => boolean,
a: A,
b: B,
c: C,
): void {
while (child !== null) {
if (child.tag === HostComponent) {
if (fn(child.stateNode, a, b, c)) {
return;
}
} else if (
child.tag === OffscreenComponent &&
child.memoizedState !== null
) {
// Skip hidden subtrees
} else {
traverseFragmentInstanceChildren(
fragmentInstance,
child.child,
fn,
a,
b,
c,
);
}
child = child.sibling;
}
}

function normalizeListenerOptions(
opts: ?EventListenerOptionsOrUseCapture,
): string {
Expand Down
37 changes: 36 additions & 1 deletion packages/react-reconciler/src/ReactFiberTreeReflection.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/

import type {Fiber} from './ReactInternalTypes';
import type {Container, SuspenseInstance} from './ReactFiberConfig';
import type {Container, SuspenseInstance, Instance} from './ReactFiberConfig';
import type {SuspenseState} from './ReactFiberSuspenseComponent';

import {
Expand All @@ -19,6 +19,7 @@ import {
HostPortal,
HostText,
SuspenseComponent,
OffscreenComponent,
} from './ReactWorkTags';
import {NoFlags, Placement, Hydrating} from './ReactFiberFlags';

Expand Down Expand Up @@ -317,3 +318,37 @@ export function doesFiberContain(
}
return false;
}

export function traverseFragmentInstance<A, B, C>(
fragmentFiber: Fiber,
fn: (Instance, A, B, C) => boolean,
a: A,
b: B,
c: C,
): void {
return traverseFragmentInstanceChildren(fragmentFiber.child, fn, a, b, c);
}

function traverseFragmentInstanceChildren<A, B, C>(
child: Fiber | null,
fn: (Instance, A, B, C) => boolean,
a: A,
b: B,
c: C,
): void {
while (child !== null) {
if (child.tag === HostComponent) {
if (fn(child.stateNode, a, b, c)) {
return;
}
} else if (
child.tag === OffscreenComponent &&
child.memoizedState !== null
) {
// Skip hidden subtrees
} else {
traverseFragmentInstanceChildren(child.child, fn, a, b, c);
}
child = child.sibling;
}
}
Loading