Files
kestrelos/app/composables/useMediaQuery.js
Madison Grubb 4e51ca5509
All checks were successful
ci/woodpecker/pr/pr Pipeline was successful
new nav system
2026-02-14 22:47:05 -05:00

22 lines
657 B
JavaScript

/**
* Reactive viewport media query. SSR-safe: defaults to true (mobile) so sidebar closed on first paint.
* @param {string} query - CSS media query, e.g. '(max-width: 767px)'
* @returns {import('vue').Ref<boolean>} Ref that is true when the media query matches.
*/
export function useMediaQuery(query) {
const matches = ref(true)
let mql = null
const handler = (e) => {
matches.value = e.matches
}
onMounted(() => {
mql = window.matchMedia(query)
matches.value = mql.matches
mql.addEventListener('change', handler)
})
onBeforeUnmount(() => {
if (mql) mql.removeEventListener('change', handler)
})
return matches
}