|
| 1 | +/** @jsxImportSource react */ |
| 2 | +import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group"; |
| 3 | +import { cn } from "@/lib/utils"; |
| 4 | +import type { ReactNode } from "react"; |
| 5 | +import { t } from "../../../../i18n"; |
| 6 | +import type { AppearanceViewProps } from "../pages/appearance-view"; |
| 7 | +import { |
| 8 | + SettingsSection, |
| 9 | + SettingsSectionHeader, |
| 10 | + SettingsSectionHeaderContent, |
| 11 | + SettingsSectionHeaderDescription, |
| 12 | + SettingsSectionHeaderTitle, |
| 13 | +} from "../settings-section"; |
| 14 | + |
| 15 | +type ThemeMode = AppearanceViewProps["themeMode"]; |
| 16 | + |
| 17 | +interface ThemeSectionProps |
| 18 | + extends Pick<AppearanceViewProps, "busy" | "themeMode" | "setThemeMode"> {} |
| 19 | + |
| 20 | +export function ThemeSection(props: ThemeSectionProps) { |
| 21 | + return ( |
| 22 | + <SettingsSection className="items-center"> |
| 23 | + <SettingsSectionHeader className="w-full"> |
| 24 | + <SettingsSectionHeaderContent> |
| 25 | + <SettingsSectionHeaderTitle>{t("settings.appearance_title")}</SettingsSectionHeaderTitle> |
| 26 | + <SettingsSectionHeaderDescription>{t("settings.appearance_hint")}</SettingsSectionHeaderDescription> |
| 27 | + </SettingsSectionHeaderContent> |
| 28 | + </SettingsSectionHeader> |
| 29 | + |
| 30 | + <ThemePicker |
| 31 | + className="pt-1" |
| 32 | + busy={props.busy} |
| 33 | + themeMode={props.themeMode} |
| 34 | + setThemeMode={props.setThemeMode} |
| 35 | + /> |
| 36 | + |
| 37 | + <div className="text-xs text-muted-foreground">{t("settings.theme_system_hint")}</div> |
| 38 | + </SettingsSection> |
| 39 | + ); |
| 40 | +} |
| 41 | + |
| 42 | +interface ThemePickerProps { |
| 43 | + className?: string; |
| 44 | + busy: boolean; |
| 45 | + themeMode: ThemeMode; |
| 46 | + setThemeMode: (value: ThemeMode) => void; |
| 47 | +} |
| 48 | + |
| 49 | +function ThemePicker(props: ThemePickerProps) { |
| 50 | + return ( |
| 51 | + <ToggleGroup |
| 52 | + value={[props.themeMode]} |
| 53 | + onValueChange={(value) => { |
| 54 | + if (value[0] === null) { |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + props.setThemeMode(value[0] as ThemeMode); |
| 59 | + }} |
| 60 | + disabled={props.busy} |
| 61 | + className={cn("w-full gap-6 max-w-xl", props.className)} |
| 62 | + > |
| 63 | + <ThemePickerItem |
| 64 | + value="system" |
| 65 | + label={t("settings.theme_system")} |
| 66 | + > |
| 67 | + <ThemePreview value="system" /> |
| 68 | + <ThemePickerLabel>{t("settings.theme_system")}</ThemePickerLabel> |
| 69 | + </ThemePickerItem> |
| 70 | + <ThemePickerItem |
| 71 | + value="light" |
| 72 | + label={t("settings.theme_light")} |
| 73 | + > |
| 74 | + <ThemePreview value="light" className="bg-white" /> |
| 75 | + <ThemePickerLabel>{t("settings.theme_light")}</ThemePickerLabel> |
| 76 | + </ThemePickerItem> |
| 77 | + <ThemePickerItem |
| 78 | + value="dark" |
| 79 | + label={t("settings.theme_dark")} |
| 80 | + > |
| 81 | + <ThemePreview value="dark" className="bg-black" /> |
| 82 | + <ThemePickerLabel>{t("settings.theme_dark")}</ThemePickerLabel> |
| 83 | + </ThemePickerItem> |
| 84 | + </ToggleGroup> |
| 85 | + ); |
| 86 | +} |
| 87 | + |
| 88 | +interface ThemePickerItemProps { |
| 89 | + value: ThemeMode; |
| 90 | + label: string; |
| 91 | + children: ReactNode; |
| 92 | +} |
| 93 | + |
| 94 | +function ThemePickerItem(props: ThemePickerItemProps) { |
| 95 | + return ( |
| 96 | + <ToggleGroupItem |
| 97 | + value={props.value} |
| 98 | + aria-label={props.label} |
| 99 | + className="group/theme h-auto flex-1 flex-col gap-3 rounded-sm px-0 py-0 hover:bg-transparent aria-pressed:bg-transparent" |
| 100 | + > |
| 101 | + {props.children} |
| 102 | + </ToggleGroupItem> |
| 103 | + ); |
| 104 | +} |
| 105 | + |
| 106 | +interface ThemePreviewProps { |
| 107 | + value: ThemeMode; |
| 108 | + className?: string; |
| 109 | +} |
| 110 | + |
| 111 | +function ThemePreview(props: ThemePreviewProps) { |
| 112 | + return ( |
| 113 | + <div |
| 114 | + className={cn( |
| 115 | + "aspect-4/3 w-full overflow-hidden rounded-md border transition-shadow group-data-pressed/theme:ring-2 group-data-pressed/theme:ring-primary group-data-pressed/theme:ring-offset-2 group-data-pressed/theme:ring-offset-background group-hover/theme:ring-1 group-hover/theme:ring-primary/40 group-hover/theme:ring-offset-2 group-hover/theme:ring-offset-background", |
| 116 | + props.className, |
| 117 | + )} |
| 118 | + > |
| 119 | + {props.value === "system" && ( |
| 120 | + <div className="flex h-full"> |
| 121 | + <div className="w-1/2 bg-white" /> |
| 122 | + <div className="w-1/2 bg-black" /> |
| 123 | + </div> |
| 124 | + )} |
| 125 | + </div> |
| 126 | + ); |
| 127 | +} |
| 128 | + |
| 129 | +interface ThemePickerLabelProps { |
| 130 | + children: string; |
| 131 | +} |
| 132 | + |
| 133 | +function ThemePickerLabel(props: ThemePickerLabelProps) { |
| 134 | + return ( |
| 135 | + <span className="text-sm text-muted-foreground group-data-pressed/theme:text-foreground"> |
| 136 | + {props.children} |
| 137 | + </span> |
| 138 | + ); |
| 139 | +} |
0 commit comments