Skip to content

Commit

Permalink
Components: Restrict Popover focusOnMount to keyboard interaction
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Mar 7, 2018
1 parent a468cf5 commit 8f715e0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
17 changes: 15 additions & 2 deletions components/popover/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* External dependencies
*/
import classnames from 'classnames';
import { isEqual, noop } from 'lodash';
import { defer, isEqual, noop } from 'lodash';

/**
* WordPress dependencies
Expand All @@ -19,6 +19,19 @@ import PopoverDetectOutside from './detect-outside';
import IconButton from '../icon-button';
import { Slot, Fill } from '../slot-fill';

/**
* Value representing whether a key is currently pressed. Bound to the document
* for use in determining whether the Popover component has mounted in response
* to a keyboard event. Popover's focusOnMount behavior is specific to keyboard
* interaction. Must be bound at the top-level and its unsetting deferred since
* the component will have already mounted by the time keyup occurs.
*
* @type {boolean}
*/
let isKeyDown = false;
document.addEventListener( 'keydown', () => isKeyDown = true );
document.addEventListener( 'keyup', defer.bind( null, () => isKeyDown = false ) );

const FocusManaged = withFocusReturn( ( { children } ) => children );

const { ESCAPE } = keycodes;
Expand Down Expand Up @@ -94,7 +107,7 @@ class Popover extends Component {

focus() {
const { focusOnMount = true } = this.props;
if ( ! focusOnMount ) {
if ( ! focusOnMount || ! isKeyDown ) {
return;
}

Expand Down
21 changes: 20 additions & 1 deletion components/popover/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ describe( 'Popover', () => {

afterEach( () => {
jest.restoreAllMocks();

// Resetting keyboard state is deferred, so ensure that timers are
// consumed to avoid leaking into other tests.
jest.runAllTimers();

if ( document.activeElement ) {
document.activeElement.blur();
}
} );

it( 'should add window events', () => {
Expand Down Expand Up @@ -59,7 +67,12 @@ describe( 'Popover', () => {
expect( Popover.prototype.setForcedPositions ).not.toHaveBeenCalled();
} );

it( 'should focus when opening', () => {
it( 'should focus when opening in response to keyboard event', () => {
// As in the real world, these occur in sequence before the popover
// has been mounted. Keyup's resetting is deferred.
document.dispatchEvent( new window.KeyboardEvent( 'keydown' ) );
document.dispatchEvent( new window.KeyboardEvent( 'keyup' ) );

// An ideal test here would mount with an input child and focus the
// child, but in context of JSDOM the inputs are not visible and
// are therefore skipped as tabbable, defaulting to popover.
Expand All @@ -70,6 +83,12 @@ describe( 'Popover', () => {
expect( document.activeElement ).toBe( content );
} );

it( 'should not focus when opening in response to pointer event', () => {
wrapper = mount( <Popover /> );

expect( document.activeElement ).toBe( document.body );
} );

it( 'should allow focus-on-open behavior to be disabled', () => {
const activeElement = document.activeElement;

Expand Down

0 comments on commit 8f715e0

Please sign in to comment.