-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathmenu.js
57 lines (48 loc) · 1.08 KB
/
menu.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
/**
* External dependencies
*/
import { includes } from 'lodash';
/**
* WordPress dependencies
*/
import { forwardRef } from '@wordpress/element';
import { UP, DOWN, LEFT, RIGHT } from '@wordpress/keycodes';
/**
* Internal dependencies
*/
import NavigableContainer from './container';
export function NavigableMenu(
{ role = 'menu', orientation = 'vertical', ...rest },
ref
) {
const eventToOffset = ( evt ) => {
const { keyCode } = evt;
let next = [ DOWN ];
let previous = [ UP ];
if ( orientation === 'horizontal' ) {
next = [ RIGHT ];
previous = [ LEFT ];
}
if ( orientation === 'both' ) {
next = [ RIGHT, DOWN ];
previous = [ LEFT, UP ];
}
if ( includes( next, keyCode ) ) {
return 1;
} else if ( includes( previous, keyCode ) ) {
return -1;
}
};
return (
<NavigableContainer
ref={ ref }
stopNavigationEvents
onlyBrowserTabstops={ false }
role={ role }
aria-orientation={ role === 'presentation' ? null : orientation }
eventToOffset={ eventToOffset }
{ ...rest }
/>
);
}
export default forwardRef( NavigableMenu );