forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits

theme-mode-control.tsx65 lines, 4.4 KiBHistoryRawDownload
1import { IconMoon, IconSun } from "@/components/ui/icons";
2import { cn } from "@/lib/cn";
3import { usePreferences } from "@/stores/preferences-context";
4
5type ThemeModeControlProps = {
6 variant: "toggle" | "compact" | "segmented" | "cards";
7};
8
9/** Shared theme control with layouts suited to each navigation surface. */
10export function ThemeModeControl(props: ThemeModeControlProps) {
11 const prefs = usePreferences();
12 const isDark = () => prefs.theme() === "dark";
13 const selectTheme = (theme: "light" | "dark") => prefs.setTheme(theme);
14 const lightLabel = () => prefs.t("theme.light");
15 const darkLabel = () => prefs.t("theme.dark");
16
17 if (props.variant === "toggle") {
18 return (
19 <button
20 type="button"
21 class="flex h-8 w-8 items-center justify-center rounded-md text-muted-foreground transition-colors hover:bg-accent hover:text-foreground"
22 aria-label={prefs.t("theme.toggle")}
23 title={isDark() ? lightLabel() : darkLabel()}
24 onClick={() => selectTheme(isDark() ? "light" : "dark")}
25 >
26 {isDark() ? <IconSun class="h-4 w-4" /> : <IconMoon class="h-4 w-4" />}
27 </button>
28 );
29 }
30
31 if (props.variant === "compact") {
32 return (
33 <div class="flex rounded-lg bg-muted p-0.5 dark:bg-white/8" role="group" aria-label={prefs.t("theme.toggle")}>
34 <button type="button" class={cn("rounded-md p-1.5 text-muted-foreground transition-colors hover:text-foreground dark:text-white/60 dark:hover:text-white", !isDark() && "bg-background text-foreground shadow-xs dark:bg-white/12 dark:text-white")} onClick={() => selectTheme("light")} aria-pressed={!isDark()} title={lightLabel()}>
35 <IconSun class="h-4 w-4" />
36 </button>
37 <button type="button" class={cn("rounded-md p-1.5 text-muted-foreground transition-colors hover:text-foreground dark:text-white/60 dark:hover:text-white", isDark() && "bg-background text-foreground shadow-xs dark:bg-white/12 dark:text-white")} onClick={() => selectTheme("dark")} aria-pressed={isDark()} title={darkLabel()}>
38 <IconMoon class="h-4 w-4" />
39 </button>
40 </div>
41 );
42 }
43
44 if (props.variant === "segmented") {
45 return (
46 <div class="flex w-full gap-0.5 rounded-lg bg-muted p-0.5" role="group" aria-label={prefs.t("theme.toggle")}>
47 <button type="button" aria-pressed={!isDark()} onClick={() => selectTheme("light")} class={cn("flex-1 rounded-md px-3 py-1.5 text-sm font-medium transition-colors outline-hidden focus-visible:ring-2 focus-visible:ring-ring", !isDark() ? "bg-background text-foreground shadow-xs" : "text-muted-foreground hover:text-foreground")}>{lightLabel()}</button>
48 <button type="button" aria-pressed={isDark()} onClick={() => selectTheme("dark")} class={cn("flex-1 rounded-md px-3 py-1.5 text-sm font-medium transition-colors outline-hidden focus-visible:ring-2 focus-visible:ring-ring", isDark() ? "bg-background text-foreground shadow-xs" : "text-muted-foreground hover:text-foreground")}>{darkLabel()}</button>
49 </div>
50 );
51 }
52
53 return (
54 <div class="grid grid-cols-2 gap-2" role="group" aria-label={prefs.t("theme.toggle")}>
55 <button type="button" role="menuitemradio" aria-checked={!isDark()} class={cn("flex min-h-16 w-full flex-col items-center justify-center gap-1.5 rounded-md border px-2 py-2.5 text-center text-xs font-medium transition-colors outline-hidden focus-visible:ring-2 focus-visible:ring-ring", !isDark() ? "border-primary bg-primary text-primary-foreground shadow-xs" : "border-border bg-background text-foreground hover:bg-accent hover:text-accent-foreground")} onClick={() => selectTheme("light")}>
56 <span class="flex h-7 w-7 items-center justify-center"><IconSun class="h-5 w-5" /></span>
57 <span class="leading-tight">{lightLabel()}</span>
58 </button>
59 <button type="button" role="menuitemradio" aria-checked={isDark()} class={cn("flex min-h-16 w-full flex-col items-center justify-center gap-1.5 rounded-md border px-2 py-2.5 text-center text-xs font-medium transition-colors outline-hidden focus-visible:ring-2 focus-visible:ring-ring", isDark() ? "border-primary bg-primary text-primary-foreground shadow-xs" : "border-border bg-background text-foreground hover:bg-accent hover:text-accent-foreground")} onClick={() => selectTheme("dark")}>
60 <span class="flex h-7 w-7 items-center justify-center"><IconMoon class="h-5 w-5" /></span>
61 <span class="leading-tight">{darkLabel()}</span>
62 </button>
63 </div>
64 );
65}