-
Notifications
You must be signed in to change notification settings - Fork 24
/
useMediaQuery.tsx
54 lines (45 loc) · 1.42 KB
/
useMediaQuery.tsx
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { useMemo, useSyncExternalStore } from "react";
const breakpoints = {
mobile: 375,
tablet: 768,
desktop: 1200,
};
export function useCustomMediaQuery(mediaQuery: string): boolean {
const [subscribe, getSnapshot] = useMemo(() => {
const mediaQueryList = globalThis.matchMedia(mediaQuery);
function subscribe(onStoreChange: () => void) {
mediaQueryList.addEventListener("change", onStoreChange);
return () => mediaQueryList.removeEventListener("change", onStoreChange);
}
const getSnapshot = () => mediaQueryList.matches;
return [subscribe, getSnapshot];
}, [mediaQuery]);
return useSyncExternalStore(subscribe, getSnapshot);
}
export const useMediaQuery = () => {
const isMobile = useCustomMediaQuery(
`(max-width: ${breakpoints.tablet - 1}px)`,
);
const isTablet = useCustomMediaQuery(
`(min-width: ${breakpoints.tablet}px) and (max-width: ${breakpoints.desktop - 1}px)`,
);
const isDesktop = useCustomMediaQuery(
`(min-width: ${breakpoints.desktop}px)`,
);
const isTabletUp = useCustomMediaQuery(
`(min-width: ${breakpoints.tablet}px)`,
);
const isTabletDown = useCustomMediaQuery(
`(max-width: ${breakpoints.desktop - 1}px)`,
);
const matches = useMemo(() => {
return {
isMobile,
isTablet,
isDesktop,
isTabletUp,
isTabletDown,
};
}, [isMobile, isTablet, isDesktop, isTabletUp, isTabletDown]);
return matches;
};