This repository was archived by the owner on Mar 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
fix(FocusTrapZone): Set focus into zone when inner content is lazy loaded #1505
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
307982f
fix(FocusTrapZone): Set focus into zone when inner content is lazy lo…
6d2621c
Update PopupExample.shorthand.tsx
sophieH29 339691b
Update PopupExample.shorthand.tsx
sophieH29 a24b57c
update changelog
26ca423
Merge branch 'fix/focus-trap-zone-lazy-load' of https://github.com/st…
98c3635
Merge branch 'master' into fix/focus-trap-zone-lazy-load
sophieH29 83d147a
Document lazy loaded content case.
c61f9bd
Merge branch 'master' into fix/focus-trap-zone-lazy-load
sophieH29 cd522a9
Merge branch 'fix/focus-trap-zone-lazy-load' of https://github.com/st…
0276766
Merge branch 'master' into fix/focus-trap-zone-lazy-load
sophieH29 4816d4c
Merge branch 'master' into fix/focus-trap-zone-lazy-load
f79d78a
Merge branch 'master' into fix/focus-trap-zone-lazy-load
sophieH29 1f979ab
Merge branch 'master' into fix/focus-trap-zone-lazy-load
sophieH29 9b25f72
Update packages/react/test/specs/lib/FocusTrapZone-test.tsx
sophieH29 705edec
Merge branch 'master' into fix/focus-trap-zone-lazy-load
sophieH29 78b16c5
update changelog
ac2c48e
Merge branch 'master' into fix/focus-trap-zone-lazy-load
sophieH29 9640a60
small improvement
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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -44,7 +44,8 @@ export class FocusTrapZone extends React.Component<FocusTrapZoneProps, {}> { | |
| ariaLabelledBy: PropTypes.string, | ||
| isClickableOutsideFocusTrap: PropTypes.bool, | ||
| ignoreExternalFocusing: PropTypes.bool, | ||
| forceFocusInsideTrap: PropTypes.bool, | ||
| forceFocusInsideTrapOnOutsideFocus: PropTypes.bool, | ||
| forceFocusInsideTrapOnComponentUpdate: PropTypes.bool, | ||
| firstFocusableSelector: PropTypes.string, | ||
| disableFirstFocus: PropTypes.bool, | ||
| focusPreviouslyFocusedInnerElement: PropTypes.bool, | ||
|
|
@@ -58,22 +59,24 @@ export class FocusTrapZone extends React.Component<FocusTrapZoneProps, {}> { | |
|
|
||
| componentDidMount(): void { | ||
| FocusTrapZone._focusStack.push(this) | ||
| const { disableFirstFocus = false } = this.props | ||
|
|
||
| this._previouslyFocusedElementOutsideTrapZone = this._getPreviouslyFocusedElementOutsideTrapZone() | ||
| this._bringFocusIntoZone() | ||
| this._hideContentFromAccessibilityTree() | ||
| } | ||
|
|
||
| if ( | ||
| !this._root.current.contains(this._previouslyFocusedElementOutsideTrapZone) && | ||
| !disableFirstFocus | ||
| ) { | ||
| this._findElementAndFocusAsync() | ||
| componentDidUpdate(): void { | ||
| if (!this.props.forceFocusInsideTrapOnComponentUpdate) { | ||
| return | ||
| } | ||
|
|
||
| this._hideContentFromAccessibilityTree() | ||
| const activeElement = document.activeElement as HTMLElement | ||
| // if after componentDidUpdate focus is not inside the focus trap, bring it back | ||
| if (!this._root.current.contains(activeElement)) { | ||
| this._bringFocusIntoZone() | ||
| } | ||
| } | ||
|
|
||
| render(): JSX.Element { | ||
| const { className, forceFocusInsideTrap, ariaLabelledBy } = this.props | ||
| const { className, forceFocusInsideTrapOnOutsideFocus, ariaLabelledBy } = this.props | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we use these new props in popupFocusTrapBehavior?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if we want it by default then probably set this prop to "true" in FocusTrapZone. But I am not sure if want this by default, maybe better to allow a user to do it through customizing the behavior |
||
| const unhandledProps = getUnhandledProps( | ||
| { handledProps: [..._.keys(FocusTrapZone.propTypes)] }, | ||
| this.props, | ||
|
|
@@ -93,7 +96,7 @@ export class FocusTrapZone extends React.Component<FocusTrapZoneProps, {}> { | |
| {this.props.children} | ||
| </ElementType> | ||
|
|
||
| {forceFocusInsideTrap && ( | ||
| {forceFocusInsideTrapOnOutsideFocus && ( | ||
| <EventListener | ||
| capture | ||
| listener={this._handleOutsideFocus} | ||
|
|
@@ -144,6 +147,19 @@ export class FocusTrapZone extends React.Component<FocusTrapZoneProps, {}> { | |
| } | ||
| } | ||
|
|
||
| _bringFocusIntoZone = () => { | ||
| const { disableFirstFocus = false } = this.props | ||
|
|
||
| this._previouslyFocusedElementOutsideTrapZone = this._getPreviouslyFocusedElementOutsideTrapZone() | ||
|
|
||
| if ( | ||
| !this._root.current.contains(this._previouslyFocusedElementOutsideTrapZone) && | ||
| !disableFirstFocus | ||
| ) { | ||
| this._findElementAndFocusAsync() | ||
| } | ||
| } | ||
|
|
||
| _findElementAndFocusAsync = () => { | ||
| if (!this._root.current) return | ||
| const { focusPreviouslyFocusedInnerElement, firstFocusableSelector } = this.props | ||
|
|
||
This file contains hidden or 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 hidden or 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
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.
I'm not sure this is a good idea. A component can update for many reasons and I am pretty sure it's not always expected behavior to bring focus back to the zone after every update.
Is see this behavior as risky because it can cause unexpected focus jumps so I'd recommend allowing the users to configure the focus zone to add / disable this behavior. Let's wait for other opinions.
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.
@Bugaa92 this will only work if the active element (currently focused element) is not inside the focus trap container (for any reason) - focus should also be there once it is rendered.
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.
as discusse doffline - one scenario where this might not work is if the popup opens another popup with focus trap. Then if the first popup updates, it might steal focus from the second popup. Can you please test this scenario?
Uh oh!
There was an error while loading. Please reload this page.
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.
@jurokapsiar I tested this scenario. Since inner popup is rendered on button click in the first popup without relation to the outside state, it doesn't trigger componentDidUpdate.
Maybe there could be another case that I am missing, I see fabric-ui is doing the same thing. Folks, let me know what you think.
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.
@sophieH29 can we make this behavior configurable via prop?
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.
I've tried this case and it works
fyi @Bugaa92 @jurokapsiar
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.
@layershifter agree, updated