/
component.ts
245 lines (217 loc) · 8.51 KB
/
component.ts
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
/**
* @license
* Copyright 2018 Google Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
import {getCorrectPropertyName} from '@material/animation/util';
import {MDCComponent} from '@material/base/component';
import {SpecificEventListener} from '@material/base/types';
import {MDCMenuSurfaceAdapter} from './adapter';
import {Corner, cssClasses, strings} from './constants';
import {MDCMenuSurfaceFoundation} from './foundation';
import {MDCMenuDistance} from './types';
type RegisterFunction = () => void;
/** MDC Menu Surface Factory */
export type MDCMenuSurfaceFactory =
(el: HTMLElement, foundation?: MDCMenuSurfaceFoundation) => MDCMenuSurface;
/** MDC Menu Surface */
export class MDCMenuSurface extends MDCComponent<MDCMenuSurfaceFoundation> {
static override attachTo(root: HTMLElement): MDCMenuSurface {
return new MDCMenuSurface(root);
}
anchorElement!: Element|null; // assigned in initialSyncWithDOM()
private previousFocus?: HTMLElement|SVGElement|null;
private handleKeydown!:
SpecificEventListener<'keydown'>; // assigned in initialSyncWithDOM()
private handleBodyClick!:
SpecificEventListener<'click'>; // assigned in initialSyncWithDOM()
private registerBodyClickListener!:
RegisterFunction; // assigned in initialSyncWithDOM()
private deregisterBodyClickListener!:
RegisterFunction; // assigned in initialSyncWithDOM()
override initialSyncWithDOM() {
const parentEl = this.root.parentElement;
this.anchorElement =
parentEl && parentEl.classList.contains(cssClasses.ANCHOR) ? parentEl :
null;
if (this.root.classList.contains(cssClasses.FIXED)) {
this.setFixedPosition(true);
}
this.handleKeydown = (event) => {
this.foundation.handleKeydown(event);
};
this.handleBodyClick = (event) => {
this.foundation.handleBodyClick(event);
};
// capture so that no race between handleBodyClick and quickOpen when
// menusurface opened on button click which registers this listener
this.registerBodyClickListener = () => {
document.body.addEventListener(
'click', this.handleBodyClick, {capture: true});
};
this.deregisterBodyClickListener = () => {
document.body.removeEventListener(
'click', this.handleBodyClick, {capture: true});
};
this.listen('keydown', this.handleKeydown);
this.listen(strings.OPENED_EVENT, this.registerBodyClickListener);
this.listen(strings.CLOSED_EVENT, this.deregisterBodyClickListener);
}
override destroy() {
this.unlisten('keydown', this.handleKeydown);
this.unlisten(strings.OPENED_EVENT, this.registerBodyClickListener);
this.unlisten(strings.CLOSED_EVENT, this.deregisterBodyClickListener);
super.destroy();
}
isOpen(): boolean {
return this.foundation.isOpen();
}
open() {
this.foundation.open();
}
close(skipRestoreFocus = false) {
this.foundation.close(skipRestoreFocus);
}
set quickOpen(quickOpen: boolean) {
this.foundation.setQuickOpen(quickOpen);
}
/**
* Sets the foundation to use page offsets for a positioning when the menu is
* hoisted to the body.
*/
setIsHoisted(isHoisted: boolean) {
this.foundation.setIsHoisted(isHoisted);
}
/** Sets the element that the menu-surface is anchored to. */
setMenuSurfaceAnchorElement(element: Element) {
this.anchorElement = element;
}
/** Sets the menu-surface to position: fixed. */
setFixedPosition(isFixed: boolean) {
if (isFixed) {
this.root.classList.add(cssClasses.FIXED);
} else {
this.root.classList.remove(cssClasses.FIXED);
}
this.foundation.setFixedPosition(isFixed);
}
/**
* Sets the absolute x/y position to position based on. Requires the menu to
* be hoisted.
*/
setAbsolutePosition(x: number, y: number) {
this.foundation.setAbsolutePosition(x, y);
this.setIsHoisted(true);
}
/**
* @param corner Default anchor corner alignment of top-left surface corner.
*/
setAnchorCorner(corner: Corner) {
this.foundation.setAnchorCorner(corner);
}
setAnchorMargin(margin: Partial<MDCMenuDistance>) {
this.foundation.setAnchorMargin(margin);
}
override getDefaultFoundation() {
// DO NOT INLINE this variable. For backward compatibility, foundations take
// a Partial<MDCFooAdapter>. To ensure we don't accidentally omit any
// methods, we need a separate, strongly typed adapter variable.
// tslint:disable:object-literal-sort-keys Methods should be in the same order as the adapter interface.
const adapter: MDCMenuSurfaceAdapter = {
addClass: (className) => {
this.root.classList.add(className);
},
removeClass: (className) => {
this.root.classList.remove(className);
},
hasClass: (className) => this.root.classList.contains(className),
hasAnchor: () => !!this.anchorElement,
notifyClose: () => {
this.emit(MDCMenuSurfaceFoundation.strings.CLOSED_EVENT, {});
},
notifyClosing: () => {
this.emit(MDCMenuSurfaceFoundation.strings.CLOSING_EVENT, {});
},
notifyOpen: () => {
this.emit(MDCMenuSurfaceFoundation.strings.OPENED_EVENT, {});
},
notifyOpening: () => {
this.emit(MDCMenuSurfaceFoundation.strings.OPENING_EVENT, {});
},
isElementInContainer: (el) => this.root.contains(el),
isRtl: () =>
getComputedStyle(this.root).getPropertyValue('direction') === 'rtl',
setTransformOrigin: (origin) => {
const propertyName =
`${getCorrectPropertyName(window, 'transform')}-origin`;
this.root.style.setProperty(propertyName, origin);
},
isFocused: () => document.activeElement === this.root,
saveFocus: () => {
this.previousFocus =
document.activeElement as HTMLElement | SVGElement | null;
},
restoreFocus: () => {
if (this.root.contains(document.activeElement)) {
if (this.previousFocus && this.previousFocus.focus) {
this.previousFocus.focus();
}
}
},
getInnerDimensions: () => {
return {width: this.root.offsetWidth, height: this.root.offsetHeight};
},
getAnchorDimensions: () => this.anchorElement ?
this.anchorElement.getBoundingClientRect() :
null,
getViewportDimensions: () => {
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) => {
const rootHTML = this.root;
rootHTML.style.left = 'left' in position ? `${position.left}px` : '';
rootHTML.style.right = 'right' in position ? `${position.right}px` : '';
rootHTML.style.top = 'top' in position ? `${position.top}px` : '';
rootHTML.style.bottom =
'bottom' in position ? `${position.bottom}px` : '';
},
setMaxHeight: (height) => {
this.root.style.maxHeight = height;
},
registerWindowEventHandler: (eventType, handler) => {
window.addEventListener(eventType, handler);
},
deregisterWindowEventHandler: (eventType, handler) => {
window.removeEventListener(eventType, handler);
},
};
// tslint:enable:object-literal-sort-keys
return new MDCMenuSurfaceFoundation(adapter);
}
}