Skip to content

Commit

Permalink
Make Popover initial focus work with screen readers. (#5732)
Browse files Browse the repository at this point in the history
  • Loading branch information
afercia authored and aduth committed Apr 11, 2018
1 parent 1325bae commit c17b668
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 21 deletions.
17 changes: 2 additions & 15 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 { defer, isEqual, noop } from 'lodash';
import { isEqual, noop } from 'lodash';

/**
* WordPress dependencies
Expand All @@ -20,19 +20,6 @@ import IconButton from '../icon-button';
import ScrollLock from '../scroll-lock';
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 @@ -108,7 +95,7 @@ class Popover extends Component {

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

Expand Down
6 changes: 0 additions & 6 deletions components/popover/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,6 @@ 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 c17b668

Please sign in to comment.