Fix/theme flicker (#1758)
* Avoid Theme Flicker. Render Server Side * Add back isReady implementation * Use shorter syntax for Tag Co-authored-by: Peer Richelsen <[email protected]> Co-authored-by: Omar López <[email protected]> Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
This commit is contained in:
7 files changed
+36
-18
No files matched your search
+24
-12
@@ -1,24 +1,36 @@
|
||||
import { useLayoutEffect, useState } from "react";
|
||||
import Head from "next/head";
|
||||
import { useEffect, 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);
|
||||
|
||||
useLayoutEffect(() => {
|
||||
if (!theme && window.matchMedia("(prefers-color-scheme: dark)").matches) {
|
||||
document.documentElement.classList.add("dark");
|
||||
} else if (!theme) {
|
||||
/** Uncovered case */
|
||||
} else {
|
||||
document.documentElement.classList.add(theme);
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
setIsReady(true);
|
||||
}, [theme]);
|
||||
}, []);
|
||||
function Theme() {
|
||||
const themeString = theme ? `"${theme}"` : null;
|
||||
return (
|
||||
<Head>
|
||||
<script
|
||||
dangerouslySetInnerHTML={{
|
||||
__html: `(function (theme) {
|
||||
if (!theme && window.matchMedia("(prefers-color-scheme: dark)").matches) {
|
||||
document.documentElement.classList.add("dark");
|
||||
} else if (!theme) {
|
||||
/** Uncovered case */
|
||||
} else {
|
||||
document.documentElement.classList.add(theme);
|
||||
}
|
||||
})(${themeString})`,
|
||||
}}></script>
|
||||
</Head>
|
||||
);
|
||||
}
|
||||
|
||||
return {
|
||||
isReady,
|
||||
Theme,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user