fix: handle prefers-color-scheme correctly (#1740)

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
pull/1674/head
Arthur Denner 2022-02-08 20:19:33 +01:00 committed by GitHub
parent 84f897e330
commit 38b41f0adb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 6 additions and 7 deletions

View File

@ -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,