@@ -3,13 +3,33 @@ import Card from "$lib/components/ui/Card.svelte"
33import Label from " $lib/components/ui/Label.svelte"
44import type { StatisticItem } from " $lib/schema/statistics"
55import NumberFlow from " @number-flow/svelte"
6+ import { onMount } from " svelte"
67
78type Props = {
89 statistic: StatisticItem
910 class? : string
1011}
1112
1213const { statistic, class : className = " " }: Props = $props ()
14+ let displayValue = $state (0 )
15+ let isFirstLoad = $state (true )
16+
17+ // Update displayValue whenever statistic.value changes
18+ $effect (() => {
19+ if (isFirstLoad ) {
20+ // On first load, animate from 0 to the value
21+ onMount (() => {
22+ displayValue = 0
23+ setTimeout (() => {
24+ displayValue = statistic .value
25+ isFirstLoad = false
26+ }, 300 ) // Small delay to sync with card fade-in
27+ })
28+ } else {
29+ // For subsequent updates, directly update the value
30+ displayValue = statistic .value
31+ }
32+ })
1333
1434// Mapping of statistic names to display names
1535const displayNames: Record <string , string > = {
@@ -35,6 +55,6 @@ function formatStatName(name: string): string {
3555<Card class ="h-22 transition-all hover:shadow-lg {className }" >
3656 <Label >{formatStatName (statistic .name )}</Label >
3757 <p class =" text-2xl font-bold mt-1" >
38- <NumberFlow value ={statistic . value } />
58+ <NumberFlow value ={displayValue } transformTiming ={{ duration : 1500 , easing : ' ease-out ' }} opacityTiming ={{ duration : 1500 , easing : ' ease-out ' }} spinTiming ={{ duration : 1500 , easing : ' ease-out ' } } />
3959 </p >
4060</Card >
0 commit comments