-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
navigateRegions: Convert to TypeScript #48632
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ import { | |
useMergeRefs, | ||
} from '@wordpress/compose'; | ||
import { isKeyboardEvent } from '@wordpress/keycodes'; | ||
import type { WPKeycodeModifier } from '@wordpress/keycodes'; | ||
|
||
const defaultShortcuts = { | ||
previous: [ | ||
|
@@ -23,7 +24,7 @@ const defaultShortcuts = { | |
modifier: 'access', | ||
character: 'p', | ||
}, | ||
], | ||
] as const, | ||
next: [ | ||
{ | ||
modifier: 'ctrl', | ||
|
@@ -33,27 +34,36 @@ const defaultShortcuts = { | |
modifier: 'access', | ||
character: 'n', | ||
}, | ||
], | ||
] as const, | ||
}; | ||
|
||
export function useNavigateRegions( shortcuts = defaultShortcuts ) { | ||
const ref = useRef(); | ||
type Shortcuts = { | ||
previous: readonly { modifier: WPKeycodeModifier; character: string }[]; | ||
next: readonly { modifier: WPKeycodeModifier; character: string }[]; | ||
}; | ||
|
||
export function useNavigateRegions( shortcuts: Shortcuts = defaultShortcuts ) { | ||
const ref = useRef< HTMLDivElement >( null ); | ||
const [ isFocusingRegions, setIsFocusingRegions ] = useState( false ); | ||
|
||
function focusRegion( offset ) { | ||
function focusRegion( offset: number ) { | ||
const regions = Array.from( | ||
ref.current.querySelectorAll( '[role="region"][tabindex="-1"]' ) | ||
ref.current?.querySelectorAll< HTMLElement >( | ||
'[role="region"][tabindex="-1"]' | ||
) ?? [] | ||
); | ||
if ( ! regions.length ) { | ||
return; | ||
} | ||
let nextRegion = regions[ 0 ]; | ||
// Based off the current element, use closest to determine the wrapping region since this operates up the DOM. Also, match tabindex to avoid edge cases with regions we do not want. | ||
const selectedIndex = regions.indexOf( | ||
ref.current.ownerDocument.activeElement.closest( | ||
const wrappingRegion = | ||
ref.current?.ownerDocument?.activeElement?.closest< HTMLElement >( | ||
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. Gotta love optional chaining ❓ ⛓️ |
||
'[role="region"][tabindex="-1"]' | ||
) | ||
); | ||
); | ||
const selectedIndex = wrappingRegion | ||
? regions.indexOf( wrappingRegion ) | ||
: -1; | ||
Comment on lines
+60
to
+66
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. Just making the fallback explicit, as |
||
if ( selectedIndex !== -1 ) { | ||
let nextIndex = selectedIndex + offset; | ||
nextIndex = nextIndex === -1 ? regions.length - 1 : nextIndex; | ||
|
@@ -83,7 +93,7 @@ export function useNavigateRegions( shortcuts = defaultShortcuts ) { | |
return { | ||
ref: useMergeRefs( [ ref, clickRef ] ), | ||
className: isFocusingRegions ? 'is-focusing-regions' : '', | ||
onKeyDown( event ) { | ||
onKeyDown( event: React.KeyboardEvent< HTMLDivElement > ) { | ||
if ( | ||
shortcuts.previous.some( ( { modifier, character } ) => { | ||
return isKeyboardEvent[ modifier ]( event, character ); | ||
|
@@ -101,6 +111,32 @@ export function useNavigateRegions( shortcuts = defaultShortcuts ) { | |
}; | ||
} | ||
|
||
/** | ||
* `navigateRegions` is a React [higher-order component](https://facebook.github.io/react/docs/higher-order-components.html) | ||
* adding keyboard navigation to switch between the different DOM elements marked as "regions" (role="region"). | ||
* These regions should be focusable (By adding a tabIndex attribute for example). For better accessibility, | ||
* these elements must be properly labelled to briefly describe the purpose of the content in the region. | ||
* For more details, see "Landmark Roles" in the [WAI-ARIA specification](https://www.w3.org/TR/wai-aria/) | ||
* and "Landmark Regions" in the [ARIA Authoring Practices Guide](https://www.w3.org/WAI/ARIA/apg/practices/landmark-regions/). | ||
* | ||
* ```jsx | ||
* import { navigateRegions } from '@wordpress/components'; | ||
* | ||
* const MyComponentWithNavigateRegions = navigateRegions( () => ( | ||
* <div> | ||
* <div role="region" tabIndex="-1" aria-label="Header"> | ||
* Header | ||
* </div> | ||
* <div role="region" tabIndex="-1" aria-label="Content"> | ||
* Content | ||
* </div> | ||
* <div role="region" tabIndex="-1" aria-label="Sidebar"> | ||
* Sidebar | ||
* </div> | ||
* </div> | ||
* ) ); | ||
* ``` | ||
*/ | ||
export default createHigherOrderComponent( | ||
( Component ) => | ||
( { shortcuts, ...props } ) => | ||
|
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
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.
Just making the fallback explicit here.