🧑‍🎨 feat: Give the Default Avatar and Avatar Placeholder Theme Roles (#16545)

* feat: Give the Default Avatar and Avatar Placeholder Theme Roles

The default user avatar filled with a literal rgb(121, 137, 255) in Avatar, MessageIcon and the endpoint Icon, and agent and assistant avatars sat on surface-secondary in light and a dark: surface-tertiary override, so no theme could repaint either.

avatar-fill and avatar-text paint the default avatar and its glyph, and avatar-placeholder paints the backdrop behind an agent or assistant image. Every bundled theme reproduces today's pixels. A theme that sets only its surfaces keeps the placeholder on the surface its mode used before the role existed, both when resolved and through the legacy applyTheme path. ClickHouse takes Click UI's avatar fill and text and its muted background.

* test: Pin the Default Avatar Fill With and Without the ClickHouse Theme

The sidebar account avatar falls back to the default avatar when its image fails; with no theme it keeps rgb(121, 137, 255) in both modes and the placeholder keeps the surface its mode used, and under ClickHouse both take the Click UI values.

* fix: Ink Every Default Avatar Glyph With the Avatar Text Role

The sidebar Avatar, the only default avatar the chat renders, still inked its glyph in text-primary, so a theme's avatar-text never reached it and ClickHouse drew a dark glyph where Click UI draws white. Avatar now reads avatar-text, which defaults to each bundled theme's primary text and follows rgb-text-primary in a theme that does not set it, through resolveTheme and the legacy applyTheme path, so every rendered default avatar keeps its pixels.

* fix: Read the Active Mode Off the Root When applyTheme Is Not Told It

applyTheme is exported, and a caller that applies a legacy palette in dark mode without the new mode argument derived the avatar placeholder from the light surface. It now falls back to the root's dark class.

* fix: Keep CSS-Themed Avatars on Their Legacy Source Variables

The avatar text and placeholder defaults were literal triplets, so a consumer theming through --text-primary and the surface variables left avatars on the stock colors. They now alias text-primary, the secondary surface and, in dark, the tertiary surface, resolving to the same pixels by default. applyTheme's mode fallback also reads a dark ancestor, not only the root itself.

* fix: Let the Stylesheet Alias Carry the Document Root Avatar Backdrop

applyTheme wrote the legacy placeholder inline for the mode active when it ran, which then overrode the per-mode stylesheet alias after a dark toggle without a reapply. On the document root the alias already follows the theme's surfaces and the mode, so the placeholder is now derived only for a scoped root, which that alias cannot see.

* fix: Keep a Scoped Legacy Avatar Backdrop Following the Mode

applyTheme wrote a scoped root's avatar placeholder inline for the mode active when it ran, so after a later dark toggle the backdrop stayed on the light surface, where canary's dark:bg-surface-tertiary switched. applyTheme now marks a scoped root with data-theme-scope and the stylesheet points its backdrop at that root's own secondary surface, or its tertiary one under a .dark ancestor, as the document root's alias does. The mode argument is no longer needed and is removed.

(cherry picked from commit 3ac6a0af37)

Original-PR: https://github.com/LibreChat-AI/LibreChat/pull/16545
This commit is contained in:
Marco Beretta 2026-09-30 10:59:06 +02:00 • committed by Lia
parent 37f02c2897
commit eb5d74dbd5
25 changed files with 377 additions and 14 deletions

View file

@ -11,8 +11,7 @@ import { isImageURL } from '~/utils/icons';
/** Callers frame the mark at two thirds of the round container around it. */
const artScale = 2 / 3;
const entityAvatarClassName =
'bg-surface-secondary h-full w-full rounded-full object-cover dark:bg-surface-tertiary';
const entityAvatarClassName = 'bg-avatar-placeholder h-full w-full rounded-full object-cover';
function AgentAvatar({
avatar,

View file

@ -84,12 +84,11 @@ const UserAvatar = memo(
) : (
<div
style={{
backgroundColor: 'rgb(121, 137, 255)',
width: '20px',
height: '20px',
boxShadow: 'rgba(240, 246, 252, 0.1) 0px 0px 0px 1px',
}}
className="relative flex h-9 w-9 items-center justify-center rounded-sm p-1 text-white"
className="bg-avatar-fill text-avatar-text relative flex h-9 w-9 items-center justify-center rounded-sm p-1"
>
<UserIcon />
</div>

View file

@ -68,12 +68,11 @@ export default function MessageIcon(
return (
<div
style={{
backgroundColor: 'rgb(121, 137, 255)',
width: '20px',
height: '20px',
boxShadow: 'rgba(240, 246, 252, 0.1) 0px 0px 0px 1px',
}}
className="relative flex h-9 w-9 items-center justify-center rounded-sm p-1 text-white"
className="bg-avatar-fill text-avatar-text relative flex h-9 w-9 items-center justify-center rounded-sm p-1"
>
<UserIcon />
</div>

View file

@ -40,7 +40,7 @@ export const AgentAvatarRender = ({ url }: { url?: string }) => {
<div className="relative h-full w-full overflow-hidden rounded-full">
<img
src={url}
className="bg-surface-secondary dark:bg-surface-tertiary h-full w-full rounded-full object-cover"
className="bg-avatar-placeholder h-full w-full rounded-full object-cover"
alt="Agent avatar"
loading="lazy"
key={url}

View file

@ -45,7 +45,7 @@ export const AssistantAvatar = ({
<div className="relative h-20 w-20 overflow-hidden rounded-full">
<img
src={url}
className="bg-surface-secondary dark:bg-surface-tertiary h-full w-full rounded-full object-cover"
className="bg-avatar-placeholder h-full w-full rounded-full object-cover"
alt="GPT"
width="80"
height="80"

View file

@ -128,6 +128,29 @@ const titleColor = (page: Page, title: string) =>
.getByText(title, { exact: true })
.evaluate((node) => getComputedStyle(node).color);
const MISSING_AVATAR = 'https://avatar.e2e.invalid/missing.png';
/** The e2e user has an avatar seed, so the default avatar only draws once its image fails:
* the user payload points at an address the page refuses to load. */
async function failUserAvatar(page: Page) {
await page.route(`${MISSING_AVATAR}*`, (route) => route.abort());
await page.route('**/api/user', async (route) => {
const response = await route.fetch();
const user = await response.json();
await route.fulfill({ response, json: { ...user, avatar: MISSING_AVATAR } });
});
}
/** The default avatar on the sidebar account button, the one a signed-in user always sees. */
const navAvatarPaint = async (page: Page) => {
const avatar = page.getByTestId('nav-user').locator('div[aria-hidden="true"]').first();
await expect(avatar).toBeVisible({ timeout: 20000 });
return avatar.evaluate((node) => {
const style = getComputedStyle(node);
return { fill: style.backgroundColor, ink: style.color };
});
};
test.describe('clickhouse reference theme', () => {
test('the ClickHouse definition repaints the chat and the settings dialog in both modes @scenario:clickhouse-definition-repaints-chat-sidebar-and-dialog', async ({
page,
@ -234,4 +257,109 @@ test.describe('clickhouse reference theme', () => {
await deleteConversations([conversationId]);
}
});
test('the default avatar keeps its fill and the placeholder its surface without a theme @scenario:default-avatar-keeps-its-fill', async ({
page,
}) => {
test.setTimeout(90000);
const conversationId = await seedChat('Default avatar');
await installThemeBridge(page, null);
await failUserAvatar(page);
try {
for (const [mode, placeholder, ink] of [
['light', defaultTheme['rgb-surface-secondary'], 'rgb(33, 33, 33)'],
['dark', darkTheme['rgb-surface-tertiary'], 'rgb(236, 236, 236)'],
] as const) {
await page.goto(`/c/${conversationId}?${THEME_PARAM}=${mode}`);
await expect(page.getByText(REPLY_TEXT, { exact: true }).first()).toBeVisible({
timeout: 20000,
});
expect(await navAvatarPaint(page)).toEqual({ fill: 'rgb(121, 137, 255)', ink });
expect(await themeValue(page, '--avatar-placeholder')).toBe(placeholder);
}
} finally {
await deleteConversations([conversationId]);
}
});
test('the ClickHouse definition paints the default avatar from Click UI @scenario:clickhouse-avatar-follows-click-ui', async ({
page,
}) => {
test.setTimeout(90000);
const conversationId = await seedChat('ClickHouse avatar');
await installThemeBridge(page, clickHouseTheme);
await failUserAvatar(page);
try {
for (const mode of MODES) {
const colors = colorsFor(mode);
await page.goto(`/c/${conversationId}?${THEME_PARAM}=${mode}`);
await expect(page.getByText(REPLY_TEXT, { exact: true }).first()).toBeVisible({
timeout: 20000,
});
await expect(page.locator('html')).toHaveAttribute('data-theme', 'clickhouse');
expect(await navAvatarPaint(page)).toEqual({
fill: rgbCss(colors['rgb-avatar-fill']),
ink: rgbCss(colors['rgb-avatar-text']),
});
expect(await themeValue(page, '--avatar-placeholder')).toBe(
colors['rgb-avatar-placeholder'],
);
}
} finally {
await deleteConversations([conversationId]);
}
});
test('a legacy backdrop follows a dark toggle on the document root and on a scoped root @scenario:legacy-avatar-backdrop-follows-mode', async ({
page,
}) => {
test.setTimeout(90000);
const conversationId = await seedChat('Legacy avatar backdrop');
await installThemeBridge(page, null);
try {
await page.goto(`/c/${conversationId}?${THEME_PARAM}=light`);
await expect(page.getByText(REPLY_TEXT, { exact: true }).first()).toBeVisible({
timeout: 20000,
});
/** What a legacy palette leaves behind: surfaces set inline, applied once in light, and a
* scoped root marked the way `applyTheme` marks one. The mode then flips with no reapply. */
const painted = await page.evaluate(() => {
const html = document.documentElement;
const read = (node: Element) => getComputedStyle(node).backgroundColor;
const backdrop = (host: Element) => {
const node = document.createElement('div');
node.className = 'bg-avatar-placeholder';
host.append(node);
return node;
};
html.classList.remove('dark');
html.style.setProperty('--surface-secondary', '20 21 22');
html.style.setProperty('--surface-tertiary', '30 31 32');
const scope = document.createElement('section');
scope.setAttribute('data-theme-scope', '');
scope.style.setProperty('--surface-secondary', '40 41 42');
scope.style.setProperty('--surface-tertiary', '50 51 52');
document.body.append(scope);
const onRoot = backdrop(document.body);
const onScope = backdrop(scope);
const light = { root: read(onRoot), scoped: read(onScope) };
html.classList.add('dark');
const dark = { root: read(onRoot), scoped: read(onScope) };
return { light, dark };
});
expect(painted).toEqual({
light: { root: 'rgb(20, 21, 22)', scoped: 'rgb(40, 41, 42)' },
dark: { root: 'rgb(30, 31, 32)', scoped: 'rgb(50, 51, 52)' },
});
} finally {
await deleteConversations([conversationId]);
}
});
});

View file

@ -1004,7 +1004,7 @@
},
"client/src/components/Endpoints/Icon.tsx": {
"shadcn/no-inline-styles": {
"count": 2
"count": 1
}
},
"client/src/components/Endpoints/MessageEndpointIcon.tsx": {
@ -1490,7 +1490,7 @@
},
"client/src/components/Share/MessageIcon.tsx": {
"shadcn/no-inline-styles": {
"count": 2
"count": 1
}
},
"client/src/components/Share/MessagesView.tsx": {
@ -2203,7 +2203,7 @@
},
"packages/client/src/components/Avatar.tsx": {
"shadcn/no-inline-styles": {
"count": 3
"count": 2
}
},
"packages/client/src/components/Badge.tsx": {

View file

@ -51,12 +51,11 @@ const Avatar: React.FC<AvatarProps> = ({
() => (
<div
style={{
backgroundColor: 'rgb(121, 137, 255)',
width: `${size}px`,
height: `${size}px`,
boxShadow: 'rgba(240, 246, 252, 0.1) 0px 0px 0px 1px',
}}
className={`relative flex items-center justify-center rounded-full p-1 text-text-primary ${className}`}
className={`bg-avatar-fill text-avatar-text relative flex items-center justify-center rounded-full p-1 ${className}`}
aria-hidden="true"
>
<UserIcon />

View file

@ -316,6 +316,13 @@ Each status family has a foreground, a `-subtle` background, a `-border`, and a
### Other Colors
- `bg-brand-purple` - Brand purple color
- `bg-avatar-fill` / `text-avatar-text` - The default user avatar drawn when a
user has no image, and its glyph. A theme that sets `rgb-text-primary` but not
`rgb-avatar-text` inks the glyph in its primary text, as it did before the role.
- `bg-avatar-placeholder` - Behind an agent or assistant avatar while its image
loads or where it is transparent. A theme that sets only its surfaces keeps it
on `surface-secondary` in light and `surface-tertiary` in dark, where it sat
before the role existed.
- `bg-presentation` - Presentation background
- `ring-ring-primary` - Decorative ring color (selection and hover rings)
- `focus-outline` - The app-wide keyboard focus outline. Defaults to black in

View file

@ -76,6 +76,9 @@
html {
--brand-purple: 126 34 206;
--avatar-fill: 121 137 255;
--avatar-text: var(--text-primary);
--avatar-placeholder: var(--surface-secondary);
--presentation: var(--white);
--text-primary: var(--gray-800);
--text-secondary: var(--gray-600);
@ -262,6 +265,9 @@ html {
}
.dark {
--brand-purple: 171 104 255;
--avatar-fill: 121 137 255;
--avatar-text: var(--text-primary);
--avatar-placeholder: var(--surface-tertiary);
--presentation: var(--gray-800);
--text-primary: var(--gray-100);
--focus-outline: 255 255 255;
@ -383,3 +389,14 @@ html {
--provider-bedrock: #268672;
--provider-foreground: #ffffff;
}
/* A root other than the document one that a legacy palette themes (`THEME_SCOPE_ATTRIBUTE` in
utils/applyTheme.ts) keeps its avatar backdrop on its own surfaces, following the mode, as the
document root's alias above does. An explicit avatar-placeholder set inline still wins. */
[data-theme-scope] {
--avatar-placeholder: var(--surface-secondary);
}
.dark [data-theme-scope],
[data-theme-scope].dark {
--avatar-placeholder: var(--surface-tertiary);
}

View file

@ -63,7 +63,7 @@ describe.each([
const tokens = Object.keys(theme) as Array<keyof IThemeRGB>;
it('covers every registry color', () => {
expect(tokens).toHaveLength(108);
expect(tokens).toHaveLength(111);
});
it('resolves every registry color to the runtime theme value', () => {

View file

@ -369,6 +369,64 @@ describe('theme registry', () => {
});
});
it('keeps the avatar backdrop on the surface each mode drew it on before the role existed', () => {
const colors = { 'rgb-surface-secondary': '20 21 22', 'rgb-surface-tertiary': '30 31 32' };
const theme = {
version: 1 as const,
name: 'legacy-avatar-placeholder',
modes: { light: { colors }, dark: { colors } },
};
expect(resolveTheme(theme, 'light').colors['rgb-avatar-placeholder']).toBe('20 21 22');
expect(resolveTheme(theme, 'dark').colors['rgb-avatar-placeholder']).toBe('30 31 32');
});
it('inks the default avatar in the primary text a theme sets, unless it sets the role', () => {
const inherited = resolveTheme(
{
version: 1,
name: 'legacy-avatar-text',
modes: { light: { colors: { 'rgb-text-primary': '10 11 12' } } },
},
'light',
);
const explicit = resolveTheme(
{
version: 1,
name: 'explicit-avatar-text',
modes: {
light: { colors: { 'rgb-text-primary': '10 11 12', 'rgb-avatar-text': '1 2 3' } },
},
},
'light',
);
expect(inherited.colors['rgb-avatar-text']).toBe('10 11 12');
expect(explicit.colors['rgb-avatar-text']).toBe('1 2 3');
});
it('preserves an explicit avatar backdrop and falls back to the bundled one otherwise', () => {
const explicit = resolveTheme(
{
version: 1,
name: 'explicit-avatar-placeholder',
modes: {
dark: {
colors: { 'rgb-surface-tertiary': '30 31 32', 'rgb-avatar-placeholder': '1 2 3' },
},
},
},
'dark',
);
const untouched = resolveTheme(
{ version: 1, name: 'no-surface', modes: { dark: { colors: {} } } },
'dark',
);
expect(explicit.colors['rgb-avatar-placeholder']).toBe('1 2 3');
expect(untouched.colors['rgb-avatar-placeholder']).toBe(darkTheme['rgb-avatar-placeholder']);
});
it('keeps the switch thumb on the surface a theme repainted before the role existed', () => {
const resolved = resolveTheme(
{

View file

@ -465,6 +465,24 @@ export function resolveTheme(theme: ThemeDefinition, mode: ThemeMode): ResolvedT
customColors?.['rgb-link-prose'] === undefined && proseLinkSource !== undefined
? { 'rgb-link-prose': proseLinkSource }
: {};
/**
* Agent and assistant avatars sat on `surface-secondary` in light and `surface-tertiary` in dark
* before they had a role, so a theme that repaints the one its mode used keeps that backdrop.
*/
const avatarPlaceholderSource =
mode === 'dark'
? customColors?.['rgb-surface-tertiary']
: customColors?.['rgb-surface-secondary'];
const avatarPlaceholderFallback =
customColors?.['rgb-avatar-placeholder'] === undefined && avatarPlaceholderSource !== undefined
? { 'rgb-avatar-placeholder': avatarPlaceholderSource }
: {};
/** The default avatar's glyph inked in `text-primary` before it had a role. */
const avatarTextFallback =
customColors?.['rgb-avatar-text'] === undefined &&
customColors?.['rgb-text-primary'] !== undefined
? { 'rgb-avatar-text': customColors['rgb-text-primary'] }
: {};
const chartWidgetSurfaceFallback =
customColors?.['rgb-chart-widget-surface'] === undefined &&
customColors?.['rgb-surface-primary'] !== undefined
@ -561,6 +579,8 @@ export function resolveTheme(theme: ThemeDefinition, mode: ThemeMode): ResolvedT
...shimmerBaseFallback,
...textMutedFallback,
...proseLinkFallback,
...avatarPlaceholderFallback,
...avatarTextFallback,
...chartWidgetSurfaceFallback,
...chartWidgetStrokeFallback,
...switchThumbFallback,

View file

@ -531,6 +531,31 @@ describe('verified fill defaults', () => {
});
});
/**
* The default avatar's glyph is a graphical object under WCAG 1.4.11, so it needs 3:1 on its fill.
* The bundled dark theme keeps the 2.6:1 its avatar painted before the role existed: the glyph is
* decorative (aria-hidden beside the account name), and raising it is tracked on its own.
*/
it('keeps the bundled dark avatar glyph no fainter than it painted before its role', () => {
expect(
contrast(toRgb(darkTheme, 'rgb-avatar-text'), toRgb(darkTheme, 'rgb-avatar-fill')),
).toBeGreaterThanOrEqual(2.6);
});
describe.each([
['default', defaultTheme],
['high contrast light', highContrastLightTheme],
['high contrast dark', highContrastDarkTheme],
['clickhouse light', clickHouseLightTheme],
['clickhouse dark', clickHouseDarkTheme],
])('%s default avatar', (_name, theme: IThemeRGB) => {
it('keeps the glyph at the 3:1 mark floor against its fill', () => {
expect(
contrast(toRgb(theme, 'rgb-avatar-text'), toRgb(theme, 'rgb-avatar-fill')),
).toBeGreaterThanOrEqual(WCAG_MARK_MIN);
});
});
/** The shared `Switch` paints this track, so it travels with the package rather
* than the app stylesheet. It is a UI component boundary under WCAG 1.4.11 and
* has to stay distinct from the `switch-thumb` knob on it and from the

View file

@ -133,6 +133,9 @@ export const clickHouseLightTheme: IThemeRGB = {
// Brand colors
'rgb-brand-purple': '136 0 204', // #8800cc (palette.violet.600)
'rgb-avatar-fill': '105 110 121', // #696e79 (avatar.color.background.default)
'rgb-avatar-text': '255 255 255', // #ffffff (avatar.color.text.default)
'rgb-avatar-placeholder': '246 247 250', // #f6f7fa (background.muted)
// Code syntax
'rgb-syntax-text': '40 40 40', // #282828 (codeblock.lightMode.color.text.default)
@ -272,6 +275,9 @@ export const clickHouseDarkTheme: IThemeRGB = {
// Brand colors
'rgb-brand-purple': '204 102 255', // #cc66ff (palette.violet.300)
'rgb-avatar-fill': '128 134 145', // #808691 (avatar.color.background.default)
'rgb-avatar-text': '31 31 28', // #1f1f1c (avatar.color.text.default)
'rgb-avatar-placeholder': '40 40 40', // #282828 (background.muted)
// Code syntax
'rgb-syntax-text': '255 255 255', // #ffffff (codeblock.darkMode.color.text.default)

View file

@ -11,6 +11,8 @@
"border.radii.2": "0.5rem",
"border.radii.3": "0.75rem",
"border.radii.full": "9999px",
"click.avatar.color.background.default": "#696e79",
"click.avatar.color.text.default": "#ffffff",
"click.badge.opaque.color.background.default": "#f6f7fa",
"click.badge.opaque.color.stroke.default": "#e6e7e9",
"click.badge.opaque.color.text.default": "#696e79",
@ -128,6 +130,8 @@
"border.radii.2": "0.5rem",
"border.radii.3": "0.75rem",
"border.radii.full": "9999px",
"click.avatar.color.background.default": "#808691",
"click.avatar.color.text.default": "#1f1f1c",
"click.badge.opaque.color.background.default": "#1f1f1c",
"click.badge.opaque.color.stroke.default": "#323232",
"click.badge.opaque.color.text.default": "#b3b6bd",

View file

@ -141,6 +141,9 @@ const colorSources: Record<ThemeMode, Partial<Record<keyof IThemeRGB, string>>>
'rgb-status-verified': 'palette.info.600',
'rgb-text-on-status': 'palette.neutral.0',
'rgb-brand-purple': 'palette.violet.600',
'rgb-avatar-fill': 'click.avatar.color.background.default',
'rgb-avatar-text': 'click.avatar.color.text.default',
'rgb-avatar-placeholder': 'global.color.background.muted',
'rgb-syntax-text': 'click.codeblock.lightMode.color.text.default',
'rgb-syntax-comment': 'global.color.text.muted',
'rgb-syntax-meta': 'palette.slate.700',
@ -250,6 +253,9 @@ const colorSources: Record<ThemeMode, Partial<Record<keyof IThemeRGB, string>>>
'rgb-status-verified': 'palette.info.300',
'rgb-text-on-status': 'global.color.iconButton.badge.foreground',
'rgb-brand-purple': 'palette.violet.300',
'rgb-avatar-fill': 'click.avatar.color.background.default',
'rgb-avatar-text': 'click.avatar.color.text.default',
'rgb-avatar-placeholder': 'global.color.background.muted',
'rgb-syntax-text': 'click.codeblock.darkMode.color.text.default',
'rgb-syntax-comment': 'global.color.text.muted',
'rgb-syntax-meta': 'palette.slate.400',

View file

@ -120,6 +120,9 @@ export const darkTheme: IThemeRGB = {
// Brand colors
'rgb-brand-purple': '171 104 255', // #ab68ff
'rgb-avatar-fill': '121 137 255', // #7989ff (the default avatar fill in every bundled theme)
'rgb-avatar-text': '236 236 236', // #ececec (gray-100, matching text-primary)
'rgb-avatar-placeholder': '47 47 47', // #2f2f2f (gray-700, matching surface-tertiary)
/** Code syntax highlighting, measured against the `surface-code` fill. The
* comment and meta values are the flattened equivalents of the alpha-blended

View file

@ -110,6 +110,9 @@ export const defaultTheme: IThemeRGB = {
// Brand colors
'rgb-brand-purple': '126 34 206', // #7e22ce (purple-700)
'rgb-avatar-fill': '121 137 255', // #7989ff (the default avatar fill in every bundled theme)
'rgb-avatar-text': '33 33 33', // #212121 (gray-800, matching text-primary)
'rgb-avatar-placeholder': '247 247 248', // #f7f7f8 (gray-50, matching surface-secondary)
/** Code syntax highlighting, measured against the `surface-code` and `surface-code-body` fills. */
'rgb-syntax-text': '33 33 33', // #212121 (gray-800)

View file

@ -145,6 +145,9 @@ export const highContrastLightTheme: IThemeRGB = {
// Brand colors
'rgb-brand-purple': '107 0 179', // #6b00b3
'rgb-avatar-fill': '121 137 255', // #7989ff (the default avatar fill in every bundled theme)
'rgb-avatar-text': '0 0 0', // #000000 (matching text-primary)
'rgb-avatar-placeholder': '255 255 255', // #ffffff (matching surface-secondary)
/** Code syntax highlighting at AAA on the white code surface. */
'rgb-syntax-text': '0 0 0', // #000000
@ -301,6 +304,9 @@ export const highContrastDarkTheme: IThemeRGB = {
// Brand colors
'rgb-brand-purple': '224 179 255', // #e0b3ff
'rgb-avatar-fill': '121 137 255', // #7989ff (the default avatar fill in every bundled theme)
'rgb-avatar-text': '255 255 255', // #ffffff (matching text-primary)
'rgb-avatar-placeholder': '0 0 0', // #000000 (matching surface-tertiary)
/** Code syntax highlighting at AAA on the black code surface. */
'rgb-syntax-text': '255 255 255', // #ffffff

View file

@ -22,6 +22,9 @@
@theme inline {
/* brand and surfaces */
--color-brand-purple: rgb(var(--brand-purple));
--color-avatar-fill: rgb(var(--avatar-fill));
--color-avatar-text: rgb(var(--avatar-text));
--color-avatar-placeholder: rgb(var(--avatar-placeholder));
--color-presentation: rgb(var(--presentation));
--color-accent-primary: rgb(var(--accent-primary));
--color-accent-primary-hover: rgb(var(--accent-primary-hover));

View file

@ -132,6 +132,13 @@ export interface IThemeRGB {
// Brand colors
'rgb-brand-purple'?: string;
/** The default user avatar's fill and glyph, drawn when a user has no image. The glyph follows
* `rgb-text-primary` in a theme that does not set it, as it did before it had a role. */
'rgb-avatar-fill'?: string;
'rgb-avatar-text'?: string;
/** Behind an agent or assistant avatar while its image loads or where it is transparent. */
'rgb-avatar-placeholder'?: string;
/**
* Code syntax highlighting. Declared here rather than left as literals in the
* stylesheet so a palette stays in one place, is covered by the registry's
@ -273,6 +280,9 @@ export interface IThemeVariables {
'--status-verified': string;
'--text-on-status': string;
'--brand-purple': string;
'--avatar-fill': string;
'--avatar-text': string;
'--avatar-placeholder': string;
'--syntax-text': string;
'--syntax-comment': string;
@ -389,6 +399,9 @@ export interface IThemeColors {
'status-verified'?: string;
'text-on-status'?: string;
'brand-purple'?: string;
'avatar-fill'?: string;
'avatar-text'?: string;
'avatar-placeholder'?: string;
'series-1'?: string;
'series-2'?: string;

View file

@ -4,6 +4,7 @@ import applyTheme, {
clearAppliedTheme,
themeOwnedProperties,
THEME_DISABLED_ATTRIBUTE,
THEME_SCOPE_ATTRIBUTE,
} from './applyTheme';
import { defaultAppearance, highContrastTheme, resolveTheme } from '../registry';
import { defaultTheme } from '../themes/default';
@ -19,6 +20,8 @@ const semanticProperties = [
'--chart-widget-surface',
'--chart-widget-stroke',
'--switch-thumb',
'--avatar-placeholder',
'--avatar-text',
'--table-header-text',
'--table-header-fill',
'--border-destructive',
@ -381,6 +384,53 @@ describe('applyTheme', () => {
expect(root.hasAttribute(THEME_DISABLED_ATTRIBUTE)).toBe(false);
});
it('leaves the document root backdrop to the stylesheet alias so it follows the mode', () => {
const root = document.documentElement;
applyTheme({ 'rgb-surface-secondary': '20 21 22', 'rgb-surface-tertiary': '30 31 32' }, root);
expect(root.style.getPropertyValue('--surface-secondary')).toBe('20 21 22');
expect(root.style.getPropertyValue('--avatar-placeholder')).toBe('');
});
it('marks a scoped root so the stylesheet derives its backdrop from its own surfaces', () => {
const scoped = document.createElement('div');
document.body.append(scoped);
try {
applyTheme(
{ 'rgb-surface-secondary': '20 21 22', 'rgb-surface-tertiary': '30 31 32' },
scoped,
);
expect(scoped.hasAttribute(THEME_SCOPE_ATTRIBUTE)).toBe(true);
expect(scoped.style.getPropertyValue('--avatar-placeholder')).toBe('');
expect(document.documentElement.hasAttribute(THEME_SCOPE_ATTRIBUTE)).toBe(false);
clearAppliedTheme(scoped);
expect(scoped.hasAttribute(THEME_SCOPE_ATTRIBUTE)).toBe(false);
} finally {
scoped.remove();
}
});
it("inks a legacy theme's default avatar in its primary text", () => {
const root = document.documentElement;
applyTheme({ 'rgb-text-primary': '10 11 12' }, root);
expect(root.style.getPropertyValue('--avatar-text')).toBe('10 11 12');
applyTheme({ 'rgb-text-primary': '10 11 12', 'rgb-avatar-text': '1 2 3' }, root);
expect(root.style.getPropertyValue('--avatar-text')).toBe('1 2 3');
});
it('leaves an explicit avatar backdrop alone', () => {
const root = document.documentElement;
applyTheme({ 'rgb-surface-tertiary': '30 31 32', 'rgb-avatar-placeholder': '1 2 3' }, root);
expect(root.style.getPropertyValue('--avatar-placeholder')).toBe('1 2 3');
});
it('keeps the switch knob of a legacy theme on the surface it repainted', () => {
const root = document.documentElement;

View file

@ -63,6 +63,10 @@ function mapColors(colors: IThemeRGB, base?: IThemeRGB): Array<[string, string]>
variables.push(['--text-muted', colors['rgb-text-tertiary']]);
}
if (colors['rgb-avatar-text'] === undefined && colors['rgb-text-primary'] !== undefined) {
variables.push(['--avatar-text', colors['rgb-text-primary']]);
}
if (
colors['rgb-chart-widget-surface'] === undefined &&
colors['rgb-surface-primary'] !== undefined
@ -156,10 +160,18 @@ function mapAppearance(appearance: IThemeAppearance): Array<[string, string]> {
*/
export const THEME_DISABLED_ATTRIBUTE = 'data-theme-disabled';
/**
* Marks a root other than the document one that a legacy palette themes. The stylesheet points
* the avatar backdrop at that root's own secondary surface, or its tertiary one under a `.dark`
* ancestor, as the document root's alias does, so the backdrop follows a later mode change.
*/
export const THEME_SCOPE_ATTRIBUTE = 'data-theme-scope';
export function clearAppliedTheme(root: HTMLElement = document.documentElement): void {
themeOwnedProperties.forEach((property) => root.style.removeProperty(property));
root.removeAttribute('data-theme');
root.removeAttribute(THEME_DISABLED_ATTRIBUTE);
root.removeAttribute(THEME_SCOPE_ATTRIBUTE);
}
export function applyResolvedTheme(
@ -196,6 +208,9 @@ export default function applyTheme(
return;
}
if (root !== root.ownerDocument.documentElement) {
root.setAttribute(THEME_SCOPE_ATTRIBUTE, '');
}
mapColors(themeRGB, base).forEach(([property, value]) => {
if (!validateRGB(value)) {
console.error(`Invalid RGB value for ${property}: ${value}`);

View file

@ -101,6 +101,9 @@ export const themeColorTokens = Object.freeze([
'rgb-status-verified',
'rgb-text-on-status',
'rgb-brand-purple',
'rgb-avatar-fill',
'rgb-avatar-text',
'rgb-avatar-placeholder',
'rgb-syntax-text',
'rgb-syntax-comment',
'rgb-syntax-meta',