|
1 | 1 | <script lang="ts"> |
2 | | - import { spring } from 'svelte/motion'; |
| 2 | + import { Spring } from 'svelte/motion'; |
3 | 3 |
|
4 | | - let count = $state(0); |
| 4 | + let count = new Spring(0); |
5 | 5 |
|
6 | | - // svelte-ignore state_referenced_locally |
7 | | - const displayedCount = spring(count); |
8 | | -
|
9 | | - $effect(() => { |
10 | | - displayedCount.set(count); |
11 | | - }); |
12 | | - let offset = $derived(modulo($displayedCount, 1)); |
| 6 | + let offset = $derived(modulo(count.current, 1)); |
13 | 7 |
|
14 | 8 | /** |
15 | 9 | * @param {number} n |
|
22 | 16 | </script> |
23 | 17 |
|
24 | 18 | <div class="counter"> |
25 | | - <button onclick={() => (count -= 1)} aria-label="Decrease the counter by one"> |
| 19 | + <button onclick={() => (count.target -= 1)} aria-label="Decrease the counter by one"> |
26 | 20 | <svg aria-hidden="true" viewBox="0 0 1 1"> |
27 | 21 | <path d="M0,0.5 L1,0.5" /> |
28 | 22 | </svg> |
29 | 23 | </button> |
30 | 24 |
|
31 | 25 | <div class="counter-viewport"> |
32 | 26 | <div class="counter-digits" style="transform: translate(0, {100 * offset}%)"> |
33 | | - <strong class="hidden" aria-hidden="true">{Math.floor($displayedCount + 1)}</strong> |
34 | | - <strong>{Math.floor($displayedCount)}</strong> |
| 27 | + <strong class="hidden" aria-hidden="true">{Math.floor(count.current + 1)}</strong> |
| 28 | + <strong>{Math.floor(count.current)}</strong> |
35 | 29 | </div> |
36 | 30 | </div> |
37 | 31 |
|
38 | | - <button onclick={() => (count += 1)} aria-label="Increase the counter by one"> |
| 32 | + <button onclick={() => (count.target += 1)} aria-label="Increase the counter by one"> |
39 | 33 | <svg aria-hidden="true" viewBox="0 0 1 1"> |
40 | 34 | <path d="M0,0.5 L1,0.5 M0.5,0 L0.5,1" /> |
41 | 35 | </svg> |
|
0 commit comments