|
1 | | -<f-loader transition:persist></f-loader> |
2 | | - |
3 | 1 | <style is:global> |
4 | 2 | @import "tailwindcss"; |
5 | 3 |
|
6 | | - f-loader { |
| 4 | + #loader { |
7 | 5 | @apply bg-blue-500 fixed h-1 hidden left-0 top-0 z-[999]; |
8 | 6 | } |
9 | 7 | </style> |
10 | 8 |
|
11 | 9 | <script> |
12 | | - import type { AnimeInstance } from "animejs"; |
13 | | - import type { TransitionBeforePreparationEvent } from "astro:transitions/client"; |
14 | | - import anime from "animejs"; |
| 10 | + import type { JSAnimation } from "animejs"; |
| 11 | + import { animate } from "animejs"; |
| 12 | + |
| 13 | + const el = document.createElement("div"); |
| 14 | + el.id = "loader"; |
15 | 15 |
|
16 | | - class Loader extends HTMLElement { |
17 | | - private instance: AnimeInstance | null = null; |
| 16 | + let animation: JSAnimation | null = null; |
18 | 17 |
|
19 | | - public connectedCallback(): void { |
20 | | - // On page before preparation |
21 | | - document.addEventListener( |
22 | | - "astro:before-preparation", |
23 | | - (ev: TransitionBeforePreparationEvent): void => { |
24 | | - // Not show load bar when get assets |
25 | | - if (ev.to.pathname.startsWith("/assets")) { |
26 | | - return; |
27 | | - } |
| 18 | + document.addEventListener("DOMContentLoaded", () => { |
| 19 | + document.body.prepend(el); |
| 20 | + }); |
28 | 21 |
|
29 | | - // Stop old animation |
30 | | - if (this.instance !== null) { |
31 | | - this.instance.pause(); |
32 | | - this.instance.complete?.(this.instance); |
33 | | - } |
| 22 | + document.addEventListener("astro:before-preparation", (ev) => { |
| 23 | + // Not show load bar when get assets |
| 24 | + if (ev.to.pathname.startsWith("/assets")) { |
| 25 | + return; |
| 26 | + } |
34 | 27 |
|
35 | | - // Start new animation |
36 | | - this.instance = anime({ |
37 | | - targets: this, |
38 | | - width: ["0%", "90%"], |
39 | | - duration: 5000, |
40 | | - easing: "easeOutCubic", |
41 | | - begin: (): void => { |
42 | | - this.style.display = "block"; |
43 | | - }, |
44 | | - complete: (): void => { |
45 | | - this.instance = null; |
46 | | - }, |
47 | | - }); |
48 | | - } |
49 | | - ); |
| 28 | + // Stop old animation |
| 29 | + if (animation !== null) { |
| 30 | + animation.complete(); |
| 31 | + } |
50 | 32 |
|
51 | | - // On page loaded |
52 | | - document.addEventListener("astro:after-preparation", (): void => { |
53 | | - // Stop old animation |
54 | | - if (this.instance !== null) { |
55 | | - this.instance.pause(); |
56 | | - this.instance.complete?.(this.instance); |
57 | | - } |
| 33 | + // Start new animation |
| 34 | + animation = animate(el, { |
| 35 | + width: ["0%", "90%"], |
| 36 | + duration: 5000, |
| 37 | + ease: "outCubic", |
| 38 | + onBegin: (): void => { |
| 39 | + el.style.display = "block"; |
| 40 | + el.style.opacity = "1"; |
| 41 | + }, |
| 42 | + onComplete: (): void => { |
| 43 | + animation = null; |
| 44 | + }, |
| 45 | + }); |
| 46 | + }); |
58 | 47 |
|
59 | | - // Start new animation |
60 | | - this.instance = anime({ |
61 | | - targets: this, |
62 | | - width: "100%", |
63 | | - duration: 500, |
64 | | - easing: "linear", |
65 | | - complete: (): void => { |
66 | | - this.style.display = "none"; |
67 | | - this.instance = null; |
68 | | - }, |
69 | | - }); |
70 | | - }); |
| 48 | + document.addEventListener("astro:before-swap", (ev) => { |
| 49 | + ev.newDocument.body.prepend(el); |
| 50 | + }); |
| 51 | + |
| 52 | + document.addEventListener("astro:after-swap", () => { |
| 53 | + // Stop old animation |
| 54 | + if (animation !== null) { |
| 55 | + animation.cancel(); |
| 56 | + animation = null; |
71 | 57 | } |
72 | | - } |
73 | | - customElements.define("f-loader", Loader); |
| 58 | + |
| 59 | + // Start new animation |
| 60 | + animation = animate(el, { |
| 61 | + width: { |
| 62 | + to: "100%", |
| 63 | + duration: 500 |
| 64 | + }, |
| 65 | + opacity: { |
| 66 | + to: "0", |
| 67 | + duration: 100, |
| 68 | + delay: 500 |
| 69 | + }, |
| 70 | + ease: "linear", |
| 71 | + onComplete: (): void => { |
| 72 | + el.style.display = "none"; |
| 73 | + animation = null; |
| 74 | + }, |
| 75 | + }); |
| 76 | + }); |
74 | 77 | </script> |
0 commit comments