Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/website/content/docs/react/chat/guides/theming.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ React chat also reads the Angular chat token names. A stylesheet that sets `--tp
| `--tplane-chat-font-*`, `--tplane-chat-line-height*`, `--tplane-chat-radius-*`, `--tplane-chat-max-width` | Typography, corner radii and the reading width |
| `--tplane-chat-citation-type-*-fg` | Source type colors for files, apps and memory |

React chat does not inject the Angular default values, so a token that the theme does not set keeps the React default. React follows `color-scheme` for light and dark defaults. A theme that switches with `[data-theme="dark"]` must set its dark values under that selector, as it does for Angular.
React chat does not inject the Angular default values, so a token that the theme does not set keeps the React default. React follows `color-scheme` for light and dark defaults. Like Angular chat, it also follows a `data-theme="light|dark"` or `data-threadplane-chat-theme="light|dark"` attribute on the chat or any ancestor, so an app theme toggle reaches the chat with no extra code. The nearest attribute wins, and either attribute beats the system `prefers-color-scheme` setting. An explicit `--tp-chat-color-scheme` on a chat root beats both. The A2UI surface stylesheet reads the same attributes. A theme that switches with `[data-theme="dark"]` must set its dark values under that selector, as it does for Angular.

<ExampleCode file="cockpit/chat/theming/react/src/styles.css" />

Expand Down
4 changes: 4 additions & 0 deletions libs/react/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,10 @@ composer, scrollable tables and native task-checkbox semantics. Override
`--tp-chat-font-family`, `--tp-chat-font-size-base`, or the
`--tp-chat-radius-*` properties on the composed part to customize it. An explicit
`color-scheme` or `--tp-chat-color-scheme` selects the host's light/dark treatment.
Without one, a `data-theme="light|dark"` or `data-threadplane-chat-theme="light|dark"`
attribute on the chat or any ancestor forces that scheme, as in Angular chat (the
nearest attribute wins); otherwise the system `prefers-color-scheme` applies. The A2UI
surface stylesheet follows the same attributes.
User bubbles use the neutral inverted palette; override `--tp-chat-user-bg` and
`--tp-chat-user-text` on the composed part to supply a different color pair.
Custom message renderers retain their own layout and need their own integration.
Expand Down
35 changes: 35 additions & 0 deletions libs/react/src/a2ui/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,41 @@
--a2ui-surface-variant: #262a33;
}
}
/* Theme attribute, as in Angular chat: data-theme or data-threadplane-chat-theme
on an ancestor forces a scheme over prefers-color-scheme. A theme file
(:root .a2ui-surface) still outranks these. */
:where([data-theme="light"], [data-threadplane-chat-theme="light"]) .a2ui-surface,
.a2ui-surface:where([data-theme="light"], [data-threadplane-chat-theme="light"]) {
--a2ui-surface: #ffffff;
--a2ui-on-surface: #1a1d23;
--a2ui-on-surface-variant: rgba(0, 0, 0, 0.6);
--a2ui-outline: rgba(0, 0, 0, 0.12);
--a2ui-elevation-1: 0 1px 2px rgba(0, 0, 0, 0.06);
--a2ui-elevation-4: 0 8px 24px rgba(0, 0, 0, 0.16);
--a2ui-primary: #2f6fed;
--a2ui-error: #b3261e;
--a2ui-scrim: rgba(0, 0, 0, 0.4);
--a2ui-input-bg: #ffffff;
--a2ui-on-primary: #ffffff;
--a2ui-primary-hover: #245bd0;
--a2ui-surface-variant: #f1f3f6;
}
:where([data-theme="dark"], [data-threadplane-chat-theme="dark"]) .a2ui-surface,
.a2ui-surface:where([data-theme="dark"], [data-threadplane-chat-theme="dark"]) {
--a2ui-surface: #1a1d23;
--a2ui-on-surface: #ffffff;
--a2ui-on-surface-variant: rgba(255, 255, 255, 0.7);
--a2ui-outline: rgba(255, 255, 255, 0.1);
--a2ui-elevation-1: 0 1px 2px rgba(0, 0, 0, 0.3);
--a2ui-elevation-4: 0 8px 24px rgba(0, 0, 0, 0.5);
--a2ui-primary: #8ab4ff;
--a2ui-error: #f2b8b5;
--a2ui-scrim: rgba(0, 0, 0, 0.6);
--a2ui-input-bg: #12141a;
--a2ui-on-primary: #0b1220;
--a2ui-primary-hover: #a9c7ff;
--a2ui-surface-variant: #262a33;
}
.a2ui-text-h1,
.a2ui-text-h2,
.a2ui-text-h3,
Expand Down
9 changes: 9 additions & 0 deletions libs/react/src/a2ui/themes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ describe('A2UI theme stylesheets', () => {
expect(declared(darkBlock ?? '').size).toBeGreaterThan(8);
});

it('forces the same dark and light values from the theme attributes', () => {
const rules = [...base.matchAll(/:where\(\[data-theme="(light|dark)"\][^{]*\{([^}]*)\}/g)];
const byScheme = new Map(rules.map((m) => [m[1], declared(m[2])]));
const dark = declared(darkBlock ?? '');
expect([...(byScheme.get('dark') ?? new Map())]).toEqual([...dark]);
const light = byScheme.get('light') ?? new Map();
for (const token of dark.keys()) expect(light.has(token)).toBe(true);
});

it.each(names)('%s is one rule that outranks the base tokens', (name) => {
const css = read(`themes/${name}.css`).replace(/\/\*[\s\S]*?\*\//g, '');
const rules = [...css.matchAll(/([^{}]+)\{([^}]*)\}/g)];
Expand Down
14 changes: 13 additions & 1 deletion libs/react/src/chat/styles.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
/* Theme attribute. Same selectors and meaning as Angular chat: data-theme or
data-threadplane-chat-theme on any ancestor (or the chat itself) forces that
scheme over prefers-color-scheme. The nearest attribute wins because the
value travels as an inherited custom property, and the dark rule comes last
so dark wins when both attributes sit on one element, as in Angular. An
explicit --tp-chat-color-scheme on a chat root still wins over both. */
:where([data-theme="light"], [data-threadplane-chat-theme="light"]) {
--tp-chat-theme-scheme: light;
}
:where([data-theme="dark"], [data-threadplane-chat-theme="dark"]) {
--tp-chat-theme-scheme: dark;
}
:where(
.tp-chat,
.tp-chat-list:not(.tp-chat .tp-chat-list),
Expand Down Expand Up @@ -61,7 +73,7 @@
--tp-chat-radius-message: var(--tplane-chat-radius-bubble, 0.9375rem);
--tp-chat-radius-input: var(--tplane-chat-radius-input, 9999px);
--tp-chat-reading-width: var(--tplane-chat-max-width, 48rem);
color-scheme: var(--tp-chat-color-scheme, light dark);
color-scheme: var(--tp-chat-color-scheme, var(--tp-chat-theme-scheme, light dark));
color: var(--tp-chat-text);
font-family: var(--tp-chat-font-family);
}
Expand Down
32 changes: 31 additions & 1 deletion libs/react/src/chat/theme-tokens.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ describe('React chat theme tokens', () => {
file === 'chat/styles.css' && property.startsWith('--tp-chat-'),
);
// Tokens with no Angular counterpart keep their own default.
const reactOnly = new Set(['--tp-chat-gap']);
const reactOnly = new Set(['--tp-chat-gap', '--tp-chat-theme-scheme']);
const unmapped = root
.filter(({ property }) => !reactOnly.has(property))
// Local re-mappings (user bubble) read other --tp-chat-* tokens.
Expand Down Expand Up @@ -170,6 +170,36 @@ describe('React chat theme tokens', () => {
expect(offenders).toEqual([]);
});

it('follows the Angular theme attributes, with an explicit scheme winning', () => {
const css = stripComments(STYLESHEETS['chat/styles.css']);
// Same selectors Angular chat uses, for both attributes and both schemes.
for (const scheme of ['light', 'dark'])
expect(css).toContain(
`:where([data-theme="${scheme}"], [data-threadplane-chat-theme="${scheme}"]) {\n --tp-chat-theme-scheme: ${scheme};`,
);
// Dark is declared after light, matching Angular's precedence on one element.
expect(css.indexOf('--tp-chat-theme-scheme: dark')).toBeGreaterThan(
css.indexOf('--tp-chat-theme-scheme: light'),
);
// Precedence: explicit app scheme, then the attribute, then the system.
expect(css).toContain(
'color-scheme: var(--tp-chat-color-scheme, var(--tp-chat-theme-scheme, light dark));',
);
});

it('forces the A2UI surface scheme from the same attributes', () => {
const css = stripComments(STYLESHEETS['a2ui/styles.css']);
for (const scheme of ['light', 'dark']) {
const sel = `[data-theme="${scheme}"], [data-threadplane-chat-theme="${scheme}"]`;
expect(css).toContain(`:where(${sel}) .a2ui-surface`);
expect(css).toContain(`.a2ui-surface:where(${sel})`);
}
// The attribute rules sit after the media query so they beat it.
expect(css.indexOf('[data-theme="dark"]')).toBeGreaterThan(
css.indexOf('@media (prefers-color-scheme: dark)'),
);
});

it('draws every focus ring with the focus-ring token', () => {
const offenders = ALL.filter(
({ property, value }) =>
Expand Down
Loading