-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
index.js
105 lines (99 loc) · 2.45 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/**
* External dependencies
*/
import classnames from 'classnames';
import { flatMap } from 'lodash';
/**
* WordPress dependencies
*/
import { DOWN } from '@wordpress/keycodes';
/**
* Internal dependencies
*/
import IconButton from '../icon-button';
import Dropdown from '../dropdown';
import { NavigableMenu } from '../navigable-container';
function DropdownMenu( {
icon = 'menu',
label,
menuLabel,
controls,
className,
position,
} ) {
if ( ! controls || ! controls.length ) {
return null;
}
// Normalize controls to nested array of objects (sets of controls)
let controlSets = controls;
if ( ! Array.isArray( controlSets[ 0 ] ) ) {
controlSets = [ controlSets ];
}
return (
<Dropdown
className={ classnames( 'components-dropdown-menu', className ) }
contentClassName="components-dropdown-menu__popover"
position={ position }
renderToggle={ ( { isOpen, onToggle } ) => {
const openOnArrowDown = ( event ) => {
if ( ! isOpen && event.keyCode === DOWN ) {
event.preventDefault();
event.stopPropagation();
onToggle();
}
};
return (
<IconButton
className="components-dropdown-menu__toggle"
icon={ icon }
onClick={ onToggle }
onKeyDown={ openOnArrowDown }
aria-haspopup="true"
aria-expanded={ isOpen }
label={ label }
tooltip={ label }
>
<span className="components-dropdown-menu__indicator" />
</IconButton>
);
} }
renderContent={ ( { onClose } ) => {
return (
<NavigableMenu
className="components-dropdown-menu__menu"
role="menu"
aria-label={ menuLabel }
>
{ flatMap( controlSets, ( controlSet, indexOfSet ) => (
controlSet.map( ( control, indexOfControl ) => (
<IconButton
key={ [ indexOfSet, indexOfControl ].join() }
onClick={ ( event ) => {
event.stopPropagation();
onClose();
if ( control.onClick ) {
control.onClick();
}
} }
className={ classnames(
'components-dropdown-menu__menu-item',
{
'has-separator': indexOfSet > 0 && indexOfControl === 0,
'is-active': control.isActive,
},
) }
icon={ control.icon }
role="menuitem"
disabled={ control.isDisabled }
>
{ control.title }
</IconButton>
) )
) ) }
</NavigableMenu>
);
} }
/>
);
}
export default DropdownMenu;