-
Notifications
You must be signed in to change notification settings - Fork 615
ActionMenu: Replace typeahead with mnemonics #2105
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a556121
Replace typeahead with mnemonics
siddharthkp 607129c
add aria-keyshortcuts as part of useMnemonics
siddharthkp b2047a3
Create gold-falcons-shake.md
siddharthkp 793e537
support user configured aria-keyshortcuts
siddharthkp d9a85d0
delete unused useTypeaheadFocus
siddharthkp 7923565
Merge branch 'main' into siddharthkp/menu-mnemonics
siddharthkp 98a21bd
Merge branch 'main' into siddharthkp/menu-mnemonics
siddharthkp 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 hidden or 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@primer/react": patch | ||
--- | ||
|
||
ActionMenu: Replace typeahead behavior with single key mnemonics |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import React from 'react' | ||
import {iterateFocusableElements} from '@primer/behaviors/utils' | ||
import {useProvidedRefOrCreate} from './useProvidedRefOrCreate' | ||
|
||
/* | ||
* A mnemonic indicates to the user which key to press (single) | ||
* to activate a command or navigate to a component | ||
* typically appearing in a menu title, menu item, or the text of a button. | ||
*/ | ||
|
||
export const useMnemonics = (open: boolean, providedRef?: React.RefObject<HTMLElement>) => { | ||
const containerRef = useProvidedRefOrCreate(providedRef) | ||
|
||
React.useEffect( | ||
function addAriaKeyshortcuts() { | ||
if (!open || !containerRef.current) return | ||
const container = containerRef.current | ||
|
||
const focusableItems = [...iterateFocusableElements(container)] | ||
|
||
focusableItems.map(item => { | ||
// if item already has aria-keyshortcuts defined by user, skip | ||
if (item.getAttribute('aria-keyshortcuts')) return | ||
|
||
const firstLetter = item.textContent?.toLowerCase()[0] | ||
if (firstLetter) item.setAttribute('aria-keyshortcuts', firstLetter) | ||
}) | ||
}, | ||
[open, containerRef] | ||
) | ||
|
||
React.useEffect( | ||
function handleKeyDown() { | ||
if (!open || !containerRef.current) return | ||
const container = containerRef.current | ||
|
||
const handler = (event: KeyboardEvent) => { | ||
// skip if a TextInput has focus | ||
const activeElement = document.activeElement as HTMLElement | ||
if (activeElement.tagName === 'INPUT') return | ||
|
||
// skip if used with modifier to preserve shortcuts like ⌘ + F | ||
const hasModifier = event.ctrlKey || event.altKey || event.metaKey | ||
if (hasModifier) return | ||
|
||
// skip if it's not a alphabet key | ||
if (!isAlphabetKey(event)) return | ||
|
||
// if this is a typeahead event, don't propagate outside of menu | ||
event.stopPropagation() | ||
|
||
const query = event.key.toLowerCase() | ||
|
||
let elementToFocus: HTMLElement | undefined | ||
|
||
const focusableItems = [...iterateFocusableElements(container)] | ||
|
||
const itemsMatchingKey = focusableItems.filter(item => { | ||
const keyshortcuts = item | ||
.getAttribute('aria-keyshortcuts') | ||
?.split(' ') | ||
.map(shortcut => shortcut.toLowerCase()) | ||
return keyshortcuts && keyshortcuts.includes(query) | ||
}) | ||
|
||
const currentActiveIndex = itemsMatchingKey.indexOf(activeElement) | ||
|
||
// If the last element is already selected, cycle through the list | ||
if (currentActiveIndex === itemsMatchingKey.length - 1) { | ||
elementToFocus = itemsMatchingKey[0] | ||
} else { | ||
elementToFocus = itemsMatchingKey.find((item, index) => { | ||
return index > currentActiveIndex | ||
}) | ||
} | ||
elementToFocus?.focus() | ||
} | ||
|
||
container.addEventListener('keydown', handler) | ||
return () => container.removeEventListener('keydown', handler) | ||
}, | ||
[open, containerRef] | ||
) | ||
|
||
const isAlphabetKey = (event: KeyboardEvent) => { | ||
return event.key.length === 1 && /[a-z\d]/i.test(event.key) | ||
} | ||
|
||
return {containerRef} | ||
} |
Oops, something went wrong.
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.
What happens if two items have the same first letter?
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.
focus loops through the items:
mnemonics-repeat.mov
(ideally, you'd want to define
aria-keyshortcuts
for them)