react icon indicating copy to clipboard operation
react copied to clipboard

onFocusIn/onFocusOut events

Open sophiebits opened this issue 10 years ago • 60 comments

Like mouse enter/leave, these are almost always what you want, not the onFocus and onBlur events we currently expose. I run into this semi-frequently when actually doing product work. We should add them.

sophiebits avatar Apr 05 '16 04:04 sophiebits

FWIW, I agree with you. People know about focusin/focusout, and so we should just make them work, despite the fact that they would map to the exact same event handler we use for focus/blur.

However, last time this was discussed, we opted to not do this (https://github.com/facebook/react/pull/6226) and decided to warn instead (https://github.com/facebook/react/pull/6296/files).

I'd be fine with reversing this decision if you can get the momentum within the team.

jimfb avatar Apr 05 '16 10:04 jimfb

It looks like it was decided against because of an incorrect claim that they're indistinguishable from onFocus/onBlur. onFocusIn/onFocusOut is different in that when moving focus between two children of an element, neither event fires on the container.

sophiebits avatar Apr 06 '16 00:04 sophiebits

Doing it properly means adapting the logic in EnterLeaveEventPlugin and isn't as simple as adding it to SimpleEventPlugin.

sophiebits avatar Apr 06 '16 00:04 sophiebits

Ah, that makes sense. Thanks for explaining!

Adding “good first bug” here. Note @spicyj’s comments: they need to behave like onMouseEnter and onMouseLeave. You’ll want to look at EnterLeaveEventPlugin.

gaearon avatar Apr 06 '16 19:04 gaearon

I'd like to work on it if nobody minds. Enough with the tests, time to add real source code!

fyodoriv avatar Apr 06 '16 20:04 fyodoriv

Hey @gaearon @spicyj apparently there is a confusion here, let me explain. mouseover and mouseout bubble up. mouseenter and mouseleave don't bubble up. Corresponding React event handlers work the same as native events - awesome.

Native focus and native blur events don't bubble. Corresponding React event handlers onFocus and onBlur do bubble - dun dun dun.

Native focusin and native focusout events do bubble. React events for these should be implemented and bubble too.

Here is a jsfiddle I made to display this. Checked in Chrome only, as since MDN documentation states that this is intended behavior then I believe it should be the same in other browsers.

If I am right about this (please correct me if I am wrong), then the following should be done: 0. Warnings about onFocusIn/onFocusOut should be removed (obviously).

  1. Currect React onFocus should be renamed to onFocusIn; onBlur - to onFocusOut as they currently work the same as native onFocusIn and onFocusOut work - they bubble.
  2. onFocus and onBlur event listeners should be implemented in React - which will not bubble.

fyodoriv avatar Apr 07 '16 19:04 fyodoriv

Hmm, that sounds plausible. Thanks for the correction. Kinda annoying that we'll end up renaming these for people but maybe it's for the best.

sophiebits avatar Apr 07 '16 19:04 sophiebits

Hey, unfortunately, I cannot invest time in this right now and I must pause. I have added onFocusIn/Out by mimicing onFocus/onBlur but I am having issues with changing onFocus/onBlur so they don't bubble. I don't want to slow down the awesomeness of React so if anyone wants to invest into this issue - please do. If nobody does - I will get back to it when I have more time. Good luck!

fyodoriv avatar Jun 05 '16 14:06 fyodoriv

but I am having issues with changing onFocus/onBlur so they don't bubble.

Do we want them to not bubble? We had previously said that all events bubble. I'm fine with whatever, so I'll leave that decision to @spicyj since he has the most situational awareness here.

jimfb avatar Jun 05 '16 15:06 jimfb

Since this is tagged as a good first bug I thought I'd take a stab at this. Let me know if you're still working on it @cbrwizard !

mrscobbler avatar Jun 13 '16 00:06 mrscobbler

I did a little more investigating and it looks like focusin and focusout events are not supported by Firefox. Is this still something you want to implement if all browsers don't support it? See here: https://developer.mozilla.org/en-US/docs/Web/Events/focusin and here: https://bugzilla.mozilla.org/show_bug.cgi?id=687787

mrscobbler avatar Jun 13 '16 01:06 mrscobbler

@mrscobbler It should be possible to polyfill the event on any browsers that don't natively support it. We do this with several events that are not supported on particular browsers.

jimfb avatar Jun 13 '16 01:06 jimfb

Ok! Good to know.

mrscobbler avatar Jun 13 '16 02:06 mrscobbler

Any status on this?

trigun539 avatar Jun 30 '16 14:06 trigun539

So I've spent a few hours figuring out the events code (I've never looked at it before) and I've made some progress. Possibly @cbrwizard could walk me through some of what he's done? I might get a better understanding of what needs to happen. @trigun539 is it something you want to tackle or do you just need/want it finished?

mrscobbler avatar Jun 30 '16 17:06 mrscobbler

@mrscobbler @spicyj Was it ever decided that this is something that should still be implemented? Seems like the discussion of whether or not this should be added based on the fact that all events currently bubble kind of ended without a consensus. I'd be willing to help out with this if we decide to go further with it.

anthonybarsotti avatar Jul 01 '16 21:07 anthonybarsotti

I am working on an app that needs to be 508 compliant, and have a flyout menu where the last inner submenu li element should fire a focusOut event (for the parent li) when you go past it. However, it doesn't fire it. I have added the onBlur event and it doesn't work as expected. I can take a look at this.

trigun539 avatar Jul 02 '16 02:07 trigun539

Navigation is all done through keyboard.

trigun539 avatar Jul 02 '16 02:07 trigun539

@trigun539 Can you post a code example by any chance? I just created a basic example with a ul and two li's that each have an inner anchor and after adding onFocus and onBlur events to the li's, the events fire correctly and bubble up from the inner anchors.

anthonybarsotti avatar Jul 02 '16 03:07 anthonybarsotti

@anthonybarsotti the set up is more around the following:

`

  • Home
  • Parent Item with Submenu
    • Submenu item 1
    • Submenu item 2(last li)
  • Another test link
`

If I tab when I am on the "last li" I should go to "Another test" li, but it should fire an onBlur event on the "parent item" li. I'll post a sample after I get it set up.

trigun539 avatar Jul 05 '16 00:07 trigun539