-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENH: rewritten Progress.svelte into Progress.astro for much better pe…
…rformance
- Loading branch information
1 parent
f0e9344
commit f6d1af9
Showing
4 changed files
with
69 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
--- | ||
type Props = { | ||
id: string; | ||
total: number; | ||
confetti: boolean; | ||
}; | ||
const { id, total, confetti = true } = Astro.props; | ||
--- | ||
|
||
<x-progress data-confetti={`${confetti}`}> | ||
<label for={id}>0 / {total}</label> | ||
<progress {id} max={total} /> | ||
{import.meta.env.DEV && <button>Reset</button>} | ||
</x-progress> | ||
|
||
<style> | ||
x-progress { | ||
display: flex; | ||
flex-direction: column; | ||
align-items: center; | ||
margin-top: 2rem; | ||
margin-bottom: -1rem; | ||
} | ||
progress { | ||
margin: 0 !important; | ||
} | ||
</style> | ||
|
||
<script> | ||
import { subscribe, add, remove } from "./Checkpoint/checkpoint"; | ||
|
||
class Progress extends HTMLElement { | ||
constructor() { | ||
super(); | ||
|
||
const progress = this.querySelector("progress") as HTMLProgressElement; | ||
const label = this.querySelector("label") as HTMLLabelElement; | ||
const confetti: boolean = this.dataset.confetti === "true"; | ||
const id: string = progress.id; | ||
const total: number = progress.max; | ||
|
||
subscribe(id, async (checkpoint) => { | ||
const value = checkpoint.size < total ? checkpoint.size : total; | ||
label.innerText = `${value} / ${total}`; | ||
progress.value = value; | ||
if (!confetti || value !== total || checkpoint.has(`${id}-confetti`)) { | ||
return; | ||
} | ||
const confettis = await import("confettis"); | ||
confettis.create({ y: 2 }); | ||
add(`${id}-confetti`); | ||
}); | ||
|
||
if (import.meta.env.DEV) { | ||
const button = this.querySelector("button") as HTMLButtonElement; | ||
button.addEventListener("click", () => remove(id)); | ||
} | ||
} | ||
} | ||
|
||
customElements.define("x-progress", Progress); | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters