Skip to content

Instantly share code, notes, and snippets.

@pearofducks
Created July 28, 2019 16:50
Show Gist options
  • Save pearofducks/20ba3351f675d242d0d2103c4a077412 to your computer and use it in GitHub Desktop.
Save pearofducks/20ba3351f675d242d0d2103c4a077412 to your computer and use it in GitHub Desktop.
Svelte media-query readable stores
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