-
Notifications
You must be signed in to change notification settings - Fork 205
/
Toast.ts
246 lines (220 loc) · 7.47 KB
/
Toast.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
/*
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,
PropertyValues,
SpectrumElement,
TemplateResult,
} from '@spectrum-web-components/base';
import { property } from '@spectrum-web-components/base/src/decorators.js';
import '@spectrum-web-components/button/sp-close-button.js';
import '@spectrum-web-components/icons-workflow/icons/sp-icon-alert.js';
import '@spectrum-web-components/icons-workflow/icons/sp-icon-info.js';
import '@spectrum-web-components/icons-workflow/icons/sp-icon-checkmark-circle.js';
import { FocusVisiblePolyfillMixin } from '@spectrum-web-components/shared/src/focus-visible.js';
import toastStyles from './toast.css.js';
export const toastVariants: ToastVariants[] = [
'negative',
'positive',
'info',
'error',
'warning',
];
export type ToastVariants =
| 'negative'
| 'positive'
| 'info'
| 'error'
| 'warning'
| '';
/**
* @element sp-toast
*
* @slot - The toast content
* @slot action - button element surfacing an action in the Toast
*
* @fires close - Announces that the Toast has been closed by the user or by its timeout.
*/
export class Toast extends FocusVisiblePolyfillMixin(SpectrumElement) {
public static override get styles(): CSSResultArray {
return [toastStyles];
}
@property({ type: Boolean, reflect: true })
public open = false;
/**
* When a timeout is provided it represents the number of milliseconds from when
* the Toast was placed on the page before it will automatically dismiss itself.
* Accessibility concerns require that a Toast is available for at least 6000ms
* before being dismissed, so any timeout of less than 6000ms will be raised to
* that baseline. It is suggested that messages longer than 120 words should
* receive another 1000ms in their timeout for each additional 120 words in the
* message. E.G. 240 words = 7000ms, 360 words = 8000ms, etc.
*
* @param {Number} timeout
*/
@property({ type: Number })
public set timeout(timeout: number | null) {
const hasTimeout = typeof timeout !== null && (timeout as number) > 0;
const newTimeout = hasTimeout
? Math.max(6000, timeout as number)
: null;
const oldValue = this.timeout;
if (newTimeout && this.countdownStart) {
this.countdownStart = performance.now();
}
this._timeout = newTimeout;
this.requestUpdate('timeout', oldValue);
}
public get timeout(): number | null {
return this._timeout;
}
public _timeout: number | null = null;
/**
* The variant applies specific styling when set to `negative`, `positive`, `info`, `error`, or `warning`.
* `variant` attribute is removed when not matching one of the above.
*
* @param {String} variant
*/
@property({ type: String })
public set variant(variant: ToastVariants) {
if (variant === this.variant) {
return;
}
const oldValue = this.variant;
if (toastVariants.includes(variant)) {
this.setAttribute('variant', variant);
this._variant = variant;
} else {
this.removeAttribute('variant');
this._variant = '';
}
this.requestUpdate('variant', oldValue);
}
public get variant(): ToastVariants {
return this._variant;
}
private _variant: ToastVariants = '';
private renderIcon(variant: string): TemplateResult {
switch (variant) {
case 'info':
return html`
<sp-icon-info
label="Information"
class="type"
></sp-icon-info>
`;
case 'negative':
case 'error': // deprecated
case 'warning': // deprecated
return html`
<sp-icon-alert label="Error" class="type"></sp-icon-alert>
`;
case 'positive':
case 'success': // deprecated
return html`
<sp-icon-checkmark-circle
label="Success"
class="type"
></sp-icon-checkmark-circle>
`;
default:
return html``;
}
}
private countdownStart = 0;
private nextCount = -1;
private doCountdown = (time: number): void => {
if (!this.countdownStart) {
this.countdownStart = performance.now();
}
if (time - this.countdownStart > (this._timeout as number)) {
this.shouldClose();
this.countdownStart = 0;
} else {
this.countdown();
}
};
private countdown = (): void => {
cancelAnimationFrame(this.nextCount);
this.nextCount = requestAnimationFrame(this.doCountdown);
};
private holdCountdown = (): void => {
this.stopCountdown();
this.addEventListener('focusout', this.resumeCountdown);
};
private resumeCountdown = (): void => {
this.removeEventListener('focusout', this.holdCountdown);
this.countdown();
};
private startCountdown(): void {
this.countdown();
this.addEventListener('focusin', this.holdCountdown);
}
private stopCountdown(): void {
cancelAnimationFrame(this.nextCount);
this.countdownStart = 0;
}
private shouldClose(): void {
const applyDefault = this.dispatchEvent(
new CustomEvent('close', {
composed: true,
bubbles: true,
cancelable: true,
})
);
if (applyDefault) {
this.close();
}
}
public close(): void {
this.open = false;
}
protected override render(): TemplateResult {
return html`
${this.renderIcon(this.variant)}
<div class="body" role="alert">
<div class="content">
<slot></slot>
</div>
<slot name="action"></slot>
</div>
<div class="buttons">
<sp-close-button
@click=${this.shouldClose}
label="Close"
static-color="white"
></sp-close-button>
</div>
`;
}
protected override updated(changes: PropertyValues): void {
super.updated(changes);
if (changes.has('open')) {
if (this.open) {
if (this.timeout) {
this.startCountdown();
}
} else {
if (this.timeout) {
this.stopCountdown();
}
}
}
if (changes.has('timeout')) {
if (this.timeout !== null && this.open) {
this.startCountdown();
} else {
this.stopCountdown();
}
}
}
}