/
MenuItem.js
160 lines (137 loc) · 4.02 KB
/
MenuItem.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import React, { memo, forwardRef, useMemo, useCallback } from 'react'
import PropTypes from 'prop-types'
import Box from 'ui-box'
import { useClickable, useStyleConfig } from '../../hooks'
import { IconWrapper } from '../../icons/src/IconWrapper'
import { Pane } from '../../layers'
import { Text } from '../../typography'
const noop = () => {}
const pseudoSelectors = {
_hover:
'&[data-isselectable="true"]:not([aria-current="true"]):not([aria-checked="true"]):not(:focus):not(:active):hover',
_focus:
'&[data-isselectable="true"]:not([aria-current="true"]):not([aria-checked="true"]):focus,&[aria-selected="true"]',
_active: '&[aria-current="true"],&[data-isselectable="true"]:active',
_current: '&[aria-current="true"],&[aria-checked="true"]',
_isSelectable: '&[data-isselectable="true"]',
_disabled: '&:disabled,&[aria-disabled="true"]'
}
const internalStyles = {
display: 'flex',
alignItems: 'center'
}
const MenuItem = memo(
forwardRef(function MenuItem(props, ref) {
const {
is = 'div',
children,
className,
appearance = 'default',
disabled,
secondaryText,
intent = 'none',
icon,
onSelect = noop,
...passthroughProps
} = props
const handleClick = useCallback(
event => {
if (disabled) return
onSelect(event)
},
[disabled, onSelect]
)
// Pass all props, so the hook can handled `disabled`, `onKeyDown`, `tabIndex`
// and any other explicit props that are passed through to the underlying component
const { onKeyDown, tabIndex } = useClickable(props)
const themedProps = useStyleConfig('MenuItem', { appearance }, pseudoSelectors, internalStyles)
let iconColor = intent === 'none' ? 'default' : intent
if (disabled) {
iconColor = 'muted'
}
const textColor = disabled ? 'muted' : intent
const secondaryTextColor = 'muted'
const disabledProps = useMemo(() => {
return disabled
? {
backgroundColor: 'tint1',
cursor: 'not-allowed',
disabled: true,
onClick: null,
onKeyPress: null,
tabIndex: -1,
'aria-disabled': 'true',
'data-isselectable': 'false'
}
: {}
}, [disabled])
return (
<Pane
is={is}
role="menuitem"
className={className}
onClick={handleClick}
data-isselectable={!disabled || undefined}
aria-disabled={disabled}
ref={ref}
height={icon ? 40 : 32}
{...themedProps}
{...passthroughProps}
{...disabledProps}
tabIndex={tabIndex}
onKeyDown={onKeyDown}
>
<IconWrapper icon={icon} color={iconColor} marginLeft={16} marginRight={-4} size={16} flexShrink={0} />
<Text color={textColor} marginLeft={16} marginRight={16} flex={1}>
{children}
</Text>
{secondaryText && (
<Text marginRight={16} color={secondaryTextColor}>
{secondaryText}
</Text>
)}
</Pane>
)
})
)
MenuItem.propTypes = {
/**
* Element type to use for the menu item.
* For example: `<MenuItem is={ReactRouterLink}>...</MenuItem>`
*/
is: Box.propTypes.is,
/**
* Class name passed to the component.
* Only use if you know what you are doing.
*/
className: PropTypes.string,
/**
* Function that is called on click and enter/space keypress.
*/
onSelect: PropTypes.func,
/**
* The Evergreen or custom icon before the label.
*/
icon: PropTypes.oneOfType([PropTypes.elementType, PropTypes.element]),
/**
* The children of the component.
*/
children: PropTypes.node,
/**
* Secondary text shown on the right.
*/
secondaryText: PropTypes.node,
/**
* The default theme only supports one default appearance.
*/
appearance: PropTypes.string,
/**
* The intent of the menu item.
*/
intent: PropTypes.string,
/**
* Flag to indicate whether the menu item is disabled or not
*/
disabled: PropTypes.bool
}
export default MenuItem