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

Overlay2 autoFocus fails when mounted as open in React 18 #6902

Open
PetrusAsikainen opened this issue Jul 9, 2024 · 0 comments
Open

Overlay2 autoFocus fails when mounted as open in React 18 #6902

PetrusAsikainen opened this issue Jul 9, 2024 · 0 comments

Comments

@PetrusAsikainen
Copy link
Contributor

PetrusAsikainen commented Jul 9, 2024

Environment

  • Package version(s): v5.10.5
  • Operating System: Windows
  • Browser name and version: Firefox 127.0.2
  • Also reproduced on Chromium 122.0.6261.94 in Debian under WSL

Code Sandbox

https://codesandbox.io/p/devbox/long-tree-htjf9p

Steps to reproduce

  1. Click "Open dialog"
  2. Press Esc
  3. (Close the dialog and click "Unmount" to reset)

Actual behavior

Dialog is always in focus (pressing Esc closes it)

Expected behavior

Pressing Esc does not close the dialog when opened for the first time

Possible solution

This comment added with Overlay2 is exactly the root cause; the dialog's autoFocus does not work properly. (I'm not sure if this is intentionally left as is.)

// container element may be undefined between component mounting and Portal rendering
// activeElement may be undefined in some rare cases in IE
const container = getRef(containerElement);
const activeElement = getActiveElement(container);
if (container == null || activeElement == null) {
return;
}

When the dialog is mounted with isOpen={true}, React 18 seems to pretty consistently mount the overlayWillOpen useEffect and even let the bringFocusInsideOverlay requestAnimationFrame run before rendering the Portal and container.

In our much larger application, I was also able to reproduce this with a mounted lazy Dialog, but not lazy={false}, so this might depend on render performance fluctuations or something. This also affects all of React 18, not just StrictMode.

This is quite a minor annoyance, but might need delaying the auto-focus to a later callback.

@PetrusAsikainen PetrusAsikainen changed the title Dialog (or Overlay2) autoFocus fails when mounted as open in React 18 Overlay2 autoFocus fails when mounted as open in React 18 Jul 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant