-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a higher order component to constrain Tab keyboard navigation. (#…
…6987) * Add a higher order component to constrain Tab keyboard navigation. * Components: Rename withFocusContain to withConstrainedTabbing * Components: Export withConstrainedTabbing from components module * Components: Use createHigherOrderComponent for withConstrainedTabbing Consistency on creation of higher-order components
- Loading branch information
Showing
4 changed files
with
72 additions
and
1 deletion.
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 @@ | ||
# withConstrainedTabbing | ||
|
||
`withConstrainedTabbing` 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 `withConstrainedTabbing`. |
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,62 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { | ||
Component, | ||
createRef, | ||
createHigherOrderComponent, | ||
} from '@wordpress/element'; | ||
import { keycodes } from '@wordpress/utils'; | ||
import { focus } from '@wordpress/dom'; | ||
|
||
const { TAB } = keycodes; | ||
|
||
const withConstrainedTabbing = createHigherOrderComponent( | ||
( WrappedComponent ) => 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 */ | ||
} | ||
}, | ||
'withConstrainedTabbing' | ||
); | ||
|
||
export default withConstrainedTabbing; |
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