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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/focus-visible.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ function applyFocusVisiblePolyfill(scope) {
var hadKeyboardEvent = true;
var hadFocusVisibleRecently = false;
var hadFocusVisibleRecentlyTimeout = null;
var hasKeyboardModality = false;

var inputTypesAllowlist = {
text: true,
Expand Down Expand Up @@ -114,7 +115,9 @@ function applyFocusVisiblePolyfill(scope) {
addFocusVisibleClass(scope.activeElement);
}

hadKeyboardEvent = true;
if (!hasKeyboardModality) {
hadKeyboardEvent = true;
}
}

/**
Expand Down Expand Up @@ -142,7 +145,8 @@ function applyFocusVisiblePolyfill(scope) {
return;
}

if (hadKeyboardEvent || focusTriggersKeyboardModality(e.target)) {
hasKeyboardModality = focusTriggersKeyboardModality(e.target);
if (hadKeyboardEvent || hasKeyboardModality) {
addFocusVisibleClass(e.target);
}
}
Expand All @@ -156,6 +160,8 @@ function applyFocusVisiblePolyfill(scope) {
return;
}

hasKeyboardModality = false;

if (
e.target.classList.contains('focus-visible') ||
e.target.hasAttribute('data-focus-visible-added')
Expand Down
39 changes: 39 additions & 0 deletions test/fixtures/has-keyboard-modality.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>has keyboard modality fixture</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.js-focus-visible :focus:not(.focus-visible) {
outline: none;
}
.focus-visible {
outline: 2px solid red;
}
</style>
<script>
function labelAndFocusButton() {
var button = document.querySelector('button');
button.innerHTML = document.querySelector('input').value;
button.focus();
event.preventDefault();
}
</script>
</head>
<body>
<p>
If you <strong>click</strong> into the input, type something and
hit Enter to submit the form, the button will not be .focus-visible.
</p>
<p>
If you <strong>tab</strong> into the input, type something and
hit Enter, the button <em>will</em> be .focus-visible.
</p>
<form method="GET" action="#" onsubmit="labelAndFocusButton()">
<input type="text" placeholder="Enter button label…" />
<button type="submit">Submit</button>
</form>
<script src="/dist/focus-visible.js"></script>
</body>
</html>