2021-09-27 16:09:19 +00:00
|
|
|
import { Maybe } from "@trpc/server";
|
2021-07-11 19:35:56 +00:00
|
|
|
import { useEffect, useState } from "react";
|
2021-07-09 22:59:21 +00:00
|
|
|
|
2021-09-14 08:45:28 +00:00
|
|
|
// makes sure the ui doesn't flash
|
2021-09-27 16:09:19 +00:00
|
|
|
export default function useTheme(theme?: Maybe<string>) {
|
2021-07-09 22:59:21 +00:00
|
|
|
const [isReady, setIsReady] = useState(false);
|
2021-07-11 19:35:56 +00:00
|
|
|
useEffect(() => {
|
2021-09-27 16:09:19 +00:00
|
|
|
setIsReady(true);
|
|
|
|
if (!theme) {
|
|
|
|
return;
|
|
|
|
}
|
2021-07-09 22:59:21 +00:00
|
|
|
if (!theme && window.matchMedia("(prefers-color-scheme: dark)").matches) {
|
|
|
|
document.documentElement.classList.add("dark");
|
2021-10-05 22:53:24 +00:00
|
|
|
} else if (!theme) {
|
|
|
|
/** Uncovered case */
|
2021-07-09 22:59:21 +00:00
|
|
|
} else {
|
|
|
|
document.documentElement.classList.add(theme);
|
|
|
|
}
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return {
|
2021-07-11 19:35:56 +00:00
|
|
|
isReady,
|
|
|
|
};
|
|
|
|
}
|