Skip to content

Commit cdfaf13

Browse files
authored
use Spring class
1 parent dbd238b commit cdfaf13

File tree

1 file changed

+7
-13
lines changed

1 file changed

+7
-13
lines changed

packages/create/templates/demo/src/routes/Counter.svelte

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
<script lang="ts">
2-
import { spring } from 'svelte/motion';
2+
import { Spring } from 'svelte/motion';
33
4-
let count = $state(0);
4+
let count = new Spring(0);
55
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));
137
148
/**
159
* @param {number} n
@@ -22,20 +16,20 @@
2216
</script>
2317

2418
<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">
2620
<svg aria-hidden="true" viewBox="0 0 1 1">
2721
<path d="M0,0.5 L1,0.5" />
2822
</svg>
2923
</button>
3024

3125
<div class="counter-viewport">
3226
<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>
3529
</div>
3630
</div>
3731

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">
3933
<svg aria-hidden="true" viewBox="0 0 1 1">
4034
<path d="M0,0.5 L1,0.5 M0.5,0 L0.5,1" />
4135
</svg>

0 commit comments

Comments
 (0)