Skip to content
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

hasKeyboardModality: suppress hadKeyboardEvent changes while typing. #260

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

joseph
Copy link

@joseph joseph commented Nov 7, 2021

This PR is submitted as a proposed improvement in behavior, and since it might be at variance with existing browser implementations of :focus-visible, it's reasonable to reject it. But I think it's worth discussing whether the minor improvement is desirable, and at least to put this PR in the repo history for anyone who feels .focus-visible should work this way.

In summary, this proposal improves the behavior when using Enter to submit a form causes the focus to shift to another element on the page.

The goal is to match user expectations in a common scenario for Single Page Apps and generally dynamic webpages. Say you have a search field that, when submitted, fetches data via XHR and displays it on the page, calling focus() on the first search result.

With the existing behavior, clicking into the field, typing the query, then hitting Enter will cause that first result to be focus-visible — due to the Enter keydown. That's unexpected for people who mostly use a pointer.

With this change, it's not the Enter keydown that determines focus-visible, it's the method by which you focused the field. If you used Tab to focus the field, the first result will be focus-visible. If you used a pointer to focus the field, it will not.

More details in the commit messages.

While keydowns inside the input are suppressed, the method by which
you focused the input is remembered. So if you Tab into a text
field, type something, and hit Enter, then the app manually calls
focus() on the result: that other element _will_ have
the .focus-visible class, due to the _Tab_ key, not the Enter key.

Whereas if you click or tap into a text field, type something, and
hit Enter: when the app manually calls focus() on the result, it
will _not_ have the .focus-visible class, because the last relevant
interaction was clicking into the text field, not hitting Enter.
@joseph
Copy link
Author

joseph commented Nov 7, 2021

tbh it might be slightly clearer to name the new variable something like isTyping, since in this codebase "keyboard modality" is effectively synonymous with hadKeyboardEvent — that is, it means "using the keyboard to navigate" rather than "using the keyboard to type".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant