You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "shivaam (via GitHub)" <gi...@apache.org> on 2026/04/05 19:26:49 UTC

[PR] UI: Rework Monaco editor theme to match Chakra UI palette [airflow]

shivaam opened a new pull request, #64748:
URL: https://github.com/apache/airflow/pull/64748

   ## Summary
   
   Register custom `airflow-light` / `airflow-dark` Monaco themes derived from Chakra UI color tokens so that every Monaco editor in the UI (DAG Code viewer, code diff viewer, JSON editor, RenderedJsonField) visually integrates with the rest of the app instead of using Monaco's default `vs` / `vs-dark` themes.
   
   closes: #64253
   
   ## Motivation
   
   Monaco was adopted across the UI via https://github.com/apache/airflow/pull/55467 (DAG code viewer + diff) and https://github.com/apache/airflow/pull/62708 (JSON editors) but has always shipped with the default VS Code themes. The result is a visible mismatch between editor panels and Chakra UI-themed surfaces — editors look like a VS Code window embedded inside a different application.
   
   ## Implementation notes
   
   - **New hook `useMonacoTheme`** (`src/context/colorMode/useMonacoTheme.ts`) — registers both custom themes exactly once via a module-level flag, returns a stable `beforeMount` callback and the correct theme name for the current color mode.
   - **`inherit: true` + empty `rules`** — Monaco's default syntax highlighting (keywords, strings, comments) is preserved. Only editor shell colors (background, foreground, gutter, selection, scrollbar, line numbers) are overridden.
   - **OKLCH → hex conversion via `getImageData`** — Chakra v3 uses OKLCH colors. `ctx.fillStyle` readback cannot be relied on because Chrome 111+ preserves the OKLCH string instead of converting to hex, which Monaco would silently ignore. Rasterizing a single pixel and reading back its sRGB bytes is the only reliable path.
   - **Single registration** — themes are registered once per session via a module-level flag so pages with many editor instances (e.g. XCom table with many `RenderedJsonField` rows) don't pay the cost on every mount.
   - All four Monaco usages (`JsonEditor`, `RenderedJsonField`, `Code`, `CodeDiffViewer`) are updated to use the hook. No changes to component structure, dynamic height behavior, or any other behavior beyond theme wiring.
   
   ## How to test
   
   1. `breeze start-airflow --dev-mode --load-example-dags`
   2. Visit the DAG Code tab, XCom page, Connections > Edit > Extra field, Trigger DAG dialog, DAG Details page
   3. Toggle between light and dark mode — editor shell colors should smoothly match the surrounding Chakra UI surfaces instead of the default VS Code palette
   4. Open devtools console on the DAG Code tab and run:
      ```js
      const editor = document.querySelector('.monaco-editor .monaco-editor-background');
      console.log(getComputedStyle(editor).backgroundColor);
      ```
      In light mode this should report `rgb(248, 250, 253)` (≈ `#f8fafd`, Chakra `gray.50`), not `rgb(255, 255, 255)` (VS Code default)
   5. `pnpm lint` — zero errors
   6. `pnpm test` — 185 tests pass (179 existing + 6 new for the hook)
   
   ## Tests
   
   New file `src/context/colorMode/useMonacoTheme.test.ts` covers:
   - Theme name returned for `colorMode = "light" | "dark" | undefined`
   - `defineTheme` invoked twice (light + dark) on first `beforeMount`
   - Module-level guard prevents re-registration on subsequent mounts
   - Both registered themes use `inherit: true` and empty `rules`
   
   The OKLCH→hex canvas conversion itself is validated end-to-end in a real browser during manual testing — jsdom/happy-dom do not implement the full Canvas2D color pipeline so unit-testing that path would only exercise the mock, not the real behaviour.
   
   ## Related work
   
   There is an earlier attempt at this same issue in https://github.com/apache/airflow/pull/64268. That PR bundles bulk task instance actions from #63855 and uses `ctx.fillStyle` readback for the OKLCH conversion, which silently falls back to default themes on Chrome 111+. This PR is scope-isolated to the Monaco theme fix and uses the `getImageData` approach which I verified works against Chrome's current behavior.
   
   ---
   
   ##### Was generative AI tooling used to co-author this PR?
   
   - [x] Yes — Claude Code (Opus 4.6)
   
   Generated-by: Claude Code (Opus 4.6) following [the guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#gen-ai-assisted-contributions)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] UI: Rework Monaco editor theme to match Chakra UI palette [airflow]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #64748:
URL: https://github.com/apache/airflow/pull/64748#issuecomment-4245353015

   ### Backport successfully created: v3-2-test
   
   Note: As of [Merging PRs targeted for Airflow 3.X](https://github.com/apache/airflow/blob/main/dev/README_AIRFLOW3_DEV.md#merging-prs-targeted-for-airflow-3x)
   the committer who merges the PR is responsible for backporting the PRs that are bug fixes (generally speaking) to the maintenance branches.
   
   In matter of doubt please ask in [#release-management](https://apache-airflow.slack.com/archives/C03G9H97MM2) Slack channel.
   
   <table>
           <tr>
           <th>Status</th>
           <th>Branch</th>
           <th>Result</th>
       </tr>
       <tr>
           <td>✅</td>
           <td>v3-2-test</td>
           <td><a href="https://github.com/apache/airflow/pull/65228"><img src="https://img.shields.io/badge/PR-65228-blue" alt="PR Link"></a></td>
       </tr>
   </table>


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] UI: Rework Monaco editor theme to match Chakra UI palette [airflow]

Posted by "pierrejeambrun (via GitHub)" <gi...@apache.org>.
pierrejeambrun commented on code in PR #64748:
URL: https://github.com/apache/airflow/pull/64748#discussion_r3064978861


##########
airflow-core/src/airflow/ui/src/context/colorMode/useMonacoTheme.ts:
##########
@@ -0,0 +1,121 @@
+/*!
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+import type { Monaco } from "@monaco-editor/react";
+
+import { useColorMode } from "./useColorMode";
+
+const LIGHT_THEME_NAME = "airflow-light";
+const DARK_THEME_NAME = "airflow-dark";
+
+let themesRegistered = false;
+
+// Convert any CSS color (including modern color spaces like OKLCH that Chakra
+// UI uses) to a #rrggbb string that Monaco's `defineTheme` accepts.
+//
+// We rasterize a single pixel and read back its sRGB bytes via `getImageData`.
+// `ctx.fillStyle` readback does NOT work for this: starting in Chrome 111, it
+// preserves the original OKLCH string instead of converting to hex, which
+// Monaco would silently ignore.
+const cssVarToHex = (ctx: CanvasRenderingContext2D, cssVar: string): string => {
+  const value = getComputedStyle(document.documentElement).getPropertyValue(cssVar).trim();
+
+  if (value === "") {
+    return "#000000";
+  }
+
+  ctx.fillStyle = value;
+  ctx.clearRect(0, 0, 1, 1);
+  ctx.fillRect(0, 0, 1, 1);
+  const [red, green, blue] = ctx.getImageData(0, 0, 1, 1).data;
+
+  return `#${[red, green, blue].map((channel) => (channel ?? 0).toString(16).padStart(2, "0")).join("")}`;

Review Comment:
   This piece looks brittle. Should we replace it with a proper third party lib doing that? Such as "culori" ? 
   
   ```
     import { formatHex, parse } from "culori";                                                                                                                    
                                                                                                                                                                   
     const cssVarToHex = (cssVar: string): string => {                                                                                                             
       const value = getComputedStyle(document.documentElement).getPropertyValue(cssVar).trim();                                                                   
       return formatHex(parse(value)) ?? "#000000";                                                                                                                
     };                                                                                                                                                            
                                                                                                                                                                   ```
   
   Would make things cleaner I believe. (And with tree shaking and stuff only `formatHex, parse` would actually be in the final bundle. What do you think? 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] UI: Rework Monaco editor theme to match Chakra UI palette [airflow]

Posted by "shivaam (via GitHub)" <gi...@apache.org>.
shivaam commented on PR #64748:
URL: https://github.com/apache/airflow/pull/64748#issuecomment-4229501354

   Pushed 7dba46a addressing review feedback:
   
   Canvas → culori for color conversion (@pierrejeambrun)
   afterEach(vi.restoreAllMocks()) to prevent mock leak (@Copilot)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] UI: Rework Monaco editor theme to match Chakra UI palette [airflow]

Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #64748:
URL: https://github.com/apache/airflow/pull/64748#discussion_r3066484239


##########
airflow-core/src/airflow/ui/src/context/colorMode/useMonacoTheme.test.ts:
##########
@@ -0,0 +1,132 @@
+/*!
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+import type { Monaco } from "@monaco-editor/react";
+import { renderHook } from "@testing-library/react";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+// `useColorMode` is the only dependency of the hook we want to test. We mock
+// it with a mutable return so individual tests can drive light/dark behaviour.
+const colorModeMock = vi.fn<() => { colorMode: "dark" | "light" | undefined }>();
+
+vi.mock("./useColorMode", () => ({
+  useColorMode: () => colorModeMock(),
+}));
+
+// The hook registers Monaco themes exactly once via a module-level flag. We
+// reset modules between tests so each test starts with a fresh flag state.
+const loadHook = async () => {
+  const module = await import("./useMonacoTheme");
+
+  return module.useMonacoTheme;
+};
+
+const createFakeMonaco = () => {
+  const defineTheme = vi.fn();
+
+  return { defineTheme, monaco: { editor: { defineTheme } } as unknown as Monaco };
+};
+
+// happy-dom does not implement Canvas2D rendering, so the hook's `getContext`
+// call would return null and short-circuit before registering themes. We stub
+// it with the minimal surface the hook needs: the draw/clear methods and a
+// `getImageData` that returns arbitrary RGB bytes (the exact hex values are
+// irrelevant to these tests — we only care that theme registration happens).
+const stubCanvasContext = () => {
+  const fakeContext = {
+    clearRect: vi.fn(),
+    fillRect: vi.fn(),
+    fillStyle: "",
+    getImageData: vi.fn(() => ({ data: new Uint8ClampedArray([0, 0, 0, 255]) })),
+  };
+
+  vi.spyOn(HTMLCanvasElement.prototype, "getContext").mockReturnValue(
+    fakeContext as unknown as CanvasRenderingContext2D,
+  );

Review Comment:
   The test stubs `HTMLCanvasElement.prototype.getContext` via `vi.spyOn(...).mockReturnValue(...)` but never restores it. This leaks a mocked `getContext` into subsequent tests in the same run and can cause cross-test interference/flakiness. Add an `afterEach` that calls `vi.restoreAllMocks()` (or explicitly `mockRestore()` the `getContext` spy).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] UI: Rework Monaco editor theme to match Chakra UI palette [airflow]

Posted by "shivaam (via GitHub)" <gi...@apache.org>.
shivaam commented on PR #64748:
URL: https://github.com/apache/airflow/pull/64748#issuecomment-4255180650

   > Cool! Thanks for this UX improvement!
   
   You are welcome. Happy to contribute:)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] UI: Rework Monaco editor theme to match Chakra UI palette [airflow]

Posted by "shivaam (via GitHub)" <gi...@apache.org>.
shivaam commented on PR #64748:
URL: https://github.com/apache/airflow/pull/64748#issuecomment-4207438141

   > Just one nit, but directions looks good to me.
   > 
   > Let me know when this is out of draft (fix static checks, CI more generally) and it's ready for a more in depth review + local testing.
   
   @pierrejeambrun handled the nits and the PR is ready for local testing 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] UI: Rework Monaco editor theme to match Chakra UI palette [airflow]

Posted by "pierrejeambrun (via GitHub)" <gi...@apache.org>.
pierrejeambrun commented on PR #64748:
URL: https://github.com/apache/airflow/pull/64748#issuecomment-4245272204

   Code looks good but the feature isn't working anymore it appears:
   
   <img width="1879" height="873" alt="Screenshot 2026-04-14 at 17 47 32" src="https://github.com/user-attachments/assets/5113aede-3712-4b5c-abc8-062f8dc1d5e2" />
   
   Background color should be the one from Chakre, more 'dark blue' than the default brown of the monaco editor (cf screenshot above when it was working)
   
   And selection should pick up the custom theme as earlier in your PR:
   <img width="1714" height="781" alt="Screenshot 2026-04-14 at 17 48 29" src="https://github.com/user-attachments/assets/0b86c0fc-09e2-4e6f-8ff1-a70830d0e1e9" />
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] UI: Rework Monaco editor theme to match Chakra UI palette [airflow]

Posted by "shivaam (via GitHub)" <gi...@apache.org>.
shivaam commented on code in PR #64748:
URL: https://github.com/apache/airflow/pull/64748#discussion_r3037267669


##########
airflow-core/src/airflow/ui/src/context/colorMode/useMonacoTheme.ts:
##########
@@ -0,0 +1,126 @@
+/*!
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+import type { Monaco } from "@monaco-editor/react";
+import { useCallback } from "react";
+
+import { useColorMode } from "./useColorMode";
+
+const LIGHT_THEME_NAME = "airflow-light";
+const DARK_THEME_NAME = "airflow-dark";
+
+let themesRegistered = false;
+

Review Comment:
   Not sure if this is the best way to get the color theme.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] UI: Rework Monaco editor theme to match Chakra UI palette [airflow]

Posted by "jscheffl (via GitHub)" <gi...@apache.org>.
jscheffl commented on PR #64748:
URL: https://github.com/apache/airflow/pull/64748#issuecomment-4254811638

   Cool! Thanks for this UX improvement!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] UI: Rework Monaco editor theme to match Chakra UI palette [airflow]

Posted by "shivaam (via GitHub)" <gi...@apache.org>.
shivaam commented on code in PR #64748:
URL: https://github.com/apache/airflow/pull/64748#discussion_r3068062167


##########
airflow-core/src/airflow/ui/src/context/colorMode/useMonacoTheme.ts:
##########
@@ -0,0 +1,121 @@
+/*!
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+import type { Monaco } from "@monaco-editor/react";
+
+import { useColorMode } from "./useColorMode";
+
+const LIGHT_THEME_NAME = "airflow-light";
+const DARK_THEME_NAME = "airflow-dark";
+
+let themesRegistered = false;
+
+// Convert any CSS color (including modern color spaces like OKLCH that Chakra
+// UI uses) to a #rrggbb string that Monaco's `defineTheme` accepts.
+//
+// We rasterize a single pixel and read back its sRGB bytes via `getImageData`.
+// `ctx.fillStyle` readback does NOT work for this: starting in Chrome 111, it
+// preserves the original OKLCH string instead of converting to hex, which
+// Monaco would silently ignore.
+const cssVarToHex = (ctx: CanvasRenderingContext2D, cssVar: string): string => {
+  const value = getComputedStyle(document.documentElement).getPropertyValue(cssVar).trim();
+
+  if (value === "") {
+    return "#000000";
+  }
+
+  ctx.fillStyle = value;
+  ctx.clearRect(0, 0, 1, 1);
+  ctx.fillRect(0, 0, 1, 1);
+  const [red, green, blue] = ctx.getImageData(0, 0, 1, 1).data;
+
+  return `#${[red, green, blue].map((channel) => (channel ?? 0).toString(16).padStart(2, "0")).join("")}`;

Review Comment:
   Agreed and I have updated the code to use culori. I didnt use it earleir as I was avoiding brining in more dependencies but you are right the tree shaking will bring only a few extra things. 
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] UI: Rework Monaco editor theme to match Chakra UI palette [airflow]

Posted by "pierrejeambrun (via GitHub)" <gi...@apache.org>.
pierrejeambrun commented on code in PR #64748:
URL: https://github.com/apache/airflow/pull/64748#discussion_r3046244412


##########
airflow-core/src/airflow/ui/src/context/colorMode/useMonacoTheme.ts:
##########
@@ -0,0 +1,126 @@
+/*!
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+import type { Monaco } from "@monaco-editor/react";
+import { useCallback } from "react";
+
+import { useColorMode } from "./useColorMode";
+
+const LIGHT_THEME_NAME = "airflow-light";
+const DARK_THEME_NAME = "airflow-dark";
+
+let themesRegistered = false;
+
+// Convert any CSS color (including modern color spaces like OKLCH that Chakra
+// UI uses) to a #rrggbb string that Monaco's `defineTheme` accepts.
+//
+// We rasterize a single pixel and read back its sRGB bytes via `getImageData`.
+// `ctx.fillStyle` readback does NOT work for this: starting in Chrome 111, it
+// preserves the original OKLCH string instead of converting to hex, which
+// Monaco would silently ignore.
+const cssVarToHex = (ctx: CanvasRenderingContext2D, cssVar: string): string => {
+  const value = getComputedStyle(document.documentElement).getPropertyValue(cssVar).trim();
+
+  if (value === "") {
+    return "#000000";
+  }
+
+  ctx.fillStyle = value;
+  ctx.clearRect(0, 0, 1, 1);
+  ctx.fillRect(0, 0, 1, 1);
+  const [red, green, blue] = ctx.getImageData(0, 0, 1, 1).data;
+
+  return `#${[red, green, blue].map((channel) => (channel ?? 0).toString(16).padStart(2, "0")).join("")}`;
+};
+
+const defineAirflowMonacoThemes = (monaco: Monaco) => {
+  if (themesRegistered) {
+    return;
+  }
+
+  const canvas = document.createElement("canvas");
+
+  canvas.width = 1;
+  canvas.height = 1;
+  const ctx = canvas.getContext("2d");
+
+  if (!ctx) {
+    return;
+  }
+
+  const toHex = (cssVar: string) => cssVarToHex(ctx, cssVar);
+
+  monaco.editor.defineTheme(LIGHT_THEME_NAME, {
+    base: "vs",
+    colors: {
+      "editor.background": toHex("--chakra-colors-gray-50"),
+      "editor.foreground": toHex("--chakra-colors-gray-900"),
+      "editor.inactiveSelectionBackground": toHex("--chakra-colors-gray-200"),
+      "editor.lineHighlightBackground": toHex("--chakra-colors-gray-100"),
+      "editor.selectionBackground": toHex("--chakra-colors-brand-200"),
+      "editorGutter.background": toHex("--chakra-colors-gray-50"),
+      "editorLineNumber.activeForeground": toHex("--chakra-colors-gray-700"),
+      "editorLineNumber.foreground": toHex("--chakra-colors-gray-400"),
+      "editorSuggestWidget.background": toHex("--chakra-colors-gray-50"),
+      "editorWidget.background": toHex("--chakra-colors-gray-50"),
+      "editorWidget.border": toHex("--chakra-colors-gray-300"),
+      "scrollbarSlider.activeBackground": `${toHex("--chakra-colors-gray-500")}c0`,
+      "scrollbarSlider.background": `${toHex("--chakra-colors-gray-300")}80`,
+      "scrollbarSlider.hoverBackground": `${toHex("--chakra-colors-gray-400")}a0`,
+    },
+    inherit: true,
+    rules: [],
+  });
+
+  monaco.editor.defineTheme(DARK_THEME_NAME, {
+    base: "vs-dark",
+    colors: {
+      "editor.background": toHex("--chakra-colors-gray-900"),
+      "editor.foreground": toHex("--chakra-colors-gray-100"),
+      "editor.inactiveSelectionBackground": toHex("--chakra-colors-gray-800"),
+      "editor.lineHighlightBackground": toHex("--chakra-colors-gray-800"),
+      "editor.selectionBackground": toHex("--chakra-colors-brand-800"),
+      "editorGutter.background": toHex("--chakra-colors-gray-900"),
+      "editorLineNumber.activeForeground": toHex("--chakra-colors-gray-300"),
+      "editorLineNumber.foreground": toHex("--chakra-colors-gray-600"),
+      "editorSuggestWidget.background": toHex("--chakra-colors-gray-900"),
+      "editorWidget.background": toHex("--chakra-colors-gray-900"),
+      "editorWidget.border": toHex("--chakra-colors-gray-700"),
+      "scrollbarSlider.activeBackground": `${toHex("--chakra-colors-gray-500")}c0`,
+      "scrollbarSlider.background": `${toHex("--chakra-colors-gray-700")}80`,
+      "scrollbarSlider.hoverBackground": `${toHex("--chakra-colors-gray-600")}a0`,
+    },
+    inherit: true,
+    rules: [],
+  });
+
+  themesRegistered = true;
+};
+
+export const useMonacoTheme = () => {
+  const { colorMode } = useColorMode();
+
+  const beforeMount = useCallback((monaco: Monaco) => {
+    defineAirflowMonacoThemes(monaco);
+  }, []);
+

Review Comment:
   I believe React Compiler handles memoization. The empty deps array and the wrapper function that just delegates to defineAirflowMonacoThemes add nothing. This could just be:
   ```
   return {
     beforeMount: defineAirflowMonacoThemes,
     theme: colorMode === "dark" ? DARK_THEME_NAME : LIGHT_THEME_NAME,
   };
   ```
   
   Can you try and see if that breaks anything? 



##########
airflow-core/src/airflow/ui/src/context/colorMode/useMonacoTheme.ts:
##########
@@ -0,0 +1,126 @@
+/*!
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+import type { Monaco } from "@monaco-editor/react";
+import { useCallback } from "react";
+
+import { useColorMode } from "./useColorMode";
+
+const LIGHT_THEME_NAME = "airflow-light";
+const DARK_THEME_NAME = "airflow-dark";
+
+let themesRegistered = false;
+

Review Comment:
   Yes I would keep that flag. 
   
   I'll try to think about other approaches on how to do that, but that's no trivial. What's nice with this is that it will actually handle custom themes added in 3.2.0 which is cool. 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] UI: Rework Monaco editor theme to match Chakra UI palette [airflow]

Posted by "pierrejeambrun (via GitHub)" <gi...@apache.org>.
pierrejeambrun merged PR #64748:
URL: https://github.com/apache/airflow/pull/64748


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] UI: Rework Monaco editor theme to match Chakra UI palette [airflow]

Posted by "shivaam (via GitHub)" <gi...@apache.org>.
shivaam commented on code in PR #64748:
URL: https://github.com/apache/airflow/pull/64748#discussion_r3068062793


##########
airflow-core/src/airflow/ui/src/context/colorMode/useMonacoTheme.test.ts:
##########
@@ -0,0 +1,132 @@
+/*!
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+import type { Monaco } from "@monaco-editor/react";
+import { renderHook } from "@testing-library/react";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+// `useColorMode` is the only dependency of the hook we want to test. We mock
+// it with a mutable return so individual tests can drive light/dark behaviour.
+const colorModeMock = vi.fn<() => { colorMode: "dark" | "light" | undefined }>();
+
+vi.mock("./useColorMode", () => ({
+  useColorMode: () => colorModeMock(),
+}));
+
+// The hook registers Monaco themes exactly once via a module-level flag. We
+// reset modules between tests so each test starts with a fresh flag state.
+const loadHook = async () => {
+  const module = await import("./useMonacoTheme");
+
+  return module.useMonacoTheme;
+};
+
+const createFakeMonaco = () => {
+  const defineTheme = vi.fn();
+
+  return { defineTheme, monaco: { editor: { defineTheme } } as unknown as Monaco };
+};
+
+// happy-dom does not implement Canvas2D rendering, so the hook's `getContext`
+// call would return null and short-circuit before registering themes. We stub
+// it with the minimal surface the hook needs: the draw/clear methods and a
+// `getImageData` that returns arbitrary RGB bytes (the exact hex values are
+// irrelevant to these tests — we only care that theme registration happens).
+const stubCanvasContext = () => {
+  const fakeContext = {
+    clearRect: vi.fn(),
+    fillRect: vi.fn(),
+    fillStyle: "",
+    getImageData: vi.fn(() => ({ data: new Uint8ClampedArray([0, 0, 0, 255]) })),
+  };
+
+  vi.spyOn(HTMLCanvasElement.prototype, "getContext").mockReturnValue(
+    fakeContext as unknown as CanvasRenderingContext2D,
+  );

Review Comment:
   resolved as we removed culori



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] UI: Rework Monaco editor theme to match Chakra UI palette [airflow]

Posted by "pierrejeambrun (via GitHub)" <gi...@apache.org>.
pierrejeambrun commented on code in PR #64748:
URL: https://github.com/apache/airflow/pull/64748#discussion_r3064978861


##########
airflow-core/src/airflow/ui/src/context/colorMode/useMonacoTheme.ts:
##########
@@ -0,0 +1,121 @@
+/*!
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+import type { Monaco } from "@monaco-editor/react";
+
+import { useColorMode } from "./useColorMode";
+
+const LIGHT_THEME_NAME = "airflow-light";
+const DARK_THEME_NAME = "airflow-dark";
+
+let themesRegistered = false;
+
+// Convert any CSS color (including modern color spaces like OKLCH that Chakra
+// UI uses) to a #rrggbb string that Monaco's `defineTheme` accepts.
+//
+// We rasterize a single pixel and read back its sRGB bytes via `getImageData`.
+// `ctx.fillStyle` readback does NOT work for this: starting in Chrome 111, it
+// preserves the original OKLCH string instead of converting to hex, which
+// Monaco would silently ignore.
+const cssVarToHex = (ctx: CanvasRenderingContext2D, cssVar: string): string => {
+  const value = getComputedStyle(document.documentElement).getPropertyValue(cssVar).trim();
+
+  if (value === "") {
+    return "#000000";
+  }
+
+  ctx.fillStyle = value;
+  ctx.clearRect(0, 0, 1, 1);
+  ctx.fillRect(0, 0, 1, 1);
+  const [red, green, blue] = ctx.getImageData(0, 0, 1, 1).data;
+
+  return `#${[red, green, blue].map((channel) => (channel ?? 0).toString(16).padStart(2, "0")).join("")}`;

Review Comment:
   This piece looks brittle. Should we replace it with a proper third party lib doing that? Such as "culori" ? 
   
   ```
     import { formatHex, parse } from "culori";                                                                                                                    
                                                                                                                                                                   
     const cssVarToHex = (cssVar: string): string => {                                                                                                             
       const value = getComputedStyle(document.documentElement).getPropertyValue(cssVar).trim();                                                                   
       return formatHex(parse(value)) ?? "#000000";                                                                                                                
     };                                                                                                                                                            
                                                                                                                                                                   ```
   
   Would make things cleaner I believe. (And with tree shaking and stuff only `formatHex, parse` would actually be in the final bundle.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org