react icon indicating copy to clipboard operation
react copied to clipboard

Bug: onBlur event does not fire if loss of focus occurred due to element being removed by react

Open reubn opened this issue 1 year ago • 6 comments

onBlur event does not fire if loss of focus occurred due to element being removed by react under certain circumstances - behaviour is not consistent.

Link to code example 18.3.0-next-32baab38f-20220811: https://codesandbox.io/s/react-focus-bug-18-3-0-qc5tu8?file=/src/App.js Link to code example 18.0.0-fc46dba67-20220329: https://codesandbox.io/s/react-focus-bug-18-0-0-bp6qn1?file=/src/App.js

Focus outside container: image Button has focus: image Button clicked - resulting in react removing it from DOM ∴ focus is lost from container - but react does not fire onBlur event: image

React versions: 18.3.0-next-32baab38f-20220811, 18.0.0-fc46dba67-20220329

Steps To Reproduce

See code example

Link to code example 18.3.0-next-32baab38f-20220811: https://codesandbox.io/s/react-focus-bug-18-3-0-qc5tu8?file=/src/App.js Link to code example 18.0.0-fc46dba67-20220329: https://codesandbox.io/s/react-focus-bug-18-0-0-bp6qn1?file=/src/App.js

The current behaviour

  • 18.3.0-next-32baab38f-20220811:

    • Chrome Dev macOS Version 105.0.5176.3:
      • Keyboard Activation: No onBlur event fires on button nor parent
      • Mouse Activation: No onBlur event fires on button nor parent
    • Safari macOS Version 15.4:
      • Keyboard Activation: No onBlur event fires on button nor parent
      • Mouse Activation: onBlur event fires on button and parent
  • 18.0.0-fc46dba67-20220329:

    • Chrome Dev macOS Version 105.0.5176.3:
      • Keyboard Activation: onBlur event fires on button and parent
      • Mouse Activation: onBlur event fires on button and parent
    • Safari macOS Version 15.4:
      • Keyboard Activation: No onBlur event fires on button nor parent
      • Mouse Activation: onBlur event fires on button and parent

See code example for more details

The expected behaviour

onBlur events fire consistently

reubn avatar Aug 14 '22 11:08 reubn

Hi @reubn your issue is not with the react rather it is regarding the core HTML implementation issue. Try giving your div a tabindex. find more details here. tabindex doc and stackoverflow Sol Thanks

malikhaideraliraza avatar Aug 15 '22 15:08 malikhaideraliraza

Thanks @malikhaideraliraza

Yeah, differences between browsers are to be expected, but if react is going to prevent the use of native onFocusIn and onFocusOut events, then the behaviour of the replacements should be consistent and stable between react versions. There is a difference in behaviour between 18.0.0 and later.

Adding a tabIndex to the div is not a workable solution. The div should not be tabbable - only the input and buttons should be.

reubn avatar Aug 15 '22 17:08 reubn

yes you right

Thanks @malikhaideraliraza

Yeah, differences between browsers are to be expected, but if react is going to prevent the use of native onFocusIn and onFocusOut events, then the behaviour of the replacements should be consistent and stable between react versions. There is a difference in behaviour between 18.0.0 and later.

Adding a tabIndex to the div is not a workable solution. The div should not be tabbable - only the input and buttons should be.

yes you are right but actualy in dom we may have div inside another div so we need to set piriority for div for focusing (like z-index).

malikhaideraliraza avatar Aug 16 '22 09:08 malikhaideraliraza

Thanks again @malikhaideraliraza

I don't follow, but this issue does not relate to having a tabIndex on a parent.

The div should not be focusable, and should not sit in the keyboard navigation tree.

React's onBlur and onFocus events should bubble to the div, but the parent should not be focusable itself.

Adding a tabIndex would make it focusable, and degrade the experience for keyboard users. This is not semantic, and is not what I am asking.

There are very few reasons to ever set tabIndex on an element - especially a non-interactive element like a div.

This is about react's reimplementation of native focus events, bubbling, and lack of consistency between browsers and react versions.

reubn avatar Aug 16 '22 10:08 reubn

Am also have this issue where an element is unmounted and blur is not being called. Reverting back to 18.0.0 fixes it.

craigwduckett avatar Jan 19 '23 18:01 craigwduckett

We are having the same issue. Is there an update please?

teadams avatar Dec 18 '23 13:12 teadams

This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment!

github-actions[bot] avatar Apr 10 '24 02:04 github-actions[bot]

Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please create a new issue with up-to-date information. Thank you!

github-actions[bot] avatar Apr 17 '24 12:04 github-actions[bot]