Skip to content

Instantly share code, notes, and snippets.

@gkweb
Created April 15, 2021 03:01
Show Gist options
  • Save gkweb/a7558977eb3b59f31fec0a4016065b28 to your computer and use it in GitHub Desktop.
Save gkweb/a7558977eb3b59f31fec0a4016065b28 to your computer and use it in GitHub Desktop.
/**
* Liteweight matchMedia
*
* @param {String} query - The media query.
* @param {Function} - onChange callback.
*/
const useMatchMedia = (query, onChange = matches => matches) => {
const mm = matchMedia(query)
// Safari still uses addListener
mm.addListener(mql => onChange(mql.matches))
return matchMedia(query)
}
// Usage:
// const isDesktop = ref(false)
// const mm = useMatchMedia('screen and (min-width: 768px), matches => (isDesktop.value = matches))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment