-
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
Add a higher order component to constrain Tab keyboard navigation. #6987
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
940b069
Add a higher order component to constrain Tab keyboard navigation.
afercia 1003e94
Components: Rename withFocusContain to withConstrainedTabbing
aduth c7f1c76
Components: Export withConstrainedTabbing from components module
aduth 9e96aa2
Components: Use createHigherOrderComponent for withConstrainedTabbing
aduth 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# withFocusContain | ||
|
||
`withFocusContain` is a React [higher-order component](https://facebook.github.io/react/docs/higher-order-components.html) adding the ability to constrain keyboard navigation with the Tab key within a component. For accessibility reasons, some UI components need to constrain Tab navigation, for example modal dialogs or similar UI. Use of this component is recommended only in cases where a way to navigate away from the wrapped component is implemented by other means, usually by pressing the Escape key or using a specific UI control, e.g. a "Close" button. | ||
|
||
## Usage | ||
|
||
Wrap your original component with `withFocusContain`. |
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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Component, createRef } from '@wordpress/element'; | ||
import { keycodes } from '@wordpress/utils'; | ||
import { focus } from '@wordpress/dom'; | ||
|
||
const { TAB } = keycodes; | ||
|
||
const withFocusContain = ( WrappedComponent ) => { | ||
return class extends Component { | ||
constructor() { | ||
super( ...arguments ); | ||
|
||
this.focusContainRef = createRef(); | ||
this.handleTabBehaviour = this.handleTabBehaviour.bind( this ); | ||
} | ||
|
||
handleTabBehaviour( event ) { | ||
if ( event.keyCode !== TAB ) { | ||
return; | ||
} | ||
|
||
const tabbables = focus.tabbable.find( this.focusContainRef.current ); | ||
if ( ! tabbables.length ) { | ||
return; | ||
} | ||
const firstTabbable = tabbables[ 0 ]; | ||
const lastTabbable = tabbables[ tabbables.length - 1 ]; | ||
|
||
if ( event.shiftKey && event.target === firstTabbable ) { | ||
event.preventDefault(); | ||
lastTabbable.focus(); | ||
} else if ( ! event.shiftKey && event.target === lastTabbable ) { | ||
event.preventDefault(); | ||
firstTabbable.focus(); | ||
} | ||
} | ||
|
||
render() { | ||
// Disable reason: this component is non-interactive, but must capture | ||
// events from the wrapped component to determine when the Tab key is used. | ||
/* eslint-disable jsx-a11y/no-static-element-interactions */ | ||
return ( | ||
<div | ||
onKeyDown={ this.handleTabBehaviour } | ||
ref={ this.focusContainRef } | ||
> | ||
<WrappedComponent { ...this.props } /> | ||
</div> | ||
); | ||
/* eslint-enable jsx-a11y/no-static-element-interactions */ | ||
} | ||
}; | ||
}; | ||
|
||
export default withFocusContain; |
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.
I'm pretty sure a similar mechanism is also implemented elsewhere. Maybe NavigableContainer? Should we consolidate?
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.
🙂 Yes and no. The mechanism in NavigableContainer is used also for elements with tabindex="-1" like menu items.