Skip to content
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

fix: restore selection should consider the window of the container #30951

Merged

Conversation

ling1726
Copy link
Contributor

Summary

Fixes #30864

Before this PR the active elemen was always taken from the global window. This is incorrect if the renderer is in one window rendering into a container element in another window. The changes in this PR adds another code branch to use a defaultView of the container element if it exists so that restoreSelection after a commit will actually restore to the correct window.

How did you test this change?

I patched these changes to the repro repo in the linked issue #39864 https://github.com/ling1726/react-child-window-focus-repro/blob/master/patches/react-dom%2B18.3.1.patch.

I followed the same repro steps in the linked issue and and could not repro the reported problem. Attaching screen recordings below:

Before
focus repro

After
after

Copy link

vercel bot commented Sep 12, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
react-compiler-playground ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 13, 2024 7:38pm

@facebook-github-bot
Copy link

Hi @ling1726!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@ling1726
Copy link
Contributor Author

I'm not sure how to test child windows in Jest, would appreciate any guidance from maintainers

Copy link
Contributor

@josephsavona josephsavona left a comment

Choose a reason for hiding this comment

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

The fix seems reasonable but we really need a test to prevent regressions. Have you tried using window.open() in Jest? What happens?

Comment on lines 60 to 63
let win = window;
let element = getActiveElement();
if (containerInfo?.ownerDocument?.defaultView) {
win = containerInfo.ownerDocument.defaultView
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Note that we try to minimize optional chaining in the runtime because of how it expands when transpiled, and this case does it twice. Maybe:

let win = containerInfo?.ownerDocument?.defaultView ?? window;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done in edd9d8a

// at runtime. We should instead inject the version number as part of the build
// process, and use the ReactVersions.js module as the single source of truth.
export default '19.0.0';
export default '19.0.0-rc-d6cb4e77-20240911';
Copy link
Contributor

Choose a reason for hiding this comment

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

revert

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done in c8f573e

@rickhanlonii
Copy link
Member

rickhanlonii commented Sep 12, 2024

If window.open() doesn't work, you could probably use an iframe

@rickhanlonii
Copy link
Member

Here's a similar test:

it('should render a text component with a text DOM node on the same document as the container', async () => {

@ling1726
Copy link
Contributor Author

@josephsavona @rickhanlonii I've added a test in e545af8 please take a look 🙏🙏

window.open is not implemented in JSDOM, but I was able to just create a new JSDOM instance to get a new window to run this test on. I also ran the test without the fix and it fails as expected with focus on the new document's body

@react-sizebot
Copy link

react-sizebot commented Sep 13, 2024

Comparing: d3d4d3a...37e1b28

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 +0.11% 1.82 kB 1.83 kB
oss-stable/react-dom/cjs/react-dom-client.production.js +0.08% 508.72 kB 509.13 kB +0.12% 90.98 kB 91.08 kB
oss-experimental/react-dom/cjs/react-dom.production.js = 6.69 kB 6.69 kB +0.11% 1.83 kB 1.83 kB
oss-experimental/react-dom/cjs/react-dom-client.production.js +0.08% 513.65 kB 514.07 kB +0.10% 91.70 kB 91.80 kB
facebook-www/ReactDOM-prod.classic.js = 604.26 kB 603.23 kB = 106.77 kB 106.70 kB
facebook-www/ReactDOM-prod.modern.js = 580.53 kB 579.50 kB = 102.91 kB 102.84 kB

Significant size changes

Includes any change greater than 0.2%:

Expand to show
Name +/- Base Current +/- gzip Base gzip Current gzip
facebook-www/ReactDOM-profiling.classic.js = 634.58 kB 632.88 kB = 111.24 kB 111.15 kB
facebook-www/ReactDOM-profiling.modern.js = 610.20 kB 608.49 kB = 107.25 kB 107.15 kB
react-native/implementations/ReactFabric-dev.fb.js = 662.60 kB 660.60 kB = 108.29 kB 108.10 kB
facebook-www/ReactReconciler-dev.classic.js = 744.70 kB 742.42 kB = 118.67 kB 118.50 kB
facebook-www/ReactReconciler-prod.classic.js = 464.10 kB 462.66 kB = 75.10 kB 74.99 kB
facebook-www/ReactReconciler-dev.modern.js = 720.81 kB 718.54 kB = 114.60 kB 114.43 kB
react-native/implementations/ReactFabric-prod.fb.js = 380.61 kB 379.40 kB = 66.28 kB 66.16 kB
facebook-www/ReactReconciler-prod.modern.js = 445.31 kB 443.87 kB = 72.28 kB 72.17 kB
react-native/implementations/ReactNativeRenderer-dev.fb.js = 670.21 kB 668.02 kB = 109.77 kB 109.58 kB
react-native/implementations/ReactNativeRenderer-prod.fb.js = 386.48 kB 385.18 kB = 67.54 kB 67.38 kB
facebook-www/ReactART-dev.classic.js = 658.24 kB 655.94 kB = 105.43 kB 105.25 kB
facebook-www/ReactART-prod.classic.js = 371.19 kB 369.87 kB = 62.76 kB 62.60 kB
facebook-www/ReactART-dev.modern.js = 635.76 kB 633.46 kB = 101.77 kB 101.58 kB
facebook-www/ReactART-prod.modern.js = 354.17 kB 352.84 kB = 60.00 kB 59.84 kB
react-native/implementations/ReactFabric-profiling.fb.js = 408.25 kB 406.47 kB = 70.66 kB 70.53 kB
react-native/implementations/ReactNativeRenderer-profiling.fb.js = 414.17 kB 412.28 kB = 71.84 kB 71.66 kB

Generated by 🚫 dangerJS against 9e79394

@josephsavona josephsavona merged commit d9c4920 into facebook:main Sep 13, 2024
184 checks passed
@josephsavona
Copy link
Contributor

Thanks for the PR!

github-actions bot pushed a commit that referenced this pull request Sep 13, 2024
…30951)

## Summary

Fixes #30864

Before this PR the active elemen was always taken from the global
`window`. This is incorrect if the renderer is in one window rendering
into a container element in another window. The changes in this PR adds
another code branch to use a `defaultView` of the container element if
it exists so that `restoreSelection` after a commit will actually
restore to the correct window.

## How did you test this change?

I patched these changes to the repro repo in the linked issue #39864
https://github.com/ling1726/react-child-window-focus-repro/blob/master/patches/react-dom%2B18.3.1.patch.

I followed the same repro steps in the linked issue and and could not
repro the reported problem. Attaching screen recordings below:

Before
![focus
repro](https://github.com/user-attachments/assets/81c4b4f9-08b5-4356-8251-49b909771f3f)

After

![after](https://github.com/user-attachments/assets/84883032-5558-4650-9b9a-bd4d5fd9cb13)

DiffTrain build for [d9c4920](d9c4920)
mofeiZ added a commit to mofeiZ/react that referenced this pull request Sep 17, 2024
Rewrite `containerInfo?.ownerDocument?.defaultView ?? window` to instead use a ternary.

This changes the compilation output (see [bundle changes from facebook#30951]).
```js
// compilation of containerInfo?.ownerDocument?.defaultView ?? window
var $jscomp$optchain$tmpm1756096108$1, $jscomp$nullish$tmp0;
containerInfo =
  null !=
  ($jscomp$nullish$tmp0 =
    null == containerInfo
      ? void 0
      : null ==
          ($jscomp$optchain$tmpm1756096108$1 = containerInfo.ownerDocument)
        ? void 0
        : $jscomp$optchain$tmpm1756096108$1.defaultView)
    ? $jscomp$nullish$tmp0
    : window;

// compilation of ternary expression
containerInfo =
  null != containerInfo &&
  null != containerInfo.ownerDocument &&
  null != containerInfo.ownerDocument.defaultView
    ? containerInfo.ownerDocument.defaultView
    : window;
```

This also reduces the number of no-op bundle syncs for Meta. Note that Closure compiler's `jscomp$optchain$tmp<HASH>` identifiers change when we rebuild (likely due to version changes). See [workflow](https://github.com/facebook/react/actions/runs/10891164281/job/30221518374) for a PR that was synced despite making no changes to the runtime.
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.

Bug: Focus restore after elements are reordered does not work in child windows
5 participants