-
Notifications
You must be signed in to change notification settings - Fork 42
/
overlay.element.ts
233 lines (198 loc) · 6.19 KB
/
overlay.element.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
/*
* Copyright (c) 2016-2021 VMware, Inc. All Rights Reserved.
* This software is released under MIT license.
* The full license information can be found in LICENSE in the root directory of this project.
*/
import {
AnimationModalEnterName,
animate,
baseStyles,
CdsBaseFocusTrap,
createId,
event,
EventEmitter,
FocusTrapTrackerService,
HTMLAttributeTuple,
i18n,
I18nService,
setAttributes,
state,
property,
reverseAnimation,
} from '@cds/core/internal';
import { html } from 'lit';
import { query } from 'lit/decorators/query.js';
import styles from './overlay.element.scss';
import sharedStyles from './shared.element.scss';
export function isNestedOverlay(
myId: string,
overlayPrefix: string,
trapIds: string[],
previousValue?: boolean
): boolean {
const overlayIds = trapIds.filter(id => id.indexOf(overlayPrefix) > -1);
if (previousValue === true && trapIds.indexOf(myId) < 0) {
// handling situation where focusTrapIds remove our overlay from the list and we still need to consider
// this overlay as nested. this happens when an overlay is being closed/hidden
return true;
}
return overlayIds.indexOf(myId) > 0; // id is present and not the first one...
}
export function overlayIsActive(overlayId: string, focusTrapService = FocusTrapTrackerService) {
return focusTrapService.getCurrent()?.focusTrapId === overlayId;
}
export type CloseChangeSources = 'backdrop-click' | 'escape-keypress' | 'close-button-click' | 'custom';
/** @private */
export class CdsInternalStaticOverlay extends CdsBaseFocusTrap {
@property({ type: Boolean })
closable = false;
@property({ type: Boolean, attribute: 'cds-ignore-focus-trap' })
ignoreFocusTrap = false;
protected get customBumpers(): [HTMLElement, HTMLElement] | [] {
return [];
}
@i18n() i18n = I18nService.keys.overlay;
// renderRoot needs delegatesFocus so that focus can cross the shadowDOM
// inside an element with aria-modal set to true
/** @private */
static get shadowRootOptions(): any {
// any is used until TS 4.4.x adopted through other @cds/* libraries. Can be removed in 6.0
return { ...super.shadowRootOptions, delegatesFocus: true };
}
private overlayIdPrefix = '_overlay-';
@event() protected closeChange: EventEmitter<CloseChangeSources>;
@state({ type: Boolean })
protected isLayered = false;
@state({ type: String })
protected focusTrapId: string;
/* @private */
getFocusTrapId(): string {
// we need this for some unit tests
return this.focusTrapId;
}
/* c8 ignore next */
@query('.overlay-backdrop') protected backdrop: HTMLElement;
connectedCallback() {
super.connectedCallback();
this.ariaModal = 'true';
}
firstUpdated(props: Map<string, any>) {
super.firstUpdated(props);
this.backdrop.addEventListener('click', this.fireEventOnBackdropClick);
}
updated(props: Map<string, any>) {
super.updated(props);
const oldLayered = this.isLayered;
const newLayered = isNestedOverlay(
this.focusTrapId,
this.overlayIdPrefix,
FocusTrapTrackerService.getTrapElements().map(e => e.focusTrapId),
oldLayered
);
if (oldLayered !== newLayered) {
this.isLayered = newLayered;
this.requestUpdate('isLayered', oldLayered);
}
this.setAriaRole();
}
disconnectedCallback() {
super.disconnectedCallback();
this.backdrop.removeEventListener('click', this.fireEventOnBackdropClick);
}
// we do this so that screen-readers can make their way through nested/layered overlays.
// it sets a virtual cursor trap on the top-most overlay
private setAriaRole() {
const myRole = FocusTrapTrackerService.getCurrent()?.focusTrapId === this.focusTrapId ? 'dialog' : 'region';
setAttributes(this, ['role', myRole]);
}
protected get closeButtonAttrs(): HTMLAttributeTuple[] {
return [
['cds-layout', 'align:top'],
['aria-label', this.i18n.closeButtonAriaLabel],
['icon-size', '24'],
];
}
constructor() {
super();
// override focus-trap base id so we know this is an overlay
this.focusTrapId = createId(this.overlayIdPrefix);
}
closeOverlay(trigger: CloseChangeSources = 'custom') {
this.closableController.close(trigger);
}
protected get closeButtonTemplate() {
return html`<cds-internal-close-button
cds-layout="align:top"
aria-label=${this.i18n.closeButtonAriaLabel}
icon-size="24"
@click=${() => this.closeOverlay('close-button-click')}
></cds-internal-close-button>`;
}
protected get backdropTemplate() {
return html`<div
class="${this.isLayered ? 'overlay-backdrop layered' : 'overlay-backdrop'}"
aria-hidden="true"
></div>`;
}
protected render() {
return html`
${this.backdropTemplate}
<div class="private-host" tabindex="-1">
<slot></slot>
</div>
`;
}
protected fireEventOnBackdropClick = () => {
if (overlayIsActive(this.focusTrapId)) {
this.closeOverlay('backdrop-click');
}
};
static get styles() {
return [baseStyles, styles, sharedStyles];
}
}
/**
*
* ```typescript
* import '@cds/core/internal-components/overlay/register.js';
* ```
*
* ```html
* <cds-internal-overlay>
* <section cds-layout="vertical align:horizontal-stretch">
* <div cds-layout="vertical pad:md gap:md">
* <h2 cds-text="display">A Title</h2>
* <div>
* <p cds-text="body">
* Content inside a generic overlay.
* </p>
* </div>
* </div>
* </section>
* </cds-internal-overlay>
* ```
*
* @beta
* @element cds-internal-overlay
* @slot - Content slot for the content inside the overlay's panel
* @event closeChange - Notify user when close event has occurred
* @cssprop --backdrop-background
* @cssprop --layered-backdrop-background
* @cssprop --animation-duration
* @cssprop --animation-easing
*
* KNOWN ISSUE: Safari jumps through the exit animation but only when the ESC key is pressed.
*
*/
@animate({
hidden: {
true: reverseAnimation(AnimationModalEnterName),
false: AnimationModalEnterName,
},
})
export class CdsInternalOverlay extends CdsInternalStaticOverlay {
@property({ type: String })
cdsMotion = 'on';
@event()
cdsMotionChange: EventEmitter<string>;
}