Created
July 28, 2019 16:50
-
-
Save pearofducks/20ba3351f675d242d0d2103c4a077412 to your computer and use it in GitHub Desktop.
Svelte media-query readable stores
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const setupMQ = (queryString) => (set) => { | |
const query = window.matchMedia(queryString) | |
const callback = (e) => set(e.matches) | |
query.addListener(callback) | |
callback(query) | |
return (query) => query.removeListener(callback) | |
} | |
// example use | |
import { readable } from 'svelte/store' | |
export const isDesktop = readable(false, setupMQ('screen and (min-width: 769px)')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment