⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.
TypeScript honoissue-managementissue-trackerjira-alternativekanbanlinear-alternativemcpproject-managementreactself-hostedtypescript
fix(site): prevent theme flash during hydration
Tin6bd0c1ac4216
diff --git a/apps/site/app/layout.tsx b/apps/site/app/layout.tsxindex 372902849..5ece6f9f2 100644--- a/apps/site/app/layout.tsx+++ b/apps/site/app/layout.tsx@@ -1,5 +1,6 @@import type { Metadata, Viewport } from "next";import Script from "next/script";+import { ThemeSync } from "@/components/landing/theme-sync";import { landing } from "@/lib/landing";import "./globals.css";@@ -102,29 +103,26 @@ export default function RootLayout({}: Readonly<{ children: React.ReactNode }>) {return (<html lang="en" suppressHydrationWarning>- <body>+ <head><script- // eslint-disable-next-line react/no-danger -- This is necessary to apply the user's preferred color scheme before React hydration to prevent a flash of incorrect theme.+ // eslint-disable-next-line react/no-danger -- Apply the theme before the page paints, including when JavaScript bundles load slowly.dangerouslySetInnerHTML={{__html: `(function() {+ var stored = null;try {- var media = window.matchMedia('(prefers-color-scheme: dark)');- function applyTheme(isDark) {- document.documentElement.classList.toggle('dark', isDark);- document.documentElement.style.colorScheme = isDark ? 'dark' : 'light';- }- applyTheme(media.matches);- if (media.addEventListener) {- media.addEventListener('change', function(e) { applyTheme(e.matches); });- } else if (media.addListener) {- media.addListener(function(e) { applyTheme(e.matches); });- }+ stored = localStorage.getItem('kaneo-site-theme');} catch (e) {}+ var isDark = stored === 'dark' || (stored !== 'light' && window.matchMedia('(prefers-color-scheme: dark)').matches);+ document.documentElement.classList.toggle('dark', isDark);+ document.documentElement.style.colorScheme = isDark ? 'dark' : 'light';})();`,}}/>+ </head>+ <body>+ <ThemeSync />{children}<scripttype="application/ld+json"diff --git a/apps/site/components/landing/theme-sync.tsx b/apps/site/components/landing/theme-sync.tsxnew file mode 100644index 000000000..56098975a--- /dev/null+++ b/apps/site/components/landing/theme-sync.tsx@@ -0,0 +1,28 @@+"use client";++import { useLayoutEffect } from "react";++export function ThemeSync() {+ useLayoutEffect(() => {+ const media = window.matchMedia("(prefers-color-scheme: dark)");+ const applyTheme = () => {+ let stored: string | null = null;+ try {+ stored = localStorage.getItem("kaneo-site-theme");+ } catch {+ // Follow the system appearance when storage is unavailable.+ }+ const isDark = stored === "dark" || (stored !== "light" && media.matches);+ document.documentElement.classList.toggle("dark", isDark);+ document.documentElement.style.colorScheme = isDark ? "dark" : "light";+ };++ // React can clear the root attributes during hydration. Restore them before+ // the browser paints, then keep system appearance changes in sync.+ applyTheme();+ media.addEventListener("change", applyTheme);+ return () => media.removeEventListener("change", applyTheme);+ }, []);++ return null;+}