-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
index.js
71 lines (61 loc) · 1.65 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
/**
* External dependencies
*/
import { omit } from 'lodash';
/**
* WordPress dependencies
*/
import { NavigableMenu, KeyboardShortcuts } from '@wordpress/components';
import { Component, createRef } from '@wordpress/element';
import { focus } from '@wordpress/dom';
class NavigableToolbar extends Component {
constructor() {
super( ...arguments );
this.focusToolbar = this.focusToolbar.bind( this );
this.toolbar = createRef();
}
focusToolbar() {
const tabbables = focus.tabbable.find( this.toolbar.current );
if ( tabbables.length ) {
tabbables[ 0 ].focus();
}
}
componentDidMount() {
if ( this.props.focusOnMount ) {
this.focusToolbar();
}
// We use DOM event listeners instead of React event listeners
// because we want to catch events from the underlying DOM tree
// The React Tree can be different from the DOM tree when using
// portals. Block Toolbars for instance are rendered in a separate
// React Tree.
this.toolbar.current.addEventListener( 'keydown', this.switchOnKeyDown );
}
componentwillUnmount() {
this.toolbar.current.removeEventListener( 'keydown', this.switchOnKeyDown );
}
render() {
const { children, ...props } = this.props;
return (
<NavigableMenu
orientation="horizontal"
role="toolbar"
ref={ this.toolbar }
{ ...omit( props, [
'focusOnMount',
] ) }
>
<KeyboardShortcuts
bindGlobal
// Use the same event that TinyMCE uses in the Classic block for its own `alt+f10` shortcut.
eventName="keydown"
shortcuts={ {
'alt+f10': this.focusToolbar,
} }
/>
{ children }
</NavigableMenu>
);
}
}
export default NavigableToolbar;