Skip to content

Open source CapCut alternative.

TypeScript

feat(site): add an appearance setting

David Le292b7dde627a
diff --git a/site/src/app/cut/app/(home)/settings/profile/EditorSection.tsx b/site/src/app/cut/app/(home)/settings/profile/EditorSection.tsx
index 1e265e2d..1921636e 100644
--- a/site/src/app/cut/app/(home)/settings/profile/EditorSection.tsx
+++ b/site/src/app/cut/app/(home)/settings/profile/EditorSection.tsx
@@ -1,22 +1,37 @@
"use client";
+import { useSyncExternalStore } from "react";
+import {
+ Select,
+ SelectContent,
+ SelectItem,
+ SelectTrigger,
+ SelectValue,
+} from "@/components/ui/select";
import { Switch } from "@/components/ui/switch";
+import {
+ readTheme,
+ setTheme,
+ subscribeTheme,
+ THEME_CHOICES,
+ THEME_LABELS,
+ type ThemeChoice,
+} from "@/lib/theme";
import { useAccountFlags, useSetAccountFlag } from "@/queries/featureFlags";
-// Editor preferences that belong to the account, so they follow the user to
-// every browser: what the top bar shows, and whatever joins it later.
+// Editor preferences: the theme this browser shows, then the account flags
+// that follow the user to every browser, such as what the top bar shows.
export function EditorSection() {
const set = useSetAccountFlag();
- const prefs = useAccountFlags().data?.filter((f) => f.group === "editor");
-
- if (!prefs?.length) return null;
+ const prefs = useAccountFlags().data?.filter((f) => f.group === "editor") ?? [];
return (
<div className="rounded-xl border bg-card p-5">
<div className="text-sm font-medium">Editor</div>
<div className="mt-4 border-t pt-4">
- {prefs.map((pref, i) => (
- <div key={pref.id} className={i > 0 ? "mt-4 border-t pt-4" : undefined}>
+ <ThemeRow />
+ {prefs.map((pref) => (
+ <div key={pref.id} className="mt-4 border-t pt-4">
<div className="flex items-start justify-between gap-6">
<span className="min-w-0">
<span className="block text-sm font-medium">{pref.title}</span>
@@ -41,3 +56,31 @@ export function EditorSection() {
</div>
);
}
+
+// Appearance: System follows the OS; Light and Dark pin it in this browser.
+function ThemeRow() {
+ const theme = useSyncExternalStore(subscribeTheme, readTheme, () => "system" as const);
+
+ return (
+ <div className="flex items-center justify-between gap-6">
+ <span className="min-w-0">
+ <span className="block text-sm font-medium">Appearance</span>
+ <span className="mt-0.5 block text-sm text-muted-foreground">
+ Light, dark, or matching your system.
+ </span>
+ </span>
+ <Select value={theme} onValueChange={(v) => setTheme(v as ThemeChoice)}>
+ <SelectTrigger aria-label="Appearance" className="w-28">
+ <SelectValue />
+ </SelectTrigger>
+ <SelectContent>
+ {THEME_CHOICES.map((choice) => (
+ <SelectItem key={choice} value={choice}>
+ {THEME_LABELS[choice]}
+ </SelectItem>
+ ))}
+ </SelectContent>
+ </Select>
+ </div>
+ );
+}
diff --git a/site/src/app/darkMode.test.ts b/site/src/app/darkMode.test.ts
index dde9d9c1..731ef19f 100644
--- a/site/src/app/darkMode.test.ts
+++ b/site/src/app/darkMode.test.ts
@@ -7,6 +7,7 @@ import { EmailAuthForm } from "./_components/landing/EmailAuthForm";
import { MentionTextarea } from "@/cut/components/AssetRefs";
import { AiChatSlide } from "@/cut/components/onboarding/slides/AiChat";
import { TopNav } from "./_components/landing/TopNav";
+import { THEME_HEAD_SCRIPT } from "@/lib/theme";
const css = postcss.parse(readFileSync(new URL("./globals.css", import.meta.url), "utf8"));
@@ -66,4 +67,18 @@ describe("dark theme", () => {
});
expect(overrides).toEqual(["var(--app-background)"]);
});
+
+ test("the head script pins a saved theme and leaves system alone", () => {
+ const run = (saved: string | null) => {
+ const classes = new Set<string>();
+ const storage = { getItem: () => saved };
+ const doc = { documentElement: { classList: { add: (c: string) => classes.add(c) } } };
+ new Function("localStorage", "document", THEME_HEAD_SCRIPT)(storage, doc);
+ return [...classes];
+ };
+ expect(run("dark")).toEqual(["dark"]);
+ expect(run("light")).toEqual(["light"]);
+ expect(run(null)).toEqual([]);
+ expect(run("sepia")).toEqual([]);
+ });
});
diff --git a/site/src/app/layout.tsx b/site/src/app/layout.tsx
index 882e098d..d6df6144 100644
--- a/site/src/app/layout.tsx
+++ b/site/src/app/layout.tsx
@@ -5,6 +5,7 @@ import { GeistSans } from "geist/font/sans";
import "./globals.css";
import { DONKEYCUT_CANONICAL } from "@/cut/lib/hosts";
+import { THEME_HEAD_SCRIPT } from "@/lib/theme";
import { QueryProvider } from "@/queries/QueryProvider";
import { MetaPageView } from "./_components/MetaPageView";
import { PostHogProvider } from "./_components/PostHogProvider";
@@ -42,7 +43,12 @@ export default function RootLayout({
<html
lang="en"
className={`${GeistSans.variable} ${GeistMono.variable} h-full antialiased`}
+ // The head script adds the saved theme's class before hydration.
+ suppressHydrationWarning
>
+ <head>
+ <script dangerouslySetInnerHTML={{ __html: THEME_HEAD_SCRIPT }} />
+ </head>
<body className="min-h-full flex flex-col">
<PostHogProvider>
<QueryProvider>{children}</QueryProvider>
diff --git a/site/src/lib/theme.ts b/site/src/lib/theme.ts
new file mode 100644
index 00000000..b270500c
--- /dev/null
+++ b/site/src/lib/theme.ts
@@ -0,0 +1,71 @@
+// The page theme. "system" follows the OS; "light" and "dark" pin the root
+// element's class, which globals.css reads ahead of the OS preference. The
+// choice belongs to this browser and is applied by a head script before first
+// paint, so a pinned theme never flashes the system one.
+
+export const THEME_CHOICES = ["system", "light", "dark"] as const;
+export type ThemeChoice = (typeof THEME_CHOICES)[number];
+
+export const THEME_LABELS: Record<ThemeChoice, string> = {
+ system: "System",
+ light: "Light",
+ dark: "Dark",
+};
+
+const THEME_KEY = "donkey-theme";
+const listeners = new Set<() => void>();
+
+const isPinned = (v: unknown): v is "light" | "dark" => v === "light" || v === "dark";
+
+/** Runs inline in <head>: pins the saved theme before the body paints. */
+export const THEME_HEAD_SCRIPT = `try{var t=localStorage.getItem(${JSON.stringify(THEME_KEY)});if(t==="light"||t==="dark"){document.documentElement.classList.add(t)}}catch(e){}`;
+
+export function readTheme(): ThemeChoice {
+ try {
+ const saved = localStorage.getItem(THEME_KEY);
+ return isPinned(saved) ? saved : "system";
+ } catch {
+ return "system";
+ }
+}
+
+// Swap the root class: "system" clears both so the OS preference rules.
+function applyTheme(choice: ThemeChoice) {
+ const root = document.documentElement.classList;
+ root.remove("light", "dark");
+ if (isPinned(choice)) {
+ root.add(choice);
+ }
+}
+
+export function setTheme(choice: ThemeChoice) {
+ try {
+ if (isPinned(choice)) {
+ localStorage.setItem(THEME_KEY, choice);
+ } else {
+ localStorage.removeItem(THEME_KEY);
+ }
+ } catch {
+ // Storage blocked: the theme still applies for this page.
+ }
+ applyTheme(choice);
+ listeners.forEach((l) => l());
+}
+
+/** Subscribes to theme changes here and in other tabs of this browser. */
+export function subscribeTheme(listener: () => void) {
+ const onStorage = (e: StorageEvent) => {
+ if (e.key !== THEME_KEY) {
+ return;
+ }
+ applyTheme(readTheme());
+ listener();
+ };
+
+ listeners.add(listener);
+ window.addEventListener("storage", onStorage);
+ return () => {
+ listeners.delete(listener);
+ window.removeEventListener("storage", onStorage);
+ };
+}