Skip to content

Commit c5790aa

Browse files
committed
fix: loader not persisted between view transition
1 parent ad0f887 commit c5790aa

1 file changed

Lines changed: 61 additions & 58 deletions

File tree

src/components/Loader.astro

Lines changed: 61 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,77 @@
1-
<f-loader transition:persist></f-loader>
2-
31
<style is:global>
42
@import "tailwindcss";
53

6-
f-loader {
4+
#loader {
75
@apply bg-blue-500 fixed h-1 hidden left-0 top-0 z-[999];
86
}
97
</style>
108

119
<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";
1515

16-
class Loader extends HTMLElement {
17-
private instance: AnimeInstance | null = null;
16+
let animation: JSAnimation | null = null;
1817

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+
});
2821

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+
}
3427

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+
}
5032

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+
});
5847

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;
7157
}
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+
});
7477
</script>

0 commit comments

Comments
 (0)