-
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.
Components: Add onFocusLoss option to withFocusReturn (#14444)
- Loading branch information
1 parent
993f8c4
commit 99309c4
Showing
7 changed files
with
286 additions
and
44 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
71 changes: 71 additions & 0 deletions
71
packages/components/src/higher-order/with-focus-return/context.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,71 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { uniq } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Component, createContext } from '@wordpress/element'; | ||
|
||
const { Provider, Consumer } = createContext( { | ||
focusHistory: [], | ||
} ); | ||
|
||
Provider.displayName = 'FocusReturnProvider'; | ||
Consumer.displayName = 'FocusReturnConsumer'; | ||
|
||
/** | ||
* The maximum history length to capture for the focus stack. When exceeded, | ||
* items should be shifted from the stack for each consecutive push. | ||
* | ||
* @type {number} | ||
*/ | ||
const MAX_STACK_LENGTH = 100; | ||
|
||
class FocusReturnProvider extends Component { | ||
constructor() { | ||
super( ...arguments ); | ||
|
||
this.onFocus = this.onFocus.bind( this ); | ||
|
||
this.state = { | ||
focusHistory: [], | ||
}; | ||
} | ||
|
||
onFocus( event ) { | ||
const { focusHistory } = this.state; | ||
|
||
// Push the focused element to the history stack, keeping only unique | ||
// members but preferring the _last_ occurrence of any duplicates. | ||
// Lodash's `uniq` behavior favors the first occurrence, so the array | ||
// is temporarily reversed prior to it being called upon. Uniqueness | ||
// helps avoid situations where, such as in a constrained tabbing area, | ||
// the user changes focus enough within a transient element that the | ||
// stack may otherwise only consist of members pending destruction, at | ||
// which point focus might have been lost. | ||
const nextFocusHistory = uniq( | ||
[ ...focusHistory, event.target ] | ||
.slice( -1 * MAX_STACK_LENGTH ) | ||
.reverse() | ||
).reverse(); | ||
|
||
this.setState( { focusHistory: nextFocusHistory } ); | ||
} | ||
|
||
render() { | ||
const { children, className } = this.props; | ||
|
||
return ( | ||
<Provider value={ this.state }> | ||
<div onFocus={ this.onFocus } className={ className }> | ||
{ children } | ||
</div> | ||
</Provider> | ||
); | ||
} | ||
} | ||
|
||
export default FocusReturnProvider; | ||
export { Consumer }; |
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
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
Oops, something went wrong.