-
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.
Remove findDOMNode usage from the NavigableToolbar component
- Loading branch information
1 parent
8322628
commit dd22b38
Showing
25 changed files
with
472 additions
and
371 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
132 changes: 132 additions & 0 deletions
132
packages/components/src/navigable-container/container.js
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,132 @@ | ||
|
||
/** | ||
* External Dependencies | ||
*/ | ||
import { omit, noop, isFunction } from 'lodash'; | ||
|
||
/** | ||
* WordPress Dependencies | ||
*/ | ||
import { Component, forwardRef } from '@wordpress/element'; | ||
import { focus } from '@wordpress/dom'; | ||
|
||
function cycleValue( value, total, offset ) { | ||
const nextValue = value + offset; | ||
if ( nextValue < 0 ) { | ||
return total + nextValue; | ||
} else if ( nextValue >= total ) { | ||
return nextValue - total; | ||
} | ||
|
||
return nextValue; | ||
} | ||
|
||
class NavigableContainer extends Component { | ||
constructor() { | ||
super( ...arguments ); | ||
this.onKeyDown = this.onKeyDown.bind( this ); | ||
this.bindContainer = this.bindContainer.bind( this ); | ||
|
||
this.getFocusableContext = this.getFocusableContext.bind( this ); | ||
this.getFocusableIndex = this.getFocusableIndex.bind( this ); | ||
} | ||
|
||
bindContainer( ref ) { | ||
const { forwardedRef } = this.props; | ||
this.container = ref; | ||
|
||
if ( isFunction( forwardedRef ) ) { | ||
forwardedRef( ref ); | ||
} else if ( forwardedRef && 'current' in forwardedRef ) { | ||
forwardedRef.current = ref; | ||
} | ||
} | ||
|
||
getFocusableContext( target ) { | ||
const { onlyBrowserTabstops } = this.props; | ||
const finder = onlyBrowserTabstops ? focus.tabbable : focus.focusable; | ||
const focusables = finder.find( this.container ); | ||
|
||
const index = this.getFocusableIndex( focusables, target ); | ||
if ( index > -1 && target ) { | ||
return { index, target, focusables }; | ||
} | ||
return null; | ||
} | ||
|
||
getFocusableIndex( focusables, target ) { | ||
const directIndex = focusables.indexOf( target ); | ||
if ( directIndex !== -1 ) { | ||
return directIndex; | ||
} | ||
} | ||
|
||
onKeyDown( event ) { | ||
if ( this.props.onKeyDown ) { | ||
this.props.onKeyDown( event ); | ||
} | ||
|
||
const { getFocusableContext } = this; | ||
const { cycle = true, eventToOffset, onNavigate = noop, stopNavigationEvents } = this.props; | ||
|
||
const offset = eventToOffset( event ); | ||
|
||
// eventToOffset returns undefined if the event is not handled by the component | ||
if ( offset !== undefined && stopNavigationEvents ) { | ||
// Prevents arrow key handlers bound to the document directly interfering | ||
event.nativeEvent.stopImmediatePropagation(); | ||
|
||
// When navigating a collection of items, prevent scroll containers | ||
// from scrolling. | ||
if ( event.target.getAttribute( 'role' ) === 'menuitem' ) { | ||
event.preventDefault(); | ||
} | ||
|
||
event.stopPropagation(); | ||
} | ||
|
||
if ( ! offset ) { | ||
return; | ||
} | ||
|
||
const context = getFocusableContext( document.activeElement ); | ||
if ( ! context ) { | ||
return; | ||
} | ||
|
||
const { index, focusables } = context; | ||
const nextIndex = cycle ? cycleValue( index, focusables.length, offset ) : index + offset; | ||
if ( nextIndex >= 0 && nextIndex < focusables.length ) { | ||
focusables[ nextIndex ].focus(); | ||
onNavigate( nextIndex, focusables[ nextIndex ] ); | ||
} | ||
} | ||
|
||
render() { | ||
const { children, ...props } = this.props; | ||
// Disable reason: Assumed role is applied by parent via props spread. | ||
/* eslint-disable jsx-a11y/no-static-element-interactions */ | ||
return ( | ||
<div ref={ this.bindContainer } | ||
{ ...omit( props, [ | ||
'stopNavigationEvents', | ||
'eventToOffset', | ||
'onNavigate', | ||
'cycle', | ||
'onlyBrowserTabstops', | ||
'forwardedRef', | ||
] ) } | ||
onKeyDown={ this.onKeyDown } | ||
onFocus={ this.onFocus }> | ||
{ children } | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
const forwardedNavigableContainer = ( props, ref ) => { | ||
return <NavigableContainer { ...props } forwardedRef={ ref } />; | ||
}; | ||
forwardedNavigableContainer.displayName = 'NavigableContainer'; | ||
|
||
export default forwardRef( forwardedNavigableContainer ); |
Oops, something went wrong.