-
Notifications
You must be signed in to change notification settings - Fork 76
/
alert.tsx
502 lines (410 loc) · 14.6 KB
/
alert.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
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
import { PropertyValues } from "lit";
import {
LitElement,
property,
createEvent,
h,
method,
state,
JsxNode,
stringOrBoolean,
} from "@arcgis/lumina";
import {
focusFirstTabbable,
setRequestedIcon,
slotChangeHasAssignedElement,
toAriaBoolean,
} from "../../utils/dom";
import { MenuPlacement } from "../../utils/floating-ui";
import { getIconScale } from "../../utils/component";
import {
componentFocusable,
LoadableComponent,
setComponentLoaded,
setUpLoadableComponent,
} from "../../utils/loadable";
import { NumberingSystem, NumberStringFormat } from "../../utils/locale";
import { onToggleOpenCloseComponent, OpenCloseComponent } from "../../utils/openCloseComponent";
import { Kind, Scale } from "../interfaces";
import { KindIcons } from "../resources";
import { IconNameOrString } from "../icon/interfaces";
import { useT9n } from "../../controllers/useT9n";
import T9nStrings from "./assets/t9n/alert.t9n.en.json";
import { AlertDuration, AlertQueue } from "./interfaces";
import { CSS, DURATIONS, SLOTS } from "./resources";
import AlertManager from "./AlertManager";
import { styles } from "./alert.scss";
declare global {
interface DeclareElements {
"calcite-alert": Alert;
}
}
const manager = new AlertManager();
/**
* Alerts are meant to provide a way to communicate urgent or important information to users, frequently as a result of an action they took in your app. Alerts are positioned
* at the bottom of the page. Multiple opened alerts will be added to a queue, allowing users to dismiss them in the order they are provided.
*
* @slot title - A slot for adding a title to the component.
* @slot message - A slot for adding main text to the component.
* @slot link - A slot for adding a `calcite-action` to take from the component such as: "undo", "try again", "link to page", etc.
* @slot actions-end - A slot for adding `calcite-action`s to the end of the component. It is recommended to use two or fewer actions.
*/
export class Alert extends LitElement implements OpenCloseComponent, LoadableComponent {
// #region Static Members
static override styles = styles;
// #endregion
// #region Private Properties
private autoCloseTimeoutId: number = null;
private initialOpenTime: number;
private isHovered: boolean;
private lastMouseOverBegin: number;
openTransitionProp = "opacity";
private totalHoverTime = 0;
private totalOpenTime = 0;
transitionEl: HTMLDivElement;
// #endregion
// #region State Properties
@state() hasEndActions = false;
@state() isFocused = false;
@state() numberStringFormatter = new NumberStringFormat();
// #endregion
// #region Public Properties
/**
* This internal property, managed by the AlertManager, is used
* to inform the component if it is the active open Alert.
*
* @private
*/
@property() active = false;
/** When `true`, the component closes automatically. Recommended for passive, non-blocking alerts. */
@property({ reflect: true }) autoClose = false;
/** Specifies the duration before the component automatically closes - only use with `autoClose`. */
@property({ reflect: true }) autoCloseDuration: AlertDuration = "medium";
/**
* This internal property, managed by a containing calcite-shell, is used
* to inform the component if special configuration or styles are needed
*
* @private
*/
@property() embedded = false;
/**
* When `true`, shows a default recommended icon. Alternatively,
* pass a Calcite UI Icon name to display a specific icon.
*/
@property({ reflect: true, converter: stringOrBoolean }) icon: IconNameOrString | boolean;
/** When `true`, the icon will be flipped when the element direction is right-to-left (`"rtl"`). */
@property({ reflect: true }) iconFlipRtl = false;
/** Specifies the kind of the component, which will apply to top border and icon. */
@property({ reflect: true }) kind: Extract<
"brand" | "danger" | "info" | "success" | "warning",
Kind
> = "brand";
/**
* Specifies an accessible name for the component.
*
* @required
*/
@property() label: string;
/** Use this property to override individual strings used by the component. */
@property() messageOverrides?: typeof this.messages._overrides;
/**
* Made into a prop for testing purposes only
*
* @private
*/
messages = useT9n<typeof T9nStrings>();
/** Specifies the Unicode numeral system used by the component for localization. */
@property({ reflect: true }) numberingSystem: NumberingSystem;
/** When `true`, displays and positions the component. */
@property({ reflect: true }) open = false;
/**
* This internal property, managed by the AlertManager, is used
* to inform the component of how many alerts are currently open.
*
* @private
*/
@property() openAlertCount = 0;
/** Specifies the placement of the component. */
@property({ reflect: true }) placement: MenuPlacement = "bottom";
/** Specifies the ordering priority of the component when opened. */
@property({ reflect: true }) queue: AlertQueue = "last";
/** Specifies the size of the component. */
@property({ reflect: true }) scale: Scale = "m";
// #endregion
// #region Public Methods
/**
* Sets focus on the component's "close" button, the first focusable item.
*
* `@returns` {Promise<void>}
*/
@method()
async setFocus(): Promise<void> {
await componentFocusable(this);
focusFirstTabbable(this.el);
}
// #endregion
// #region Events
/** Fires when the component is requested to be closed and before the closing transition begins. */
calciteAlertBeforeClose = createEvent({ cancelable: false });
/** Fires when the component is added to the DOM but not rendered, and before the opening transition begins. */
calciteAlertBeforeOpen = createEvent({ cancelable: false });
/** Fires when the component is closed and animation is complete. */
calciteAlertClose = createEvent({ cancelable: false });
/** Fires when the component is open and animation is complete. */
calciteAlertOpen = createEvent({ cancelable: false });
// #endregion
// #region Lifecycle
override connectedCallback(): void {
const open = this.open;
if (open) {
manager.registerElement(this.el);
}
this.numberStringFormatter.numberFormatOptions = {
locale: this.messages._lang,
numberingSystem: this.numberingSystem,
signDisplay: "always",
};
}
async load(): Promise<void> {
setUpLoadableComponent(this);
if (this.open) {
onToggleOpenCloseComponent(this);
}
}
override willUpdate(changes: PropertyValues<this>): void {
/* TODO: [MIGRATION] First time Lit calls willUpdate(), changes will include not just properties provided by the user, but also any default values your component set.
To account for this semantics change, the checks for (this.hasUpdated || value != defaultValue) was added in this method
Please refactor your code to reduce the need for this check.
Docs: https://qawebgis.esri.com/arcgis-components/?path=/docs/lumina-transition-from-stencil--docs#watching-for-property-changes */
if (changes.has("active") && (this.hasUpdated || this.active !== false)) {
this.handleActiveChange();
}
if (changes.has("open") && (this.hasUpdated || this.open !== false)) {
this.openHandler();
}
if (
changes.has("autoCloseDuration") &&
(this.hasUpdated || this.autoCloseDuration !== "medium")
) {
this.updateDuration();
}
if (changes.has("queue") && (this.hasUpdated || this.queue !== "last")) {
this.handleQueueChange();
}
if (changes.has("numberingSystem")) {
this.numberingSystemChange();
}
if (changes.has("messages")) {
this.effectiveLocaleChange();
}
}
loaded(): void {
setComponentLoaded(this);
}
override disconnectedCallback(): void {
manager.unregisterElement(this.el);
this.clearAutoCloseTimeout();
this.embedded = false;
}
// #endregion
// #region Private Methods
private handleActiveChange(): void {
this.clearAutoCloseTimeout();
if (this.active && this.autoClose && !this.autoCloseTimeoutId) {
this.initialOpenTime = Date.now();
this.autoCloseTimeoutId = window.setTimeout(
() => this.closeAlert(),
DURATIONS[this.autoCloseDuration],
);
}
}
private openHandler(): void {
onToggleOpenCloseComponent(this);
if (this.open) {
manager.registerElement(this.el);
} else {
manager.unregisterElement(this.el);
}
}
private updateDuration(): void {
if (this.autoClose && this.autoCloseTimeoutId) {
this.clearAutoCloseTimeout();
this.autoCloseTimeoutId = window.setTimeout(
() => this.closeAlert(),
DURATIONS[this.autoCloseDuration],
);
}
}
private handleQueueChange(): void {
if (this.open) {
manager.unregisterElement(this.el);
manager.registerElement(this.el);
}
}
private handleKeyBoardFocus(): void {
this.isFocused = true;
this.handleFocus();
}
private handleKeyBoardBlur(): void {
this.isFocused = false;
if (!this.isHovered) {
this.handleBlur();
}
}
private effectiveLocaleChange(): void {
this.numberStringFormatter.numberFormatOptions = {
locale: this.messages._lang,
numberingSystem: this.numberingSystem,
signDisplay: "always",
};
}
private numberingSystemChange(): void {
this.numberStringFormatter.numberFormatOptions = {
locale: this.messages._lang,
numberingSystem: this.numberingSystem,
signDisplay: "always",
};
}
private clearAutoCloseTimeout(): void {
window.clearTimeout(this.autoCloseTimeoutId);
this.autoCloseTimeoutId = null;
}
private setTransitionEl(el: HTMLDivElement): void {
this.transitionEl = el;
}
/** close and emit calciteInternalAlertSync event with the updated queue payload */
private closeAlert(): void {
this.open = false;
this.clearAutoCloseTimeout();
}
onBeforeOpen(): void {
this.calciteAlertBeforeOpen.emit();
}
onOpen(): void {
this.calciteAlertOpen.emit();
}
onBeforeClose(): void {
this.calciteAlertBeforeClose.emit();
}
onClose(): void {
this.calciteAlertClose.emit();
}
private actionsEndSlotChangeHandler(event: Event): void {
this.hasEndActions = slotChangeHasAssignedElement(event);
}
private handleMouseOver(): void {
this.isHovered = true;
this.handleFocus();
}
private handleMouseLeave(): void {
this.isHovered = false;
if (!this.isFocused) {
this.handleBlur();
}
}
private handleFocus(): void {
this.clearAutoCloseTimeout();
this.totalOpenTime = Date.now() - this.initialOpenTime;
this.lastMouseOverBegin = Date.now();
}
private handleBlur(): void {
const hoverDuration = Date.now() - this.lastMouseOverBegin;
const timeRemaining =
DURATIONS[this.autoCloseDuration] - this.totalOpenTime + this.totalHoverTime;
this.totalHoverTime = this.totalHoverTime ? hoverDuration + this.totalHoverTime : hoverDuration;
this.autoCloseTimeoutId = window.setTimeout(() => this.closeAlert(), timeRemaining);
}
// #endregion
// #region Rendering
override render(): JsxNode {
const { open, autoClose, label, placement, active, openAlertCount } = this;
const role = autoClose ? "alert" : "alertdialog";
const hidden = !open;
const effectiveIcon = setRequestedIcon(KindIcons, this.icon, this.kind);
const hasQueuedAlerts = openAlertCount > 1;
/* TODO: [MIGRATION] This used <Host> before. In Stencil, <Host> props overwrite user-provided props. If you don't wish to overwrite user-values, replace "=" here with "??=" */
this.el.ariaHidden = toAriaBoolean(hidden);
/* TODO: [MIGRATION] This used <Host> before. In Stencil, <Host> props overwrite user-provided props. If you don't wish to overwrite user-values, replace "=" here with "??=" */
this.el.ariaLabel = label;
this.el.toggleAttribute("calcite-hydrated-hidden", hidden);
/* TODO: [MIGRATION] This used <Host> before. In Stencil, <Host> props overwrite user-provided props. If you don't wish to overwrite user-values, replace "=" here with "??=" */
this.el.role = role;
return (
<div
class={{
[CSS.container]: true,
[CSS.containerActive]: active,
[`${CSS.container}--${placement}`]: true,
[CSS.containerEmbedded]: this.embedded,
[CSS.focused]: this.isFocused,
}}
onPointerEnter={this.autoClose && this.autoCloseTimeoutId ? this.handleMouseOver : null}
onPointerLeave={this.autoClose ? this.handleMouseLeave : null}
ref={this.setTransitionEl}
>
{effectiveIcon && this.renderIcon(effectiveIcon)}
<div
class={CSS.textContainer}
onFocusIn={this.autoClose && this.autoCloseTimeoutId ? this.handleKeyBoardFocus : null}
onFocusOut={this.autoClose ? this.handleKeyBoardBlur : null}
>
<slot name={SLOTS.title} />
<slot name={SLOTS.message} />
<slot name={SLOTS.link} />
</div>
{this.renderActionsEnd()}
{hasQueuedAlerts ? this.renderQueueCount() : null}
{this.renderCloseButton()}
{open && active && autoClose ? <div class={CSS.dismissProgress} /> : null}
</div>
);
}
private renderCloseButton(): JsxNode {
return (
<button
ariaLabel={this.messages.close}
class={CSS.close}
key="close"
onClick={this.closeAlert}
onFocusIn={this.autoClose ? this.handleKeyBoardFocus : null}
onFocusOut={this.autoClose ? this.handleKeyBoardBlur : null}
type="button"
>
<calcite-icon icon="x" scale={getIconScale(this.scale)} />
</button>
);
}
private renderQueueCount(): JsxNode {
const { openAlertCount } = this;
const queueNumber = openAlertCount > 2 ? openAlertCount - 1 : 1;
const queueText = this.numberStringFormatter.numberFormatter.format(queueNumber);
return (
<div
class={{
[CSS.queueCount]: true,
[CSS.queueCountActive]: openAlertCount > 1,
}}
key="queue-count"
>
<calcite-chip label={queueText} scale={this.scale} value={queueText}>
{queueText}
</calcite-chip>
</div>
);
}
private renderActionsEnd(): JsxNode {
return (
<div class={CSS.actionsEnd}>
<slot name={SLOTS.actionsEnd} onSlotChange={this.actionsEndSlotChangeHandler} />
</div>
);
}
private renderIcon(icon: IconNameOrString): JsxNode {
return (
<div class={CSS.icon}>
<calcite-icon flipRtl={this.iconFlipRtl} icon={icon} scale={getIconScale(this.scale)} />
</div>
);
}
// #endregion
}