react-spectrum icon indicating copy to clipboard operation
react-spectrum copied to clipboard

FocusVisible triggers on window blur

Open BartoszGamza opened this issue 5 years ago โ€ข 3 comments

๐Ÿ› 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: CleanShot 2021-05-13 at 13 37 31

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

BartoszGamza avatar May 13 '21 12:05 BartoszGamza

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 avatar May 13 '21 21:05 snowystinger

@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

BartoszGamza avatar May 14 '21 06:05 BartoszGamza

Looks like this should have been closed with this PR: https://github.com/adobe/react-spectrum/pull/1925

yihuiliao avatar Dec 05 '24 22:12 yihuiliao