-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
index.js
73 lines (65 loc) · 1.7 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
* WordPress dependencies
*/
import { NavigableMenu, KeyboardShortcuts } from '@wordpress/components';
import { Component, findDOMNode } from '@wordpress/element';
import { focus, keycodes } from '@wordpress/utils';
/**
* Module Constants
*/
const { ESCAPE } = keycodes;
class NavigableToolbar extends Component {
constructor() {
super( ...arguments );
this.bindNode = this.bindNode.bind( this );
this.focusToolbar = this.focusToolbar.bind( this );
this.onToolbarKeyDown = this.onToolbarKeyDown.bind( this );
}
bindNode( ref ) {
// Disable reason: Need DOM node for finding first focusable element
// on keyboard interaction to shift to toolbar.
// eslint-disable-next-line react/no-find-dom-node
this.toolbar = findDOMNode( ref );
}
focusToolbar() {
const tabbables = focus.tabbable.find( this.toolbar );
if ( tabbables.length ) {
tabbables[ 0 ].focus();
}
}
onToolbarKeyDown( event ) {
if ( event.keyCode !== ESCAPE ) {
return;
}
// Is there a better way to focus the selected block
// TODO: separate focused/selected block state and use Redux actions instead
const selectedBlock = document.querySelector( '.editor-block-list__block.is-selected' );
if ( !! selectedBlock ) {
event.stopPropagation();
selectedBlock.focus();
}
}
render() {
const { children, ...props } = this.props;
return (
<NavigableMenu
orientation="horizontal"
role="toolbar"
deep
ref={ this.bindNode }
onKeyDown={ this.onToolbarKeyDown }
{ ...props }
>
<KeyboardShortcuts
bindGlobal
eventName="keyup"
shortcuts={ {
'alt+f10': this.focusToolbar,
} }
/>
{ children }
</NavigableMenu>
);
}
}
export default NavigableToolbar;