Created
April 15, 2021 03:01
-
-
Save gkweb/a7558977eb3b59f31fec0a4016065b28 to your computer and use it in GitHub Desktop.
This file contains 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
/** | |
* 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