@@ -3,20 +3,22 @@ import { set, source } from '../internal/client/reactivity/sources.js';
33import { 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 */
88export 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