-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Components: Add onFocusLoss option to withFocusReturn #14444
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
b3b764d
Components: Add onFocusLoss option to withFocusReturn
aduth 1ad3f95
Edit Post: Add application-wide focus loss handler
aduth 79420a2
Edit Post: Add sidebar focus loss handler
aduth ee486fe
Components: Add onFocusLoss prop to Modal component
aduth a69f29d
Edit Post: Direct More Menu modals focus loss to menu button
aduth a203b08
Edit Post: Apply withFocusReturn to rendered DOM element
aduth 5586d29
Components: Return focus by stack memory
aduth 13c7ae3
Edit Post: Collapse FocusReturnProvider to handle className applicati…
aduth 902a163
Components: Keep only unique entries of focus history
aduth 76cdae1
Components: Monitor own focused elements for focus return unmount
aduth 015ad3d
Components: Document FocusReturnProvider, onFocusLoss
aduth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens if I use two of these providers? Say each
BlockEditorProvider
uses one?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generally, I'd say it should probably be avoided, if possible. The component should fall under a "one-per-app" recommendation.
That said, in practice it shouldn't be too problematic. Any
withFocusReturn
-enhanced component would rely on its closest provider as a source to determine where focus returns. The only potential problem is that the highest provider would still detect focus changes into an area technically governed by another provider, so they're not mutually exclusive. It's hard to imagine it would result in any unexpected behavior, though.