Skip to content

πŸ™Œ OpenHands: AI-Driven Development

fix: make color-theme overrides win regardless of stylesheet order (#1645)

Hiep Le12d2b5728b4a
diff --git a/__tests__/themes/color-themes.test.tsx b/__tests__/themes/color-themes.test.tsx
index 854197d4ad..e4eeb68891 100644
--- a/__tests__/themes/color-themes.test.tsx
+++ b/__tests__/themes/color-themes.test.tsx
@@ -44,6 +44,39 @@ describe("color themes", () => {
document.body.style.removeProperty("--oh-warning");
});
+ it("injects override rules with order-independent doubled scope selectors", () => {
+ // Act
+ applyColorTheme("openhands-neutral");
+
+ // Assert: doubled selectors (0,2,0) out-specify the base sheet's unlayered
+ // [data-agent-server-ui] variable rules (0,1,0), so the override wins even
+ // when React 19 re-inserts the base stylesheet <link> after this tag.
+ const styleEl = document.getElementById("oh-color-theme-override");
+ expect(styleEl?.textContent).toContain(
+ "[data-agent-server-ui][data-agent-server-ui] {",
+ );
+ expect(styleEl?.textContent).toContain("[data-theme=dark][data-theme=dark] {");
+
+ styleEl?.remove();
+ });
+
+ it("re-appends the override style tag to the end of <head> on every apply", () => {
+ // Arrange: first apply creates the tag, then a later stylesheet lands
+ // after it (as React 19 does with the base CSS <link> in the built SPA).
+ applyColorTheme("openhands-neutral");
+ const laterSheet = document.createElement("style");
+ document.head.appendChild(laterSheet);
+
+ // Act
+ applyColorTheme("openhands-deepsea");
+
+ // Assert
+ expect(document.head.lastElementChild?.id).toBe("oh-color-theme-override");
+
+ laterSheet.remove();
+ document.getElementById("oh-color-theme-override")?.remove();
+ });
+
it("applies Neo button tokens on the scoped UI root used by primary buttons", () => {
render(
<AgentServerUIRoot>
diff --git a/src/themes/color-themes.ts b/src/themes/color-themes.ts
index 51f5ac66bc..3d1687395d 100644
--- a/src/themes/color-themes.ts
+++ b/src/themes/color-themes.ts
@@ -221,13 +221,24 @@ const THEME_STYLE_TAG_ID = "oh-color-theme-override";
* PostCSS transforms :root / body to [data-agent-server-ui], so --cool-grey-*
* is set on EVERY element carrying that attribute. A body inline-style only
* overrides body itself β€” inner matching elements keep the stylesheet value.
- * Injecting a stylesheet appended to <head> wins on equal specificity because
- * later sheets take precedence in the cascade.
*
* Why heroui variables:
* HeroUI stores colors as HSL channels in --heroui-* vars on [data-theme=dark].
* They reference their own token system and are unaffected by --cool-grey-*
* changes, so we override them from the same injected sheet.
+ *
+ * Why doubled selectors + re-append on every call:
+ * "Later sheet wins the tie" cannot be relied on: in the built SPA
+ * (ssr:false, prerendered shell) React 19 re-creates the <head> elements it
+ * manages (<Meta/>/<Links/>) whenever the tree above the router remounts β€”
+ * e.g. PostHogWrapper swapping in <PostHogProvider> once the analytics key
+ * resolves, which only happens in builds with VITE_POSTHOG_CLIENT_KEY baked
+ * (the published npm package / tagged Docker images). That re-inserts the
+ * base stylesheet <link> AFTER this tag, which would let the base sheet's
+ * unlayered [data-agent-server-ui] variable rules (0,1,0) win every tie.
+ * Doubling the attribute selectors ([x][x], 0,2,0) beats them from any
+ * position in <head>; re-appending on each apply keeps document order
+ * favorable as well.
*/
export function applyColorTheme(key: ColorThemeKey): void {
if (typeof document === "undefined") return;
@@ -250,9 +261,11 @@ export function applyColorTheme(key: ColorThemeKey): void {
// portalled popover/listbox content inherits the overridden values.
// [data-theme=dark] β€” covers the inner AgentServerUIRoot wrapper so
// components scoped inside the dark theme wrapper also pick them up.
+ // Both are doubled to out-specify the base sheet regardless of stylesheet
+ // order (see the doc comment above).
const css = [
- `[data-agent-server-ui] {\n${scaleDecls}\n${herouiDecls}\n${tokenDecls}\n}`,
- `[data-theme=dark] {\n${herouiDecls}\n}`,
+ `[data-agent-server-ui][data-agent-server-ui] {\n${scaleDecls}\n${herouiDecls}\n${tokenDecls}\n}`,
+ `[data-theme=dark][data-theme=dark] {\n${herouiDecls}\n}`,
].join("\n");
let styleEl = document.getElementById(
@@ -261,9 +274,11 @@ export function applyColorTheme(key: ColorThemeKey): void {
if (!styleEl) {
styleEl = document.createElement("style");
styleEl.id = THEME_STYLE_TAG_ID;
- document.head.appendChild(styleEl);
}
styleEl.textContent = css;
+ // Re-append even when the tag already exists (appendChild relocates a
+ // connected node) so the override also stays after any re-inserted <link>.
+ document.head.appendChild(styleEl);
syncColorThemeTokensOnScopeRoots(tokens);
}