-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
index.js
87 lines (76 loc) · 1.77 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
/**
* External dependencies
*/
import classnames from 'classnames';
import { isString } from 'lodash';
/**
* WordPress dependencies
*/
import { createElement, cloneElement } from '@wordpress/element';
import { withInstanceId } from '@wordpress/compose';
/**
* Internal dependencies
*/
import Button from '../button';
import Shortcut from '../shortcut';
import IconButton from '../icon-button';
/**
* Renders a generic menu item for use inside the more menu.
*
* @return {WPElement} More menu item.
*/
export function MenuItem( {
children,
info,
className,
icon,
shortcut,
isSelected,
role = 'menuitem',
instanceId,
...props
} ) {
className = classnames( 'components-menu-item__button', className, {
'has-icon': icon,
} );
if ( info ) {
const infoId = 'edit-post-feature-toggle__info-' + instanceId;
// Deconstructed props is scoped to the function; mutation is fine.
props[ 'aria-describedby' ] = infoId;
children = (
<span className="components-menu-item__info-wrapper">
{ children }
<span
id={ infoId }
className="components-menu-item__info">
{ info }
</span>
</span>
);
}
let tagName = Button;
if ( icon ) {
if ( ! isString( icon ) ) {
icon = cloneElement( icon, {
className: 'components-menu-items__item-icon',
height: 20,
width: 20,
} );
}
tagName = IconButton;
props.icon = icon;
}
return createElement(
tagName,
{
// Make sure aria-checked matches spec https://www.w3.org/TR/wai-aria-1.1/#aria-checked
'aria-checked': ( role === 'menuitemcheckbox' || role === 'menuitemradio' ) ? isSelected : undefined,
role,
className,
...props,
},
children,
<Shortcut className="components-menu-item__shortcut" shortcut={ shortcut } />
);
}
export default withInstanceId( MenuItem );