-
Notifications
You must be signed in to change notification settings - Fork 251
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
.focus() does not fire focus events if window is not focussed #553
Comments
I don't think this is entirely "fixable" as it is immanent to how the real browser applies the concept of focus:
If you programmatically focus an element, the browser does change the
What we could do:
What we can not do:
https://codesandbox.io/s/gifted-gauss-49ecm?file=/src/index.js |
We have an issue that is related to this one. We use Angular + Karma and some of our inputs use the blur event (either directly or via third-party directive). In our tests we use I was able to reproduce the problem in a new, simple Angular app with an input and a blur event listener that just does The exact same test in React + Jest works fine and I guess it would also work with Angular + Jest (but switching to Jest is not an option for us). Our current workaround is to use Related (for Firefox): karma-runner/karma-firefox-launcher#56 |
Browsers (confirmed in Chrome/Firefox) will not dispatch focus events when the window is not focused when calling
.focus()
.Here is a codepen that demonstrates this behavior: https://codepen.io/fritsvancampen/pen/OJbPaLX
This becomes a problem if you're using a test-runner that uses a real browser to test (such as Karma) and you're running tests locally, or testing with more than one browser at once (ie Chrome and Firefox). Only one browser window can have focus at a time so your code under test is going to behave differently. You can imagine the kind of debug-hell you end up in, it's an actual Heisenbug!
I understand that if you're using a headless browser or a mocked browser this is not an issue.
Is this a use-case that this library wants to support?
I have been tinkering with a workaround but I haven't found a perfect solution but perhaps we don't need a perfect solution.
Instead of calling
.focus()
you can fire the focus events manually (with dispatchEvent/fireEvent), and set theactiveElement
. However the latter doesn't get picked up by CSS which might be a problem because of:focus
rules.Another possibility is to fire the focus events and call
.focus()
(to move theactiveElement
). This has the same problem: it also doesn't trigger:focus
CSS-rules. This also generates duplicate events, which is acceptable for my use case.Trying to retro-actively apply the workaround to this library is a bit of a headache so an integrated solution would be nice to have. I would find it acceptable to activate this workaround behavior through some kind of config.
Here is a codepen with the workaround: https://codepen.io/fritsvancampen/pen/VwmYVQe
I would offer a PR but I can't seem to get the test-suite running (detects 0 tests).
The text was updated successfully, but these errors were encountered: