Add accessibility tab and accessible color correction (#1664)

<!-- Please read
https://github.com/SableClient/Sable/blob/dev/CONTRIBUTING.md before
submitting your pull request -->

### Description

<!-- Please include a summary of the change. Please also include
relevant motivation and context. List any dependencies that are required
for this change. -->

Adds a new tab, which takes some of the settings from appearance. Also
adds 'name color contrast correction', which can fix name colors which
are not readable enough for light/dark backgrounds.

#### Type of change

- [ ] Bug fix (non-breaking change which fixes an issue)
- [x] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to not work as expected)
- [ ] This change requires a documentation update

### Checklist:

- [x] My code follows the style guidelines of this project
- [x] I have performed a self-review of my own code
- [x] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings

### AI disclosure:

- [ ] Partially AI assisted (clarify which code was AI assisted and
briefly explain what it does).
- [ ] Fully AI generated (explain what all the generated code does in
moderate detail).
<!-- Write any explanation required here, but do not generate the
explanation using AI!! You must prove you understand what the code in
this PR does. -->
This commit is contained in:
Erwan Leboucher authored and GitHub committed 2026-08-07 11:14:01 +00:00
commit 079ac6ee5a
12 files changed
+303 -105

No files matched your search

@@ -0,0 +1,5 @@
---
default: minor
---
# Add accessibility tab and accessible color correction
+25 -5
View File
@@ -68,6 +68,7 @@ import {
convertBeeperFormatToOurPerMessageProfile,
stripPerMessageProfileFormattedBody,
} from '$hooks/usePerMessageProfile';
import { accessibleColor, accessibleColorWeakCorrection } from '$plugins/color';
const ROOM_REPLY_TIMELINE_EVENT_TYPES = new Set<string>([
EventType.RoomMessage as string,
@@ -296,10 +297,29 @@ export const Reply = as<'div', ReplyProps>(
const { color: usernameColor, font: usernameFont } = useSableCosmetics(sender ?? '', room);
const activeTheme = useActiveTheme();
const pmpNameColor =
activeTheme.kind === ThemeKind.Dark
? pmp?.['eu.she-a.color']?.on_dark
: pmp?.['eu.she-a.color']?.on_light;
const [nameColorLightnessCorrection] = useSetting(settingsAtom, 'nameColorLightnessCorrection');
const accessibleNameColor = useCallback(
(nameColor: string | undefined) => {
const colorCorrection =
nameColorLightnessCorrection === 'strong'
? accessibleColor
: nameColorLightnessCorrection === 'weak'
? accessibleColorWeakCorrection
: () => nameColor;
return nameColor ? colorCorrection(activeTheme.kind, nameColor) : nameColor;
},
[nameColorLightnessCorrection, activeTheme]
);
const pmpNameColor = useMemo(() => {
return accessibleNameColor(
activeTheme.kind === ThemeKind.Dark
? pmp?.['eu.she-a.color']?.on_dark
: pmp?.['eu.she-a.color']?.on_light
);
}, [activeTheme, pmp, accessibleNameColor]);
const nicknames = useAtomValue(nicknamesAtom);
const cachedProfiles = useAtomValue(profilesCacheAtom);
useRoomMemberHydration(room, sender ?? '');
@@ -512,7 +532,7 @@ export const Reply = as<'div', ReplyProps>(
)}
<ReplyLayout
as="button"
userColor={pmpNameColor ?? usernameColor}
userColor={accessibleNameColor(pmpNameColor) ?? usernameColor}
icon={image}
replyIcon={replyIcon}
mentioned={mentioned}
+19 -2
View File
@@ -134,6 +134,7 @@ export type MessageProps = {
import { useMenuAnchor } from '$hooks/useMenuAnchor';
import { ThemeKind, useActiveTheme } from '$hooks/useTheme';
import { shouldIgnoreMessageLongPress } from './messageTouch';
import { accessibleColor, accessibleColorWeakCorrection } from '$plugins/color';
const clamp = (str: string, len: number) => (str.length > len ? `${str.slice(0, len)}...` : str);
@@ -373,6 +374,7 @@ function MessageInternal(
const [isVisible, setIsVisible] = useState(() => typeof IntersectionObserver === 'undefined');
const activeTheme = useActiveTheme();
const [renderPersonaColors] = useSetting(settingsAtom, 'renderPersonaColors');
const [nameColorLightnessCorrection] = useSetting(settingsAtom, 'nameColorLightnessCorrection');
useEffect(() => {
const element = messageRef.current;
@@ -477,6 +479,21 @@ function MessageInternal(
false,
isVisible
);
const accessibleNameColor = useCallback(
(color: string | undefined) => {
if (!color || nameColorLightnessCorrection === 'off') {
return color;
} else if (nameColorLightnessCorrection === 'strong') {
return accessibleColor(activeTheme.kind, color);
} else {
/* weak color correction */
return accessibleColorWeakCorrection(activeTheme.kind, color);
}
},
[nameColorLightnessCorrection, activeTheme]
);
const senderFallbackName = getMxIdLocalPart(senderId) ?? senderId;
const resolvedSenderDisplayName =
senderDisplayName === senderFallbackName || senderDisplayName === senderId
@@ -588,7 +605,7 @@ function MessageInternal(
<Username
as="button"
style={{
color: pmpNameColor ?? usernameColor,
color: accessibleNameColor(pmpNameColor) ?? usernameColor,
fontFamily: usernameFont,
}}
data-user-id={senderId}
@@ -606,7 +623,7 @@ function MessageInternal(
{showPronouns && (
<Pronouns
pronouns={mergedPronouns}
tagColor={pmpNameColor ?? usernameColor ?? 'currentColor'}
tagColor={accessibleNameColor(pmpNameColor) ?? usernameColor ?? 'currentColor'}
/>
)}
{showPmPInfo && (
+7 -1
View File
@@ -1,6 +1,6 @@
import { useMemo, useState } from 'react';
import type { ComponentType, ReactNode } from 'react';
import { DesktopIcon, type IconProps } from '@phosphor-icons/react';
import { DesktopIcon, PersonSimpleCircleIcon, type IconProps } from '@phosphor-icons/react';
import { Avatar, Box, Button, config, Text } from 'folds';
import { ScreenSize, useScreenSizeContext } from '$hooks/useScreenSize';
import { useUserProfile } from '$hooks/useUserProfile';
@@ -46,10 +46,12 @@ import { SettingsLinkProvider } from './SettingsLinkContext';
import { useSettingsLinkBaseUrl } from './useSettingsLinkBaseUrl';
import { Desktop } from './desktop';
import { isDesktopTauri } from '$utils/platform';
import { Accessibility } from './accessibility/Accessibility';
enum SettingsPages {
GeneralPage,
AccountPage,
AccessibilityPage,
PerMessageProfilesPage,
NotificationPage,
DevicesPage,
@@ -79,6 +81,7 @@ const settingsMenuIcons: Record<
account: { icon: User },
persona: { icon: UsersThree },
appearance: { icon: Palette },
accessibility: { icon: PersonSimpleCircleIcon },
notifications: { icon: Bell },
devices: { icon: DevicesIcon },
desktop: { icon: DesktopIcon },
@@ -92,6 +95,7 @@ const settingsMenuIcons: Record<
const settingsPageToSectionId: Record<SettingsPages, SettingsSectionId> = {
[SettingsPages.GeneralPage]: 'general',
[SettingsPages.AccountPage]: 'account',
[SettingsPages.AccessibilityPage]: 'accessibility',
[SettingsPages.PerMessageProfilesPage]: 'persona',
[SettingsPages.NotificationPage]: 'notifications',
[SettingsPages.DevicesPage]: 'devices',
@@ -109,6 +113,7 @@ const settingsSectionIdToPage: Record<SettingsSectionId, SettingsPages> = {
account: SettingsPages.AccountPage,
persona: SettingsPages.PerMessageProfilesPage,
appearance: SettingsPages.CosmeticsPage,
accessibility: SettingsPages.AccessibilityPage,
notifications: SettingsPages.NotificationPage,
devices: SettingsPages.DevicesPage,
desktop: SettingsPages.DesktopPage,
@@ -124,6 +129,7 @@ const settingsSectionComponents = {
account: Account,
persona: PerMessageProfilePage,
appearance: Cosmetics,
accessibility: Accessibility,
notifications: Notifications,
devices: Devices,
desktop: Desktop,
@@ -0,0 +1,181 @@
import { useRef } from 'react';
import { Box, config, Scroll, Text, toRem } from 'folds';
import { PageContent, SettingsSectionPage } from '$components/page';
import { SequenceCard, SequenceCardStyle } from '$components/sequence-card';
import { useSetting } from '$state/hooks/settings';
import { settingsAtom } from '$state/settings';
import { SettingTile, SettingToggle } from '$components/setting-tile';
import { SettingMenuSelector } from '$components/setting-menu-selector';
function MotionPreferences() {
const [reducedMotion, setReducedMotion] = useSetting(settingsAtom, 'reducedMotion');
const [autoplayGifs, setAutoplayGifs] = useSetting(settingsAtom, 'autoplayGifs');
const [autoplayStickers, setAutoplayStickers] = useSetting(settingsAtom, 'autoplayStickers');
const [autoplayEmojis, setAutoplayEmojis] = useSetting(settingsAtom, 'autoplayEmojis');
return (
<Box direction="Column" gap="100">
<Text size="L400">Motion</Text>
<SettingToggle
title="Reduced Motion"
focusId="reduced-motion"
description="Stops animations and sliding UI elements."
value={reducedMotion}
onChange={setReducedMotion}
/>
<SettingToggle
title="Autoplay GIFs"
focusId="autoplay-gifs"
description="Automatically play animated image uploads and links."
value={autoplayGifs}
onChange={setAutoplayGifs}
/>
<SettingToggle
title="Autoplay Stickers"
focusId="autoplay-stickers"
description="Automatically play animated stickers."
value={autoplayStickers}
onChange={setAutoplayStickers}
/>
<SettingToggle
title="Autoplay Emojis"
focusId="autoplay-emojis"
description="Automatically play animated custom emojis."
value={autoplayEmojis}
onChange={setAutoplayEmojis}
/>
</Box>
);
}
function TextColorPreferences() {
const [saturation, setSaturation] = useSetting(settingsAtom, 'saturationLevel');
const [underlineLinks, setUnderlineLinks] = useSetting(settingsAtom, 'underlineLinks');
const [renderGlobalColors, setRenderGlobalColors] = useSetting(
settingsAtom,
'renderGlobalNameColors'
);
const [renderRoomColors, setRenderRoomColors] = useSetting(settingsAtom, 'renderRoomColors');
const [renderPersonaColors, setRenderPersonaColors] = useSetting(
settingsAtom,
'renderPersonaColors'
);
const [nameColorLightnessCorrection, setNameColorLightnessCorrection] = useSetting(
settingsAtom,
'nameColorLightnessCorrection'
);
const [renderRoomFonts, setRenderRoomFonts] = useSetting(settingsAtom, 'renderRoomFonts');
return (
<Box direction="Column" gap="100">
<Text size="L400">Text and Color</Text>
<SequenceCard className={SequenceCardStyle} variant="SurfaceVariant" direction="Column">
<SettingTile
title="Saturation"
focusId="saturation"
description={`${saturation}%`}
after={
<input
type="range"
min="0"
max="100"
step="1"
value={saturation}
onChange={(e) => setSaturation(Number.parseInt(e.target.value, 10))}
style={{
width: toRem(160),
cursor: 'pointer',
appearance: 'none',
height: toRem(6),
borderRadius: config.radii.Pill,
backgroundColor: 'var(--sable-surface-container-line)',
accentColor: 'var(--sable-primary-main)',
}}
/>
}
/>
</SequenceCard>
<SettingToggle
title="Render Global Username Colors"
focusId="render-global-username-colors"
description="Display the username colors anyone can set in their account settings."
value={renderGlobalColors}
onChange={setRenderGlobalColors}
/>
<SettingToggle
title="Render Space/Room Username Colors"
focusId="render-space-room-username-colors"
description="Display the username colors that can be set with /color."
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"
description="Display the username fonts that can be set with /font."
value={renderRoomFonts}
onChange={setRenderRoomFonts}
/>
<SettingToggle
title="Underline Links"
focusId="underline-links"
description="Always show underlines on links in chat, bios and room descriptions."
value={underlineLinks}
onChange={setUnderlineLinks}
/>
<SequenceCard className={SequenceCardStyle} variant="SurfaceVariant" direction="Column">
<SettingTile
title="Name color contrast correction"
focusId="name-color-lightness-correction"
description="Automatically correct the lightness of colors to be more readable."
after={
<SettingMenuSelector
value={nameColorLightnessCorrection}
options={[
{ value: 'off', label: 'None' },
{ value: 'weak', label: 'Weak' },
{ value: 'strong', label: 'Strong' },
]}
onSelect={setNameColorLightnessCorrection}
/>
}
/>
</SequenceCard>
</Box>
);
}
type AccessibilityProps = {
requestBack?: () => void;
requestClose: () => void;
};
export function Accessibility({ requestBack, requestClose }: AccessibilityProps) {
const appearanceScrollRef = useRef<HTMLDivElement | null>(null);
return (
<SettingsSectionPage title="Appearance" requestBack={requestBack} requestClose={requestClose}>
<Box grow="Yes">
<Scroll ref={appearanceScrollRef} hideTrack visibility="Hover">
<PageContent>
<Box direction="Column" gap="700">
<TextColorPreferences />
<MotionPreferences />
</Box>
</PageContent>
</Scroll>
</Box>
</SettingsSectionPage>
);
}
@@ -407,16 +407,6 @@ function IdentityCosmetics() {
);
const [showPronouns, setShowPronouns] = useSetting(settingsAtom, 'showPronouns');
const [parsePronouns, setParsePronouns] = useSetting(settingsAtom, 'parsePronouns');
const [renderGlobalColors, setRenderGlobalColors] = useSetting(
settingsAtom,
'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');
return (
@@ -477,34 +467,6 @@ function IdentityCosmetics() {
after={<SelectRenderCustomProfileCards />}
/>
</SequenceCard>
<SettingToggle
title="Render Global Username Colors"
focusId="render-global-username-colors"
description="Display the username colors anyone can set in their account settings."
value={renderGlobalColors}
onChange={setRenderGlobalColors}
/>
<SettingToggle
title="Render Space/Room Username Colors"
focusId="render-space-room-username-colors"
description="Display the username colors that can be set with /color."
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"
description="Display the username fonts that can be set with /font."
value={renderRoomFonts}
onChange={setRenderRoomFonts}
/>
<SettingToggle
title="Consistent Icon Style"
focusId="consistent-icon-style"
@@ -212,11 +212,6 @@ const onNumberInputKeyDown =
function ThemeVisualPreferences() {
const [saturation, setSaturation] = useSetting(settingsAtom, 'saturationLevel');
const [underlineLinks, setUnderlineLinks] = useSetting(settingsAtom, 'underlineLinks');
const [reducedMotion, setReducedMotion] = useSetting(settingsAtom, 'reducedMotion');
const [autoplayGifs, setAutoplayGifs] = useSetting(settingsAtom, 'autoplayGifs');
const [autoplayStickers, setAutoplayStickers] = useSetting(settingsAtom, 'autoplayStickers');
const [autoplayEmojis, setAutoplayEmojis] = useSetting(settingsAtom, 'autoplayEmojis');
const [oldSidebar, setOldSidebar] = useSetting(settingsAtom, 'oldSidebar');
const [pixelatedImageRendering, setPixelatedImageRendering] = useSetting(
settingsAtom,
@@ -301,27 +296,6 @@ function ThemeVisualPreferences() {
}
/>
</SequenceCard>
<SettingToggle
title="Underline Links"
focusId="underline-links"
description="Always show underlines on links in chat, bios and room descriptions."
value={underlineLinks}
onChange={setUnderlineLinks}
/>
<SettingToggle
title="Reduced Motion"
focusId="reduced-motion"
description="Stops animations and sliding UI elements."
value={reducedMotion}
onChange={setReducedMotion}
/>
<SettingToggle
title="Autoplay GIFs"
focusId="autoplay-gifs"
description="Automatically play animated image uploads and links."
value={autoplayGifs}
onChange={setAutoplayGifs}
/>
<SettingToggle
title="Go back to old sidebar"
focusId="old-sidebar"
@@ -343,21 +317,6 @@ function ThemeVisualPreferences() {
}
/>
</SequenceCard>
<SettingToggle
title="Autoplay Stickers"
focusId="autoplay-stickers"
description="Automatically play animated stickers."
value={autoplayStickers}
onChange={setAutoplayStickers}
/>
<SettingToggle
title="Autoplay Emojis"
focusId="autoplay-emojis"
description="Automatically play animated custom emojis."
value={autoplayEmojis}
onChange={setAutoplayEmojis}
/>
<SettingToggle
title="Display Room banners"
focusId="display-room-banners"
+2
View File
@@ -3,6 +3,7 @@ export type SettingsSectionId =
| 'account'
| 'persona'
| 'appearance'
| 'accessibility'
| 'notifications'
| 'devices'
| 'desktop'
@@ -22,6 +23,7 @@ export const settingsSections = [
{ id: 'account', label: 'Account' },
{ id: 'persona', label: 'Persona' },
{ id: 'appearance', label: 'Appearance' },
{ id: 'accessibility', label: 'Accessibility' },
{ id: 'notifications', label: 'Notifications' },
{ id: 'devices', label: 'Devices' },
{ id: 'desktop', label: 'Desktop' },
+9 -6
View File
@@ -153,11 +153,6 @@ export const settingsLinkFocusIdsBySection: Record<SettingsSectionId, readonly s
'pronoun-pill-max-count',
'pronoun-pill-max-length',
'pronoun-pills-for-all',
'reduced-motion',
'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',
@@ -166,7 +161,6 @@ export const settingsLinkFocusIdsBySection: Record<SettingsSectionId, readonly s
'subspace-hierarchy-limit',
'system-theme',
'twitter-emoji',
'underline-links',
'show-room-icons',
'room-icon-overlay',
'show-room-home-icons',
@@ -175,6 +169,15 @@ export const settingsLinkFocusIdsBySection: Record<SettingsSectionId, readonly s
'incoming-inline-images-max-height',
'link-preview-image-max-height',
],
accessibility: [
'underline-links',
'reduced-motion',
'render-global-username-colors',
'render-space-room-fonts',
'render-space-room-username-colors',
'render-persona-username-colors',
'name-color-lightness-correction',
],
notifications: [
'background-push-notifications',
'clear-notifications-when-read-elsewhere',
+20 -1
View File
@@ -8,6 +8,9 @@ import { useRoomCreatorsTag } from './useRoomCreatorsTag';
import { usePowerLevelTags } from './usePowerLevelTags';
import { useTheme } from './useTheme';
import { useUserProfile } from './useUserProfile';
import { useSetting } from '$state/hooks/settings';
import { settingsAtom } from '$state/settings';
import { accessibleColor, accessibleColorWeakCorrection } from '$plugins/color';
export function useSableCosmetics(
userId: string,
@@ -25,11 +28,25 @@ export function useSableCosmetics(
const getPowerTag = useGetMemberPowerTag(room, creators, powerLevels);
const accessibleTagColors = useAccessiblePowerTagColors(theme.kind, creatorsTag, powerLevelTags);
const [nameColorLightnessCorrection] = useSetting(settingsAtom, 'nameColorLightnessCorrection');
return useMemo(() => {
if (!room || !userId) return { color: undefined, font: undefined };
let finalColor = isUserHero ? profile.heroNameColor : profile.resolvedColor;
const accessibleNameColor = (color: string | undefined) => {
if (!color || nameColorLightnessCorrection === 'off') {
return color;
} else if (nameColorLightnessCorrection === 'strong') {
return accessibleColor(theme.kind, color);
} else {
/* weak color correction */
return accessibleColorWeakCorrection(theme.kind, color);
}
};
let finalColor = accessibleNameColor(
isUserHero ? profile.heroNameColor : profile.resolvedColor
);
if (!finalColor) {
const memberPowerTag = getPowerTag(userId);
finalColor = memberPowerTag?.color
@@ -51,5 +68,7 @@ export function useSableCosmetics(
profile.resolvedFont,
getPowerTag,
accessibleTagColors,
nameColorLightnessCorrection,
theme.kind,
]);
}
+25 -6
View File
@@ -1,16 +1,35 @@
import chroma from 'chroma-js';
import { ThemeKind } from '$hooks/useTheme';
const clampColor = (
lightness: number,
lightCap: number,
darkCap: number,
themeKind: ThemeKind
): number => {
if (themeKind === ThemeKind.Dark && lightness < darkCap) {
lightness = darkCap;
}
if (themeKind === ThemeKind.Light && lightness > lightCap) {
lightness = lightCap;
}
return lightness;
};
export const accessibleColor = (themeKind: ThemeKind, color: string): string => {
if (!chroma.valid(color)) return color;
let lightness = chroma(color).lab()[0];
if (themeKind === ThemeKind.Dark && lightness < 60) {
lightness = 60;
}
if (themeKind === ThemeKind.Light && lightness > 50) {
lightness = 50;
}
lightness = clampColor(lightness, 50, 60, themeKind);
return chroma(color).set('lab.l', lightness).hex();
};
export const accessibleColorWeakCorrection = (themeKind: ThemeKind, color: string): string => {
if (!chroma.valid(color)) return color;
let lightness = chroma(color).lab()[0];
lightness = clampColor(lightness, 70, 40, themeKind);
return chroma(color).set('lab.l', lightness).hex();
};
+10 -5
View File
@@ -216,11 +216,6 @@ export interface Settings {
showEasterEggs: boolean;
hideReads: boolean;
emojiSuggestThreshold: number;
underlineLinks: boolean;
reducedMotion: boolean;
autoplayGifs: boolean;
autoplayStickers: boolean;
autoplayEmojis: boolean;
oldSidebar: boolean;
pixelatedImageRendering: PixelatedImageRenderingMode;
incomingInlineImagesDefaultHeight: number;
@@ -262,6 +257,14 @@ export interface Settings {
isShowingAllRoomsInHome: boolean;
sendIndividualAttachmentAsCaption: boolean;
// accessibility stuff
underlineLinks: boolean;
reducedMotion: boolean;
autoplayGifs: boolean;
autoplayStickers: boolean;
autoplayEmojis: boolean;
nameColorLightnessCorrection: 'off' | 'weak' | 'strong';
// furry stuff
renderAnimals: boolean;
animalKind: string | undefined;
@@ -443,6 +446,8 @@ export const defaultSettings: Settings = {
widgetSidebarWidth: 420,
isShowingAllRoomsInHome: false,
sendIndividualAttachmentAsCaption: true,
nameColorLightnessCorrection: 'off',
// furry stuff
renderAnimals: true,
animalKind: undefined,