fix: handle prefers-color-scheme correctly (#1740)
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
This commit is contained in:
		
							parent
							
								
									84f897e330
								
							
						
					
					
						commit
						38b41f0adb
					
				
					 1 changed files with 6 additions and 7 deletions
				
			
		|  | @ -1,15 +1,12 @@ | |||
| import { useEffect, useState } from "react"; | ||||
| import { useLayoutEffect, useState } from "react"; | ||||
| 
 | ||||
| import { Maybe } from "@trpc/server"; | ||||
| 
 | ||||
| // makes sure the ui doesn't flash
 | ||||
| export default function useTheme(theme?: Maybe<string>) { | ||||
|   const [isReady, setIsReady] = useState(false); | ||||
|   useEffect(() => { | ||||
|     setIsReady(true); | ||||
|     if (!theme) { | ||||
|       return; | ||||
|     } | ||||
| 
 | ||||
|   useLayoutEffect(() => { | ||||
|     if (!theme && window.matchMedia("(prefers-color-scheme: dark)").matches) { | ||||
|       document.documentElement.classList.add("dark"); | ||||
|     } else if (!theme) { | ||||
|  | @ -17,7 +14,9 @@ export default function useTheme(theme?: Maybe<string>) { | |||
|     } else { | ||||
|       document.documentElement.classList.add(theme); | ||||
|     } | ||||
|   }, []); | ||||
| 
 | ||||
|     setIsReady(true); | ||||
|   }, [theme]); | ||||
| 
 | ||||
|   return { | ||||
|     isReady, | ||||
|  |  | |||
		Loading…
	
		Reference in a new issue
	
	 Arthur Denner
						Arthur Denner