Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

🪆 fix: Resolve theme markers to the nearest themed root (#16589)

Marco Beretta5b98963ca2c5
diff --git a/e2e/specs/mock/scenarios/field-focus.spec.ts b/e2e/specs/mock/scenarios/field-focus.spec.ts
index fe42b1adf1..957639fe5e 100644
--- a/e2e/specs/mock/scenarios/field-focus.spec.ts
+++ b/e2e/specs/mock/scenarios/field-focus.spec.ts
@@ -126,6 +126,7 @@ test.describe('theme field focus', () => {
await page.evaluate((className) => {
const scope = document.createElement('div');
scope.setAttribute('data-theme-field-focus', 'border');
+ scope.style.setProperty('--theme-field-focus-style', 'border');
scope.style.setProperty('--border-field-focus', '10 20 30');
const probe = document.createElement('input');
probe.className = className;
diff --git a/e2e/specs/mock/scenarios/theme-root-nesting.spec.ts b/e2e/specs/mock/scenarios/theme-root-nesting.spec.ts
new file mode 100644
index 0000000000..dca1685915
--- /dev/null
+++ b/e2e/specs/mock/scenarios/theme-root-nesting.spec.ts
@@ -0,0 +1,171 @@
+import { expect, test } from '@playwright/test';
+import type { Page } from '@playwright/test';
+import { disabledFillClasses } from '../../../../packages/client/src/utils/theme';
+import { NEW_CHAT_PATH } from '../helpers';
+
+/**
+ * A control follows the theme root nearest to it. `applyTheme` writes `--theme-field-focus-style`
+ * and `--theme-disabled-style` on every root it themes (and mirrors a non-default choice as a
+ * `data-theme-*` attribute), and the variants read the inherited property, so a root themed with
+ * the default style inside one themed with the other gets the default behavior back, and a third
+ * level gets the other one again. The roots below write what `applyTheme` writes.
+ */
+
+type Root = { field: 'ring' | 'border'; disabled: 'dim' | 'fill' };
+
+/** The focus classes `fieldBase` carries, so nothing but the shared field rules styles the probe. */
+const FIELD_CLASSES =
+ 'lc-field border border-border-control focus-visible:outline-hidden focus-visible:ring-2 focus-visible:ring-focus-control theme-field-border:focus:border-border-field-focus theme-field-border:focus-visible:ring-1 theme-field-border:focus-visible:ring-border-field-focus';
+
+/** The disabled treatment every primitive carries: half opacity, or the fill a `fill` theme paints. */
+const DISABLED_CLASSES = `disabled:opacity-50 ${disabledFillClasses}`;
+
+/** The edge a border-focus field swaps to, set on the outermost root so every level inherits it. */
+const FIELD_FOCUS_EDGE = 'rgb(10, 20, 30)';
+
+/**
+ * Builds nested roots, outermost first, each holding a field and a disabled button labelled by
+ * its depth, and returns the depths.
+ */
+async function nestRoots(page: Page, roots: Root[]): Promise<number[]> {
+ await page.evaluate(
+ ([levels, fieldClasses, disabledClasses]) => {
+ let parent: HTMLElement = document.body;
+ levels.forEach((level, depth) => {
+ const root = document.createElement('div');
+ root.dataset.theme = `nested-${depth}`;
+ root.style.setProperty('--theme-field-focus-style', level.field);
+ root.style.setProperty('--theme-disabled-style', level.disabled);
+ if (level.field === 'border') {
+ root.setAttribute('data-theme-field-focus', 'border');
+ }
+ if (level.disabled === 'fill') {
+ root.setAttribute('data-theme-disabled', 'fill');
+ }
+ if (depth === 0) {
+ root.style.setProperty('--border-field-focus', '10 20 30');
+ }
+ const field = document.createElement('input');
+ field.className = fieldClasses;
+ field.setAttribute('aria-label', `field ${depth}`);
+ const button = document.createElement('button');
+ button.className = disabledClasses;
+ button.disabled = true;
+ button.textContent = `button ${depth}`;
+ root.append(field, button);
+ parent.append(root);
+ parent = root;
+ });
+ },
+ [roots, FIELD_CLASSES, DISABLED_CLASSES] as const,
+ );
+ return roots.map((_, depth) => depth);
+}
+
+async function openChat(page: Page) {
+ await page.goto(NEW_CHAT_PATH, { timeout: 15000 });
+ await expect(page.getByRole('textbox', { name: 'Message input' })).toBeVisible({
+ timeout: 30000,
+ });
+}
+
+/** The ring's spread on keyboard focus (`2px` for the ring style, `1px` for the edge one). */
+async function keyboardRing(page: Page, depth: number): Promise<string> {
+ const field = page.getByRole('textbox', { name: `field ${depth}` });
+ await field.click();
+ await page.keyboard.press('Shift+Tab');
+ await page.keyboard.press('Tab');
+ await expect(field).toBeFocused();
+ return field.evaluate((node) => {
+ const layers = getComputedStyle(node).boxShadow.split(/,(?![^(]*\))/);
+ const ring = layers.find((layer) => !/0px 0px 0px 0px\s*$/.test(layer.trim())) ?? 'none';
+ return /(\d+px)\s*$/.exec(ring.trim())?.[1] ?? ring;
+ });
+}
+
+/** The edge a pointer-focused field keeps: the swapped edge, or the reset one. */
+async function pointerEdge(page: Page, depth: number): Promise<string> {
+ const field = page.getByRole('textbox', { name: `field ${depth}` });
+ await page.evaluate(() =>
+ document.documentElement.setAttribute('data-input-modality', 'pointer'),
+ );
+ await field.click();
+ await expect(field).toBeFocused();
+ return field.evaluate((node) => getComputedStyle(node).borderTopColor);
+}
+
+const disabledOpacity = (page: Page, depth: number) =>
+ page
+ .getByRole('button', { name: `button ${depth}` })
+ .evaluate((node) => getComputedStyle(node).opacity);
+
+test.describe('nested theme roots', () => {
+ test('a field follows the focus style of its nearest themed root @scenario:nested-theme-roots-field-focus', async ({
+ page,
+ }) => {
+ await openChat(page);
+ const depths = await nestRoots(page, [
+ { field: 'border', disabled: 'dim' },
+ { field: 'ring', disabled: 'dim' },
+ { field: 'border', disabled: 'dim' },
+ ]);
+
+ const rings = [];
+ for (const depth of depths) {
+ rings.push(await keyboardRing(page, depth));
+ }
+ expect(rings).toEqual(['1px', '2px', '1px']);
+
+ const edges = [];
+ for (const depth of depths) {
+ edges.push(await pointerEdge(page, depth));
+ }
+ expect(edges[0]).toBe(FIELD_FOCUS_EDGE);
+ expect(edges[1]).not.toBe(FIELD_FOCUS_EDGE);
+ expect(edges[2]).toBe(FIELD_FOCUS_EDGE);
+ });
+
+ test('a ring root keeps its ring and a border root nested in it swaps the edge @scenario:nested-theme-roots-border-inside-ring', async ({
+ page,
+ }) => {
+ await openChat(page);
+ const depths = await nestRoots(page, [
+ { field: 'ring', disabled: 'dim' },
+ { field: 'border', disabled: 'dim' },
+ ]);
+
+ const rings = [];
+ for (const depth of depths) {
+ rings.push(await keyboardRing(page, depth));
+ }
+ expect(rings).toEqual(['2px', '1px']);
+ });
+
+ test('a disabled control follows the disabled style of its nearest themed root @scenario:nested-theme-roots-disabled', async ({
+ page,
+ }) => {
+ await openChat(page);
+ const fillOuter = await nestRoots(page, [
+ { field: 'ring', disabled: 'fill' },
+ { field: 'ring', disabled: 'dim' },
+ { field: 'ring', disabled: 'fill' },
+ ]);
+ const opacities = [];
+ for (const depth of fillOuter) {
+ opacities.push(await disabledOpacity(page, depth));
+ }
+ expect(opacities).toEqual(['1', '0.5', '1']);
+
+ await openChat(page);
+ const dimOuter = await nestRoots(page, [
+ { field: 'ring', disabled: 'dim' },
+ { field: 'ring', disabled: 'fill' },
+ { field: 'ring', disabled: 'dim' },
+ ]);
+ const dimmed = [];
+ for (const depth of dimOuter) {
+ dimmed.push(await disabledOpacity(page, depth));
+ }
+ expect(dimmed).toEqual(['0.5', '1', '0.5']);
+ });
+});
diff --git a/packages/client/src/components/Field.css b/packages/client/src/components/Field.css
index 263f3a660d..c67fd3fd19 100644
--- a/packages/client/src/components/Field.css
+++ b/packages/client/src/components/Field.css
@@ -4,27 +4,34 @@
by useInputModality (mount it once near the app root). Without the hook the
attribute is absent and the ring always shows, which is the safe fallback.
A theme that focuses fields by swapping their edge (`fieldFocusStyle: border`)
- keeps that edge on pointer focus too, so only the edge reset skips it, for a
- field under any root that theme is applied to, not only the document. */
+ keeps that edge on pointer focus too, so it puts the edge back over the
+ reset. The style is read from the `--theme-field-focus-style` property of the
+ nearest themed root, so a field under a ring root nested in a border root is
+ reset again, and a browser without style queries keeps the default reset. */
html[data-input-modality='pointer'] .lc-field:focus,
html[data-input-modality='pointer'] .lc-field:focus-visible {
+ border-color: rgb(var(--border-light) / var(--border-light-alpha, 1));
box-shadow: none;
outline: none;
}
-html[data-input-modality='pointer'] .lc-field:not([data-theme-field-focus='border'] *):focus,
-html[data-input-modality='pointer']
- .lc-field:not([data-theme-field-focus='border'] *):focus-visible {
- border-color: rgb(var(--border-light) / var(--border-light-alpha, 1));
+@container style(--theme-field-focus-style: border) {
+ html[data-input-modality='pointer'] .lc-field:is(:root *):focus,
+ html[data-input-modality='pointer'] .lc-field:is(:root *):focus-visible {
+ border-color: rgb(var(--border-field-focus, var(--focus-control)));
+ }
}
/* A field focused by its edge draws no outline either. The app's dark-mode
`:focus-visible` outline is unlayered, so it outranks the field's own
`focus-visible:outline-hidden` utility and would add a second indicator
- around the swapped edge. Forced colors drop the edge color and the ring, so
- there the outline stays as the only indicator. */
+ around the swapped edge; `:is(:root *)` keeps this rule's weight above it.
+ Forced colors drop the edge color and the ring, so there the outline stays
+ as the only indicator. */
@media not (forced-colors: active) {
- [data-theme-field-focus='border'] .lc-field:focus-visible {
- outline: none;
+ @container style(--theme-field-focus-style: border) {
+ .lc-field:is(:root *):focus-visible {
+ outline: none;
+ }
}
}
diff --git a/packages/client/src/theme/README.md b/packages/client/src/theme/README.md
index f04c7aacbb..702c0a11db 100644
--- a/packages/client/src/theme/README.md
+++ b/packages/client/src/theme/README.md
@@ -351,7 +351,12 @@ Each status family has a foreground, a `-subtle` background, a `-border`, and a
default) draws the keyboard-only focus ring, and `border` swaps the field's edge
to `border-field-focus` on any focus, adding a 1px ring in that color on keyboard
focus so the indicator keeps the 2px focus floor, through the
- `theme-field-border:` variant `applyTheme` enables with `data-theme-field-focus`.
+ `theme-field-border:` variant.
+- Nested theme roots - `fieldFocusStyle` and `disabledStyle` are read from the
+ `--theme-field-focus-style` and `--theme-disabled-style` properties
+ `applyTheme` writes on every root it themes, through CSS style queries, so a
+ control follows the nearest themed root. A browser without style queries
+ (Firefox before 151) keeps the default `ring` and `dim` treatment.
`Label` reads `labelSize` (follows `textSm`), `labelLeading` and
`labelFontWeight` (`inherit` by default, so a label keeps the weight around it).
- `text-badge-label` - The shared `Badge`'s resting label ink; a hovered or selected
diff --git a/packages/client/src/theme/tokens.css b/packages/client/src/theme/tokens.css
index a89fbd434d..544f5ef651 100644
--- a/packages/client/src/theme/tokens.css
+++ b/packages/client/src/theme/tokens.css
@@ -196,20 +196,41 @@
}
/* A theme whose `disabledStyle` is `fill` paints a disabled control in the
- * disabled roles instead of fading it; `ThemeProvider` mirrors that choice as
- * `data-theme-disabled` on the root. The default `dim` style keeps the
- * `disabled:opacity-50` every primitive already carries. `:is()` carries the
- * attribute's weight, so the fill outranks a checked or variant fill. Radix
- * marks a disabled part that is not a form control (a slider thumb, a menu
- * item) with `data-disabled`, and a composite widget row with `aria-disabled`;
- * both count too. */
-@custom-variant theme-disabled (&:is([data-theme-disabled='fill'] *):is(:disabled, [data-disabled], [aria-disabled='true']));
+ * disabled roles instead of fading it. The choice is read from the
+ * `--theme-disabled-style` property `applyTheme` writes on every root it themes,
+ * so a control inherits it from the nearest themed root: a `dim` root nested in
+ * a `fill` one fades its controls again. The default `dim` style keeps the
+ * `disabled:opacity-50` every primitive already carries. `:is(:root *)` matches
+ * every element and carries a pseudo-class's weight, so the fill outranks a
+ * checked or variant fill. Radix marks a disabled part that is not a form
+ * control (a slider thumb, a menu item) with `data-disabled`, and a composite
+ * widget row with `aria-disabled`; both count too. */
+@custom-variant theme-disabled {
+ @container style(--theme-disabled-style: fill) {
+ &:is(:root *):is(:disabled, [data-disabled], [aria-disabled='true']) {
+ @slot;
+ }
+ }
+}
/* The same fill for a wrapper whose disabled control sits inside it, as
* `rc-input-number` renders its input inside the element that takes the
* component's classes. */
-@custom-variant theme-disabled-within (&:is([data-theme-disabled='fill'] *):has(:disabled));
+@custom-variant theme-disabled-within {
+ @container style(--theme-disabled-style: fill) {
+ &:is(:root *):has(:disabled) {
+ @slot;
+ }
+ }
+}
/* A theme whose `fieldFocusStyle` is `border` swaps a focused field's edge color instead of
- * drawing a ring; `applyTheme` mirrors that choice as `data-theme-field-focus` on the root. */
-@custom-variant theme-field-border (&:is([data-theme-field-focus='border'] *));
+ * drawing a ring. Read, like the disabled style, from the `--theme-field-focus-style` property
+ * the nearest themed root carries. */
+@custom-variant theme-field-border {
+ @container style(--theme-field-focus-style: border) {
+ &:is(:root *) {
+ @slot;
+ }
+ }
+}
diff --git a/packages/client/src/theme/utils/applyTheme.spec.ts b/packages/client/src/theme/utils/applyTheme.spec.ts
index f838a500ae..52eca774e2 100644
--- a/packages/client/src/theme/utils/applyTheme.spec.ts
+++ b/packages/client/src/theme/utils/applyTheme.spec.ts
@@ -460,6 +460,34 @@ describe('applyTheme', () => {
expect(root.hasAttribute(THEME_FIELD_FOCUS_ATTRIBUTE)).toBe(false);
});
+ it('writes the default focus and disabled styles on a nested root, so it overrides its ancestor', () => {
+ const outer = document.createElement('div');
+ const inner = document.createElement('div');
+ outer.append(inner);
+ document.body.append(outer);
+ const edge: ThemeDefinition = {
+ version: 1,
+ name: 'edge-fill-reference',
+ modes: { light: { appearance: { fieldFocusStyle: 'border', disabledStyle: 'fill' } } },
+ };
+ try {
+ applyResolvedTheme(resolveTheme(edge, 'light'), outer);
+ applyResolvedTheme(resolveTheme({ ...edge, modes: {} }, 'light'), inner);
+
+ /** The style queries read these inherited properties, so the nearest root decides. */
+ expect(outer.style.getPropertyValue('--theme-field-focus-style')).toBe('border');
+ expect(outer.style.getPropertyValue('--theme-disabled-style')).toBe('fill');
+ expect(inner.style.getPropertyValue('--theme-field-focus-style')).toBe('ring');
+ expect(inner.style.getPropertyValue('--theme-disabled-style')).toBe('dim');
+
+ clearAppliedTheme(inner);
+ expect(inner.style.getPropertyValue('--theme-field-focus-style')).toBe('');
+ expect(inner.style.getPropertyValue('--theme-disabled-style')).toBe('');
+ } finally {
+ outer.remove();
+ }
+ });
+
it('focuses the fields of a legacy theme in the focus color it names', () => {
const root = document.documentElement;
diff --git a/packages/client/src/theme/utils/applyTheme.ts b/packages/client/src/theme/utils/applyTheme.ts
index dc8b562cc2..335e9afabf 100644
--- a/packages/client/src/theme/utils/applyTheme.ts
+++ b/packages/client/src/theme/utils/applyTheme.ts
@@ -167,8 +167,9 @@ function mapAppearance(appearance: IThemeAppearance): Array<[string, string]> {
}
/**
- * Mirrors the applied theme's `disabledStyle` on the root, where the
- * `theme-disabled:` variant reads it. Absent means the default `dim` style.
+ * Mirrors the applied theme's `disabledStyle` on the root for host stylesheets and tests. Absent
+ * means the default `dim` style. The `theme-disabled:` variants read the inherited
+ * `--theme-disabled-style` property instead, so the nearest themed root decides.
*/
export const THEME_DISABLED_ATTRIBUTE = 'data-theme-disabled';
@@ -180,8 +181,9 @@ export const THEME_DISABLED_ATTRIBUTE = 'data-theme-disabled';
export const THEME_SCOPE_ATTRIBUTE = 'data-theme-scope';
/**
- * Mirrors the applied theme's `fieldFocusStyle` on the root, where the `theme-field-border:`
- * variant reads it. Absent means the default `ring` style.
+ * Mirrors the applied theme's `fieldFocusStyle` on the root, like `THEME_DISABLED_ATTRIBUTE`.
+ * The `theme-field-border:` variant and `Field.css` read the inherited
+ * `--theme-field-focus-style` property instead, so the nearest themed root decides.
*/
export const THEME_FIELD_FOCUS_ATTRIBUTE = 'data-theme-field-focus';