mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-21 17:41:25 -08:00
Suggest accessible light/dark name colors when not filled
This commit is contained in:
1 parent
2e2f20c010
commit
c40df64e2d
4 files changed
+95
-15
No files matched your search
@@ -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 && (
|
||||
<Box direction="Column" alignItems="End">
|
||||
<Button
|
||||
size="300"
|
||||
fill="Soft"
|
||||
onClick={() => {
|
||||
setNewNameColorLight(accessibleColor(ThemeKind.Light, newNameColorDark));
|
||||
}}
|
||||
>
|
||||
<Text size="T200">Create new color from dark theme</Text>
|
||||
</Button>
|
||||
</Box>
|
||||
)}
|
||||
</SequenceCard>
|
||||
<SequenceCard
|
||||
className={SequenceCardStyle}
|
||||
@@ -617,8 +633,22 @@ export function PerMessageProfileEditor({
|
||||
description="This persona's name color for a light theme user."
|
||||
focusId={`nameColorLightTheme-${profileId}`}
|
||||
current={currentNameColorLight ?? undefined}
|
||||
newNameColor={newNameColorLight ?? undefined}
|
||||
onChange={setNewNameColorLight}
|
||||
/>
|
||||
{!newNameColorDark && newNameColorLight && (
|
||||
<Box direction="Column" alignItems="End">
|
||||
<Button
|
||||
size="300"
|
||||
fill="Soft"
|
||||
onClick={() => {
|
||||
setNewNameColorDark(accessibleColor(ThemeKind.Dark, newNameColorLight));
|
||||
}}
|
||||
>
|
||||
<Text size="T200">Create new color from light theme</Text>
|
||||
</Button>
|
||||
</Box>
|
||||
)}
|
||||
</SequenceCard>
|
||||
<Box
|
||||
direction="Row"
|
||||
|
||||
@@ -11,6 +11,7 @@ type NameColorEditorProps = {
|
||||
description?: string;
|
||||
focusId?: string;
|
||||
current?: string;
|
||||
newNameColor?: string;
|
||||
onChange?: (color: string | null) => void;
|
||||
onSave?: (color: string | null) => void;
|
||||
disabled?: boolean;
|
||||
@@ -27,18 +28,27 @@ export function NameColorEditor({
|
||||
description,
|
||||
focusId,
|
||||
current,
|
||||
newNameColor,
|
||||
onChange,
|
||||
onSave,
|
||||
disabled,
|
||||
}: Readonly<NameColorEditorProps>) {
|
||||
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.
|
||||
|
||||
@@ -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<ProfileProps>) {
|
||||
[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<string | null>(null);
|
||||
const [newColorOnLight, setNewColorOnLight] = useState<string | null>(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<ProfileProps>) {
|
||||
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<ProfileProps>) {
|
||||
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) && (
|
||||
<Box direction="Column" alignItems="End">
|
||||
{!newColorOnDark && newColorOnLight && (
|
||||
<Button
|
||||
size="300"
|
||||
fill="Soft"
|
||||
onClick={() => {
|
||||
setNewColorOnDark(accessibleColor(ThemeKind.Dark, newColorOnLight));
|
||||
}}
|
||||
>
|
||||
<Text size="T200">Create new color from light theme</Text>
|
||||
</Button>
|
||||
)}
|
||||
{newColorOnDark && !newColorOnLight && (
|
||||
<Button
|
||||
size="300"
|
||||
fill="Soft"
|
||||
onClick={() => {
|
||||
setNewColorOnLight(accessibleColor(ThemeKind.Light, newColorOnDark));
|
||||
}}
|
||||
>
|
||||
<Text size="T200">Create new color from dark theme</Text>
|
||||
</Button>
|
||||
)}
|
||||
</Box>
|
||||
)}
|
||||
</SequenceCard>
|
||||
<SequenceCard
|
||||
className={SequenceCardStyle}
|
||||
|
||||
@@ -151,3 +151,7 @@ export const splitWithSpace = (content: string): string[] => {
|
||||
if (trimmedContent === '') return [];
|
||||
return trimmedContent.split(' ');
|
||||
};
|
||||
|
||||
export const xor = (left: unknown, right: unknown): boolean => {
|
||||
return !left !== !right;
|
||||
};
|
||||
Reference in new issue
Block a user