@ -3,6 +3,19 @@ import { ReactiveValue } from './reactive-value.js';
const parenthesis _regex = /\(.+\)/ ;
const parenthesis _regex = /\(.+\)/ ;
// these keywords are valid media queries but they need to be without parenthesis
//
// eg: new MediaQuery('screen')
//
// however because of the auto-parenthesis logic in the constructor since there's no parentehesis
// in the media query they'll be surrounded by parenthesis
//
// however we can check if the media query is only composed of these keywords
// and skip the auto-parenthesis
//
// https://github.com/sveltejs/svelte/issues/15930
const non _parenthesized _keywords = new Set ( [ 'all' , 'print' , 'screen' , 'and' , 'or' , 'not' , 'only' ] ) ;
/ * *
/ * *
* Creates a media query and provides a ` current ` property that reflects whether or not it matches .
* Creates a media query and provides a ` current ` property that reflects whether or not it matches .
*
*
@ -27,7 +40,12 @@ export class MediaQuery extends ReactiveValue {
* @ param { boolean } [ fallback ] Fallback value for the server
* @ param { boolean } [ fallback ] Fallback value for the server
* /
* /
constructor ( query , fallback ) {
constructor ( query , fallback ) {
let final _query = parenthesis _regex . test ( query ) ? query : ` ( ${ query } ) ` ;
let final _query =
parenthesis _regex . test ( query ) ||
// we need to use `some` here because technically this `window.matchMedia('random,screen')` still returns true
query . split ( /[\s,]+/ ) . some ( ( keyword ) => non _parenthesized _keywords . has ( keyword . trim ( ) ) )
? query
: ` ( ${ query } ) ` ;
const q = window . matchMedia ( final _query ) ;
const q = window . matchMedia ( final _query ) ;
super (
super (
( ) => q . matches ,
( ) => q . matches ,