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