From 747d19b5b2e1921795f598f0c59ce2d6fe1ea300 Mon Sep 17 00:00:00 2001 From: Brian Love Date: Tue, 17 Nov 2026 15:46:02 -0800 Subject: [PATCH] feat(react): follow a data-theme ancestor in React chat Angular chat forces light or dark from [data-theme] or [data-threadplane-chat-theme] on any ancestor. React chat only read prefers-color-scheme, so an app theme toggle never reached it. The chat color-scheme now reads an inherited scheme set by those attributes (nearest wins, dark last on one element), and an explicit --tp-chat-color-scheme still wins. The A2UI surface stylesheet, which used a prefers-color-scheme block, follows the same attributes. Co-Authored-By: Claude Sonnet 5.5 --- .../docs/react/chat/guides/theming.mdx | 2 +- libs/react/README.md | 4 +++ libs/react/src/a2ui/styles.css | 35 +++++++++++++++++++ libs/react/src/a2ui/themes.spec.ts | 9 +++++ libs/react/src/chat/styles.css | 14 +++++++- libs/react/src/chat/theme-tokens.spec.ts | 32 ++++++++++++++++- 6 files changed, 93 insertions(+), 3 deletions(-) diff --git a/apps/website/content/docs/react/chat/guides/theming.mdx b/apps/website/content/docs/react/chat/guides/theming.mdx index fae2ce417..f81d6904a 100644 --- a/apps/website/content/docs/react/chat/guides/theming.mdx +++ b/apps/website/content/docs/react/chat/guides/theming.mdx @@ -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. diff --git a/libs/react/README.md b/libs/react/README.md index b12ea107b..64774b357 100644 --- a/libs/react/README.md +++ b/libs/react/README.md @@ -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. diff --git a/libs/react/src/a2ui/styles.css b/libs/react/src/a2ui/styles.css index 43fabd734..88a352b0c 100644 --- a/libs/react/src/a2ui/styles.css +++ b/libs/react/src/a2ui/styles.css @@ -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, diff --git a/libs/react/src/a2ui/themes.spec.ts b/libs/react/src/a2ui/themes.spec.ts index 97cfa027d..6b5890ae2 100644 --- a/libs/react/src/a2ui/themes.spec.ts +++ b/libs/react/src/a2ui/themes.spec.ts @@ -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)]; diff --git a/libs/react/src/chat/styles.css b/libs/react/src/chat/styles.css index 19c3dc71f..51b2f8e63 100644 --- a/libs/react/src/chat/styles.css +++ b/libs/react/src/chat/styles.css @@ -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), @@ -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); } diff --git a/libs/react/src/chat/theme-tokens.spec.ts b/libs/react/src/chat/theme-tokens.spec.ts index 3213fb384..c08a58e5b 100644 --- a/libs/react/src/chat/theme-tokens.spec.ts +++ b/libs/react/src/chat/theme-tokens.spec.ts @@ -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. @@ -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 }) =>