1- import { writable } from 'svelte/store' ;
1+ import { type Readable , readable } from 'svelte/store' ;
22
3- type MediaQuery = {
3+ export type Queries = {
44 [ key : string ] : boolean | string ;
55} ;
6- type BrowserStorage = {
7- type : string
8- key : string
6+
7+ type MediaObject = {
8+ [ key : string ] : MediaQueryList
99}
1010
11- const mediaQueries : MediaQuery = {
11+ const queries : Queries = {
1212 xs : '(max-width: 480px)' ,
1313 sm : '(max-width: 600px)' ,
1414 md : '(max-width: 840px)' ,
@@ -23,43 +23,29 @@ const mediaQueries: MediaQuery = {
2323 touch : '(hover: none)' ,
2424} ;
2525
26- export const media = watchMedia ( mediaQueries , { key : 'optimade-media' } )
27-
28- function watchMedia ( queries : MediaQuery , storage : Partial < BrowserStorage > ) {
29- const { subscribe, set, update } = writable ( { } ) ;
30- const base = { type : 'session' , key : 'media-query' }
31-
32- storage = { ...base , ...storage }
33-
34- if ( persist ( storage ) ) {
35- const match : MediaQuery = JSON . parse ( persist ( storage ) . getItem ( storage . key as string ) as string ) || { } ;
36-
37- for ( const query in queries ) {
38- const media = window . matchMedia ( queries [ query ] as string )
39- setMatches ( media , query )
40- media . onchange = ( e ) => setMatches ( e , query )
26+ export const media : Readable < Queries > = mediaStore ( queries )
27+
28+ function mediaStore ( queries : Queries = { } ) {
29+ return readable ( { } , ( set ) => {
30+ let mqs = Object . entries ( queries ) . reduce ( ( mqs : MediaObject , [ key , query ] ) => {
31+ mqs [ key ] = window . matchMedia ( query as string ) ;
32+ mqs [ key ] . onchange = ( mq ) => {
33+ mqs [ key ] = mq ;
34+ update ( ) ;
35+ } ;
36+ return mqs as MediaObject ;
37+ } , { } ) ;
38+
39+ function update ( ) {
40+ const matches : Queries = Object . entries ( mqs ) . reduce ( ( matches : Queries , [ key , mq ] ) => {
41+ matches [ key ] = mq . matches ;
42+ return matches ;
43+ } , { } ) ;
44+ set ( matches ) ;
4145 }
4246
43- subscribe ( ( match ) : void => persist ( storage ) . setItem ( storage . key as string , JSON . stringify ( match ) ) ) ;
44-
45- function setMatches ( media : MediaQueryList | MediaQueryListEvent , query : string ) {
46- if ( 'target' in media ) match [ query ] = media . matches ;
47- else match [ query ] ??= media . matches ;
48- set ( match ) ;
49- persist ( storage ) . setItem ( storage . key as string , JSON . stringify ( match ) ) ;
50- }
51- }
52-
53- function persist ( storage : Partial < BrowserStorage > ) : Storage {
54- try {
55- const store = storage . type === 'session' ? sessionStorage : localStorage
56- store . setItem ( storage . key as string , storage . key as string ) ;
57- store . removeItem ( storage . key as string ) ;
58- return store ;
59- } catch ( e ) {
60- console . error ( e )
61- }
62- }
47+ update ( ) ;
6348
64- return { subscribe, set, update }
49+ return ( ) => mqs = { } ;
50+ } ) ;
6551}
0 commit comments