-
Notifications
You must be signed in to change notification settings - Fork 152
/
menu-surface.tsx
372 lines (338 loc) · 11 KB
/
menu-surface.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
import * as RMWC from '@rmwc/types';
import * as React from 'react';
import {
MDCMenuSurfaceFoundation,
util,
MDCMenuDimensions,
Corner
} from '@material/menu-surface';
import { componentFactory, FoundationComponent, closest } from '@rmwc/base';
import { MDCMenuDistance } from '@material/menu-surface';
const ANCHOR_CORNER_MAP: {
[key: string]: keyof typeof MDCMenuSurfaceFoundation.Corner;
} = {
bottomEnd: 'BOTTOM_END',
bottomLeft: 'BOTTOM_LEFT',
bottomRight: 'BOTTOM_RIGHT',
bottomStart: 'BOTTOM_START',
topEnd: 'TOP_END',
topLeft: 'TOP_LEFT',
topRight: 'TOP_RIGHT',
topStart: 'TOP_START'
};
const getAnchorCornerFromProp = (
anchorCorner: keyof typeof ANCHOR_CORNER_MAP
) => MDCMenuSurfaceFoundation.Corner[ANCHOR_CORNER_MAP[anchorCorner]];
export type AnchorT =
| 'bottomEnd'
| 'bottomLeft'
| 'bottomRight'
| 'bottomStart'
| 'topEnd'
| 'topLeft'
| 'topRight'
| 'topStart';
export type MenuSurfaceOnOpenEventT = RMWC.CustomEventT<{}>;
export type MenuSurfaceOnCloseEventT = RMWC.CustomEventT<{}>;
export interface MenuSurfaceProps {
/** Opens the menu. */
open?: boolean;
/** Make the menu position fixed. */
fixed?: boolean;
/** Moves the menu to the body. Useful for situations where the content might be cutoff by an overflow: hidden container. */
hoistToBody?: boolean;
/** Manually position the menu to one of the corners. */
anchorCorner?: AnchorT;
/** Callback for when the menu is opened. */
onOpen?: (evt: MenuSurfaceOnOpenEventT) => void;
/** Callback for when the menu is closed. */
onClose?: (evt: MenuSurfaceOnCloseEventT) => void;
/** Children to render. */
children?: React.ReactNode;
}
/****************************************************************
* MenuSurface
****************************************************************/
const MenuSurfaceRoot = componentFactory<{}>({
displayName: 'MenuSurfaceRoot',
classNames: (props: MenuSurfaceProps) => [
'mdc-menu-surface',
{
'mdc-menu-surface--fixed': props.fixed
}
],
consumeProps: ['fixed']
});
/** A generic menu component for displaying any type of content. */
export class MenuSurface extends FoundationComponent<
MDCMenuSurfaceFoundation,
MenuSurfaceProps
> {
static displayName = 'MenuSurface';
private root = this.createElement('root');
anchorElement: HTMLElement | null = null;
previousFocus: HTMLElement | null = null;
firstFocusableElement: HTMLElement | null = null;
lastFocusableElement: HTMLElement | null = null;
hoisted = false;
constructor(props: MenuSurfaceProps) {
super(props);
this.handleKeydown = this.handleKeydown.bind(this);
this.handleBodyClick = this.handleBodyClick.bind(this);
}
componentDidMount() {
if (this.root.ref) {
const anchor = closest(
this.root.ref,
`.${MDCMenuSurfaceFoundation.cssClasses.ANCHOR}`
);
anchor && (this.anchorElement = anchor);
}
// this has to be run AFTER we try to get our anchor
super.componentDidMount();
}
componentWillUnmount() {
// if we are hoisted, unhoist the
// component so it gets cleaned up properly
if (this.hoisted) {
this.unhoistMenuFromBody();
}
super.componentWillUnmount();
}
get open() {
return this.foundation.isOpen();
}
set open(value) {
if (value && this.foundation && !this.foundation.isOpen()) {
const focusableElements = this.root.ref
? this.root.ref.querySelectorAll<HTMLElement>(
MDCMenuSurfaceFoundation.strings.FOCUSABLE_ELEMENTS
)
: [];
this.firstFocusableElement =
focusableElements.length > 0 ? focusableElements[0] : null;
this.lastFocusableElement =
focusableElements.length > 0
? focusableElements[focusableElements.length - 1]
: null;
this.foundation.open();
} else {
if (this.foundation && this.foundation.isOpen()) {
this.foundation.close();
}
}
}
getDefaultFoundation() {
return new MDCMenuSurfaceFoundation({
addClass: (className: string) => {
this.root.addClass(className);
},
removeClass: (className: string) => {
this.root.removeClass(className);
},
hasClass: (className: string) =>
className === 'mdc-menu-surface' ? true : this.root.hasClass(className),
hasAnchor: () => !!this.anchorElement,
notifyClose: () => {
this.emit('onClose', {});
this.deregisterBodyClickListener();
// an annoying hack... this is the only
// place to catch the normal close and bodyClick handler
// and correct it if we still want to be open.
if (this.props.open) {
this.open = this.props.open;
}
},
notifyOpen: () => {
this.emit('onOpen', {});
this.registerBodyClickListener();
},
isElementInContainer: (el: HTMLElement) =>
this.root.ref === el || (!!this.root.ref && this.root.ref.contains(el)),
isRtl: () =>
!!this.root.ref &&
getComputedStyle(this.root.ref).getPropertyValue('direction') === 'rtl',
setTransformOrigin: (origin: string) => {
this.root.setStyle(
`${util.getTransformPropertyName(window)}-origin`,
origin
);
},
...this.getFocusAdapterMethods(),
...this.getDimensionAdapterMethods()
});
}
getFocusAdapterMethods() {
return {
isFocused: () => document.activeElement === this.root.ref,
saveFocus: () => {
this.previousFocus = document.activeElement as HTMLElement;
},
restoreFocus: () => {
if (this.root.ref && this.root.ref.contains(document.activeElement)) {
if (this.previousFocus && this.previousFocus.focus) {
this.previousFocus.focus();
}
}
},
isFirstElementFocused: () =>
!!this.firstFocusableElement &&
this.firstFocusableElement === document.activeElement,
isLastElementFocused: () =>
!!this.firstFocusableElement &&
this.firstFocusableElement === document.activeElement,
focusFirstElement: () =>
!!this.firstFocusableElement &&
this.firstFocusableElement.focus &&
this.firstFocusableElement.focus(),
focusLastElement: () =>
!!this.firstFocusableElement &&
this.firstFocusableElement.focus &&
this.firstFocusableElement.focus()
};
}
getDimensionAdapterMethods() {
return {
getInnerDimensions: (): MDCMenuDimensions => {
return {
width: this.root.ref ? this.root.ref.offsetWidth : 0,
height: this.root.ref ? this.root.ref.offsetHeight : 0
};
},
getAnchorDimensions: () =>
this.anchorElement && this.anchorElement.getBoundingClientRect(),
getWindowDimensions: () => {
return { width: window.innerWidth, height: window.innerHeight };
},
getBodyDimensions: () => {
return {
width: document.body.clientWidth,
height: document.body.clientHeight
};
},
getWindowScroll: () => {
return { x: window.pageXOffset, y: window.pageYOffset };
},
setPosition: (position: Partial<MDCMenuDistance>) => {
this.root.setStyle(
'left',
position.left !== undefined ? position.left : null
);
this.root.setStyle(
'right',
position.right !== undefined ? position.right : null
);
this.root.setStyle(
'top',
position.top !== undefined ? position.top : null
);
this.root.setStyle(
'bottom',
position.bottom !== undefined ? position.bottom : null
);
},
setMaxHeight: (height: string) => {
this.root.setStyle('maxHeight', height);
}
};
}
sync(props: MenuSurfaceProps, prevProps: MenuSurfaceProps) {
// fixed
this.syncProp(props.fixed, prevProps.fixed, () => {
this.foundation.setFixedPosition(!!props.fixed);
});
// hoistToBody
this.syncProp(props.hoistToBody, prevProps.hoistToBody, () => {
props.hoistToBody ? this.hoistMenuToBody() : this.unhoistMenuFromBody();
});
// anchorCorner
const anchorCorner =
props.anchorCorner && getAnchorCornerFromProp(props.anchorCorner);
this.syncProp(anchorCorner, (this.foundation as any).anchorCorner_, () => {
if (anchorCorner) {
this.foundation.setAnchorCorner(anchorCorner);
(this.foundation as any).dimensions_ = (this
.foundation as any).adapter_.getInnerDimensions();
(this.foundation as any).autoPosition_();
}
});
// open
this.syncProp(props.open, prevProps.open, () => {
this.open = !!props.open;
});
}
hoistMenuToBody() {
if (this.root.ref && this.root.ref.parentElement) {
document.body.appendChild(
this.root.ref.parentElement.removeChild(this.root.ref)
);
this.hoisted = true;
this.foundation.setIsHoisted(true);
// correct layout for open menu
if (this.props.open) {
// wait an extra frame so that the element is actually
// done being hoisted and painting. Fixes Issue #453
setTimeout(() => (this.foundation as any).autoPosition_());
}
}
}
unhoistMenuFromBody() {
if (this.anchorElement && this.root.ref) {
this.anchorElement.appendChild(this.root.ref);
this.hoisted = false;
this.foundation.setIsHoisted(false);
}
}
setAnchorCorner(corner: Corner) {
this.foundation.setAnchorCorner(corner);
}
registerBodyClickListener() {
/**
* Corrects issue on mobile devices that don't support fast click
* Causing the menu to close as soon as its open
**/
setTimeout(() => {
document.body.addEventListener('click', this.handleBodyClick);
document.body.addEventListener('touchstart', this.handleBodyClick);
}, 150);
}
deregisterBodyClickListener() {
document.body.removeEventListener('click', this.handleBodyClick);
document.body.removeEventListener('touchstart', this.handleBodyClick);
}
handleBodyClick(evt: MouseEvent | TouchEvent) {
this.foundation && this.foundation.handleBodyClick(evt as MouseEvent);
}
handleKeydown(evt: React.KeyboardEvent & KeyboardEvent) {
this.props.onKeyDown && this.props.onKeyDown(evt);
this.foundation.handleKeydown(evt);
}
render() {
const {
children,
open,
anchorCorner,
onOpen,
onClose,
hoistToBody,
...rest
} = this.props;
return (
<MenuSurfaceRoot
{...this.root.props(rest)}
ref={this.root.setRef}
onKeyDown={this.handleKeydown}
>
{children}
</MenuSurfaceRoot>
);
}
}
/****************************************************************
* MenuSurfaceAnchor
****************************************************************/
/** A Menu Anchor. When using the anchorCorner prop of Menu, you must set MenuSurfaceAnchors css style position to absolute. */
export const MenuSurfaceAnchor = componentFactory({
displayName: 'MenuSurfaceAnchor',
classNames: ['mdc-menu-surface--anchor']
});