|
1 | | -import { useEffect, useState } from "react"; |
| 1 | +import { useCallback, useSyncExternalStore } from "react"; |
2 | 2 |
|
3 | | -function getMediaQueryMatch(query: string): boolean { |
4 | | - if (typeof window === "undefined") { |
5 | | - return false; |
| 3 | +const BREAKPOINTS = { |
| 4 | + "2xl": 1536, |
| 5 | + "3xl": 1600, |
| 6 | + "4xl": 2000, |
| 7 | + lg: 1024, |
| 8 | + md: 768, |
| 9 | + sm: 640, |
| 10 | + xl: 1280, |
| 11 | +} as const; |
| 12 | + |
| 13 | +type Breakpoint = keyof typeof BREAKPOINTS; |
| 14 | + |
| 15 | +type BreakpointQuery = Breakpoint | `max-${Breakpoint}` | `${Breakpoint}:max-${Breakpoint}`; |
| 16 | + |
| 17 | +function resolveMin(value: Breakpoint | number): string { |
| 18 | + const px = typeof value === "number" ? value : BREAKPOINTS[value]; |
| 19 | + return `(min-width: ${px}px)`; |
| 20 | +} |
| 21 | + |
| 22 | +function resolveMax(value: Breakpoint | number): string { |
| 23 | + const px = typeof value === "number" ? value : BREAKPOINTS[value]; |
| 24 | + return `(max-width: ${px - 1}px)`; |
| 25 | +} |
| 26 | + |
| 27 | +function parseQuery(query: BreakpointQuery | MediaQueryInput | (string & {})): string { |
| 28 | + if (typeof query !== "string") { |
| 29 | + const parts: string[] = []; |
| 30 | + if (query.min != null) parts.push(resolveMin(query.min)); |
| 31 | + if (query.max != null) parts.push(resolveMax(query.max)); |
| 32 | + if (query.pointer === "coarse") parts.push("(pointer: coarse)"); |
| 33 | + if (query.pointer === "fine") parts.push("(pointer: fine)"); |
| 34 | + if (parts.length === 0) return "(min-width: 0px)"; |
| 35 | + return parts.join(" and "); |
| 36 | + } |
| 37 | + |
| 38 | + if (query.startsWith("(")) return query; |
| 39 | + |
| 40 | + const parts: string[] = []; |
| 41 | + for (const segment of query.split(":")) { |
| 42 | + if (segment.startsWith("max-")) { |
| 43 | + const bp = segment.slice(4); |
| 44 | + if (bp in BREAKPOINTS) parts.push(resolveMax(bp as Breakpoint)); |
| 45 | + } else if (segment in BREAKPOINTS) { |
| 46 | + parts.push(resolveMin(segment as Breakpoint)); |
| 47 | + } |
6 | 48 | } |
7 | | - return window.matchMedia(query).matches; |
| 49 | + |
| 50 | + return parts.length > 0 ? parts.join(" and ") : query; |
| 51 | +} |
| 52 | + |
| 53 | +function getServerSnapshot(): boolean { |
| 54 | + return false; |
8 | 55 | } |
9 | 56 |
|
10 | | -export function useMediaQuery(query: string): boolean { |
11 | | - const [matches, setMatches] = useState(() => getMediaQueryMatch(query)); |
| 57 | +export type MediaQueryInput = { |
| 58 | + min?: Breakpoint | number; |
| 59 | + max?: Breakpoint | number; |
| 60 | + /** Touch-like input (finger). Use "fine" for mouse/trackpad. */ |
| 61 | + pointer?: "coarse" | "fine"; |
| 62 | +}; |
| 63 | + |
| 64 | +export function useMediaQuery(query: BreakpointQuery | MediaQueryInput | (string & {})): boolean { |
| 65 | + const mediaQuery = parseQuery(query); |
12 | 66 |
|
13 | | - useEffect(() => { |
14 | | - const mediaQueryList = window.matchMedia(query); |
15 | | - const handleChange = () => { |
16 | | - setMatches(mediaQueryList.matches); |
17 | | - }; |
| 67 | + const subscribe = useCallback( |
| 68 | + (callback: () => void) => { |
| 69 | + if (typeof window === "undefined") return () => {}; |
| 70 | + const mql = window.matchMedia(mediaQuery); |
| 71 | + mql.addEventListener("change", callback); |
| 72 | + return () => mql.removeEventListener("change", callback); |
| 73 | + }, |
| 74 | + [mediaQuery], |
| 75 | + ); |
18 | 76 |
|
19 | | - setMatches(mediaQueryList.matches); |
20 | | - mediaQueryList.addEventListener("change", handleChange); |
21 | | - return () => { |
22 | | - mediaQueryList.removeEventListener("change", handleChange); |
23 | | - }; |
24 | | - }, [query]); |
| 77 | + const getSnapshot = useCallback(() => { |
| 78 | + if (typeof window === "undefined") return false; |
| 79 | + return window.matchMedia(mediaQuery).matches; |
| 80 | + }, [mediaQuery]); |
| 81 | + |
| 82 | + return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot); |
| 83 | +} |
25 | 84 |
|
26 | | - return matches; |
| 85 | +export function useIsMobile(): boolean { |
| 86 | + return useMediaQuery("max-md"); |
27 | 87 | } |
0 commit comments