File tree Expand file tree Collapse file tree 1 file changed +20
-5
lines changed
Expand file tree Collapse file tree 1 file changed +20
-5
lines changed Original file line number Diff line number Diff line change @@ -2,20 +2,35 @@ import { useSyncExternalStore } from 'react';
22
33const MOBILE_BREAKPOINT = 768 ;
44
5- const mql = window . matchMedia ( `(max-width: ${ MOBILE_BREAKPOINT - 1 } px)` ) ;
5+ const mql =
6+ typeof window === 'undefined'
7+ ? undefined
8+ : window . matchMedia ( `(max-width: ${ MOBILE_BREAKPOINT - 1 } px)` ) ;
69
710function mediaQueryListener ( callback : ( event : MediaQueryListEvent ) => void ) {
11+ if ( ! mql ) {
12+ return ( ) => { } ;
13+ }
14+
815 mql . addEventListener ( 'change' , callback ) ;
916
1017 return ( ) => {
1118 mql . removeEventListener ( 'change' , callback ) ;
1219 } ;
1320}
1421
15- function isSmallerThanBreakpoint ( ) {
16- return mql . matches ;
22+ function isSmallerThanBreakpoint ( ) : boolean {
23+ return mql ?. matches ?? false ;
24+ }
25+
26+ function getServerSnapshot ( ) : boolean {
27+ return false ;
1728}
1829
19- export function useIsMobile ( ) {
20- return useSyncExternalStore ( mediaQueryListener , isSmallerThanBreakpoint ) ;
30+ export function useIsMobile ( ) : boolean {
31+ return useSyncExternalStore (
32+ mediaQueryListener ,
33+ isSmallerThanBreakpoint ,
34+ getServerSnapshot
35+ ) ;
2136}
You can’t perform that action at this time.
0 commit comments