forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits

theme-mode-control.tsx67 lines, 4.9 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-10 w-10 items-center justify-center rounded-md text-muted-foreground outline-hidden transition-colors hover:bg-accent hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring sm:h-8 sm:w-8 touch:h-10 touch:w-10"
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 // Icon-only, so each button carries its name; a thumb-sized target on
33 // touch screens.
34 return (
35 <div class="flex rounded-lg bg-muted p-0.5 dark:bg-white/8" role="group" aria-label={prefs.t("theme.toggle")}>
36 <button type="button" class={cn("rounded-md p-3 text-muted-foreground outline-hidden transition-colors hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring sm:p-1.5 touch:p-3.5 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()} aria-label={lightLabel()} title={lightLabel()}>
37 <IconSun class="h-4 w-4" />
38 </button>
39 <button type="button" class={cn("rounded-md p-3 text-muted-foreground outline-hidden transition-colors hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring sm:p-1.5 touch:p-3.5 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()} aria-label={darkLabel()} title={darkLabel()}>
40 <IconMoon class="h-4 w-4" />
41 </button>
42 </div>
43 );
44 }
45
46 if (props.variant === "segmented") {
47 return (
48 <div class="flex w-full gap-0.5 rounded-lg bg-muted p-0.5" role="group" aria-label={prefs.t("theme.toggle")}>
49 <button type="button" aria-pressed={!isDark()} onClick={() => selectTheme("light")} class={cn("h-10 flex-1 rounded-md px-3 text-sm font-medium sm:h-8 touch:h-10 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>
50 <button type="button" aria-pressed={isDark()} onClick={() => selectTheme("dark")} class={cn("h-10 flex-1 rounded-md px-3 text-sm font-medium sm:h-8 touch:h-10 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>
51 </div>
52 );
53 }
54
55 return (
56 <div class="grid grid-cols-2 gap-2" role="group" aria-label={prefs.t("theme.toggle")}>
57 <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-transparent bg-primary bg-clip-padding text-primary-foreground shadow-xs" : "border-border bg-background text-foreground hover:bg-accent hover:text-accent-foreground")} onClick={() => selectTheme("light")}>
58 <span class="flex h-7 w-7 items-center justify-center"><IconSun class="h-5 w-5" /></span>
59 <span class="leading-tight">{lightLabel()}</span>
60 </button>
61 <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-transparent bg-primary bg-clip-padding text-primary-foreground shadow-xs" : "border-border bg-background text-foreground hover:bg-accent hover:text-accent-foreground")} onClick={() => selectTheme("dark")}>
62 <span class="flex h-7 w-7 items-center justify-center"><IconMoon class="h-5 w-5" /></span>
63 <span class="leading-tight">{darkLabel()}</span>
64 </button>
65 </div>
66 );
67}