forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMenuItem.tsx
275 lines (252 loc) · 7.38 KB
/
MenuItem.tsx
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import { css, cx } from '@emotion/css';
import React, { ReactElement, useCallback, useState, useRef, useImperativeHandle, CSSProperties } from 'react';
import { GrafanaTheme2, LinkTarget } from '@grafana/data';
import { useStyles2 } from '../../themes';
import { getFocusStyles } from '../../themes/mixins';
import { IconName } from '../../types';
import { Icon } from '../Icon/Icon';
import { SubMenu } from './SubMenu';
/** @internal */
export type MenuItemElement = HTMLAnchorElement & HTMLButtonElement & HTMLDivElement;
/** @internal */
export interface MenuItemProps<T = any> {
/** Label of the menu item */
label: string;
/** Aria label for accessibility support */
ariaLabel?: string;
/** Aria checked for accessibility support */
ariaChecked?: boolean;
/** Target of the menu item (i.e. new window) */
target?: LinkTarget;
/** Icon of the menu item */
icon?: IconName;
/** Role of the menu item */
role?: string;
/** Url of the menu item */
url?: string;
/** Handler for the click behaviour */
onClick?: (event: React.MouseEvent<HTMLElement>, payload?: T) => void;
/** Custom MenuItem styles*/
className?: string;
/** Active */
active?: boolean;
/** Disabled */
disabled?: boolean;
/** Show in destructive style (error color) */
destructive?: boolean;
tabIndex?: number;
/** List of menu items for the subMenu */
childItems?: Array<ReactElement<MenuItemProps>>;
/** Custom style for SubMenu */
customSubMenuContainerStyles?: CSSProperties;
/** Shortcut key combination */
shortcut?: string;
/** Test id for e2e tests and fullstory*/
testId?: string;
}
/** @internal */
export const MenuItem = React.memo(
React.forwardRef<MenuItemElement, MenuItemProps>((props, ref) => {
const {
url,
icon,
label,
ariaLabel,
ariaChecked,
target,
onClick,
className,
active,
disabled,
destructive,
childItems,
role = 'menuitem',
tabIndex = -1,
customSubMenuContainerStyles,
shortcut,
testId,
} = props;
const styles = useStyles2(getStyles);
const [isActive, setIsActive] = useState(active);
const [isSubMenuOpen, setIsSubMenuOpen] = useState(false);
const [openedWithArrow, setOpenedWithArrow] = useState(false);
const onMouseEnter = useCallback(() => {
if (disabled) {
return;
}
setIsSubMenuOpen(true);
setIsActive(true);
}, [disabled]);
const onMouseLeave = useCallback(() => {
if (disabled) {
return;
}
setIsSubMenuOpen(false);
setIsActive(false);
}, [disabled]);
const hasSubMenu = childItems && childItems.length > 0;
const ItemElement = hasSubMenu ? 'div' : url === undefined ? 'button' : 'a';
const itemStyle = cx(
{
[styles.item]: true,
[styles.active]: isActive,
[styles.disabled]: disabled,
[styles.destructive]: destructive && !disabled,
},
className
);
const disabledProps = {
[ItemElement === 'button' ? 'disabled' : 'aria-disabled']: disabled,
...(ItemElement === 'a' && disabled && { href: undefined, onClick: undefined }),
...(disabled && {
tabIndex: -1,
['data-disabled']: disabled, // used to identify disabled items in Menu.tsx
}),
};
const localRef = useRef<MenuItemElement>(null);
useImperativeHandle(ref, () => localRef.current!);
const handleKeys = (event: React.KeyboardEvent) => {
switch (event.key) {
case 'ArrowRight':
event.preventDefault();
event.stopPropagation();
if (hasSubMenu) {
setIsSubMenuOpen(true);
setOpenedWithArrow(true);
setIsActive(true);
}
break;
default:
break;
}
};
const closeSubMenu = () => {
setIsSubMenuOpen(false);
setIsActive(false);
localRef?.current?.focus();
};
const hasShortcut = Boolean(shortcut && shortcut.length > 0);
return (
<ItemElement
target={target}
className={itemStyle}
rel={target === '_blank' ? 'noopener noreferrer' : undefined}
href={url}
onClick={onClick}
onMouseEnter={onMouseEnter}
onMouseLeave={onMouseLeave}
onKeyDown={handleKeys}
role={url === undefined ? role : undefined}
data-role="menuitem" // used to identify menuitem in Menu.tsx
ref={localRef}
data-testid={testId}
aria-label={ariaLabel}
aria-checked={ariaChecked}
tabIndex={tabIndex}
{...disabledProps}
>
<>
{/* @ts-ignore */}
{icon && <Icon name={icon} className={styles.icon} aria-hidden />}
{label}
<div className={cx(styles.rightWrapper, { [styles.withShortcut]: hasShortcut })}>
{hasShortcut && (
<div className={styles.shortcut}>
{/* @ts-ignore */}
<Icon name="keyboard" aria-hidden />
{shortcut}
</div>
)}
{hasSubMenu && (
<>
{/* @ts-ignore */}
<SubMenu
items={childItems}
isOpen={isSubMenuOpen}
openedWithArrow={openedWithArrow}
setOpenedWithArrow={setOpenedWithArrow}
close={closeSubMenu}
customStyle={customSubMenuContainerStyles}
/>
</>
)}
</div>
</>
</ItemElement>
);
})
);
MenuItem.displayName = 'MenuItem';
const getStyles = (theme: GrafanaTheme2) => {
return {
item: css({
background: 'none',
cursor: 'pointer',
whiteSpace: 'nowrap',
color: theme.colors.text.primary,
display: 'flex',
alignItems: 'center',
padding: theme.spacing(0.5, 2),
minHeight: theme.spacing(4),
margin: 0,
border: 'none',
width: '100%',
position: 'relative',
'&:hover, &:focus, &:focus-visible': {
background: theme.colors.action.hover,
color: theme.colors.text.primary,
textDecoration: 'none',
},
'&:focus-visible': getFocusStyles(theme),
}),
active: css({
background: theme.colors.action.hover,
}),
destructive: css({
color: theme.colors.error.text,
svg: {
color: theme.colors.error.text,
},
'&:hover, &:focus, &:focus-visible': {
background: theme.colors.error.main,
color: theme.colors.error.contrastText,
svg: {
color: theme.colors.error.contrastText,
},
},
}),
disabled: css({
color: theme.colors.action.disabledText,
'&:hover, &:focus, &:focus-visible': {
cursor: 'not-allowed',
background: 'none',
color: theme.colors.action.disabledText,
},
}),
icon: css({
opacity: 0.7,
marginRight: '10px',
marginLeft: '-4px',
color: theme.colors.text.secondary,
}),
rightWrapper: css({
display: 'flex',
alignItems: 'center',
marginLeft: 'auto',
}),
shortcutIcon: css({
marginRight: theme.spacing(1),
}),
withShortcut: css({
minWidth: theme.spacing(10.5),
}),
shortcut: css({
display: 'flex',
alignItems: 'center',
gap: theme.spacing(1),
marginLeft: theme.spacing(2),
color: theme.colors.text.secondary,
opacity: 0.7,
}),
};
};