|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// Copyright 2026 Universität Osnabrück (virtUOS) |
| 3 | + |
| 4 | +/** Shared preference controls for the -bar tools: language and appearance, |
| 5 | + * either as rows inside the app's own account menu (`LanguageOptions` + |
| 6 | + * `AppearanceControl`) or, for signed-out visitors, as a round popover |
| 7 | + * button (`PreferencesMenu`). */ |
| 8 | +import { Check, Monitor, Moon, SlidersHorizontal, Sun } from "lucide-react"; |
| 9 | +import { useEffect, useRef, useState } from "react"; |
| 10 | +import { useTranslation } from "react-i18next"; |
| 11 | +import { SUPPORTED_LANGUAGES } from "./i18n"; |
| 12 | +import { useTheme, type Appearance } from "./theme"; |
| 13 | + |
| 14 | +/** Auto / Light / Dark setting. Auto follows the OS; an explicit pick |
| 15 | + * overrides it. Full-width radio rows (icon + label + check on the active |
| 16 | + * option) to match the menu vocabulary. */ |
| 17 | +export function AppearanceControl() { |
| 18 | + const { t } = useTranslation(); |
| 19 | + const { appearance, setAppearance } = useTheme(); |
| 20 | + const options: { |
| 21 | + value: Appearance; |
| 22 | + label: string; |
| 23 | + hint?: string; |
| 24 | + icon: typeof Sun; |
| 25 | + }[] = [ |
| 26 | + { value: "auto", label: t("Auto"), hint: t("(follows your system)"), icon: Monitor }, |
| 27 | + { value: "light", label: t("Light"), icon: Sun }, |
| 28 | + { value: "dark", label: t("Dark"), icon: Moon }, |
| 29 | + ]; |
| 30 | + return ( |
| 31 | + <div role="radiogroup" aria-label={t("Appearance")}> |
| 32 | + <p className="px-3 pb-0.5 pt-1 text-xs text-slate-400 dark:text-slate-300"> |
| 33 | + {t("Appearance")} |
| 34 | + </p> |
| 35 | + {options.map((opt) => { |
| 36 | + const active = appearance === opt.value; |
| 37 | + return ( |
| 38 | + <button |
| 39 | + key={opt.value} |
| 40 | + type="button" |
| 41 | + role="radio" |
| 42 | + aria-checked={active} |
| 43 | + onClick={() => setAppearance(opt.value)} |
| 44 | + className="flex w-full items-center gap-2 px-3 py-2 text-left text-sm text-slate-700 hover:bg-slate-50 dark:text-slate-200 dark:hover:bg-slate-700" |
| 45 | + > |
| 46 | + <opt.icon aria-hidden className="h-4 w-4 text-slate-400" /> |
| 47 | + <span className="flex-1"> |
| 48 | + {opt.label} |
| 49 | + {opt.hint && ( |
| 50 | + <span className="block text-xs text-slate-400 dark:text-slate-300"> |
| 51 | + {opt.hint} |
| 52 | + </span> |
| 53 | + )} |
| 54 | + </span> |
| 55 | + {active && <Check aria-hidden className="h-4 w-4 text-brand-600" />} |
| 56 | + </button> |
| 57 | + ); |
| 58 | + })} |
| 59 | + </div> |
| 60 | + ); |
| 61 | +} |
| 62 | + |
| 63 | +/** Language options as `menuitemradio` rows. There is deliberately no "Auto" |
| 64 | + * entry: without a manual pick the detector follows the system and the |
| 65 | + * resolved language is the one marked; a pick is cached by i18next (the |
| 66 | + * detector's cache) and is therefore binding from then on. `onChange` lets |
| 67 | + * the app persist the choice server-side; `onPicked` lets a menu close. */ |
| 68 | +export function LanguageOptions({ |
| 69 | + onChange, |
| 70 | + onPicked, |
| 71 | +}: { |
| 72 | + onChange?: (lang: string) => void; |
| 73 | + onPicked?: () => void; |
| 74 | +}) { |
| 75 | + const { i18n } = useTranslation(); |
| 76 | + const resolved = i18n.resolvedLanguage ?? i18n.language; |
| 77 | + const current = SUPPORTED_LANGUAGES.some((l) => l.code === resolved) |
| 78 | + ? resolved |
| 79 | + : SUPPORTED_LANGUAGES[0]?.code; |
| 80 | + return ( |
| 81 | + <> |
| 82 | + {SUPPORTED_LANGUAGES.map((lang) => ( |
| 83 | + <button |
| 84 | + key={lang.code} |
| 85 | + type="button" |
| 86 | + role="menuitemradio" |
| 87 | + aria-checked={lang.code === current} |
| 88 | + onClick={() => { |
| 89 | + i18n.changeLanguage(lang.code); |
| 90 | + onChange?.(lang.code); |
| 91 | + onPicked?.(); |
| 92 | + }} |
| 93 | + className="flex w-full items-center justify-between px-3 py-2.5 text-left text-sm text-slate-700 hover:bg-slate-50 dark:text-slate-200 dark:hover:bg-slate-700" |
| 94 | + > |
| 95 | + {lang.label} |
| 96 | + {lang.code === current && ( |
| 97 | + <Check aria-hidden className="h-4 w-4 text-brand-600" /> |
| 98 | + )} |
| 99 | + </button> |
| 100 | + ))} |
| 101 | + </> |
| 102 | + ); |
| 103 | +} |
| 104 | + |
| 105 | +/** Round "preferences" button with a popover holding language + appearance — |
| 106 | + * for signed-out visitors (signed-in users get the same rows in their |
| 107 | + * account menu). */ |
| 108 | +export function PreferencesMenu({ |
| 109 | + onLanguageChange, |
| 110 | +}: { |
| 111 | + onLanguageChange?: (lang: string) => void; |
| 112 | +}) { |
| 113 | + const { t } = useTranslation(); |
| 114 | + const [open, setOpen] = useState(false); |
| 115 | + const ref = useRef<HTMLDivElement>(null); |
| 116 | + const triggerRef = useRef<HTMLButtonElement>(null); |
| 117 | + |
| 118 | + useEffect(() => { |
| 119 | + if (!open) return; |
| 120 | + function onPointerDown(event: PointerEvent) { |
| 121 | + if (ref.current && !ref.current.contains(event.target as Node)) { |
| 122 | + setOpen(false); |
| 123 | + } |
| 124 | + } |
| 125 | + function onKeyDown(event: KeyboardEvent) { |
| 126 | + if (event.key === "Escape") { |
| 127 | + setOpen(false); |
| 128 | + triggerRef.current?.focus(); |
| 129 | + } |
| 130 | + } |
| 131 | + document.addEventListener("pointerdown", onPointerDown); |
| 132 | + document.addEventListener("keydown", onKeyDown); |
| 133 | + return () => { |
| 134 | + document.removeEventListener("pointerdown", onPointerDown); |
| 135 | + document.removeEventListener("keydown", onKeyDown); |
| 136 | + }; |
| 137 | + }, [open]); |
| 138 | + |
| 139 | + function closeAndFocus() { |
| 140 | + setOpen(false); |
| 141 | + triggerRef.current?.focus(); |
| 142 | + } |
| 143 | + |
| 144 | + return ( |
| 145 | + <div className="relative" ref={ref}> |
| 146 | + <button |
| 147 | + ref={triggerRef} |
| 148 | + type="button" |
| 149 | + onClick={() => setOpen((o) => !o)} |
| 150 | + aria-haspopup="menu" |
| 151 | + aria-expanded={open} |
| 152 | + aria-label={t("Preferences")} |
| 153 | + className="flex h-9 w-9 items-center justify-center rounded-full sm:h-10 sm:w-10 text-slate-600 transition-colors duration-150 hover:bg-slate-100 hover:text-slate-900 dark:text-slate-300 dark:hover:bg-slate-800 dark:hover:text-slate-100" |
| 154 | + > |
| 155 | + <SlidersHorizontal aria-hidden className="h-5 w-5" /> |
| 156 | + </button> |
| 157 | + {open && ( |
| 158 | + <div |
| 159 | + role="menu" |
| 160 | + className="absolute right-0 z-30 mt-2 w-56 animate-fade-up overflow-hidden rounded-xl border border-slate-200 bg-white py-1 shadow-lg shadow-slate-900/5 dark:border-slate-700 dark:bg-slate-800" |
| 161 | + > |
| 162 | + <p className="px-3 pb-0.5 pt-1 text-xs text-slate-400 dark:text-slate-300"> |
| 163 | + {t("Language")} |
| 164 | + </p> |
| 165 | + <LanguageOptions onChange={onLanguageChange} onPicked={closeAndFocus} /> |
| 166 | + <div className="my-1 border-t border-slate-100 dark:border-slate-700" /> |
| 167 | + <AppearanceControl /> |
| 168 | + </div> |
| 169 | + )} |
| 170 | + </div> |
| 171 | + ); |
| 172 | +} |
0 commit comments