FocusVisible triggers on window blur
๐ Bug Report
First of all, I know it was intentional at some point although it generates unexpected behaviour for certain scenarios including autofocus (see gif below).
real life scenario:

This is somehow related to the recent issue I reported: https://github.com/adobe/react-spectrum/issues/1813 but then it was about switching tabs, after all I noticed it also happens for window blur and therefore my question is:
should blurring the window always result in changed modality?
I find it hard to actually find a case when it is needed. When I removed this handling everything seems to work properly (but then whole hasEventBeforeFocus and handleFocusEvent() looses its meaning, right?). Theres this comment: when navigating a form with the next/previous buttons on iOS. which is meant to explain the shift of focus to virtual. In my testing it doesn't work ideal - it switches while navigating to second input and inputs are properly focusing anyway (and therefore applying correct styles indicating focus). Theres second commend sort of questioning it: If this is a text input component, don't update the focus visible style . iOS arrows are navigating only through form inputs and then disappear anyway which, in my opinion, should bring back the previous modality (which does not happen). And here I wanted to raise second question:
is hasEventBeforeFocus property really needed (as of 2021, maybe something changed since the time of its introduction)?
iOS arrow buttons behaviour: https://user-images.githubusercontent.com/26162660/118122809-a44f0900-b3f3-11eb-904d-5eb7ba183c2a.mp4
๐ค Expected Behavior
isFocusVisible should remain false when returing back to blurred (browser) window as we cannot determine if modality changed.
๐ฏ Current Behavior
if element gets focused after returning back to blurred window isFocusVisible returns true suggesting that modality changed.
๐ Possible Solution
removing hasEventBeforeFocus, handleFocusEvent() or finding better, more precise way to solve the problem they are intended to.
Im willing to help with either ๐
๐ฆ Context
At LiveChat we use useFocusVisible to provide proper keyboard navigation support by highlighting elements. We are truly devoted to accessibility while not affecting regular user workflow in any way. The described behaviour is confusing to users and can cause problems in cases where we display certain UI elements depending on modality.
๐ป Code Sample
You can replicate the scenario from first gif I attached on livechat.com by interacting with the chat widget on the bottom-right corner of page.
๐ Your Environment
| Software | Version(s) |
|---|---|
| react-spectrum | @react-aria/interactions: 3.0.0-nightly.1037 |
| Browser | Chrome 90.0.4430.93 |
| Operating System | OSX 11.2.3 |
๐งข Your Company/Team
Chat Widget at livechat.com
So it looks like going to a different window and coming back doesn't fire the document visibility changes you relied on in your fix for #1813
I think it should behave the same as changing tabs in that the modality shouldn't change, that seems reasonable to me.
We welcome any ideas you have for how to handle this. I don't think we can remove hasEventBeforeFocus, at least, not without finding a different solution. For some context, the commit that added it noted
- Show focus ring if there is a focus event without a preceding keyboard or pointer event
https://github.com/adobe/react-spectrum/commit/7583eea988c0106ccd3cc51d38361ec4219dd24e I don't know the exact specifics of why this was required, but feel free to tag the author of that commit if you aren't sure about it.
@snowystinger thanks for reply! As I showed on iOS behaviour gif, the current handling of it is imperfect, not sure yet how it could be improved but if its the only reason for hasEventBeforeFocus being used and reset on window.blur() I would try tackling it down with touch events. Will work on this today and let you know here about results
Looks like this should have been closed with this PR: https://github.com/adobe/react-spectrum/pull/1925