-
Notifications
You must be signed in to change notification settings - Fork 192
/
Dialog.ts
303 lines (269 loc) · 9.27 KB
/
Dialog.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
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
/*
Copyright 2020 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/
import {
CSSResultArray,
html,
nothing,
PropertyValues,
SpectrumElement,
TemplateResult,
} from '@spectrum-web-components/base';
import {
property,
query,
} from '@spectrum-web-components/base/src/decorators.js';
import { classMap } from '@spectrum-web-components/base/src/directives.js';
import { conditionAttributeWithId } from '@spectrum-web-components/base/src/condition-attribute-with-id.js';
import '@spectrum-web-components/divider/sp-divider.js';
import '@spectrum-web-components/button/sp-close-button.js';
import '@spectrum-web-components/button-group/sp-button-group.js';
import '@spectrum-web-components/icons-workflow/icons/sp-icon-alert.js';
import {
FocusVisiblePolyfillMixin,
ObserveSlotPresence,
} from '@spectrum-web-components/shared';
import styles from './dialog.css.js';
import type { CloseButton } from '@spectrum-web-components/button';
let appliedIds = 0;
function gatherAppliedIdsFromSlottedChildren(
slot: HTMLSlotElement,
idBase: string
): string[] {
const assignedElements = slot.assignedElements();
const ids: string[] = [];
assignedElements.forEach((el) => {
if (el.id) {
ids.push(el.id);
} else {
const id = idBase + `-${appliedIds++}`;
el.id = id;
ids.push(id);
}
});
return ids;
}
/**
* @element sp-dialog
*
* @slot hero - Accepts a hero image to display at the top of the dialog
* @slot heading - Acts as the heading of the dialog. This should be an actual heading tag `<h1-6 />`
* @slot - Content not addressed to a specific slot will be interpreted as the main content of the dialog
* @slot footer - Content addressed to the `footer` will be placed below the main content and to the side of any `[slot='button']` content
* @slot button - Button elements addressed to this slot may be placed below the content when not delivered in a fullscreen mode
* @fires close - Announces that the dialog has been closed.
*/
export class Dialog extends FocusVisiblePolyfillMixin(
ObserveSlotPresence(SpectrumElement, [
'[slot="hero"]',
'[slot="footer"]',
'[slot="button"]',
])
) {
public static override get styles(): CSSResultArray {
return [styles];
}
@query('.close-button')
closeButton?: CloseButton;
@query('.content')
private contentElement!: HTMLDivElement;
@property({ type: Boolean, reflect: true })
public error = false;
@property({ type: Boolean, reflect: true })
public dismissable = false;
protected get hasFooter(): boolean {
return this.getSlotContentPresence('[slot="footer"]');
}
protected get hasButtons(): boolean {
return this.getSlotContentPresence('[slot="button"]');
}
/* c8 ignore next 3 */
protected get hasHero(): boolean {
return this.getSlotContentPresence('[slot="hero"]');
}
@property({ type: Boolean, reflect: true, attribute: 'no-divider' })
public noDivider = false;
@property({ type: String, reflect: true })
public mode?: 'fullscreen' | 'fullscreenTakeover';
@property({ type: String, reflect: true })
public size?: 's' | 'm' | 'l';
public close(): void {
this.dispatchEvent(
new Event('close', {
bubbles: true,
composed: true,
cancelable: true,
})
);
}
protected renderHero(): TemplateResult {
return html`
<slot name="hero"></slot>
`;
}
protected renderHeading(): TemplateResult {
return html`
<slot name="heading" @slotchange=${this.onHeadingSlotchange}></slot>
`;
}
protected renderContent(): TemplateResult {
return html`
<div class="content">
<slot @slotchange=${this.onContentSlotChange}></slot>
</div>
`;
}
protected renderFooter(): TemplateResult {
return html`
<div class="footer">
<slot name="footer"></slot>
</div>
`;
}
protected renderButtons(): TemplateResult {
const classes = {
'button-group': true,
'button-group--noFooter': !this.hasFooter,
};
return html`
<sp-button-group class=${classMap(classes)}>
<slot name="button"></slot>
</sp-button-group>
`;
}
protected renderDismiss(): TemplateResult {
return html`
<sp-close-button
class="close-button"
label="Close"
quiet
size="m"
@click=${this.close}
></sp-close-button>
`;
}
protected override render(): TemplateResult {
return html`
<div class="grid">
${this.renderHero()} ${this.renderHeading()}
${this.error
? html`
<sp-icon-alert class="type-icon"></sp-icon-alert>
`
: nothing}
${this.noDivider
? nothing
: html`
<sp-divider size="m" class="divider"></sp-divider>
`}
${this.renderContent()}
${this.hasFooter ? this.renderFooter() : nothing}
${this.hasButtons ? this.renderButtons() : nothing}
${this.dismissable ? this.renderDismiss() : nothing}
</div>
`;
}
public shouldManageTabOrderForScrolling = (): void => {
const { offsetHeight, scrollHeight } = this.contentElement;
if (offsetHeight < scrollHeight) {
this.contentElement.tabIndex = 0;
} else {
this.contentElement.removeAttribute('tabindex');
}
};
protected override shouldUpdate(changes: PropertyValues): boolean {
if (changes.has('mode') && !!this.mode) {
this.dismissable = false;
}
if (changes.has('dismissable') && this.dismissable) {
this.dismissable = !this.mode;
}
return super.shouldUpdate(changes);
}
protected override firstUpdated(changes: PropertyValues): void {
super.firstUpdated(changes);
this.setAttribute('role', 'dialog');
}
static instanceCount = 0;
private labelledbyId = `sp-dialog-label-${Dialog.instanceCount++}`;
private conditionLabelledby?: () => void;
private conditionDescribedby?: () => void;
private onHeadingSlotchange({
target,
}: Event & { target: HTMLSlotElement }): void {
if (this.conditionLabelledby) {
this.conditionLabelledby();
delete this.conditionLabelledby;
}
const ids = gatherAppliedIdsFromSlottedChildren(
target,
this.labelledbyId
);
if (ids.length) {
this.conditionLabelledby = conditionAttributeWithId(
this,
'aria-labelledby',
ids
);
}
}
private describedbyId = `sp-dialog-description-${Dialog.instanceCount++}`;
protected onContentSlotChange({
target,
}: Event & { target: HTMLSlotElement }): void {
requestAnimationFrame(() => {
// Content must be available _AND_ styles must be applied.
this.shouldManageTabOrderForScrolling();
});
if (this.conditionDescribedby) {
this.conditionDescribedby();
delete this.conditionDescribedby;
}
const ids = gatherAppliedIdsFromSlottedChildren(
target,
this.describedbyId
);
if (ids.length && ids.length < 4) {
this.conditionDescribedby = conditionAttributeWithId(
this,
'aria-describedby',
ids
);
} else if (!ids.length) {
const idProvided = !!this.id;
if (!idProvided) this.id = this.describedbyId;
const conditionDescribedby = conditionAttributeWithId(
this,
'aria-describedby',
this.id
);
this.conditionDescribedby = () => {
conditionDescribedby();
if (!idProvided) {
this.removeAttribute('id');
}
};
}
}
public override connectedCallback(): void {
super.connectedCallback();
window.addEventListener(
'resize',
this.shouldManageTabOrderForScrolling
);
}
public override disconnectedCallback(): void {
window.removeEventListener(
'resize',
this.shouldManageTabOrderForScrolling
);
super.disconnectedCallback();
}
}