-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
23 lines (19 loc) · 932 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import { readable, derived } from 'svelte/store'
const setupMq = (queryString) => (set) => {
const query = window.matchMedia(queryString)
const callback = (e) => set(e.matches)
query.addListener(callback)
callback(query)
return () => query.removeListener(callback)
}
const defaultQueries = {
desktop: 'screen and (min-width: 769px)',
mobile: 'screen and (max-width: 768px)'
}
const makeMediaStoreInstance = (queries) => {
const qs = Object.entries(queries).reduce((acc, [mediaName, queryString]) => (acc[mediaName] = readable(false, setupMq(queryString)), acc), {})
return derived(Object.values(qs), ($queryStores) => $queryStores.reduce((acc, q, i) => (acc[Object.keys(qs)[i]] = q, acc), {}))
}
export let media
export const setup = (queries = defaultQueries) => { media = makeMediaStoreInstance(queries) }
export const createMedia = (queries = defaultQueries) => ({ media: makeMediaStoreInstance(queries) })