|
| 1 | +import { RotateCcw, Sun, Moon, Monitor } from "lucide-react"; |
| 2 | +import { useTranslation } from "react-i18next"; |
| 3 | +import { useAppearanceStore } from "@/stores/appearance-store"; |
| 4 | +import { THEME_PRESETS } from "@/lib/theme-utils"; |
| 5 | +import { Button } from "@/components/ui/button"; |
| 6 | +import { Card } from "@/components/ui/card"; |
| 7 | +import { Input } from "@/components/ui/input"; |
| 8 | +import type { ThemeMode } from "@/stores/appearance-store"; |
| 9 | + |
| 10 | +function ThemeModeToggle() { |
| 11 | + const { t } = useTranslation(); |
| 12 | + const themeMode = useAppearanceStore((s) => s.themeMode); |
| 13 | + const setThemeMode = useAppearanceStore((s) => s.setThemeMode); |
| 14 | + |
| 15 | + const modes: { value: ThemeMode; label: string; icon: React.ReactNode }[] = [ |
| 16 | + { value: "light", label: t("settings.appearance.light"), icon: <Sun size={14} /> }, |
| 17 | + { value: "dark", label: t("settings.appearance.dark"), icon: <Moon size={14} /> }, |
| 18 | + { value: "system", label: t("settings.appearance.system"), icon: <Monitor size={14} /> }, |
| 19 | + ]; |
| 20 | + |
| 21 | + return ( |
| 22 | + <div className="flex rounded-lg border border-border overflow-hidden"> |
| 23 | + {modes.map((m) => ( |
| 24 | + <button |
| 25 | + key={m.value} |
| 26 | + onClick={() => setThemeMode(m.value)} |
| 27 | + className={`flex-1 flex items-center justify-center gap-1.5 px-3 py-2 text-xs font-medium transition-colors ${ |
| 28 | + themeMode === m.value |
| 29 | + ? "bg-primary text-primary-foreground" |
| 30 | + : "bg-card text-muted-foreground hover:text-foreground hover:bg-secondary" |
| 31 | + }`} |
| 32 | + > |
| 33 | + {m.icon} |
| 34 | + {m.label} |
| 35 | + </button> |
| 36 | + ))} |
| 37 | + </div> |
| 38 | + ); |
| 39 | +} |
| 40 | + |
| 41 | +function PresetGrid() { |
| 42 | + const { t } = useTranslation(); |
| 43 | + const activePresetId = useAppearanceStore((s) => s.activePresetId); |
| 44 | + const applyPreset = useAppearanceStore((s) => s.applyPreset); |
| 45 | + |
| 46 | + return ( |
| 47 | + <div className="grid grid-cols-2 gap-2"> |
| 48 | + {THEME_PRESETS.map((preset) => { |
| 49 | + const isActive = activePresetId === preset.id; |
| 50 | + return ( |
| 51 | + <button |
| 52 | + key={preset.id} |
| 53 | + onClick={() => applyPreset(preset.id)} |
| 54 | + className={`flex items-center gap-3 p-3 rounded-lg border-2 text-left transition-all ${ |
| 55 | + isActive |
| 56 | + ? "border-primary bg-primary/5" |
| 57 | + : "border-border bg-card hover:border-muted-foreground/30 hover:bg-secondary" |
| 58 | + }`} |
| 59 | + > |
| 60 | + {/* Color swatches */} |
| 61 | + <div className="flex flex-col gap-0.5 shrink-0"> |
| 62 | + <div className="w-5 h-5 rounded-full border border-border" style={{ backgroundColor: preset.accent }} /> |
| 63 | + <div className="flex gap-0.5"> |
| 64 | + <div className="w-2.5 h-2.5 rounded-sm border border-border/50" style={{ backgroundColor: preset.background }} /> |
| 65 | + <div className="w-2.5 h-2.5 rounded-sm border border-border/50" style={{ backgroundColor: preset.foreground }} /> |
| 66 | + </div> |
| 67 | + </div> |
| 68 | + <span className="text-xs font-medium text-foreground"> |
| 69 | + {t(preset.name)} |
| 70 | + </span> |
| 71 | + </button> |
| 72 | + ); |
| 73 | + })} |
| 74 | + </div> |
| 75 | + ); |
| 76 | +} |
| 77 | + |
| 78 | +function ColorRow({ |
| 79 | + label, |
| 80 | + value, |
| 81 | + onChange, |
| 82 | +}: { |
| 83 | + label: string; |
| 84 | + value: string; |
| 85 | + onChange: (hex: string) => void; |
| 86 | +}) { |
| 87 | + return ( |
| 88 | + <div className="flex items-center justify-between gap-4"> |
| 89 | + <span className="text-sm text-foreground">{label}</span> |
| 90 | + <div className="flex items-center gap-2"> |
| 91 | + <div className="relative"> |
| 92 | + <div |
| 93 | + className="w-8 h-8 rounded-full border border-border cursor-pointer" |
| 94 | + style={{ backgroundColor: value }} |
| 95 | + /> |
| 96 | + <input |
| 97 | + type="color" |
| 98 | + value={value} |
| 99 | + onChange={(e) => onChange(e.target.value)} |
| 100 | + className="absolute inset-0 opacity-0 cursor-pointer w-full h-full" |
| 101 | + /> |
| 102 | + </div> |
| 103 | + <Input |
| 104 | + value={value.toUpperCase()} |
| 105 | + onChange={(e) => { |
| 106 | + const v = e.target.value; |
| 107 | + if (/^#[0-9a-f]{6}$/i.test(v)) { |
| 108 | + onChange(v); |
| 109 | + } |
| 110 | + }} |
| 111 | + className="w-24 h-8 text-xs font-mono bg-background" |
| 112 | + /> |
| 113 | + </div> |
| 114 | + </div> |
| 115 | + ); |
| 116 | +} |
| 117 | + |
| 118 | +function ContrastSlider() { |
| 119 | + const { t } = useTranslation(); |
| 120 | + const contrast = useAppearanceStore((s) => s.contrast); |
| 121 | + const setContrast = useAppearanceStore((s) => s.setContrast); |
| 122 | + |
| 123 | + return ( |
| 124 | + <div> |
| 125 | + <div className="flex items-center justify-between mb-2"> |
| 126 | + <span className="text-sm text-foreground">{t("settings.appearance.contrast")}</span> |
| 127 | + <span className="text-xs text-muted-foreground font-mono w-8 text-right">{contrast}</span> |
| 128 | + </div> |
| 129 | + <input |
| 130 | + type="range" |
| 131 | + min={0} |
| 132 | + max={100} |
| 133 | + value={contrast} |
| 134 | + onChange={(e) => setContrast(Number(e.target.value))} |
| 135 | + className="w-full appearance-slider" |
| 136 | + /> |
| 137 | + <p className="text-[10px] text-muted-foreground mt-1"> |
| 138 | + {t("settings.appearance.contrastDesc")} |
| 139 | + </p> |
| 140 | + </div> |
| 141 | + ); |
| 142 | +} |
| 143 | + |
| 144 | +export function AppearanceSection() { |
| 145 | + const { t } = useTranslation(); |
| 146 | + const accentColor = useAppearanceStore((s) => s.accentColor); |
| 147 | + const backgroundColor = useAppearanceStore((s) => s.backgroundColor); |
| 148 | + const foregroundColor = useAppearanceStore((s) => s.foregroundColor); |
| 149 | + const setAccentColor = useAppearanceStore((s) => s.setAccentColor); |
| 150 | + const setBackgroundColor = useAppearanceStore((s) => s.setBackgroundColor); |
| 151 | + const setForegroundColor = useAppearanceStore((s) => s.setForegroundColor); |
| 152 | + const resetToDefault = useAppearanceStore((s) => s.resetToDefault); |
| 153 | + |
| 154 | + return ( |
| 155 | + <div className="max-w-2xl mx-auto p-6"> |
| 156 | + <h1 className="text-lg font-semibold text-foreground mb-6"> |
| 157 | + {t("settings.appearance.title")} |
| 158 | + </h1> |
| 159 | + |
| 160 | + <div className="space-y-6"> |
| 161 | + {/* Theme Mode */} |
| 162 | + <section> |
| 163 | + <h2 className="text-sm font-medium text-muted-foreground mb-3"> |
| 164 | + {t("settings.appearance.themeMode")} |
| 165 | + </h2> |
| 166 | + <ThemeModeToggle /> |
| 167 | + </section> |
| 168 | + |
| 169 | + {/* Presets */} |
| 170 | + <section> |
| 171 | + <h2 className="text-sm font-medium text-muted-foreground mb-3"> |
| 172 | + {t("settings.appearance.presets")} |
| 173 | + </h2> |
| 174 | + <PresetGrid /> |
| 175 | + </section> |
| 176 | + |
| 177 | + {/* Colors */} |
| 178 | + <section> |
| 179 | + <h2 className="text-sm font-medium text-muted-foreground mb-3"> |
| 180 | + {t("settings.appearance.colors")} |
| 181 | + </h2> |
| 182 | + <Card className="p-4 space-y-4"> |
| 183 | + <ColorRow |
| 184 | + label={t("settings.appearance.accentColor")} |
| 185 | + value={accentColor} |
| 186 | + onChange={setAccentColor} |
| 187 | + /> |
| 188 | + <ColorRow |
| 189 | + label={t("settings.appearance.backgroundColor")} |
| 190 | + value={backgroundColor} |
| 191 | + onChange={setBackgroundColor} |
| 192 | + /> |
| 193 | + <ColorRow |
| 194 | + label={t("settings.appearance.foregroundColor")} |
| 195 | + value={foregroundColor} |
| 196 | + onChange={setForegroundColor} |
| 197 | + /> |
| 198 | + </Card> |
| 199 | + </section> |
| 200 | + |
| 201 | + {/* Contrast */} |
| 202 | + <section> |
| 203 | + <ContrastSlider /> |
| 204 | + </section> |
| 205 | + |
| 206 | + {/* Reset */} |
| 207 | + <section> |
| 208 | + <Button variant="outline" size="sm" onClick={resetToDefault}> |
| 209 | + <RotateCcw size={12} /> |
| 210 | + {t("settings.appearance.resetToDefault")} |
| 211 | + </Button> |
| 212 | + </section> |
| 213 | + </div> |
| 214 | + </div> |
| 215 | + ); |
| 216 | +} |
0 commit comments