From c40df64e2dd8184db830f3c093bdb60779c08dda Mon Sep 17 00:00:00 2001 From: Josie Date: Fri, 7 Aug 2026 09:47:37 +0100 Subject: [PATCH] Suggest accessible light/dark name colors when not filled --- .../Persona/PerMessageProfileEditor.tsx | 30 ++++++++++ .../settings/account/NameColorEditor.tsx | 16 ++++- src/app/features/settings/account/Profile.tsx | 60 +++++++++++++++---- src/app/utils/common.ts | 4 ++ 4 files changed, 95 insertions(+), 15 deletions(-) diff --git a/src/app/features/settings/Persona/PerMessageProfileEditor.tsx b/src/app/features/settings/Persona/PerMessageProfileEditor.tsx index 9677ec821..0e2fb8ba3 100644 --- a/src/app/features/settings/Persona/PerMessageProfileEditor.tsx +++ b/src/app/features/settings/Persona/PerMessageProfileEditor.tsx @@ -29,6 +29,8 @@ import { MATRIX_UNSTABLE_COLORS, MATRIX_UNSTABLE_PROFILE_PRONOUNS_PROPERTY_NAME, } from '$unstable/prefixes'; +import { accessibleColor } from '$plugins/color'; +import { ThemeKind } from '$hooks/useTheme'; type Shorthand = { prefix?: string; suffix?: string }; type ShorthandRow = Shorthand & { id: string }; @@ -603,8 +605,22 @@ export function PerMessageProfileEditor({ description="This persona's name color for a dark theme user." focusId={`nameColorDarkTheme-${profileId}`} current={currentNameColorDark ?? undefined} + newNameColor={newNameColorDark ?? undefined} onChange={setNewNameColorDark} /> + {!newNameColorLight && newNameColorDark && ( + + + + )} + {!newNameColorDark && newNameColorLight && ( + + + + )} void; onSave?: (color: string | null) => void; disabled?: boolean; @@ -27,18 +28,27 @@ export function NameColorEditor({ description, focusId, current, + newNameColor, onChange, onSave, disabled, }: Readonly) { - const [tempColor, setTempColor] = useState(stripQuotes(current) || undefined); + const [tempColor, setTempColor] = useState( + stripQuotes(newNameColor) || stripQuotes(current) || undefined + ); const [hasChanged, setHasChanged] = useState(false); useEffect(() => { - const sanitized = stripQuotes(current); + // this condition stops infinite loops on input clear when onChange is changing newColor in outer scope + if (!newNameColor && onChange) return; + const sanitized = stripQuotes(newNameColor || current); if (sanitized) setTempColor(sanitized); else setTempColor(undefined); - }, [current]); + }, [current, newNameColor, onChange]); + + useEffect(() => { + setHasChanged(newNameColor !== current); + }, [newNameColor, current]); // Valid in the sense that it should look like an error during input. // It's disruptive to the user to immediately present their input as an error. diff --git a/src/app/features/settings/account/Profile.tsx b/src/app/features/settings/account/Profile.tsx index 652691c69..3b89d2e62 100644 --- a/src/app/features/settings/account/Profile.tsx +++ b/src/app/features/settings/account/Profile.tsx @@ -13,7 +13,7 @@ import { useUserProfile } from '$hooks/useUserProfile'; import { getMxIdLocalPart, mxcUrlToHttp } from '$utils/matrix'; import { UserAvatar } from '$components/user-avatar'; import { useMediaAuthentication } from '$hooks/useMediaAuthentication'; -import { nameInitials } from '$utils/common'; +import { nameInitials, xor } from '$utils/common'; import { AsyncStatus, useAsyncCallback } from '$hooks/useAsyncCallback'; import { useFilePicker } from '$hooks/useFilePicker'; import { useObjectURL } from '$hooks/useObjectURL'; @@ -40,6 +40,8 @@ import { AnimalCosmetics } from './AnimalCosmetics'; import * as prefix from '$unstable/prefixes'; import { confirm } from '$components/confirm/confirm'; import { AvatarUploadTile } from '$components/avatar-upload-tile/AvatarUploadTile'; +import { accessibleColor } from '$plugins/color'; +import { ThemeKind } from '$hooks/useTheme'; type PronounSet = { summary: string; @@ -426,21 +428,25 @@ function ProfileExtended({ profile, userId }: Readonly) { [mx, presence] ); - const color_on_dark = + const colorOnDark = profile.nameColors?.on_dark ?? - (profile.extended?.[prefix.MATRIX_UNSTABLE_COLORS] as ColorSet | undefined)?.on_dark; - const color_on_light = + (profile.extended?.[prefix.MATRIX_UNSTABLE_COLORS] as ColorSet | undefined)?.on_dark ?? + null; + const colorOnLight = profile.nameColors?.on_light ?? - (profile.extended?.[prefix.MATRIX_UNSTABLE_COLORS] as ColorSet | undefined)?.on_light; + (profile.extended?.[prefix.MATRIX_UNSTABLE_COLORS] as ColorSet | undefined)?.on_light ?? + null; + const [newColorOnDark, setNewColorOnDark] = useState(null); + const [newColorOnLight, setNewColorOnLight] = useState(null); // Deletes the depricated key, the color specific ones should be depricated too at a later date if (profile.nameColor || (profile.extended?.[prefix.MATRIX_UNSTABLE_COLORS] as string)) { const fallback = profile.nameColor ?? (profile.extended?.[prefix.MATRIX_UNSTABLE_COLORS] as string); - if (!color_on_light || !color_on_dark) + if (!newColorOnLight || !newColorOnDark) handleSaveField(prefix.MATRIX_UNSTABLE_COLORS, { - on_dark: color_on_dark ?? fallback, - on_light: color_on_light ?? fallback, + on_dark: newColorOnDark ?? fallback, + on_light: newColorOnLight ?? fallback, }); handleSaveField(prefix.MATRIX_SABLE_UNSTABLE_NAME_COLOR_PROPERTY_NAME, null); } @@ -466,11 +472,13 @@ function ProfileExtended({ profile, userId }: Readonly) { title="Dark theme Global Name Color" description="Your name's color for a dark theme user." focusId="name-color-dark-theme" - current={color_on_dark} + current={colorOnDark ?? undefined} + newNameColor={newColorOnDark ?? undefined} + onChange={setNewColorOnDark} onSave={(color) => handleSaveField(prefix.MATRIX_UNSTABLE_COLORS, { on_dark: color, - on_light: color_on_light, + on_light: newColorOnLight, }) } /> @@ -478,14 +486,42 @@ function ProfileExtended({ profile, userId }: Readonly) { title="Light theme Global Name Color" description="Your name's color for a light theme user." focusId="name-color-light-theme" - current={color_on_light} + current={colorOnLight ?? undefined} + newNameColor={newColorOnLight ?? undefined} + onChange={setNewColorOnLight} onSave={(color) => handleSaveField(prefix.MATRIX_UNSTABLE_COLORS, { - on_dark: color_on_dark, + on_dark: newColorOnDark, on_light: color, }) } /> + {xor(newColorOnDark, newColorOnLight) && ( + + {!newColorOnDark && newColorOnLight && ( + + )} + {newColorOnDark && !newColorOnLight && ( + + )} + + )} { if (trimmedContent === '') return []; return trimmedContent.split(' '); }; + +export const xor = (left: unknown, right: unknown): boolean => { + return !left !== !right; +};