-
Notifications
You must be signed in to change notification settings - Fork 46.9k
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
React's blur may not have relatedTarget in IE 9-11 where it is supported. #3751
Comments
cc @syranide, our resident event expert these days. |
@JSFB Haha, I'm not that familiar with the events actually ;) Anyway, #2011 is kind of related. If the value that's meant to go in |
#2011 is related, but this is separate. Since that's about what can be done when a browser has failed to implement relatedTarget at all. While this is about React not exposing relatedTarget when it is implemented in a browser. The fix for this bug is probably the simple change of using focusin wherever it is supported instead of only using it when addEventListener isn't supported. ie: Flip the order we I make use of relatedTarget because I've gone the extra effort to make my navigation respond to the keyboard, based on WAI-ARIA's recommendations on how doing this with the keyboard should function. relatedTarget is relevant to a form of "close on blur" functionality. When navigating with the keyboard menu items are given focus one at a time and when focus leaves the menu (user has tabbed out, clicked somewhere else, or taken any other action that has moved focus out of the menu) the menu closes. Because any form of delegated blur event (which is required when you are trying to tell if focus has left a region rather than if a single form field is no longer focused) fires on every single blur event within a region, relatedTarget is necessary to tell the difference between the user moving from one menu item to another and focus leaving the menu. As a bonus, because I focus the menu container itself when a user has normally opened the menu by a click, I get a free way to automatically close the menu when the user clicks somewhere else. This type of menu closing is actually a lot less of a hack than other methods, like trapping clicks on the body, covering the page in a transparent overlay, etc. |
yep, @dantman 's technique is the only sane way i've found to handle the whole "close when I click away / click outside of this component" UX |
Any idea how to get this to work? Or any workaround? Please see http://stackoverflow.com/questions/38019140/react-and-blur-event/38019906#comment63483092_38019906. |
Same issue when building an autocomplete and having to make heads/tails whether a fired |
Related: #6410 "onFocusIn/onFocusOut events" Contrary to @tvararu 's experience above, using I ended up adding a |
We just hit this too and were very surprised to not find it normalized by React. |
its not possible to normalize unfortunately. The only viable approaches are timeout based and have caveats and failure cases. This should probably be marked as |
@jquense IE (at least 11) sets |
@craigkovatch |
Not sure I should need to reiterate the topic of the issue. Normalizing would be impossible if this were an issue of a browser not supporting This discrepancy has only gotten worse over time. To my knowledge Firefox fixed its issue and implemented |
The back and forth on this is frustrating. I get wanting to normalize to Manually binding dom events or doing timeouts is not a great workaround, especially if the "fix" is changing the order of the if/else if clause. This has been open since 2015. For my use-case specifically: I have a bunch of inputs within a component and I need to know if focus left the component. Setting a boolean from false => true => false between onBlur/onFocus doesn't work in my case because I need to do something onBlur, but only if newly focused element is not also in the component. relatedTarget is perfect for this - but IE 11 is still used, sadly. |
So, I've had mixed results with this in IE11. In one of my react apps ( 16.8.4 ) - I can reliably get an element from const newTargetElement = event.relatedTarget || document.activeElement |
@StJohn3D I would recommend flagging the |
…tiveElement` instead of `event.relatedTarget` on onBlur callbacks See: facebook/react#3751
I think we can close this because React 17 switches to |
ReactBrowserEventEmitter has some handling for
onBlur
andonFocus
.See:
react/src/browser/ReactBrowserEventEmitter.js
Lines 262 to 293 in 94a3b0f
React will first check if it can trap
focus
with a capturing event usingaddEventListener
. If it can't and the browser supportsfocusin
then it will usefocusin
.However there is a problem with this pattern. Internet Explorer implements
relatedTarget
onfocusin
andfocusout
but it does not implement it infocus
andblur
. As of IE 9, IE supports addEventListener and trapping capturing events.This means that IE 8 and before React will have
relatedTarget
ononFocus
andonBlur
handlers. But in IE 9-11relatedTarget
will be null in React'sonFocus
andonBlur
handlers even though IE would support it iffocusin
andfocusout
had been used.The text was updated successfully, but these errors were encountered: