Skip to content

Commit cf5fb16

Browse files
fix: use modern Spring and MediaQuery implementation (#361)
* fix: use modern Spring implementation * use `MediaQuery` * fix changeset * add to changeset --------- Co-authored-by: Manuel Serret <mserret99@gmail.com>
1 parent aed15fb commit cf5fb16

File tree

5 files changed

+19
-44
lines changed

5 files changed

+19
-44
lines changed

.changeset/tiny-colts-trade.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'sv': patch
3+
---
4+
5+
fix: use modern `Spring` and `MediaQuery` implementation

packages/create/templates/demo/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
"devDependencies": {
1212
"@fontsource/fira-mono": "^5.0.0",
1313
"@neoconfetti/svelte": "^2.0.0",
14-
"@sveltejs/adapter-auto": "workspace:*",
15-
"@sveltejs/kit": "workspace:*",
14+
"@sveltejs/adapter-auto": "^3",
15+
"@sveltejs/kit": "^2",
1616
"@sveltejs/vite-plugin-svelte": "^4.0.0",
1717
"svelte": "^5.0.0",
1818
"typescript": "^5.3.3",

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

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
11
<script lang="ts">
2-
import { spring } from 'svelte/motion';
2+
import { Spring } from 'svelte/motion';
33
4-
let count = $state(0);
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));
4+
const count = new Spring(0);
5+
const offset = $derived(modulo(count.current, 1));
136
147
/**
158
* @param {number} n
@@ -22,20 +15,20 @@
2215
</script>
2316

2417
<div class="counter">
25-
<button onclick={() => (count -= 1)} aria-label="Decrease the counter by one">
18+
<button onclick={() => (count.target -= 1)} aria-label="Decrease the counter by one">
2619
<svg aria-hidden="true" viewBox="0 0 1 1">
2720
<path d="M0,0.5 L1,0.5" />
2821
</svg>
2922
</button>
3023

3124
<div class="counter-viewport">
3225
<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>
26+
<strong class="hidden" aria-hidden="true">{Math.floor(count.current + 1)}</strong>
27+
<strong>{Math.floor(count.current)}</strong>
3528
</div>
3629
</div>
3730

38-
<button onclick={() => (count += 1)} aria-label="Increase the counter by one">
31+
<button onclick={() => (count.target += 1)} aria-label="Increase the counter by one">
3932
<svg aria-hidden="true" viewBox="0 0 1 1">
4033
<path d="M0,0.5 L1,0.5 M0.5,0 L0.5,1" />
4134
</svg>

packages/create/templates/demo/src/routes/sverdle/+page.svelte

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { enhance } from '$app/forms';
33
import { confetti } from '@neoconfetti/svelte';
44
import type { ActionData, PageData } from './$types';
5-
import { reducedMotion } from './reduced-motion';
5+
import { MediaQuery } from 'svelte/reactivity';
66
77
interface Props {
88
data: PageData;
@@ -19,6 +19,9 @@
1919
*/
2020
let { data, form = $bindable() }: Props = $props();
2121
22+
/** Whether the user prefers reduced motion */
23+
const reducedMotion = new MediaQuery('(prefers-reduced-motion: reduce)');
24+
2225
/** Whether or not the user has won */
2326
let won = $derived(data.answers.at(-1) === 'xxxxx');
2427
@@ -203,7 +206,7 @@
203206
<div
204207
style="position: absolute; left: 50%; top: 30%"
205208
use:confetti={{
206-
particleCount: $reducedMotion ? 0 : undefined,
209+
particleCount: reducedMotion.current ? 0 : undefined,
207210
force: 0.7,
208211
stageWidth: window.innerWidth,
209212
stageHeight: window.innerHeight,

packages/create/templates/demo/src/routes/sverdle/reduced-motion.ts

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)