Add PMP name colors

This commit is contained in:
Daisy committed 2026-07-28 21:10:04 +01:00
1 parent 7e26e8147c
commit e935f8aa8e
15 files changed
+203 -25

No files matched your search

+5
View File
@@ -0,0 +1,5 @@
---
default: minor
---
# Add persona name colors
+13 -2
View File
@@ -11,6 +11,7 @@ type UserAvatarProps = {
userId: string;
src?: string;
alt?: string;
fallbackColor?: string;
renderFallback: () => ReactNode;
};
@@ -18,7 +19,14 @@ const handleImageLoad: ReactEventHandler<HTMLImageElement> = (evt) => {
evt.currentTarget.setAttribute('data-image-loaded', 'true');
};
export function UserAvatar({ className, userId, src, alt, renderFallback }: UserAvatarProps) {
export function UserAvatar({
className,
userId,
src,
alt,
fallbackColor,
renderFallback,
}: UserAvatarProps) {
const [error, setError] = useState(false);
const resolvedSrc = useRenderableMediaUrl(src);
@@ -29,7 +37,10 @@ export function UserAvatar({ className, userId, src, alt, renderFallback }: User
if (!src || error) {
return (
<AvatarFallback
style={{ backgroundColor: colorMXID(userId), color: color.Surface.Container }}
style={{
backgroundColor: fallbackColor ?? colorMXID(userId),
color: color.Surface.Container,
}}
className={classNames(css.UserAvatar, className)}
>
{renderFallback()}
+16 -2
View File
@@ -131,6 +131,7 @@ export type MessageProps = {
};
import { useMenuAnchor } from '$hooks/useMenuAnchor';
import { ThemeKind, useActiveTheme } from '$hooks/useTheme';
const clamp = (str: string, len: number) => (str.length > len ? `${str.slice(0, len)}...` : str);
@@ -367,6 +368,8 @@ function MessageInternal(
const messageRef = useRef<HTMLDivElement>(null);
useImperativeHandle(ref, () => messageRef.current as HTMLDivElement);
const [isVisible, setIsVisible] = useState(() => typeof IntersectionObserver === 'undefined');
const activeTheme = useActiveTheme();
const [renderPersonaColors] = useSetting(settingsAtom, 'renderPersonaColors');
useEffect(() => {
const element = messageRef.current;
@@ -450,6 +453,14 @@ function MessageInternal(
return convertBeeperFormatToOurPerMessageProfile(pmp);
}, [pmp]);
const pmpNameColor = useMemo(() => {
if (!renderPersonaColors) return undefined;
const pmpNameColorLight = parsedPMPContent?.nameColorLightTheme;
const pmpNameColorDark = parsedPMPContent?.nameColorDarkTheme;
return activeTheme.kind === ThemeKind.Dark ? pmpNameColorDark : pmpNameColorLight;
}, [parsedPMPContent, activeTheme, renderPersonaColors]);
/**
* boolean to indicate wheather we should indicate to the user that it is a pmp
* We want to not show it, when the name is unset, or whitespace only
@@ -574,7 +585,7 @@ function MessageInternal(
<Username
as="button"
style={{
color: usernameColor,
color: pmpNameColor ?? usernameColor,
fontFamily: usernameFont,
}}
data-user-id={senderId}
@@ -590,7 +601,10 @@ function MessageInternal(
</Text>
</Username>
{showPronouns && (
<Pronouns pronouns={mergedPronouns} tagColor={usernameColor ?? 'currentColor'} />
<Pronouns
pronouns={mergedPronouns}
tagColor={pmpNameColor ?? usernameColor ?? 'currentColor'}
/>
)}
{showPmPInfo && (
<Box>
@@ -37,6 +37,7 @@ import { useCallback, useEffect, useRef, useState, type FormEvent } from 'react'
import * as css from './PersonaPicker.css.ts';
import { InfoCard } from '$components/info-card/InfoCard.tsx';
import { InfoIcon } from '@phosphor-icons/react';
import { ThemeKind, useActiveTheme } from '$hooks/useTheme.ts';
const pillStyles = {
cursor: 'pointer',
@@ -63,6 +64,7 @@ export function PersonaPicker({
onTabChange,
}: PersonaPickerProps) {
const useAuthentication = useMediaAuthentication();
const activeTheme = useActiveTheme();
const [AddPersonaMenuAnchor, setAddPersonaMenuAnchor] = useState<RectCords>();
const [profiles, setProfiles] = useState<PerMessageProfile[] | undefined>(undefined);
const [selectedGlobalPersona, setSelectedGlobalPersona] = useState<PerMessageProfile | null>(
@@ -75,6 +77,14 @@ export function PersonaPicker({
return persona.id === selectedPersona?.id ? true : undefined;
};
const nameColor = useCallback(
(persona: PerMessageProfile) =>
activeTheme.kind === ThemeKind.Dark
? persona.nameColorDarkTheme
: persona.nameColorLightTheme,
[activeTheme]
);
const defactoPersona = () => selectedRoomPersona ?? selectedGlobalPersona;
const searchInputRef = useRef<HTMLInputElement>(null);
@@ -256,6 +266,7 @@ export function PersonaPicker({
<UserAvatar
userId={profile.id}
src={avatarUrl(profile)}
fallbackColor={profile.nameColorLightTheme ?? undefined}
renderFallback={() => (
<Text as="span" size="H4" aria-label="Avatar fallback">
{nameInitials(profile.name)}
@@ -266,7 +277,10 @@ export function PersonaPicker({
</Avatar>
}
>
<Text truncate style={{ maxWidth: toRem(150) }}>
<Text
truncate
style={{ color: nameColor(profile) ?? undefined, maxWidth: toRem(150) }}
>
{profile.name}
</Text>
</MenuItem>
@@ -28,6 +28,7 @@ import { parsePronounsStringToPronounsSetArray } from '$utils/pronouns';
import { generateShortId } from '$utils/shortIdGen';
import { SettingTile } from '$components/setting-tile';
import { type AsyncState, AsyncStatus, useAsyncCallback } from '$hooks/useAsyncCallback';
import { NameColorEditor } from '../account/NameColorEditor';
const constructProxyString = (s: Shorthand) => {
return `${s.prefix ?? ''}text${s.suffix ?? ''}`;
@@ -319,6 +320,8 @@ type PerMessageProfileEditorProps = {
avatarMxcUrl?: string;
displayName?: string;
pronouns?: PronounSet[];
nameColorLightTheme?: string;
nameColorDarkTheme?: string;
shorthands?: Shorthand[];
onDelete?: (profileId: string) => void;
};
@@ -329,6 +332,8 @@ export function PerMessageProfileEditor({
avatarMxcUrl,
displayName,
pronouns = Array<PronounSet>(),
nameColorLightTheme,
nameColorDarkTheme,
onDelete,
}: Readonly<PerMessageProfileEditorProps>) {
const useAuthentication = useMediaAuthentication();
@@ -353,6 +358,12 @@ export function PerMessageProfileEditor({
return pronounsString;
});
// Name color
const [currentNameColorLight, setCurrentNameColorLight] = useState(nameColorLightTheme ?? null);
const [newNameColorLight, setNewNameColorLight] = useState(nameColorLightTheme ?? null);
const [currentNameColorDark, setCurrentNameColorDark] = useState(nameColorDarkTheme ?? null);
const [newNameColorDark, setNewNameColorDark] = useState(nameColorDarkTheme ?? null);
const [newDisplayName, setNewDisplayName] = useState(currentDisplayName);
const [imageFile, setImageFile] = useState<File | undefined>();
const [imageHasChanges, setImageHasChanges] = useState(false);
@@ -396,6 +407,8 @@ export function PerMessageProfileEditor({
() =>
newDisplayName !== (currentDisplayName ?? '') ||
newPronounsString !== currentPronounsString ||
newNameColorLight !== currentNameColorLight ||
newNameColorDark !== currentNameColorDark ||
hasIdChange ||
imageHasChanges,
[
@@ -403,6 +416,10 @@ export function PerMessageProfileEditor({
currentDisplayName,
newPronounsString,
currentPronounsString,
newNameColorLight,
currentNameColorLight,
newNameColorDark,
currentNameColorDark,
hasIdChange,
imageHasChanges,
]
@@ -433,10 +450,14 @@ export function PerMessageProfileEditor({
name: newDisplayName,
avatarUrl: avatarMxc,
pronouns: newPronouns,
nameColorLightTheme: newNameColorLight ?? undefined,
nameColorDarkTheme: newNameColorDark ?? undefined,
});
setCurrentDisplayName(newDisplayName);
setCurrentPronouns(newPronouns);
setCurrentNameColorLight(newNameColorLight);
setCurrentNameColorDark(newNameColorDark);
setImageHasChanges(false);
setChangingDisplayName(false);
setDisableSetDisplayname(false);
@@ -445,7 +466,17 @@ export function PerMessageProfileEditor({
setCurrentId(newId);
});
}
}, [mx, profileId, newDisplayName, avatarMxc, newPronouns, hasIdChange, newId])
}, [
mx,
profileId,
newDisplayName,
avatarMxc,
newPronouns,
newNameColorLight,
newNameColorDark,
hasIdChange,
newId,
])
);
const [deleteState, handleDelete] = useAsyncCallback(
@@ -649,6 +680,35 @@ export function PerMessageProfileEditor({
}
></SettingTile>
</SequenceCard>
<SequenceCard
className={SequenceCardStyle}
variant="SurfaceVariant"
direction="Column"
gap="400"
>
<NameColorEditor
title="Dark theme Name Color"
description="This persona's name color for a dark theme user."
focusId={`nameColorDarkTheme-${profileId}`}
current={currentNameColorDark ?? undefined}
onChange={setNewNameColorDark}
/>
</SequenceCard>
<SequenceCard
className={SequenceCardStyle}
variant="SurfaceVariant"
direction="Column"
gap="400"
>
<NameColorEditor
title="Light theme Name Color"
description="This persona's name color for a light theme user."
focusId={`nameColorLightTheme-${profileId}`}
current={currentNameColorLight ?? undefined}
onChange={setNewNameColorLight}
/>
</SequenceCard>
<Box
direction="Row"
alignItems="Center"
@@ -11,6 +11,8 @@ type PerMessageProfileEditorViewProps = {
avatarMxcUrl?: string;
displayName?: string;
pronouns?: PronounSet[];
nameColorLightTheme?: string;
nameColorDarkTheme?: string;
onChange?: (profile: { id: string; name: string; avatarUrl?: string }) => void;
onDelete?: (profileId: string) => void;
requestClose: () => void;
@@ -21,6 +23,8 @@ export function PerMessageProfileEditorView({
avatarMxcUrl,
displayName,
pronouns = Array<PronounSet>(),
nameColorLightTheme,
nameColorDarkTheme,
requestClose,
}: Readonly<PerMessageProfileEditorViewProps>) {
return (
@@ -53,6 +57,8 @@ export function PerMessageProfileEditorView({
avatarMxcUrl={avatarMxcUrl}
displayName={displayName}
pronouns={pronouns}
nameColorLightTheme={nameColorLightTheme}
nameColorDarkTheme={nameColorDarkTheme}
onDelete={requestClose}
/>
</PageContent>
@@ -9,6 +9,7 @@ import type { PronounSet } from '$utils/pronouns';
import { SettingTile } from '$components/setting-tile';
import { toSettingsFocusIdPart } from '../settingsLink';
import { Pronouns } from '$features/room/message';
import { ThemeKind, useActiveTheme } from '$hooks/useTheme';
/**
* the props we use for the per-message profile list item, which is used to display a per-message profile. This is used in the settings page when the user wants to view all of their profiles.
@@ -18,6 +19,8 @@ type PerMessageProfileListItemProps = {
profileId: string;
avatarMxcUrl?: string;
displayName?: string;
nameColorLight?: string;
nameColorDark?: string;
pronouns?: PronounSet[];
onOpenEditor: (profileId: string) => void;
};
@@ -28,9 +31,16 @@ export function PerMessageProfileListItem({
avatarMxcUrl,
displayName,
pronouns = Array<PronounSet>(),
nameColorLight,
nameColorDark,
onOpenEditor,
}: Readonly<PerMessageProfileListItemProps>) {
const useAuthentication = useMediaAuthentication();
const activeTheme = useActiveTheme();
const nameColor = useMemo(
() => (activeTheme.kind === ThemeKind.Dark ? nameColorDark : nameColorLight),
[activeTheme, nameColorLight, nameColorDark]
);
const avatarUrl = useMemo(() => {
if (avatarMxcUrl) {
@@ -46,9 +56,9 @@ export function PerMessageProfileListItem({
focusId={`persona-${toSettingsFocusIdPart(profileId)}`}
showSettingLinkAction={false}
title={
<Text>
<Text style={{ color: nameColor ?? undefined }}>
{displayName ?? 'Unnamed Profile'}
<Pronouns pronouns={pronouns} tagColor={color.Primary.Main} />
<Pronouns pronouns={pronouns} tagColor={nameColor ?? color.Primary.Main} />
</Text>
}
description={profileId}
@@ -69,6 +79,7 @@ export function PerMessageProfileListItem({
<UserAvatar
userId={profileId}
src={avatarUrl}
fallbackColor={nameColorLight}
renderFallback={() => (
<Box>
<Text size="H4" aria-label="Avatar fallback">
@@ -96,6 +96,8 @@ export function PerMessageProfileOverview({
avatarMxcUrl={profile.avatarUrl}
displayName={profile.name}
pronouns={profile.pronouns}
nameColorLight={profile.nameColorLightTheme}
nameColorDark={profile.nameColorDarkTheme}
onOpenEditor={handleEdit}
/>
</SequenceCard>
@@ -29,6 +29,8 @@ export function PerMessageProfilePage({ requestBack, requestClose }: PerMessageP
avatarMxcUrl={editingProfile.avatarUrl}
displayName={editingProfile.name}
pronouns={editingProfile.pronouns}
nameColorLightTheme={editingProfile.nameColorLightTheme}
nameColorDarkTheme={editingProfile.nameColorDarkTheme}
requestClose={handleEditorClose}
/>
);
@@ -1,5 +1,5 @@
import { useState, useEffect } from 'react';
import { Box, Button, config, Text, Input, IconButton } from 'folds';
import { Box, Button, config, color, Text, Input, IconButton } from 'folds';
import { menuIcon, X } from '$components/icons/phosphor';
import { HexColorPicker } from 'react-colorful';
import { SettingTile } from '$components/setting-tile';
@@ -10,7 +10,8 @@ type NameColorEditorProps = {
description?: string;
focusId?: string;
current?: string;
onSave: (color: string | null) => void;
onChange?: (color: string | null) => void;
onSave?: (color: string | null) => void;
disabled?: boolean;
};
@@ -19,44 +20,62 @@ const stripQuotes = (str?: string) => {
// to solve the silly tuwunel
return str.replaceAll(/^["']|["']$/g, '');
};
const colorPattern = /^#[0-9A-F]{6}$/i;
export function NameColorEditor({
title,
description,
focusId,
current,
onChange,
onSave,
disabled,
}: Readonly<NameColorEditorProps>) {
const [tempColor, setTempColor] = useState(stripQuotes(current) || '#FFFFFF');
const [tempColor, setTempColor] = useState(stripQuotes(current) || undefined);
const [hasChanged, setHasChanged] = useState(false);
useEffect(() => {
const sanitized = stripQuotes(current);
if (sanitized) setTempColor(sanitized);
else setTempColor('#FFFFFF');
else setTempColor(undefined);
}, [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.
// We should only present an error if they fail the regex test :)
// (Also this change makes the fallback "undefined" instead of #fff so this is required)
const validInput = (testColor: string | undefined) => {
if (!testColor) return true;
return colorPattern.test(testColor);
};
const handleUpdate = (newColor: string) => {
let sanitized = stripQuotes(newColor);
sanitized = sanitized.startsWith('#') ? sanitized : `#${sanitized}`;
setTempColor(sanitized);
const currentSanitized = stripQuotes(current) || '#FFFFFF';
const currentSanitized = stripQuotes(current) || '#ffffff';
setHasChanged(sanitized.toUpperCase() !== currentSanitized.toUpperCase());
};
const handleSave = () => {
if (/^#[0-9A-F]{6}$/i.test(tempColor)) {
if (!!onSave && !!tempColor && colorPattern.test(tempColor)) {
onSave(tempColor);
setHasChanged(false);
}
};
const handleReset = () => {
onSave(null);
if (onChange) onChange(null);
if (onSave) onSave(null);
setHasChanged(false);
setTempColor('#FFFFFF');
setTempColor(undefined);
};
useEffect(() => {
const validColor = tempColor !== undefined && colorPattern.test(tempColor);
if (onChange) onChange(validColor ? tempColor : null);
}, [onChange, tempColor]);
return (
<Box direction="Column" gap="100">
<SettingTile
@@ -70,24 +89,24 @@ export function NameColorEditor({
gap="300"
style={{
padding: config.space.S100,
backgroundColor: 'var(--sable-surface-container)',
backgroundColor: color.Surface.Container,
borderRadius: config.radii.R400,
}}
>
<Box alignItems="Center" gap="300" grow="Yes">
{hasChanged && (
{onSave && hasChanged && (
<Button
variant="Primary"
size="300"
radii="Pill"
onClick={handleSave}
disabled={!/^#[0-9A-F]{6}$/i.test(tempColor)}
disabled={!tempColor || !colorPattern.test(tempColor)}
>
<Text size="B300">Save</Text>
</Button>
)}
<HexColorPickerPopOut
picker={<HexColorPicker color={tempColor} onChange={handleUpdate} />}
picker={<HexColorPicker color={tempColor ?? '#FFFFFF'} onChange={handleUpdate} />}
>
{(onOpen, opened) => (
<Button
@@ -107,7 +126,7 @@ export function NameColorEditor({
width: '32px',
height: '32px',
borderRadius: '50%',
backgroundColor: tempColor,
backgroundColor: tempColor ?? '#FFFFFF',
boxShadow: 'inset 0 0 0 1px rgba(0,0,0,0.1)',
}}
/>
@@ -117,10 +136,10 @@ export function NameColorEditor({
<Box direction="Row" alignItems="Center" gap="100">
<Input
value={tempColor}
value={tempColor ?? ''}
onChange={(e) => handleUpdate(e.currentTarget.value)}
placeholder="#FFFFFF"
variant="Background"
variant={validInput(tempColor) ? 'Background' : 'Critical'}
size="300"
radii="300"
disabled={disabled ?? false}
@@ -130,7 +149,7 @@ export function NameColorEditor({
width: '100px',
}}
/>
{current && (
{tempColor && (
<IconButton
variant="Secondary"
size="300"
@@ -412,6 +412,10 @@ function IdentityCosmetics() {
'renderGlobalNameColors'
);
const [renderRoomColors, setRenderRoomColors] = useSetting(settingsAtom, 'renderRoomColors');
const [renderPersonaColors, setRenderPersonaColors] = useSetting(
settingsAtom,
'renderPersonaColors'
);
const [renderRoomFonts, setRenderRoomFonts] = useSetting(settingsAtom, 'renderRoomFonts');
const [uniformIcons, setUniformIcons] = useSetting(settingsAtom, 'uniformIcons');
@@ -487,6 +491,13 @@ function IdentityCosmetics() {
value={renderRoomColors}
onChange={setRenderRoomColors}
/>
<SettingToggle
title="Render Persona Username Colors"
focusId="render-persona-username-colors"
description="Display the username colors that can be set on personas."
value={renderPersonaColors}
onChange={setRenderPersonaColors}
/>
<SettingToggle
title="Render Space/Room Fonts"
focusId="render-space-room-fonts"
@@ -149,6 +149,7 @@ export const settingsLinkFocusIdsBySection: Record<SettingsSectionId, readonly s
'render-global-username-colors',
'render-space-room-fonts',
'render-space-room-username-colors',
'render-persona-username-colors',
'saturation',
'selected-language-for-pronouns',
'show-easter-eggs',
+16 -1
View File
@@ -3,7 +3,10 @@ import type { AccountDataCompatVersion } from '$types/matrix/accountData';
import type { PronounSet } from '$utils/pronouns';
import type { MatrixClient } from '$types/matrix-sdk';
import { CustomAccountDataEvent } from '$types/matrix/accountData';
import { MATRIX_UNSTABLE_PROFILE_PRONOUNS_PROPERTY_NAME } from '$unstable/prefixes';
import {
MATRIX_UNSTABLE_PROFILE_NAME_COLOR_PROPERTY_NAME,
MATRIX_UNSTABLE_PROFILE_PRONOUNS_PROPERTY_NAME,
} from '$unstable/prefixes';
const ACCOUNT_DATA_PREFIX = CustomAccountDataEvent.SablePerProfileMessageProfiles;
@@ -31,6 +34,10 @@ export type PerMessageProfile = {
*/
pronouns?: PronounSet[];
compat?: AccountDataCompatVersion;
/* name colors */
nameColorLightTheme?: string;
nameColorDarkTheme?: string;
};
/**
@@ -55,6 +62,8 @@ export type PerMessageProfileBeeperFormat = {
* using the unstable prefix for pronouns, under which it is also stored in profiles
*/
[MATRIX_UNSTABLE_PROFILE_PRONOUNS_PROPERTY_NAME]?: PronounSet[];
[MATRIX_UNSTABLE_PROFILE_NAME_COLOR_PROPERTY_NAME]?: { light?: string; dark?: string };
has_fallback?: boolean;
};
@@ -74,6 +83,10 @@ export function convertPerMessageProfileToBeeperFormat(
displayname: profile.name,
avatar_url: profile.avatarUrl,
[MATRIX_UNSTABLE_PROFILE_PRONOUNS_PROPERTY_NAME]: profile.pronouns,
[MATRIX_UNSTABLE_PROFILE_NAME_COLOR_PROPERTY_NAME]: {
light: profile.nameColorLightTheme,
dark: profile.nameColorDarkTheme,
},
has_fallback,
};
// delete empty fields
@@ -102,6 +115,8 @@ export function convertBeeperFormatToOurPerMessageProfile(
name: beeperProfile.displayname ?? '',
avatarUrl: beeperProfile.avatar_url,
pronouns: beeperProfile[MATRIX_UNSTABLE_PROFILE_PRONOUNS_PROPERTY_NAME],
nameColorLightTheme: beeperProfile[MATRIX_UNSTABLE_PROFILE_NAME_COLOR_PROPERTY_NAME]?.light,
nameColorDarkTheme: beeperProfile[MATRIX_UNSTABLE_PROFILE_NAME_COLOR_PROPERTY_NAME]?.dark,
};
}
+2
View File
@@ -201,6 +201,7 @@ export interface Settings {
filterPronounsLanguages?: string[];
renderRoomColors: boolean;
renderRoomFonts: boolean;
renderPersonaColors: boolean;
captionPosition: CaptionPosition;
customDMCards: boolean;
@@ -380,6 +381,7 @@ export const defaultSettings: Settings = {
renderUserCards: 'both',
renderRoomColors: true,
renderRoomFonts: true,
renderPersonaColors: true,
captionPosition: CaptionPosition.Below,
customDMCards: true,
+5
View File
@@ -6,6 +6,11 @@
*/
export const MATRIX_UNSTABLE_PROFILE_PRONOUNS_PROPERTY_NAME = 'io.fsky.nyx.pronouns';
/**
* unstable prefix for profile name colors
*/
export const MATRIX_UNSTABLE_PROFILE_NAME_COLOR_PROPERTY_NAME = 'net.f0rest.name_color';
/**
* unstable prefix for profile biography
* defined in MSC4440 https://github.com/matrix-org/matrix-spec-proposals/pull/4440