-
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.
Components: Return focus by stack memory
- Loading branch information
Showing
9 changed files
with
101 additions
and
92 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
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,54 @@ | ||
/** | ||
* 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; | ||
const nextFocusHistory = [ | ||
...focusHistory, | ||
event.target, | ||
].slice( -1 * MAX_STACK_LENGTH ); | ||
|
||
this.setState( { focusHistory: nextFocusHistory } ); | ||
} | ||
|
||
render() { | ||
return ( | ||
<Provider value={ this.state }> | ||
<div onFocus={ this.onFocus }> | ||
{ this.props.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
20 changes: 0 additions & 20 deletions
20
packages/edit-post/src/components/header/more-menu/modal.js
This file was deleted.
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
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