Skip to content

Commit 62d370a

Browse files
committed
matches -> current, server fallback
1 parent c5c4bea commit 62d370a

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

packages/svelte/src/reactivity/index-server.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,12 @@ export const SvelteURL = globalThis.URL;
55
export const SvelteURLSearchParams = globalThis.URLSearchParams;
66

77
export class MediaQuery {
8-
matches = false;
8+
current;
9+
/**
10+
* @param {string} query
11+
* @param {boolean} [matches]
12+
*/
13+
constructor(query, matches = false) {
14+
this.current = matches;
15+
}
916
}

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,22 @@ import { set, source } from '../internal/client/reactivity/sources.js';
33
import { effect_tracking, render_effect } from '../internal/client/reactivity/effects.js';
44

55
/**
6-
* Creates a media query and provides a `matches` property that reflects its current state.
6+
* Creates a media query and provides a `current` property that reflects whether or not it matches.
77
*/
88
export class MediaQuery {
9-
#matches = source(false);
9+
#version = source(0);
1010
#subscribers = 0;
1111
#query;
1212
/** @type {any} */
1313
#listener;
1414

15-
get matches() {
15+
get current() {
1616
if (effect_tracking()) {
17+
get(this.#version);
18+
1719
render_effect(() => {
1820
if (this.#subscribers === 0) {
19-
this.#listener = () => set(this.#matches, this.#query.matches);
21+
this.#listener = () => set(this.#version, this.#version.v + 1);
2022
this.#query.addEventListener('change', this.#listener);
2123
}
2224

@@ -37,13 +39,14 @@ export class MediaQuery {
3739
});
3840
}
3941

40-
return get(this.#matches);
42+
return this.#query.matches;
4143
}
4244

43-
/** @param {string} query */
44-
constructor(query) {
45+
/**
46+
* @param {string} query A media query string (don't forget the braces)
47+
* @param {boolean} [matches] Fallback value for the server
48+
*/
49+
constructor(query, matches) {
4550
this.#query = window.matchMedia(query);
46-
console.log('MediaQuery.constructor', query, this.#query);
47-
this.#matches.v = this.#query.matches;
4851
}
4952
}

0 commit comments

Comments
 (0)