-
Notifications
You must be signed in to change notification settings - Fork 81
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
Kdropdown keyboard navigation #185
Merged
rtibbles
merged 14 commits into
learningequality:main
from
marcellamaki:kdropdown-keyboard-navigation
May 24, 2022
+137
−23
Merged
Changes from 12 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
0841da6
Add keyboard navigation to dropdown menu
marcellamaki 481189c
re-add commented out code, and remove unnecessary containFocus prop
marcellamaki 8b015f4
update code to use arrow keys for menu navigation
marcellamaki 321a649
fix linting
marcellamaki 4d794ce
Fix tab navigation in popover. Tabbing does not move between menu opt…
marcellamaki 2361a84
Add keyboard modality support for UiMenuOption and so works
marcellamaki 07ca4ef
fix linting
marcellamaki e4b530a
Revert some changes to trackInputModality, but add new options to bet…
marcellamaki 65fe3a8
Manage 'clicks' separately based on mouseup vs. keyboard presses
marcellamaki f60af80
Revert test changes; update git: reference to https: in yarn.lock
marcellamaki 2ee8772
Switch to event.key reference, using .keyCode as fallback only
marcellamaki 8dc1b3d
Ensure both JS state and the body attribute are set for keyboard moda…
marcellamaki 8cdef91
Resolve feedback
marcellamaki fcb8cb0
Remove console
marcellamaki File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,13 +7,68 @@ | |
|
||
import globalThemeState from './globalThemeState'; | ||
|
||
// only keys listed here will change modality to keyboard | ||
const KEYS_WHITELIST = ['Tab']; | ||
|
||
function setUpEventHandlers(disableFocusRingByDefault) { | ||
let recentKeyboardEvent = null; | ||
let hadKeyboardEvent = false; | ||
let hadClickEvent = false; | ||
const keyboardModalityDefaultElements = [ | ||
'input:not([type])', | ||
'input[type=text]', | ||
'input[type=radio]', | ||
'input[type=checkbox]', | ||
'input[type=number]', | ||
'input[type=date]', | ||
'input[type=time]', | ||
'input[type=datetime]', | ||
'textarea', | ||
'[role=textbox]', | ||
'a', | ||
'button', | ||
].join(','); | ||
|
||
// add this to any element to allow keyboard navigation, regardless of focus event | ||
const keyboardModalityOverride = ['[supports-modality=keyboard]']; | ||
|
||
let isHandlingKeyboardThrottle; | ||
|
||
const matcher = (() => { | ||
const el = document.body; | ||
|
||
if (el.matchesSelector) { | ||
return el.matchesSelector; | ||
} | ||
|
||
if (el.webkitMatchesSelector) { | ||
return el.webkitMatchesSelector; | ||
} | ||
|
||
if (el.mozMatchesSelector) { | ||
return el.mozMatchesSelector; | ||
} | ||
|
||
if (el.msMatchesSelector) { | ||
return el.msMatchesSelector; | ||
} | ||
})(); | ||
|
||
const focusTriggersKeyboardModality = function(el) { | ||
let triggers = false; | ||
|
||
if (matcher) { | ||
triggers = | ||
matcher.call(el, keyboardModalityDefaultElements) && matcher.call(el, ':not([readonly])'); | ||
} | ||
return triggers; | ||
}; | ||
|
||
const focusSetExplicitly = function(el) { | ||
let triggers = false; | ||
|
||
if (matcher) { | ||
triggers = matcher.call(el, keyboardModalityOverride) && matcher.call(el, ':not([readonly])'); | ||
} | ||
return triggers; | ||
}; | ||
|
||
if (disableFocusRingByDefault) { | ||
const css = 'body:not([modality=keyboard]) :focus { outline: none; }'; | ||
const head = document.head || document.getElementsByTagName('head')[0]; | ||
|
@@ -33,25 +88,39 @@ function setUpEventHandlers(disableFocusRingByDefault) { | |
|
||
document.body.addEventListener( | ||
'keydown', | ||
event => { | ||
recentKeyboardEvent = event; | ||
() => { | ||
hadKeyboardEvent = true; | ||
hadClickEvent = false; | ||
|
||
if (isHandlingKeyboardThrottle) { | ||
clearTimeout(isHandlingKeyboardThrottle); | ||
} | ||
|
||
isHandlingKeyboardThrottle = setTimeout(() => { | ||
recentKeyboardEvent = null; | ||
hadKeyboardEvent = false; | ||
}, 100); | ||
}, | ||
true | ||
); | ||
|
||
document.body.addEventListener('mouseup', () => { | ||
marcellamaki marked this conversation as resolved.
Show resolved
Hide resolved
|
||
hadClickEvent = true; | ||
hadKeyboardEvent = false; | ||
}); | ||
|
||
document.body.addEventListener( | ||
'focus', | ||
() => { | ||
if (recentKeyboardEvent && KEYS_WHITELIST.includes(recentKeyboardEvent.key)) { | ||
e => { | ||
if ( | ||
(hadKeyboardEvent && focusTriggersKeyboardModality(e.target)) || | ||
(focusSetExplicitly(e.target) && !hadClickEvent) | ||
) { | ||
// both the JS state and the body attribute for keyboard modality | ||
globalThemeState.inputModality = 'keyboard'; | ||
document.body.setAttribute('modality', 'keyboard'); | ||
} else { | ||
globalThemeState.inputModality = null; | ||
document.body.setAttribute('modality', ''); | ||
Comment on lines
+120
to
+123
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See learningequality/kolibri#4822 and learningequality/kolibri#4847 for some historical context on how the code got into its current state.
marcellamaki marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
}, | ||
true | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When we're on the lastChild and hit tab (keycode 9) then the focus outline is lost altogether and I'm not sure where it is. The menu stays open as well.
On that W3 link I shared in my previous comment, if you keyboard navigate to the dropdown (you'll have to eyeball it because it doesn't show a focus indicator on that site) - but if you hit space to open it, arrows to navigate within it, and then hit tab - the tab key moves the focus to the next element in the DOM.
So the listed items in the drop-down should not be a part of the tab navigation.