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

Conversation

jackpope
Copy link
Contributor

This enables focus and focusLast methods on FragmentInstances to search nested host components, depth first. Attempts focus on each child and bails if one is successful. Previously, only the first level of host children would attempt focus.

Now if we have an example like

component MenuItem() {
  return (<div><a>{...}</a></div>)
}

component Menu() {
  return <Fragment>{items.map(i => <MenuItem i={i} />)}</Fragment>
}

We can target focus on the first or last a tag, rather than checking each wrapping div and then noop.

@react-sizebot
Copy link

react-sizebot commented Apr 29, 2025

Comparing: e5a8de8...0daf77b

Critical size changes

Includes critical production bundles, as well as any change greater than 2%:

Name +/- Base Current +/- gzip Base gzip Current gzip
oss-stable/react-dom/cjs/react-dom.production.js = 6.68 kB 6.68 kB = 1.83 kB 1.83 kB
oss-stable/react-dom/cjs/react-dom-client.production.js = 528.26 kB 528.26 kB = 93.16 kB 93.16 kB
oss-experimental/react-dom/cjs/react-dom.production.js = 6.69 kB 6.69 kB = 1.83 kB 1.83 kB
oss-experimental/react-dom/cjs/react-dom-client.production.js = 645.02 kB 644.90 kB = 113.47 kB 113.47 kB
facebook-www/ReactDOM-prod.classic.js = 673.72 kB 673.60 kB = 118.32 kB 118.31 kB
facebook-www/ReactDOM-prod.modern.js = 664.00 kB 663.88 kB = 116.73 kB 116.73 kB

Significant size changes

Includes any change greater than 0.2%:

Expand to show
Name +/- Base Current +/- gzip Base gzip Current gzip
oss-experimental/react-reconciler/cjs/react-reconciler-reflection.production.js +0.26% 9.91 kB 9.93 kB = 2.39 kB 2.38 kB
oss-stable-semver/react-reconciler/cjs/react-reconciler-reflection.production.js +0.26% 9.91 kB 9.93 kB = 2.39 kB 2.38 kB
oss-stable/react-reconciler/cjs/react-reconciler-reflection.production.js +0.26% 9.91 kB 9.93 kB = 2.39 kB 2.38 kB
oss-experimental/react-reconciler/cjs/react-reconciler-reflection.development.js = 11.25 kB 11.22 kB = 2.45 kB 2.44 kB
oss-stable-semver/react-reconciler/cjs/react-reconciler-reflection.development.js = 11.25 kB 11.22 kB = 2.45 kB 2.44 kB
oss-stable/react-reconciler/cjs/react-reconciler-reflection.development.js = 11.25 kB 11.22 kB = 2.45 kB 2.44 kB

Generated by 🚫 dangerJS against 0daf77b

Copy link
Contributor

@jbrown215 jbrown215 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jackpope and I discussed the tradeoff of looking through top-level host elements to expose focus and decided that the feature is more useful if we expose this capability.

That would mean that this is a semantically meaningful change:

const ref = useRef(null);
return (
  <div ref={ref}>
    <a />
  </div>
);

// --> calling .focus will now focus the anchor element
const ref = useRef(null);
return (
  <React.Fragment ref={ref}>
    <div>
      <a />
    </div>
  <React.Fragment/>
);

@jackpope jackpope merged commit 4206fe4 into facebook:main May 7, 2025
239 checks passed
@jackpope jackpope deleted the fragment-refs-deep-focus branch May 7, 2025 16:47
github-actions bot pushed a commit that referenced this pull request May 7, 2025
#33058)

This enables `focus` and `focusLast` methods on FragmentInstances to
search nested host components, depth first. Attempts focus on each child
and bails if one is successful. Previously, only the first level of host
children would attempt focus.

Now if we have an example like

```
component MenuItem() {
  return (<div><a>{...}</a></div>)
}

component Menu() {
  return <Fragment>{items.map(i => <MenuItem i={i} />)}</Fragment>
}
```
We can target focus on the first or last a tag, rather than checking
each wrapping div and then noop.

DiffTrain build for [4206fe4](4206fe4)
github-actions bot pushed a commit that referenced this pull request May 7, 2025
#33058)

This enables `focus` and `focusLast` methods on FragmentInstances to
search nested host components, depth first. Attempts focus on each child
and bails if one is successful. Previously, only the first level of host
children would attempt focus.

Now if we have an example like

```
component MenuItem() {
  return (<div><a>{...}</a></div>)
}

component Menu() {
  return <Fragment>{items.map(i => <MenuItem i={i} />)}</Fragment>
}
```
We can target focus on the first or last a tag, rather than checking
each wrapping div and then noop.

DiffTrain build for [4206fe4](4206fe4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants