-
Notifications
You must be signed in to change notification settings - Fork 839
/
Copy pathcontext_menu_panel.tsx
526 lines (456 loc) Β· 15 KB
/
context_menu_panel.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import React, {
cloneElement,
Component,
HTMLAttributes,
ReactElement,
ReactNode,
} from 'react';
import classNames from 'classnames';
import { tabbable, FocusableElement } from 'tabbable';
import { CommonProps, NoArgCallback, keysOf } from '../common';
import { EuiIcon } from '../icon';
import { EuiResizeObserver } from '../observer/resize_observer';
import { cascadingMenuKeys } from '../../services';
import {
EuiContextMenuItem,
EuiContextMenuItemProps,
} from './context_menu_item';
export type EuiContextMenuPanelHeightChangeHandler = (height: number) => void;
export type EuiContextMenuPanelTransitionType = 'in' | 'out';
export type EuiContextMenuPanelTransitionDirection = 'next' | 'previous';
export type EuiContextMenuPanelShowPanelCallback = (
currentPanelIndex?: number
) => void;
const titleSizeToClassNameMap = {
s: 'euiContextMenuPanelTitle--small',
m: null,
};
export const SIZES = keysOf(titleSizeToClassNameMap);
export interface EuiContextMenuPanelProps {
initialFocusedItemIndex?: number;
items?: ReactElement[];
onClose?: NoArgCallback<void>;
onHeightChange?: EuiContextMenuPanelHeightChangeHandler;
onTransitionComplete?: NoArgCallback<void>;
onUseKeyboardToNavigate?: NoArgCallback<void>;
showNextPanel?: EuiContextMenuPanelShowPanelCallback;
showPreviousPanel?: NoArgCallback<void>;
title?: ReactNode;
transitionDirection?: EuiContextMenuPanelTransitionDirection;
transitionType?: EuiContextMenuPanelTransitionType;
watchedItemProps?: string[];
/**
* Alters the size of the items and the title
*/
size?: typeof SIZES[number];
}
type Props = CommonProps &
Omit<
HTMLAttributes<HTMLDivElement>,
'onKeyDown' | 'tabIndex' | 'onAnimationEnd' | 'title'
> &
EuiContextMenuPanelProps;
const transitionDirectionAndTypeToClassNameMap = {
next: {
in: 'euiContextMenuPanel-txInLeft',
out: 'euiContextMenuPanel-txOutLeft',
},
previous: {
in: 'euiContextMenuPanel-txInRight',
out: 'euiContextMenuPanel-txOutRight',
},
};
interface State {
prevProps: {
items: Props['items'];
};
menuItems: FocusableElement[];
focusedItemIndex?: number;
currentHeight?: number;
height?: number;
}
export class EuiContextMenuPanel extends Component<Props, State> {
static defaultProps: Partial<Props> = {
items: [],
};
private _isMounted = false;
private backButton?: HTMLElement | null = null;
private panel?: HTMLElement | null = null;
constructor(props: Props) {
super(props);
this.state = {
prevProps: {
items: this.props.items,
},
menuItems: [],
focusedItemIndex:
props.onClose && props.initialFocusedItemIndex != null
? props.initialFocusedItemIndex + 1 // Account for panel title back button
: props.initialFocusedItemIndex,
currentHeight: undefined,
};
}
incrementFocusedItemIndex = (amount: number) => {
let nextFocusedItemIndex;
if (this.state.focusedItemIndex === undefined) {
// If this is the beginning of the user's keyboard navigation of the menu, then we'll focus
// either the first or last item.
nextFocusedItemIndex = amount < 0 ? this.state.menuItems.length - 1 : 0;
} else {
nextFocusedItemIndex = this.state.focusedItemIndex + amount;
if (nextFocusedItemIndex < 0) {
nextFocusedItemIndex = this.state.menuItems.length - 1;
} else if (nextFocusedItemIndex === this.state.menuItems.length) {
nextFocusedItemIndex = 0;
}
}
this.setState({
focusedItemIndex: nextFocusedItemIndex,
});
};
onKeyDown = (event: React.KeyboardEvent<HTMLDivElement>) => {
// If this panel contains items you can use the left arrow key to go back at any time.
// But if it doesn't contain items, then you have to focus on the back button specifically,
// since there could be content inside the panel which requires use of the left arrow key,
// e.g. text inputs.
const { items, onClose, showPreviousPanel } = this.props;
if (
onClose &&
(items?.length ||
document.activeElement === this.backButton ||
document.activeElement === this.panel)
) {
if (event.key === cascadingMenuKeys.ARROW_LEFT) {
if (showPreviousPanel) {
event.preventDefault();
event.stopPropagation();
showPreviousPanel();
if (this.props.onUseKeyboardToNavigate) {
this.props.onUseKeyboardToNavigate();
}
}
}
}
if (items?.length) {
switch (event.key) {
case cascadingMenuKeys.TAB:
requestAnimationFrame(() => {
// NOTE: document.activeElement is stale if not wrapped in requestAnimationFrame
const focusedItemIndex = this.state.menuItems.indexOf(
document.activeElement as HTMLElement
);
// We need to sync our internal state with the user tabbing through items
this.setState({
focusedItemIndex:
focusedItemIndex >= 0 &&
focusedItemIndex < this.state.menuItems.length
? focusedItemIndex
: undefined,
});
});
break;
case cascadingMenuKeys.ARROW_UP:
event.preventDefault();
this.incrementFocusedItemIndex(-1);
if (this.props.onUseKeyboardToNavigate) {
this.props.onUseKeyboardToNavigate();
}
break;
case cascadingMenuKeys.ARROW_DOWN:
event.preventDefault();
this.incrementFocusedItemIndex(1);
if (this.props.onUseKeyboardToNavigate) {
this.props.onUseKeyboardToNavigate();
}
break;
case cascadingMenuKeys.ARROW_RIGHT:
if (this.props.showNextPanel) {
event.preventDefault();
this.props.showNextPanel(
onClose && this.state.focusedItemIndex
? this.state.focusedItemIndex - 1 // Account for panel title back button
: this.state.focusedItemIndex
);
if (this.props.onUseKeyboardToNavigate) {
this.props.onUseKeyboardToNavigate();
}
}
break;
default:
break;
}
}
};
updateFocus() {
// Give positioning time to render before focus is applied. Otherwise page jumps.
requestAnimationFrame(() => {
if (!this._isMounted) {
return;
}
// Setting focus while transitioning causes the animation to glitch, so we have to wait
// until it's finished before we focus anything.
if (this.props.transitionType) {
// If the panel is transitioning, set focus to the panel so that users using
// arrow keys that are fast clickers don't accidentally get stranded focus
// or trigger keystrokes when it shouldn't
this.panel?.focus({ preventScroll: true });
return;
}
// If menuItems has been cleared, iterate through and set menuItems from tabbableItems
if (!this.state.menuItems.length && this.panel) {
const tabbableItems = tabbable(this.panel);
if (tabbableItems.length) {
this.setState({ menuItems: tabbableItems });
}
}
if (this.state.menuItems.length) {
// If an item is focused, focus it
if (this.state.focusedItemIndex != null) {
this.state.menuItems[this.state.focusedItemIndex].focus();
return;
}
// Otherwise, if the back button panel title is present, focus it
if (this.props.onClose) {
this.setState({ focusedItemIndex: 0 });
this.state.menuItems[0].focus();
return;
}
}
// Focus on the panel as a last resort.
if (this.panel && !this.panel.contains(document.activeElement)) {
this.panel.focus();
}
});
}
// If EuiContextMenu is used within an EuiPopover, EuiPopover's own
// `updateFocus()` method hijacks EuiContextMenuPanel's `updateFocus()`
// 350ms after the popover finishes transitioning in. This workaround
// reclaims focus from parent EuiPopovers that do not set an `initialFocus`
reclaimPopoverFocus() {
if (!this.panel) return;
const parent = this.panel.parentNode as HTMLElement;
if (!parent) return;
const hasEuiContextMenuParent = parent.classList.contains('euiContextMenu');
// It's possible to use an EuiContextMenuPanel directly in a popover without
// an EuiContextMenu, so we need to account for that when searching parent nodes
const popoverParent = hasEuiContextMenuParent
? (parent?.parentNode?.parentNode as HTMLElement)
: (parent?.parentNode as HTMLElement);
if (!popoverParent) return;
const hasPopoverParent = popoverParent.classList.contains(
'euiPopover__panel'
);
if (!hasPopoverParent) return;
// If the popover panel gains focus, switch it to the context menu panel instead
popoverParent.addEventListener('focus', () => {
this.updateFocus();
});
}
onTransitionComplete = () => {
if (this.props.onTransitionComplete) {
this.props.onTransitionComplete();
}
};
componentDidMount() {
this.updateFocus();
this.reclaimPopoverFocus();
this._isMounted = true;
}
componentWillUnmount() {
this._isMounted = false;
}
static getDerivedStateFromProps(
nextProps: Props,
prevState: State
): Partial<State> | null {
let needsUpdate = false;
const nextState: Partial<State> = {};
// Clear refs to menuItems if we're getting new ones.
if (nextProps.items !== prevState.prevProps.items) {
needsUpdate = true;
nextState.menuItems = [];
nextState.prevProps = { items: nextProps.items };
}
if (needsUpdate) {
return nextState;
}
return null;
}
getWatchedPropsForItems(items: ReactElement[]) {
// This lets us compare prevProps and nextProps among items so we can re-render if our items
// have changed.
const { watchedItemProps } = this.props;
// Create fingerprint of all item's watched properties
if (items.length && watchedItemProps && watchedItemProps.length) {
return JSON.stringify(
items.map((item) => {
// Create object of item properties and values
const props: any = {
key: item.key,
};
watchedItemProps.forEach((prop: string) => {
props[prop] = item.props[prop];
});
return props;
})
);
}
return null;
}
didItemsChange(prevItems: ReactElement[], nextItems: ReactElement[]) {
// If the count of items has changed then update
if (prevItems.length !== nextItems.length) {
return true;
}
// Check if any watched item properties changed by quick string comparison
if (
this.getWatchedPropsForItems(nextItems) !==
this.getWatchedPropsForItems(prevItems)
) {
return true;
}
}
shouldComponentUpdate(nextProps: Props, nextState: State) {
// Prevent calling `this.updateFocus()` below if we don't have to.
if (nextProps.transitionType !== this.props.transitionType) {
return true;
}
if (nextState.focusedItemIndex !== this.state.focusedItemIndex) {
return true;
}
// **
// this component should have either items or children,
// if there are items we can determine via `watchedItemProps` if we should update
// if there are children we can't know if they have changed so return true
// **
if (
(this.props.items && this.props.items.length > 0) ||
(nextProps.items && nextProps.items.length > 0)
) {
if (this.didItemsChange(this.props.items!, nextProps.items!)) {
return true;
}
}
// it's not possible (in any good way) to know if `children` has changed, assume they might have
if (this.props.children != null) {
return true;
}
return false;
}
updateHeight() {
const currentHeight = this.panel ? this.panel.clientHeight : 0;
if (this.state.height !== currentHeight) {
if (this.props.onHeightChange) {
this.props.onHeightChange(currentHeight);
this.setState({ height: currentHeight });
}
}
}
componentDidUpdate() {
this.updateFocus();
}
panelRef = (node: HTMLElement | null) => {
this.panel = node;
this.updateHeight();
};
render() {
const {
children,
className,
onClose,
title,
onHeightChange,
transitionType,
transitionDirection,
onTransitionComplete,
onUseKeyboardToNavigate,
items,
watchedItemProps,
initialFocusedItemIndex,
showNextPanel,
showPreviousPanel,
size,
...rest
} = this.props;
let panelTitle;
if (title) {
const titleClasses = classNames(
'euiContextMenuPanelTitle',
size && titleSizeToClassNameMap[size]
);
if (Boolean(onClose)) {
panelTitle = (
<button
className={titleClasses}
type="button"
onClick={onClose}
ref={(node) => {
this.backButton = node;
}}
data-test-subj="contextMenuPanelTitleButton"
>
<span className="euiContextMenu__itemLayout">
<EuiIcon
type="arrowLeft"
size="m"
className="euiContextMenu__icon"
/>
<span className="euiContextMenu__text">{title}</span>
</span>
</button>
);
} else {
panelTitle = (
<div className={titleClasses}>
<span className="euiContextMenu__itemLayout">{title}</span>
</div>
);
}
}
const classes = classNames(
'euiContextMenuPanel',
className,
transitionDirection &&
transitionType &&
transitionDirectionAndTypeToClassNameMap[transitionDirection]
? transitionDirectionAndTypeToClassNameMap[transitionDirection][
transitionType
]
: undefined
);
const content =
items && items.length
? items.map((MenuItem) => {
const cloneProps: Partial<EuiContextMenuItemProps> = {};
if (size) {
cloneProps.size = size;
}
return MenuItem.type === EuiContextMenuItem
? cloneElement(MenuItem, cloneProps)
: MenuItem;
})
: children;
return (
<div
ref={this.panelRef}
className={classes}
onKeyDown={this.onKeyDown}
tabIndex={-1}
onAnimationEnd={this.onTransitionComplete}
{...rest}
>
{panelTitle}
<EuiResizeObserver onResize={() => this.updateHeight()}>
{(resizeRef) => <div ref={resizeRef}>{content}</div>}
</EuiResizeObserver>
</div>
);
}
}