aboutsummaryrefslogtreecommitdiff
path: root/src/lib/Layout/NumberTicker.svelte
blob: 3bd7e6c878c523471e8d251d16b8fcc9ece9398b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<script>
  import { run } from 'svelte/legacy';

  import { tweened } from 'svelte/motion';
  import { cubicOut } from 'svelte/easing';

  /**
   * @typedef {Object} Props
   * @property {number} [start]
   * @property {number} [end]
   * @property {number} [duration]
   * @property {number} [delay]
   */

  /** @type {Props} */
  let {
    start = 0,
    end = 100,
    duration = 2000,
    delay = 0
  } = $props();

  const count = tweened(start, {
    duration: duration,
    easing: cubicOut,
    delay: delay
  });

  run(() => {
    count.set(end);
  });
</script>

<span class="counter" class:visible={$count !== start}>
  {Math.round($count)}
</span>

<style lang="scss">
  .counter {
    $duration: 0.2s;

    opacity: 0;
    transition: opacity $duration ease-out, transform $duration ease-out;
  }

  .counter.visible {
    opacity: 1;
  }
</style>