-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathPopup.js
477 lines (412 loc) · 10.7 KB
/
Popup.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
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
import litRender from "@ui5/webcomponents-base/dist/renderer/LitRenderer.js";
import UI5Element from "@ui5/webcomponents-base/dist/UI5Element.js";
import { getFirstFocusableElement, getLastFocusableElement } from "@ui5/webcomponents-base/dist/util/FocusableElements.js";
import createStyleInHead from "@ui5/webcomponents-base/dist/util/createStyleInHead.js";
import PopupTemplate from "./generated/templates/PopupTemplate.lit.js";
import PopupBlockLayer from "./generated/templates/PopupBlockLayerTemplate.lit.js";
import { getNextZIndex, getFocusedElement, isFocusedElementWithinNode } from "./popup-utils/PopupUtils.js";
import { addOpenedPopup, removeOpenedPopup } from "./popup-utils/OpenedPopupsRegistry.js";
// Styles
import styles from "./generated/themes/Popup.css.js";
import staticAreaStyles from "./generated/themes/PopupStaticAreaStyles.css.js";
/**
* @public
*/
const metadata = {
managedSlots: true,
slots: /** @lends sap.ui.webcomponents.main.Popup.prototype */ {
/**
* Defines the content of the Popup.
* @type {Node[]}
* @slot
* @public
*/
"default": {
type: HTMLElement,
},
},
properties: /** @lends sap.ui.webcomponents.main.Popup.prototype */ {
/**
* Defines the ID of the HTML Element, which will get the initial focus.
*
* @type {string}
* @defaultvalue ""
* @public
*/
initialFocus: {
type: String,
},
/**
* Defines if the focus should be returned to the previously focused element,
* when the popup closes.
* @type {boolean}
* @defaultvalue false
* @public
* @since 1.0.0-rc.8
*/
preventFocusRestore: {
type: Boolean,
},
/**
* Indicates if the elements is open
* @private
* @type {boolean}
* @defaultvalue false
*/
opened: {
type: Boolean,
},
/**
* Defines the aria-label attribute for the popup
*
* @type {String}
* @defaultvalue: ""
* @private
* @since 1.0.0-rc.8
*/
ariaLabel: {
type: String,
defaultValue: undefined,
},
/**
* @private
*/
_disableInitialFocus: {
type: Boolean,
},
_blockLayerHidden: {
type: Boolean,
},
},
events: /** @lends sap.ui.webcomponents.main.Popup.prototype */ {
/**
* Fired before the component is opened. This event can be cancelled, which will prevent the popup from opening. This event does not bubble.
*
* @public
* @event sap.ui.webcomponents.main.Popup#before-open
*/
"before-open": {},
/**
* Fired after the component is opened. This event does not bubble.
*
* @public
* @event sap.ui.webcomponents.main.Popup#after-open
*/
"after-open": {},
/**
* Fired before the component is closed. This event can be cancelled, which will prevent the popup from closing. This event does not bubble.
*
* @public
* @event sap.ui.webcomponents.main.Popup#before-close
* @param {Boolean} escPressed Indicates that <code>ESC</code> key has triggered the event.
*/
"before-close": {
escPressed: { type: Boolean },
},
/**
* Fired after the component is closed. This event does not bubble.
*
* @public
* @event sap.ui.webcomponents.main.Popup#after-close
*/
"after-close": {},
},
};
let customBlockingStyleInserted = false;
const createBlockingStyle = () => {
if (customBlockingStyleInserted) {
return;
}
createStyleInHead(`
.ui5-popup-scroll-blocker {
width: 100%;
height: 100%;
position: fixed;
overflow: hidden;
}
`, { "data-ui5-popup-scroll-blocker": "" });
customBlockingStyleInserted = true;
};
createBlockingStyle();
/**
* @class
* <h3 class="comment-api-title">Overview</h3>
* Base class for all popup Web Components.
*
* If you need to create your own popup-like custom UI5 Web Components, it is highly recommended that you extend
* at least Popup in order to have consistency with other popups in terms of modal behavior and z-index management.
*
* 1. The Popup class handles modality:
* - The "isModal" getter can be overridden by derivatives to provide their own conditions when they are modal or not
* - Derivatives may call the "blockBodyScrolling" and "unblockBodyScrolling" static methods to temporarily remove scrollbars on the body
* - Derivatives may call the "open" and "close" methods which handle focus, manage the popup registry and for modal popups, manage the blocking layer
*
* 2. Provides blocking layer (relevant for modal popups only):
* - It is in the static area
* - Controlled by the "open" and "close" methods
*
* 3. The Popup class "traps" focus:
* - Derivatives may call the "applyInitialFocus" method (usually when opening, to transfer focus inside the popup)
*
* 4. The Popup class automatically assigns "z-index"
* - Each time a popup is opened, it gets a higher than the previously opened popup z-index
*
* 5. The template of this component exposes two inline partials you can override in derivatives:
* - beforeContent (upper part of the box, useful for header/title/close button)
* - afterContent (lower part, useful for footer/action buttons)
*
* @constructor
* @author SAP SE
* @alias sap.ui.webcomponents.main.Popup
* @extends sap.ui.webcomponents.base.UI5Element
* @public
*/
class Popup extends UI5Element {
static get metadata() {
return metadata;
}
static get render() {
return litRender;
}
static get styles() {
return styles;
}
static get template() {
return PopupTemplate;
}
static get staticAreaTemplate() {
return PopupBlockLayer;
}
static get staticAreaStyles() {
return staticAreaStyles;
}
get _displayProp() {
return "block";
}
/**
* Prevents the user from interacting with the content under the block layer
*/
_preventBlockLayerFocus(event) {
event.preventDefault();
}
/**
* Temporarily removes scrollbars from the body
* @protected
*/
static blockBodyScrolling() {
document.body.style.top = `-${window.pageYOffset}px`;
document.body.classList.add("ui5-popup-scroll-blocker");
}
/**
* Restores scrollbars on the body, if needed
* @protected
*/
static unblockBodyScrolling() {
document.body.classList.remove("ui5-popup-scroll-blocker");
window.scrollTo(0, -parseFloat(document.body.style.top));
document.body.style.top = "";
}
_scroll(e) {
this.fireEvent("scroll", {
scrollTop: e.target.scrollTop,
targetRef: e.target,
});
}
/**
* Focus trapping
* @private
*/
forwardToFirst() {
const firstFocusable = getFirstFocusableElement(this);
if (firstFocusable) {
firstFocusable.focus();
}
}
/**
* Focus trapping
* @private
*/
forwardToLast() {
const lastFocusable = getLastFocusableElement(this);
if (lastFocusable) {
lastFocusable.focus();
}
}
/**
* Use this method to focus the element denoted by "initialFocus", if provided, or the first focusable element otherwise.
* @protected
*/
applyInitialFocus() {
this.applyFocus();
}
/**
* Focuses the element denoted by <code>initialFocus</code>, if provided,
* or the first focusable element otherwise.
* @public
*/
applyFocus() {
const element = this.getRootNode().getElementById(this.initialFocus)
|| document.getElementById(this.initialFocus)
|| getFirstFocusableElement(this);
if (element) {
element.focus();
}
}
/**
* Override this method to provide custom logic for the popup's open/closed state. Maps to the "opened" property by default.
* @public
* @returns {boolean}
*/
isOpen() {
return this.opened;
}
isFocusWithin() {
return isFocusedElementWithinNode(this.shadowRoot.querySelector(".ui5-popup-root"));
}
/**
* Shows the block layer (for modal popups only) and sets the correct z-index for the purpose of popup stacking
* @param {boolean} preventInitialFocus prevents applying the focus inside the popup
* @public
*/
open(preventInitialFocus) {
const prevented = !this.fireEvent("before-open", {}, true, false);
if (prevented) {
return;
}
if (this.isModal) {
// create static area item ref for block layer
this.getStaticAreaItemDomRef();
this._blockLayerHidden = false;
Popup.blockBodyScrolling();
}
this._zIndex = getNextZIndex();
this.style.zIndex = this._zIndex;
this._focusedElementBeforeOpen = getFocusedElement();
this.show();
if (!this._disableInitialFocus && !preventInitialFocus) {
this.applyInitialFocus();
}
this._addOpenedPopup();
this.opened = true;
this.fireEvent("after-open", {}, false, false);
}
/**
* Adds the popup to the "opened popups registry"
* @protected
*/
_addOpenedPopup() {
addOpenedPopup(this);
}
/**
* Hides the block layer (for modal popups only)
* @public
*/
close(escPressed = false, preventRegistryUpdate = false, preventFocusRestore = false) {
if (!this.opened) {
return;
}
const prevented = !this.fireEvent("before-close", { escPressed }, true, false);
if (prevented) {
return;
}
if (this.isModal) {
this._blockLayerHidden = true;
Popup.unblockBodyScrolling();
}
this.hide();
this.opened = false;
if (!preventRegistryUpdate) {
this._removeOpenedPopup();
}
if (!this.preventFocusRestore && !preventFocusRestore) {
this.resetFocus();
}
this.fireEvent("after-close", {}, false, false);
}
/**
* Removes the popup from the "opened popups registry"
* @protected
*/
_removeOpenedPopup() {
removeOpenedPopup(this);
}
/**
* Returns the focus to the previously focused element
* @protected
*/
resetFocus() {
if (!this._focusedElementBeforeOpen) {
return;
}
this._focusedElementBeforeOpen.focus();
this._focusedElementBeforeOpen = null;
}
/**
* Sets "block" display to the popup. The property can be overriden by derivatives of Popup.
* @protected
*/
show() {
this.style.display = this._displayProp;
}
/**
* Sets "none" display to the popup
* @protected
*/
hide() {
this.style.display = "none";
}
onExitDOM() {
if (this.isOpen()) {
Popup.unblockBodyScrolling();
this._removeOpenedPopup();
}
}
/**
* Implement this getter with relevant logic regarding the modality of the popup (e.g. based on a public property)
*
* @protected
* @abstract
* @returns {boolean}
*/
get isModal() {} // eslint-disable-line
/**
* Return the ID of an element in the shadow DOM that is going to label this popup
*
* @protected
* @abstract
* @returns {String}
*/
get _ariaLabelledBy() {} // eslint-disable-line
/**
* Return the value for aria-modal for this popup
*
* @protected
* @abstract
* @returns {String}
*/
get _ariaModal() {} // eslint-disable-line
/**
* Ensures ariaLabel is never null or empty string
* @returns {String|undefined}
* @protected
*/
get _ariaLabel() {
return this.ariaLabel || undefined;
}
get styles() {
return {
root: {},
content: {},
blockLayer: {
"zIndex": (this._zIndex - 1),
},
};
}
get classes() {
return {
root: {},
content: {},
};
}
}
export default Popup;