Skip to content

A self-hostable bookmark-everything app (links, notes and images) with AI-based automatic tagging and full text search

feat(web): Sync theme-color with the in-app theme (#3052)

Puneet Deshwani5ada473988c1
diff --git a/apps/web/app/layout.tsx b/apps/web/app/layout.tsx
index 62e9877e..f1ccd460 100644
--- a/apps/web/app/layout.tsx
+++ b/apps/web/app/layout.tsx
@@ -7,6 +7,7 @@ import "@karakeep/tailwind-config/globals.css";
import type { Viewport } from "next";
import React from "react";
import Providers from "@/lib/providers";
+import { THEME_COLORS } from "@/lib/themeColors";
import { getUserLocalSettings } from "@/lib/userLocalSettings/userLocalSettings";
import { getServerAuthSession } from "@/server/auth";
import { ReactQueryDevtools } from "@tanstack/react-query-devtools";
@@ -56,12 +57,11 @@ export const viewport: Viewport = {
maximumScale: 1,
userScalable: false,
// Values match the `--background` token in tooling/tailwind/globals.css.
- // These follow the OS color scheme only. An in-app theme override via
- // next-themes is not reflected here, so chrome can differ from the page
- // when a user picks a theme opposite to their system preference.
+ // These cover the pre-hydration and no-JavaScript cases; once mounted,
+ // <ThemeColorSync /> updates them to follow an in-app theme override.
themeColor: [
- { media: "(prefers-color-scheme: light)", color: "#ffffff" },
- { media: "(prefers-color-scheme: dark)", color: "#030303" },
+ { media: "(prefers-color-scheme: light)", color: THEME_COLORS.light },
+ { media: "(prefers-color-scheme: dark)", color: THEME_COLORS.dark },
],
};
diff --git a/apps/web/components/theme-color-sync.tsx b/apps/web/components/theme-color-sync.tsx
new file mode 100644
index 00000000..354bebfb
--- /dev/null
+++ b/apps/web/components/theme-color-sync.tsx
@@ -0,0 +1,36 @@
+"use client";
+
+import { useEffect } from "react";
+import { THEME_COLORS } from "@/lib/themeColors";
+import { useTheme } from "next-themes";
+
+/**
+ * Keeps the `theme-color` meta tags in sync with the theme the app is actually
+ * rendering.
+ *
+ * The static entries in `viewport.themeColor` use `prefers-color-scheme`, so on
+ * their own they follow the operating system rather than an in-app override. A
+ * user who selects a theme opposite to their system preference would otherwise
+ * get browser chrome that does not match the page.
+ *
+ * Every `theme-color` tag is set to the resolved color, so whichever media
+ * query the browser matches, it reads the same value. The static tags are left
+ * in the markup so that the correct color is still applied before hydration and
+ * when JavaScript is unavailable.
+ */
+export function ThemeColorSync() {
+ const { resolvedTheme } = useTheme();
+
+ useEffect(() => {
+ if (!resolvedTheme) {
+ return;
+ }
+ const color =
+ resolvedTheme === "dark" ? THEME_COLORS.dark : THEME_COLORS.light;
+ document
+ .querySelectorAll('meta[name="theme-color"]')
+ .forEach((tag) => tag.setAttribute("content", color));
+ }, [resolvedTheme]);
+
+ return null;
+}
diff --git a/apps/web/lib/providers.tsx b/apps/web/lib/providers.tsx
index d02a6b0a..4bc31bca 100644
--- a/apps/web/lib/providers.tsx
+++ b/apps/web/lib/providers.tsx
@@ -2,6 +2,7 @@
import type { UserLocalSettings } from "@/lib/userLocalSettings/types";
import React, { useEffect, useState } from "react";
+import { ThemeColorSync } from "@/components/theme-color-sync";
import { ThemeProvider } from "@/components/theme-provider";
import { TooltipProvider } from "@/components/ui/tooltip";
import { Session, SessionProvider } from "@/lib/auth/client";
@@ -99,6 +100,7 @@ export default function Providers({
enableSystem
disableTransitionOnChange
>
+ <ThemeColorSync />
<TooltipProvider delayDuration={0}>
{children}
</TooltipProvider>
diff --git a/apps/web/lib/themeColors.ts b/apps/web/lib/themeColors.ts
new file mode 100644
index 00000000..a2efabd1
--- /dev/null
+++ b/apps/web/lib/themeColors.ts
@@ -0,0 +1,11 @@
+/**
+ * Browser chrome colors, matching the `--background` token defined in
+ * tooling/tailwind/globals.css.
+ *
+ * Shared between the static `viewport.themeColor` entries in the root layout
+ * and the client-side sync that follows the in-app theme.
+ */
+export const THEME_COLORS = {
+ light: "#ffffff",
+ dark: "#030303",
+} as const;