Skip to content

Commit 5b9f2ec

Browse files
Sync svelte docs (#938)
sync svelte docs Co-authored-by: Rich-Harris <[email protected]>
1 parent ea6528e commit 5b9f2ec

File tree

2 files changed

+170
-2
lines changed

2 files changed

+170
-2
lines changed

apps/svelte.dev/content/docs/svelte/98-reference/21-svelte-motion.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,48 @@ title: svelte/motion
66

77
```js
88
// @noErrors
9-
import { spring, tweened } from 'svelte/motion';
9+
import { prefersReducedMotion, spring, tweened } from 'svelte/motion';
1010
```
1111

12+
## prefersReducedMotion
13+
14+
<blockquote class="since note">
15+
16+
Available since 5.7.0
17+
18+
</blockquote>
19+
20+
A [media query](/docs/svelte/svelte-reactivity#MediaQuery) that matches if the user [prefers reduced motion](https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-reduced-motion).
21+
22+
```svelte
23+
<script>
24+
import { prefersReducedMotion } from 'svelte/motion';
25+
import { fly } from 'svelte/transition';
26+
27+
let visible = $state(false);
28+
</script>
29+
30+
<button onclick={() => visible = !visible}>
31+
toggle
32+
</button>
33+
34+
{#if visible}
35+
<p transition:fly={{ y: prefersReducedMotion.current ? 0 : 200 }}>
36+
flies in, unless the user prefers reduced motion
37+
</p>
38+
{/if}
39+
```
40+
41+
<div class="ts-block">
42+
43+
```dts
44+
const prefersReducedMotion: MediaQuery;
45+
```
46+
47+
</div>
48+
49+
50+
1251
## spring
1352

1453
The spring function in Svelte creates a store whose value is animated, with a motion that simulates the behavior of a spring. This means when the value changes, instead of transitioning at a steady rate, it "bounces" like a spring would, depending on the physics parameters provided. This adds a level of realism to the transitions and can enhance the user experience.

apps/svelte.dev/content/docs/svelte/98-reference/21-svelte-reactivity.md

Lines changed: 130 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,83 @@ Svelte provides reactive versions of various built-ins like `SvelteMap`, `Svelte
2727
```js
2828
// @noErrors
2929
import {
30+
MediaQuery,
3031
SvelteDate,
3132
SvelteMap,
3233
SvelteSet,
3334
SvelteURL,
34-
SvelteURLSearchParams
35+
SvelteURLSearchParams,
36+
createSubscriber
3537
} from 'svelte/reactivity';
3638
```
3739

40+
## MediaQuery
41+
42+
<blockquote class="since note">
43+
44+
Available since 5.7.0
45+
46+
</blockquote>
47+
48+
Creates a media query and provides a `current` property that reflects whether or not it matches.
49+
50+
Use it carefully — during server-side rendering, there is no way to know what the correct value should be, potentially causing content to change upon hydration.
51+
If you can use the media query in CSS to achieve the same effect, do that.
52+
53+
```svelte
54+
<script>
55+
import { MediaQuery } from 'svelte/reactivity';
56+
57+
const large = new MediaQuery('min-width: 800px');
58+
</script>
59+
60+
<h1>{large.current ? 'large screen' : 'small screen'}</h1>
61+
```
62+
63+
<div class="ts-block">
64+
65+
```dts
66+
class MediaQuery {/*…*/}
67+
```
68+
69+
<div class="ts-block-property">
70+
71+
```dts
72+
constructor(query: string, matches?: boolean | undefined);
73+
```
74+
75+
<div class="ts-block-property-details">
76+
77+
<div class="ts-block-property-bullets">
78+
79+
- `query` A media query string
80+
- `matches` Fallback value for the server
81+
82+
</div>
83+
84+
</div>
85+
</div>
86+
87+
<div class="ts-block-property">
88+
89+
```dts
90+
get current(): boolean;
91+
```
92+
93+
<div class="ts-block-property-details"></div>
94+
</div>
95+
96+
<div class="ts-block-property">
97+
98+
```dts
99+
#private;
100+
```
101+
102+
<div class="ts-block-property-details"></div>
103+
</div></div>
104+
105+
106+
38107
## SvelteDate
39108

40109
<div class="ts-block">
@@ -193,4 +262,64 @@ class SvelteURLSearchParams extends URLSearchParams {/*…*/}
193262

194263

195264

265+
## createSubscriber
266+
267+
<blockquote class="since note">
268+
269+
Available since 5.7.0
270+
271+
</blockquote>
272+
273+
Returns a `subscribe` function that, if called in an effect (including expressions in the template),
274+
calls its `start` callback with an `update` function. Whenever `update` is called, the effect re-runs.
275+
276+
If `start` returns a function, it will be called when the effect is destroyed.
277+
278+
If `subscribe` is called in multiple effects, `start` will only be called once as long as the effects
279+
are active, and the returned teardown function will only be called when all effects are destroyed.
280+
281+
It's best understood with an example. Here's an implementation of [`MediaQuery`](/docs/svelte/svelte-reactivity#MediaQuery):
282+
283+
```js
284+
// @errors: 7031
285+
import { createSubscriber } from 'svelte/reactivity';
286+
import { on } from 'svelte/events';
287+
288+
export class MediaQuery {
289+
#query;
290+
#subscribe;
291+
292+
constructor(query) {
293+
this.#query = window.matchMedia(`(${query})`);
294+
295+
this.#subscribe = createSubscriber((update) => {
296+
// when the `change` event occurs, re-run any effects that read `this.current`
297+
const off = on(this.#query, 'change', update);
298+
299+
// stop listening when all the effects are destroyed
300+
return () => off();
301+
});
302+
}
303+
304+
get current() {
305+
this.#subscribe();
306+
307+
// Return the current state of the query, whether or not we're in an effect
308+
return this.#query.matches;
309+
}
310+
}
311+
```
312+
313+
<div class="ts-block">
314+
315+
```dts
316+
function createSubscriber(
317+
start: (update: () => void) => (() => void) | void
318+
): () => void;
319+
```
320+
321+
</div>
322+
323+
324+
196325

0 commit comments

Comments
 (0)