-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add inert component to redirect unfocusable
- Loading branch information
Showing
3 changed files
with
120 additions
and
11 deletions.
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 |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Component } from 'element'; | ||
|
||
const focusable = [ | ||
'INPUT', | ||
'SELECT', | ||
'TEXTAREA', | ||
'BUTTON', | ||
'OBJECT', | ||
]; | ||
|
||
function isFocusable( node ) { | ||
if ( node.tabIndex < 0 ) { | ||
return false; | ||
} else if ( -1 !== focusable.indexOf( node.nodeName ) ) { | ||
return ! node.disabled; | ||
} else if ( 'A' === node.nodeName || 'AREA' === node.nodeName ) { | ||
return node.hasAttribute( 'href' ); | ||
} | ||
|
||
return node.tabIndex >= 0; | ||
} | ||
|
||
function getNextFocusable( node, siblingDirection ) { | ||
let parent = node.parentNode; | ||
|
||
let childDirection; | ||
if ( 'nextSibling' === siblingDirection ) { | ||
childDirection = 'firstChild'; | ||
} else { | ||
childDirection = 'lastChild'; | ||
} | ||
|
||
do { | ||
while ( node[ siblingDirection ] ) { | ||
if ( isFocusable( node[ siblingDirection ] ) ) { | ||
return node[ siblingDirection ]; | ||
} | ||
|
||
node = node[ siblingDirection ]; | ||
} | ||
|
||
node = parent[ siblingDirection ]; | ||
if ( node ) { | ||
while ( node[ childDirection ] ) { | ||
node = node[ childDirection ]; | ||
} | ||
} else { | ||
node = parent; | ||
} | ||
|
||
if ( isFocusable( node ) ) { | ||
return node; | ||
} | ||
|
||
parent = node.parentNode; | ||
} while ( parent ); | ||
} | ||
|
||
class Inert extends Component { | ||
constructor() { | ||
super( ...arguments ); | ||
|
||
this.onKeyPress = this.onKeyPress.bind( this ); | ||
} | ||
|
||
componentDidMount() { | ||
document.addEventListener( 'keydown', this.onKeyPress ); | ||
} | ||
|
||
componentWillUnmount() { | ||
document.removeEventListener( 'keydown', this.onKeyPress ); | ||
} | ||
|
||
onKeyPress( event ) { | ||
if ( event.which !== 9 ) { | ||
return; | ||
} | ||
|
||
const testDirection = event.shiftKey ? 'nextSibling' : 'previousSibling'; | ||
if ( event.target !== getNextFocusable( this.node, testDirection ) ) { | ||
return; | ||
} | ||
|
||
const nextDirection = event.shiftKey ? 'previousSibling' : 'nextSibling'; | ||
const nextFocusable = getNextFocusable( this.node, nextDirection ); | ||
if ( nextFocusable ) { | ||
nextFocusable.focus(); | ||
event.preventDefault(); | ||
} | ||
} | ||
|
||
render() { | ||
return ( | ||
<div ref={ ( node ) => this.node = node }> | ||
{ this.props.children } | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export default Inert; |
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