@@ -6,18 +6,26 @@ import { useEffect, useState } from "react";
66const storageKey = "theme-index" ;
77const darkThemeMatch = window . matchMedia ( "(prefers-color-scheme: dark)" ) ;
88const list = [ < IconDesktop /> , < IconSun /> , < IconMoon /> ] ;
9+
910const handler = ( e : MediaQueryListEvent ) => {
10- if ( e . matches ) document . body . setAttribute ( "arco-theme" , "dark" ) ;
11- else document . body . removeAttribute ( "arco-theme" ) ;
11+ if ( e . matches ) {
12+ document . body . setAttribute ( "arco-theme" , "dark" ) ;
13+ } else {
14+ document . body . removeAttribute ( "arco-theme" ) ;
15+ }
1216} ;
17+
1318export const ThemeAction : FC = ( ) => {
14- const [ index , setIndex ] = useState ( Storage . local . get < number > ( storageKey ) || 0 ) ;
19+ const [ index , setIndex ] = useState ( ( ) => Storage . local . get < number > ( storageKey ) || 0 ) ;
1520
1621 useEffect ( ( ) => {
1722 switch ( index ) {
1823 case 0 : {
19- if ( darkThemeMatch . matches ) document . body . setAttribute ( "arco-theme" , "dark" ) ;
20- else document . body . removeAttribute ( "arco-theme" ) ;
24+ if ( darkThemeMatch . matches ) {
25+ document . body . setAttribute ( "arco-theme" , "dark" ) ;
26+ } else {
27+ document . body . removeAttribute ( "arco-theme" ) ;
28+ }
2129 darkThemeMatch . onchange = handler ;
2230 break ;
2331 }
@@ -34,11 +42,11 @@ export const ThemeAction: FC = () => {
3442 }
3543 } , [ index ] ) ;
3644
37- const changeTheme = ( ) => {
45+ const onChangeTheme = ( ) => {
3846 const nextIndex = ( index + 1 ) % list . length ;
3947 setIndex ( nextIndex ) ;
4048 Storage . local . set ( storageKey , nextIndex ) ;
4149 } ;
4250
43- return < div onClick = { changeTheme } > { list [ index ] } </ div > ;
51+ return < div onClick = { onChangeTheme } > { list [ index ] } </ div > ;
4452} ;
0 commit comments