-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathresponsive.ts
More file actions
36 lines (32 loc) · 1.01 KB
/
responsive.ts
File metadata and controls
36 lines (32 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { onUnmounted, ref } from 'vue'
export type ReponsiveSize = 'mobile' | 'tablet' | 'desktop' | 'desktop-large'
function getQuery(size: ReponsiveSize) {
let query: string
switch (size) {
// NOTE: remember to keep these values in sync with the breakpoints in @/app.css
case 'mobile':
query = '(max-width: 767px)'
break
case 'tablet':
query = '(min-width: 768px) and (max-width: 1279px)'
break
case 'desktop':
query = '(min-width: 1280px) and (max-width: 1679px)'
break
case 'desktop-large':
query = '(min-width: 1680px)'
break
}
return query
}
export function useResponsive(size: ReponsiveSize) {
const query = getQuery(size)
const mqList = window.matchMedia(query)
const isMatched = ref(mqList.matches)
function onMQListChange(event: MediaQueryListEvent) {
isMatched.value = event.matches
}
mqList.addEventListener('change', onMQListChange)
onUnmounted(() => mqList.removeEventListener('change', onMQListChange))
return isMatched
}