11import { get } from '../internal/client/runtime.js' ;
2- import { set , source } from '../internal/client/reactivity/sources.js' ;
2+ import { source } from '../internal/client/reactivity/sources.js' ;
33import { effect_tracking } from '../internal/client/reactivity/effects.js' ;
44import { createStartStopNotifier } from './start-stop-notifier.js' ;
5+ import { on } from '../events/index.js' ;
6+ import { increment } from './utils.js' ;
57
68/**
79 * Creates a media query and provides a `current` property that reflects whether or not it matches.
810 */
911export class MediaQuery {
10- #version = source ( 0 ) ;
1112 #query;
12- #notify;
13+ #version = source ( 0 ) ;
14+ #notify = createStartStopNotifier ( ( ) => {
15+ return on ( this . #query, 'change' , ( ) => increment ( this . #version) ) ;
16+ } ) ;
1317
1418 get current ( ) {
1519 if ( effect_tracking ( ) ) {
@@ -21,15 +25,11 @@ export class MediaQuery {
2125 }
2226
2327 /**
24- * @param {string } query A media query string (don't forget the braces)
28+ * @param {string } query A media query string
2529 * @param {boolean } [matches] Fallback value for the server
2630 */
2731 constructor ( query , matches ) {
28- this . #query = window . matchMedia ( query ) ;
29- this . #notify = createStartStopNotifier ( ( ) => {
30- const listener = ( ) => set ( this . #version, this . #version. v + 1 ) ;
31- this . #query. addEventListener ( 'change' , listener ) ;
32- return ( ) => this . #query. removeEventListener ( 'change' , listener ) ;
33- } ) ;
32+ // For convenience (and because people likely forget them) we add the parentheses; double parantheses are not a problem
33+ this . #query = window . matchMedia ( `(${ query } )` ) ;
3434 }
3535}
0 commit comments