Skip to content

Commit

Permalink
Components: Avoid body check on focus return unmount
Browse files Browse the repository at this point in the history
Existed prior to refactoring to track events (#2321, #931).

Causes conflict where intentional tabs away from the mounted element would revert focus back to the element (twice tab to dismiss a tooltip'd element)
  • Loading branch information
aduth committed Mar 16, 2018
1 parent 4eb60df commit 5f79ee8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
17 changes: 12 additions & 5 deletions components/higher-order/with-focus-return/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,20 @@ function withFocusReturn( WrappedComponent ) {
}

componentWillUnmount() {
this.returnFocus();
}

/**
* Upon component unmount, verify whether focus is within the element
* and, if it is, return focus to the element where focus had existed
* at the point of component mount.
*/
returnFocus() {
const { activeElementOnMount, isFocused } = this;
if ( ! activeElementOnMount ) {
return;
}

const { body, activeElement } = document;
if ( isFocused || null === activeElement || body === activeElement ) {
// Verify there is an element to which focus should return, and
// that focus is within the wrapped element while unmount occurs.
if ( activeElementOnMount && isFocused ) {
activeElementOnMount.focus();
}
}
Expand Down
4 changes: 0 additions & 4 deletions components/higher-order/with-focus-return/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,6 @@ describe( 'withFocusReturn()', () => {
const mountedComposite = mount( <Composite /> );
expect( mountedComposite.instance().activeElementOnMount ).toBe( activeElement );

// Change activeElement.
document.activeElement.blur();
expect( document.activeElement ).toBe( document.body );

// Should return to the activeElement saved with this component.
mountedComposite.unmount();
expect( document.activeElement ).toBe( activeElement );
Expand Down

0 comments on commit 5f79ee8

Please sign in to comment.