Skip to content

Commit 4e59abc

Browse files
committed
add example for MediaQuery
1 parent 7900d9b commit 4e59abc

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

packages/svelte/src/reactivity/media-query.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@ import { on } from '../events/index.js';
33

44
/**
55
* Creates a media query and provides a `current` property that reflects whether or not it matches.
6+
*
7+
* 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.
8+
*
9+
* ```svelte
10+
* <script>
11+
* import { MediaQuery } from 'svelte/reactivity';
12+
*
13+
* const large = new MediaQuery('min-width: 800px');
14+
* </script>
15+
*
16+
* <h1>{large.current ? 'large screen' : 'small screen'}</h1>
17+
* ```
618
*/
719
export class MediaQuery {
820
#query;

packages/svelte/types/index.d.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1695,11 +1695,13 @@ declare module 'svelte/motion' {
16951695
* let visible = $state(false);
16961696
* </script>
16971697
*
1698-
* <button onclick={() => visible = !visible}>toggle</button>
1698+
* <button onclick={() => visible = !visible}>
1699+
* toggle
1700+
* </button>
16991701
*
17001702
* {#if visible}
17011703
* <p transition:fly={{ y: prefersReducedMotion.current ? 0 : 200 }}>
1702-
* flies in, unless the user prefers reduced motion
1704+
* flies in, unless the user prefers reduced motion
17031705
* </p>
17041706
* {/if}
17051707
* ```
@@ -1751,6 +1753,18 @@ declare module 'svelte/reactivity' {
17511753
}
17521754
/**
17531755
* Creates a media query and provides a `current` property that reflects whether or not it matches.
1756+
*
1757+
* 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.
1758+
*
1759+
* ```svelte
1760+
* <script>
1761+
* import { MediaQuery } from 'svelte/reactivity';
1762+
*
1763+
* const large = new MediaQuery('min-width: 800px');
1764+
* </script>
1765+
*
1766+
* <h1>{large.current ? 'large screen' : 'small screen'}</h1>
1767+
* ```
17541768
*/
17551769
export class MediaQuery {
17561770
/**

0 commit comments

Comments
 (0)