|
| 1 | +import React from "react"; |
| 2 | +import { View, Text, Pressable } from "react-native"; |
| 3 | +import { useTranslation } from "react-i18next"; |
| 4 | +import { Check, Sun, Moon, SmartphoneNfc } from "lucide-react-native"; |
| 5 | +import * as Haptics from "expo-haptics"; |
| 6 | +import { useUIStore, type ThemeMode } from "~/stores/ui-store"; |
| 7 | +import { cn } from "~/lib/utils"; |
| 8 | + |
| 9 | +const OPTIONS: { mode: ThemeMode; icon: typeof Sun; labelKey: string }[] = [ |
| 10 | + { mode: "light", icon: Sun, labelKey: "appearance.light" }, |
| 11 | + { mode: "dark", icon: Moon, labelKey: "appearance.dark" }, |
| 12 | + { mode: "system", icon: SmartphoneNfc, labelKey: "appearance.system" }, |
| 13 | +]; |
| 14 | + |
| 15 | +/** |
| 16 | + * Theme/appearance selector — light / dark / system. Mirrors LanguageSelector; |
| 17 | + * writes through `useUIStore.setTheme`, which applies the NativeWind color |
| 18 | + * scheme live and persists the choice. |
| 19 | + */ |
| 20 | +export function ThemeSelector({ className }: { className?: string }) { |
| 21 | + const { t } = useTranslation(); |
| 22 | + const theme = useUIStore((s) => s.theme); |
| 23 | + const setTheme = useUIStore((s) => s.setTheme); |
| 24 | + |
| 25 | + return ( |
| 26 | + <View className={cn("gap-1", className)}> |
| 27 | + <Text className="mb-2 text-sm font-medium text-muted-foreground"> |
| 28 | + {t("appearance.title")} |
| 29 | + </Text> |
| 30 | + {OPTIONS.map(({ mode, icon: Icon, labelKey }) => { |
| 31 | + const isActive = theme === mode; |
| 32 | + return ( |
| 33 | + <Pressable |
| 34 | + key={mode} |
| 35 | + accessibilityRole="button" |
| 36 | + accessibilityState={{ selected: isActive }} |
| 37 | + accessibilityLabel={t(labelKey)} |
| 38 | + className={cn( |
| 39 | + "flex-row items-center justify-between rounded-lg px-4 py-3 active:opacity-70", |
| 40 | + isActive ? "bg-primary/10" : "bg-card", |
| 41 | + )} |
| 42 | + onPress={() => { |
| 43 | + if (!isActive) { |
| 44 | + void Haptics.selectionAsync(); |
| 45 | + setTheme(mode); |
| 46 | + } |
| 47 | + }} |
| 48 | + > |
| 49 | + <View className="flex-row items-center gap-3"> |
| 50 | + <Icon size={20} color={isActive ? "#1e40af" : "#64748b"} /> |
| 51 | + <Text |
| 52 | + className={cn( |
| 53 | + "text-base", |
| 54 | + isActive ? "font-semibold text-primary" : "text-foreground", |
| 55 | + )} |
| 56 | + > |
| 57 | + {t(labelKey)} |
| 58 | + </Text> |
| 59 | + </View> |
| 60 | + {isActive && <Check size={18} color="#1e40af" />} |
| 61 | + </Pressable> |
| 62 | + ); |
| 63 | + })} |
| 64 | + </View> |
| 65 | + ); |
| 66 | +} |
0 commit comments