feat: add workspace theme toggle

This commit is contained in:
2026-07-03 04:28:01 -07:00
parent c4d5222ddd
commit fb15d4db47
4 changed files with 147 additions and 6 deletions

View File

@@ -6,6 +6,7 @@ import { SignOutButton } from "@/modules/auth/components/SignOutButton";
import { AccountMenu } from "@/modules/shared/components/AccountMenu";
import { AppBreadcrumbs } from "@/modules/shared/components/AppBreadcrumbs";
import { AppSidebarNav } from "@/modules/shared/components/AppSidebarNav";
import { ThemeToggle } from "@/modules/shared/components/ThemeToggle";
import { UserAvatar } from "@/modules/shared/components/UserAvatar";
import { navItems } from "@/modules/shared/lib/navigation";
import { getWorkspaceHref } from "@/modules/shared/lib/workspace-routing";
@@ -30,7 +31,7 @@ export function AppShell({
return (
<div className="flex h-svh items-stretch overflow-hidden bg-background text-foreground">
<aside className="hidden h-svh w-64 shrink-0 border-r bg-white/78 backdrop-blur xl:block">
<aside className="hidden h-svh w-64 shrink-0 border-r bg-card/78 backdrop-blur xl:block">
<div className="flex h-full min-h-0 flex-col">
<Link href={dashboardHref} className="flex h-16 shrink-0 items-center gap-3 border-b px-5">
<span className="inline-flex size-10 items-center justify-center rounded-md bg-primary text-primary-foreground">
@@ -61,15 +62,18 @@ export function AppShell({
</span>
<AppBreadcrumbs />
</div>
<div className="flex min-w-0 items-center gap-2 xl:hidden">
<div className="hidden min-w-0 items-center gap-2 sm:flex">
<div className="flex min-w-0 items-center gap-2">
<ThemeToggle />
<div className="hidden min-w-0 items-center gap-2 sm:flex xl:hidden">
<p className="min-w-0 max-w-[min(52vw,24rem)] truncate whitespace-nowrap text-right text-sm leading-tight">
<span className="font-medium">{account.name}</span>
<span className="ml-2 text-muted-foreground">{account.email}</span>
</p>
<UserAvatar avatarUrl={account.avatarUrl} name={account.name} size="sm" />
</div>
<SignOutButton />
<div className="xl:hidden">
<SignOutButton />
</div>
</div>
</header>

View File

@@ -0,0 +1,91 @@
"use client";
import { Moon, Monitor, Sun } from "lucide-react";
import { useEffect, useState } from "react";
import { cn } from "@/lib/utils";
type ThemeMode = "light" | "dark" | "system";
const storageKey = "teatea-theme";
const themeOptions: Array<{
mode: ThemeMode;
label: string;
icon: React.ComponentType<{ className?: string; "aria-hidden"?: boolean }>;
}> = [
{ mode: "light", label: "浅色主题", icon: Sun },
{ mode: "dark", label: "深色主题", icon: Moon },
{ mode: "system", label: "跟随系统", icon: Monitor },
];
function getStoredTheme(): ThemeMode {
if (typeof window === "undefined") {
return "system";
}
const storedTheme = window.localStorage.getItem(storageKey);
return storedTheme === "light" || storedTheme === "dark" || storedTheme === "system" ? storedTheme : "system";
}
function applyTheme(mode: ThemeMode): void {
const prefersDark = window.matchMedia("(prefers-color-scheme: dark)").matches;
document.documentElement.classList.toggle("dark", mode === "dark" || (mode === "system" && prefersDark));
document.documentElement.style.colorScheme = mode === "dark" || (mode === "system" && prefersDark) ? "dark" : "light";
}
export function ThemeToggle(): React.ReactElement {
const [theme, setTheme] = useState<ThemeMode>("system");
useEffect(() => {
const storedTheme = getStoredTheme();
setTheme(storedTheme);
applyTheme(storedTheme);
const media = window.matchMedia("(prefers-color-scheme: dark)");
const handleChange = (): void => {
if (getStoredTheme() === "system") {
applyTheme("system");
}
};
media.addEventListener("change", handleChange);
return () => media.removeEventListener("change", handleChange);
}, []);
function handleThemeChange(nextTheme: ThemeMode): void {
window.localStorage.setItem(storageKey, nextTheme);
setTheme(nextTheme);
applyTheme(nextTheme);
}
return (
<div
aria-label="主题切换"
className="inline-flex h-10 shrink-0 items-center rounded-md border bg-card p-1 shadow-sm"
role="group"
>
{themeOptions.map((option) => {
const Icon = option.icon;
const isSelected = theme === option.mode;
return (
<button
aria-label={option.label}
aria-pressed={isSelected}
className={cn(
"inline-flex size-8 items-center justify-center rounded-sm text-muted-foreground transition-colors hover:bg-secondary hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background",
isSelected && "bg-primary text-primary-foreground hover:bg-primary hover:text-primary-foreground",
)}
key={option.mode}
onClick={() => handleThemeChange(option.mode)}
title={option.label}
type="button"
>
<Icon className="size-4" aria-hidden={true} />
</button>
);
})}
</div>
);
}