@@ -106,46 +106,37 @@ export const useTheme = () => {
106106} ;
107107
108108export function ModeToggle ( ) {
109- const { theme, toggleTheme } = useTheme ( ) ;
110- const [ currentIconIndex , setCurrentIconIndex ] = useState ( 0 ) ;
111-
112- const icons = [
113- < Moon key = "dark" className = "h-[1.2rem] w-[1.2rem]" /> ,
114- < Sun key = "light" className = "h-[1.2rem] w-[1.2rem]" /> ,
115- < Monitor key = "system" className = "h-[1.2rem] w-[1.2rem]" /> ,
116- ] ;
117-
118-
109+ const { toggleTheme } = useTheme ( ) ;
110+ const [ systemMode , setSystemMode ] = useState ( false ) ;
111+
119112 useEffect ( ( ) => {
120- const savedTheme = getTheme ( ) ;
121- const index = icons . findIndex ( icon => icon . key === savedTheme ) ;
122- setCurrentIconIndex ( index >= 0 ? index : 0 ) ;
123- } , [ theme ] ) ;
113+ const currentTheme = getTheme ( ) ;
114+ setSystemMode ( currentTheme === "system" ) ;
115+ } , [ ] ) ;
124116
125117 const handleClick = ( ) => {
126118 toggleTheme ?.( ) ;
127119
128- const currentTheme = getTheme ( ) ;
129- let nextTheme : Theme ;
130- if ( currentTheme === "dark" ) {
131- nextTheme = "light" ;
132- } else if ( currentTheme === "light" ) {
133- nextTheme = "system" ;
134- } else {
135- nextTheme = "dark" ;
136- }
137- const nextIndex = icons . findIndex ( icon => icon . key === nextTheme ) ;
138- setCurrentIconIndex ( nextIndex >= 0 ? nextIndex : 0 ) ;
139-
120+ const newTheme = getTheme ( ) ;
121+ setSystemMode ( newTheme === "system" ) ;
140122 } ;
141123
124+ if ( systemMode ) {
125+ return (
126+ < Button variant = "outline" size = "icon" onClick = { handleClick } >
127+ < Monitor className = "h-[1.2rem] w-[1.2rem]" />
128+ </ Button >
129+ ) ;
130+ }
131+
142132 return (
143- < Button
144- variant = "outline"
145- size = "icon"
146- onClick = { handleClick }
147- >
148- { icons [ currentIconIndex ] }
133+ < Button variant = "outline" size = "icon" onClick = { handleClick } >
134+ < Sun
135+ className = { `relative dark:absolute h-[1.2rem] w-[1.2rem] rotate-0 scale-100 transition-all dark:-rotate-90 dark:scale-0` }
136+ />
137+ < Moon
138+ className = { `absolute dark:relative h-[1.2rem] w-[1.2rem] rotate-90 scale-0 transition-all dark:rotate-0 dark:scale-100` }
139+ />
149140 </ Button >
150141 ) ;
151142}
0 commit comments