Lib: Implement countdown component

This commit is contained in:
2025-01-30 22:33:52 +01:00
parent b4931611b3
commit f4b89ec839
2 changed files with 42 additions and 0 deletions

View File

@ -0,0 +1,40 @@
<script lang="ts">
interface CountdownProps {
date: string;
extraclass?: string;
}
let { date, extraclass = "" }: CountdownProps = $props();
// Set the date we're counting down to
const countDownDate = new Date(date).getTime();
let distance: number = $state(0);
let days: number = $state(0);
let hours: number = $state(0);
let minutes: number = $state(0);
let seconds: number = $state(0);
// Update the countdown every 1 second
setInterval(function () {
// Get today's date and time
const now = new Date().getTime();
// Find the distance between now and the countdown date
distance = countDownDate - now;
// Time calculations for days, hours, minutes and seconds
days = Math.floor(distance / (1000 * 60 * 60 * 24));
hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
seconds = Math.floor((distance % (1000 * 60)) / 1000);
}, 1000);
</script>
<span class={extraclass}>
{#if distance > 0}
{days + "d " + hours + "h " + minutes + "m " + seconds + "s"}
{:else}
GO GO GO GO
{/if}
</span>

View File

@ -1,3 +1,4 @@
import Countdown from "./Countdown.svelte";
import LazyImage from "./LazyImage.svelte";
import LoadingIndicator from "./LoadingIndicator.svelte";
import Table from "./Table.svelte";
@ -26,6 +27,7 @@ import UserIcon from "./svg/UserIcon.svelte";
export {
// Components
Countdown,
LazyImage,
LoadingIndicator,
Table,