|
1 |
| -import anim from "animejs"; |
2 |
| - |
3 | 1 | import { BaseOptions } from "./component";
|
4 | 2 |
|
5 | 3 | export interface ToastOptions extends BaseOptions {
|
@@ -207,36 +205,32 @@ export class Toast {
|
207 | 205 | const toast = this.options.toastId
|
208 | 206 | ? document.getElementById(this.options.toastId)
|
209 | 207 | : document.createElement('div');
|
210 |
| - //const toast = document.createElement('div'); |
211 | 208 | toast.classList.add('toast');
|
212 | 209 | toast.setAttribute('role', 'alert');
|
213 | 210 | toast.setAttribute('aria-live', 'assertive');
|
214 | 211 | toast.setAttribute('aria-atomic', 'true');
|
215 |
| - |
216 | 212 | // Add custom classes onto toast
|
217 | 213 | if (this.options.classes.length > 0) {
|
218 | 214 | toast.classList.add(...this.options.classes.split(' '));
|
219 | 215 | }
|
220 |
| - |
221 |
| - // Set text content |
222 |
| - if (this.message) |
223 |
| - toast.innerText = this.message; |
224 |
| - |
225 |
| - // Append toast |
| 216 | + if (this.message) toast.innerText = this.message; |
226 | 217 | Toast._container.appendChild(toast);
|
227 | 218 | return toast;
|
228 | 219 | }
|
229 | 220 |
|
230 | 221 | _animateIn() {
|
231 | 222 | // Animate toast in
|
232 |
| - this.el.style.display = "" |
233 |
| - anim({ |
234 |
| - targets: this.el, |
235 |
| - top: 0, |
236 |
| - opacity: 1, |
237 |
| - duration: this.options.inDuration, |
238 |
| - easing: 'easeOutCubic' |
239 |
| - }); |
| 223 | + this.el.style.display = ""; |
| 224 | + this.el.style.opacity = '0'; |
| 225 | + // easeOutCubic |
| 226 | + this.el.style.transition = ` |
| 227 | + top ${this.options.inDuration}ms ease, |
| 228 | + opacity ${this.options.inDuration}ms ease |
| 229 | + `; |
| 230 | + setTimeout(() => { |
| 231 | + this.el.style.top = '0'; |
| 232 | + this.el.style.opacity = '1'; |
| 233 | + }, 1); |
240 | 234 | }
|
241 | 235 |
|
242 | 236 | /**
|
@@ -271,27 +265,30 @@ export class Toast {
|
271 | 265 | this.el.style.opacity = '0';
|
272 | 266 | }
|
273 | 267 |
|
274 |
| - anim({ |
275 |
| - targets: this.el, |
276 |
| - opacity: 0, |
277 |
| - marginTop: -40, |
278 |
| - duration: this.options.outDuration, |
279 |
| - easing: 'easeOutExpo', |
280 |
| - complete: () => { |
281 |
| - // Call the optional callback |
282 |
| - if (typeof this.options.completeCallback === 'function') { |
283 |
| - this.options.completeCallback(); |
284 |
| - } |
285 |
| - // Remove toast from DOM |
286 |
| - if (!this.options.toastId) { |
287 |
| - this.el.remove(); |
288 |
| - Toast._toasts.splice(Toast._toasts.indexOf(this), 1); |
289 |
| - if (Toast._toasts.length === 0) { |
290 |
| - Toast._removeContainer(); |
291 |
| - } |
| 268 | + // easeOutExpo |
| 269 | + this.el.style.transition = ` |
| 270 | + margin ${this.options.outDuration}ms ease, |
| 271 | + opacity ${this.options.outDuration}ms ease`; |
| 272 | + |
| 273 | + setTimeout(() => { |
| 274 | + this.el.style.opacity = '0'; |
| 275 | + this.el.style.marginTop = '-40px'; |
| 276 | + }, 1); |
| 277 | + |
| 278 | + setTimeout(() => { |
| 279 | + // Call the optional callback |
| 280 | + if (typeof this.options.completeCallback === 'function') { |
| 281 | + this.options.completeCallback(); |
| 282 | + } |
| 283 | + // Remove toast from DOM |
| 284 | + if (!this.options.toastId) { |
| 285 | + this.el.remove(); |
| 286 | + Toast._toasts.splice(Toast._toasts.indexOf(this), 1); |
| 287 | + if (Toast._toasts.length === 0) { |
| 288 | + Toast._removeContainer(); |
292 | 289 | }
|
293 | 290 | }
|
294 |
| - }); |
| 291 | + }, this.options.outDuration); |
295 | 292 | }
|
296 | 293 |
|
297 | 294 | static {
|
|
0 commit comments