Skip to content

Commit

Permalink
fix: theme flash on load
Browse files Browse the repository at this point in the history
  • Loading branch information
jer3m01 committed Oct 11, 2024
1 parent 1764559 commit 58eab9b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
3 changes: 2 additions & 1 deletion docs/src/entry-server.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getTheme } from "@kobalte/solidbase/client";
import { SolidBaseServerScript, getTheme } from "@kobalte/solidbase/client";
// @refresh reload
import { StartServer, createHandler } from "@solidjs/start/server";

Expand All @@ -10,6 +10,7 @@ export default createHandler(() => (
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="icon" href="/favicon.ico" />
<SolidBaseServerScript />
{assets}
</head>
<body>
Expand Down
16 changes: 16 additions & 0 deletions src/client/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,20 @@ function Layout(rootProps: RouteSectionProps) {
);
}

export function SolidBaseServerScript() {
return (
<script>
{`
function getThemeCookie() {
if (!document.cookie) return undefined;
const match = document.cookie.match(new RegExp(\`\\\\W?theme=(?<theme>\\\\w+)\`));
return match?.groups?.theme;
}
console.log("data-theme", getThemeCookie() ?? (window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light"));
`}
</script>
);
}

export { getTheme, setTheme } from "./theme";

0 comments on commit 58eab9b

Please sign in to comment.