Skip to content

Allow fragment refs to attempt focus/focusLast on nested host children #33058

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
May 7, 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
13 changes: 10 additions & 3 deletions fixtures/dom/src/components/fixtures/fragment-refs/FocusCase.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,18 @@ export default function FocusCase() {
</Fixture.Controls>
<div className="highlight-focused-children" style={{display: 'flex'}}>
<Fragment ref={fragmentRef}>
<div style={{outline: '1px solid black'}}>Unfocusable div</div>
<button>Button 1</button>
<div style={{outline: '1px solid black'}}>
<p>Unfocusable div</p>
</div>
<div style={{outline: '1px solid black'}}>
<p>Unfocusable div with nested focusable button</p>
<button>Button 1</button>
</div>
<button>Button 2</button>
<input type="text" placeholder="Input field" />
<div style={{outline: '1px solid black'}}>Unfocusable div</div>
<div style={{outline: '1px solid black'}}>
<p>Unfocusable div</p>
</div>
</Fragment>
</div>
</Fixture>
Expand Down
4 changes: 4 additions & 0 deletions fixtures/dom/src/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,10 @@ tbody tr:nth-child(even) {
background-color: green;
}

.highlight-focused-children * {
margin-left: 10px;
}

.highlight-focused-children *:focus {
outline: 2px solid green;
}
9 changes: 7 additions & 2 deletions packages/react-dom-bindings/src/client/ReactFiberConfigDOM.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ import {
getFragmentParentHostFiber,
getNextSiblingHostFiber,
getInstanceFromHostFiber,
traverseFragmentInstanceDeeply,
} from 'react-reconciler/src/ReactFiberTreeReflection';

export {detachDeletedInstance};
Expand Down Expand Up @@ -2698,7 +2699,7 @@ FragmentInstance.prototype.focus = function (
this: FragmentInstanceType,
focusOptions?: FocusOptions,
): void {
traverseFragmentInstance(
traverseFragmentInstanceDeeply(
this._fragmentFiber,
setFocusOnFiberIfFocusable,
focusOptions,
Expand All @@ -2717,7 +2718,11 @@ FragmentInstance.prototype.focusLast = function (
focusOptions?: FocusOptions,
): void {
const children: Array<Fiber> = [];
traverseFragmentInstance(this._fragmentFiber, collectChildren, children);
traverseFragmentInstanceDeeply(
this._fragmentFiber,
collectChildren,
children,
);
for (let i = children.length - 1; i >= 0; i--) {
const child = children[i];
if (setFocusOnFiberIfFocusable(child, focusOptions)) {
Expand Down
54 changes: 54 additions & 0 deletions packages/react-dom/src/__tests__/ReactDOMFragmentRefs-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,32 @@ describe('FragmentRefs', () => {
document.activeElement.blur();
});

// @gate enableFragmentRefs
it('focuses deeply nested focusable children, depth first', async () => {
const fragmentRef = React.createRef();
const root = ReactDOMClient.createRoot(container);

function Test() {
return (
<Fragment ref={fragmentRef}>
<div id="child-a">
<div tabIndex={0} id="grandchild-a">
<a id="greatgrandchild-a" href="/" />
</div>
</div>
<a id="child-b" href="/" />
</Fragment>
);
}
await act(() => {
root.render(<Test />);
});
await act(() => {
fragmentRef.current.focus();
});
expect(document.activeElement.id).toEqual('grandchild-a');
});

// @gate enableFragmentRefs
it('preserves document order when adding and removing children', async () => {
const fragmentRef = React.createRef();
Expand Down Expand Up @@ -228,6 +254,34 @@ describe('FragmentRefs', () => {
expect(document.activeElement.id).toEqual('child-c');
document.activeElement.blur();
});

// @gate enableFragmentRefs
it('focuses deeply nested focusable children, depth first', async () => {
const fragmentRef = React.createRef();
const root = ReactDOMClient.createRoot(container);

function Test() {
return (
<Fragment ref={fragmentRef}>
<div id="child-a" href="/">
<a id="grandchild-a" href="/" />
<a id="grandchild-b" href="/" />
</div>
<div tabIndex={0} id="child-b">
<a id="grandchild-a" href="/" />
<a id="grandchild-b" href="/" />
</div>
</Fragment>
);
}
await act(() => {
root.render(<Test />);
});
await act(() => {
fragmentRef.current.focusLast();
});
expect(document.activeElement.id).toEqual('grandchild-b');
});
});

describe('blur()', () => {
Expand Down
31 changes: 13 additions & 18 deletions packages/react-reconciler/src/ReactFiberTreeReflection.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,16 @@ export function traverseFragmentInstance<A, B, C>(
traverseVisibleHostChildren(fragmentFiber.child, false, fn, a, b, c);
}

export function traverseFragmentInstanceDeeply<A, B, C>(
fragmentFiber: Fiber,
fn: (Fiber, A, B, C) => boolean,
a: A,
b: B,
c: C,
): void {
traverseVisibleHostChildren(fragmentFiber.child, true, fn, a, b, c);
}

function traverseVisibleHostChildren<A, B, C>(
child: Fiber | null,
searchWithinHosts: boolean,
Expand All @@ -363,31 +373,16 @@ function traverseVisibleHostChildren<A, B, C>(
c: C,
): boolean {
while (child !== null) {
if (child.tag === HostComponent) {
if (fn(child, a, b, c)) {
return true;
}
if (searchWithinHosts) {
if (
traverseVisibleHostChildren(
child.child,
searchWithinHosts,
fn,
a,
b,
c,
)
) {
return true;
}
}
if (child.tag === HostComponent && fn(child, a, b, c)) {
return true;
} else if (
child.tag === OffscreenComponent &&
child.memoizedState !== null
) {
// Skip hidden subtrees
} else {
if (
(searchWithinHosts || child.tag !== HostComponent) &&
traverseVisibleHostChildren(child.child, searchWithinHosts, fn, a, b, c)
) {
return true;
Expand Down
Loading