File tree Expand file tree Collapse file tree 1 file changed +11
-7
lines changed Expand file tree Collapse file tree 1 file changed +11
-7
lines changed Original file line number Diff line number Diff line change 2
2
// https://github.com/rfoel/use-prefers-color-scheme/blob/v1.0.0/src/index.ts
3
3
// The code is copied instead of being imported as the package seems to cause
4
4
// issues when used in a NextJS project.
5
- import { useEffect , useState } from "react" ;
6
-
7
- const darkQuery = window ?. matchMedia ?.( "(prefers-color-scheme: dark)" ) ;
8
-
9
- const lightQuery = window ?. matchMedia ?.( "(prefers-color-scheme: light)" ) ;
5
+ import { useEffect , useMemo , useState } from "react" ;
10
6
11
7
export const usePrefersColorScheme = ( ) => {
8
+ const darkQuery = useMemo (
9
+ ( ) => window . matchMedia ?.( "(prefers-color-scheme: dark)" ) ,
10
+ [ ]
11
+ ) ;
12
+ const lightQuery = useMemo (
13
+ ( ) => window . matchMedia ?.( "(prefers-color-scheme: light)" ) ,
14
+ [ ]
15
+ ) ;
12
16
const isDark = darkQuery ?. matches ;
13
17
const isLight = lightQuery ?. matches ;
14
18
@@ -69,9 +73,9 @@ export const usePrefersColorScheme = () => {
69
73
lightQuery ?. removeEventListener ( "change" , listener ) ;
70
74
} ;
71
75
}
72
- } , [ ] ) ;
76
+ } , [ darkQuery , lightQuery ] ) ;
73
77
74
- if ( typeof window ? .matchMedia !== "function" ) {
78
+ if ( typeof window . matchMedia !== "function" ) {
75
79
return preferredColorSchema ;
76
80
}
77
81
You can’t perform that action at this time.
0 commit comments