Skip to content

feat(CodeEditor): add isHighContrastTheme#12384

Open
logonoff wants to merge 1 commit intopatternfly:mainfrom
logonoff:12383-codeeditor-hc
Open

feat(CodeEditor): add isHighContrastTheme#12384
logonoff wants to merge 1 commit intopatternfly:mainfrom
logonoff:12383-codeeditor-hc

Conversation

@logonoff
Copy link
Copy Markdown
Member

@logonoff logonoff commented Apr 27, 2026

What: Closes #12383

Adds a new prop, isHighContrastTheme, which switches the CodeEditor over to the monaco-provided hc-black and hc-light themes (based on the value of isDarkTheme) when enabled.

A custom PatternFly variant of the hc-black and hc-light themes can come in a follow up, depending on the direction design wants to take on this

Dark mode state Contrast state Monaco theme
light theme no high contrast image
light theme high contrast image
dark theme no high contrast image
dark theme high contrast image

Summary by CodeRabbit

  • New Features
    • Added high-contrast theme support to the Code Editor component.
    • New theme toggle option lets users switch between standard and high-contrast color schemes.
    • Example implementations and the configuration modal include controls to enable the high-contrast theme.
  • Documentation
    • Examples updated with an icon for the high-contrast control.

@patternfly-build
Copy link
Copy Markdown
Collaborator

patternfly-build commented Apr 27, 2026

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 27, 2026

Walkthrough

Adds a new isHighContrastTheme prop to CodeEditor (default false) that selects Monaco's built-in hc-black/hc-light themes when enabled; examples and the configuration modal gain UI controls to toggle the high-contrast theme and pass the prop to CodeEditor. Documentation example imports an additional icon.

Changes

Cohort / File(s) Summary
Core Component
packages/react-code-editor/src/components/CodeEditor/CodeEditor.tsx
Adds isHighContrastTheme?: boolean to CodeEditorProps, computes a memoized theme that chooses hc-black/hc-light when high-contrast is enabled, otherwise uses existing pf-v6-theme-dark/pf-v6-theme-light; passes computed theme into Monaco Editor.
Examples
packages/react-code-editor/src/components/CodeEditor/examples/CodeEditorBasic.tsx, packages/react-code-editor/src/components/CodeEditor/examples/CodeEditorConfigurationModal.tsx
Introduce isHighContrastTheme component state, add Checkbox/Switch UI controls to toggle high-contrast, and forward the prop to CodeEditor; add AdjustIcon for the new control.
Documentation
packages/react-code-editor/src/components/CodeEditor/examples/CodeEditor.md
Adds AdjustIcon import to example docs (documentation-only change).

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested reviewers

  • thatblindgeye
  • nicolethoen
  • mcoker
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: adding an isHighContrastTheme prop to the CodeEditor component.
Linked Issues check ✅ Passed The PR fully implements all requirements from issue #12383: adds isHighContrastTheme prop, implements the 2×2 theme mapping (light/dark × normal/high-contrast), and maintains backward compatibility with existing isDarkTheme behavior.
Out of Scope Changes check ✅ Passed All changes are directly related to implementing high-contrast theme support as specified in issue #12383; no extraneous modifications are present.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (2)
packages/react-code-editor/src/components/CodeEditor/CodeEditor.tsx (2)

160-161: Document the interaction with isDarkTheme in the JSDoc.

The behavior depends on isDarkTheme (selecting hc-black vs hc-light), but the doc comment doesn't surface that. Consumers reading IDE hints will likely miss the dependency.

📝 Proposed JSDoc clarification
-  /** Flag indicating the editor is styled using monaco's high contrast themes. */
+  /** Flag indicating the editor uses Monaco's built-in high-contrast themes. When enabled,
+   * `isDarkTheme` selects between `hc-black` (dark) and `hc-light` (light), overriding the
+   * default PatternFly themes. */
   isHighContrastTheme?: boolean;
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/react-code-editor/src/components/CodeEditor/CodeEditor.tsx` around
lines 160 - 161, Update the JSDoc for the isHighContrastTheme prop to explicitly
state it depends on isDarkTheme: document that when isHighContrastTheme is true
the component will select Monaco's "hc-black" theme if isDarkTheme is true and
"hc-light" if isDarkTheme is false (and that isDarkTheme takes precedence in
choosing the specific high-contrast variant); reference the isHighContrastTheme
and isDarkTheme symbols so consumers can see the interaction in IDE hints.

468-473: useMemo is unnecessary for this computation.

The branches return string literals from two boolean inputs — recomputing per render is effectively free, and the memoized value is consumed as a Monaco prop string, so referential stability buys nothing. A plain ternary keeps the code simpler.

♻️ Proposed simplification
-  const theme = useMemo(() => {
-    if (isHighContrastTheme) {
-      return isDarkTheme ? 'hc-black' : 'hc-light';
-    }
-    return isDarkTheme ? 'pf-v6-theme-dark' : 'pf-v6-theme-light';
-  }, [isHighContrastTheme, isDarkTheme]);
+  const theme = isHighContrastTheme
+    ? (isDarkTheme ? 'hc-black' : 'hc-light')
+    : (isDarkTheme ? 'pf-v6-theme-dark' : 'pf-v6-theme-light');

If you keep useMemo, useMemo can be removed from the imports on line 1 if no other usage remains.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/react-code-editor/src/components/CodeEditor/CodeEditor.tsx` around
lines 468 - 473, The computed theme value using useMemo can be simplified:
replace the useMemo-based assignment to the theme constant inside CodeEditor.tsx
(which currently references isHighContrastTheme and isDarkTheme) with a direct
ternary expression that returns the same string literals, and remove useMemo
from the imports if no other usages remain.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@packages/react-code-editor/src/components/CodeEditor/CodeEditor.tsx`:
- Around line 160-161: Update the JSDoc for the isHighContrastTheme prop to
explicitly state it depends on isDarkTheme: document that when
isHighContrastTheme is true the component will select Monaco's "hc-black" theme
if isDarkTheme is true and "hc-light" if isDarkTheme is false (and that
isDarkTheme takes precedence in choosing the specific high-contrast variant);
reference the isHighContrastTheme and isDarkTheme symbols so consumers can see
the interaction in IDE hints.
- Around line 468-473: The computed theme value using useMemo can be simplified:
replace the useMemo-based assignment to the theme constant inside CodeEditor.tsx
(which currently references isHighContrastTheme and isDarkTheme) with a direct
ternary expression that returns the same string literals, and remove useMemo
from the imports if no other usages remain.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 9ba3d96d-1801-4009-8968-8da952588ef4

📥 Commits

Reviewing files that changed from the base of the PR and between 3392662 and 222a78d.

📒 Files selected for processing (4)
  • packages/react-code-editor/src/components/CodeEditor/CodeEditor.tsx
  • packages/react-code-editor/src/components/CodeEditor/examples/CodeEditor.md
  • packages/react-code-editor/src/components/CodeEditor/examples/CodeEditorBasic.tsx
  • packages/react-code-editor/src/components/CodeEditor/examples/CodeEditorConfigurationModal.tsx

Adds a new prop, `isHighContrastTheme`, which switches the CodeEditor over to the monaco-provided `hc-black` and `hc-light` themes (based on the value of `isDarkTheme`) when enabled.

A custom PatternFly variant of the hc-black and hc-light themes can come in a follow up, depending on the direction design wants to take on this
@logonoff logonoff force-pushed the 12383-codeeditor-hc branch from 222a78d to e963ba4 Compare April 27, 2026 20:45
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@packages/react-code-editor/src/components/CodeEditor/CodeEditor.tsx`:
- Around line 468-473: The computed theme in CodeEditor is getting overridden by
caller-supplied editorProps.theme because editorProps is spread after theme; to
fix, ensure the component's computed theme cannot be overridden by moving the
spread before theme or by merging theme into editorProps (e.g., create
mergedEditorProps = { ...editorProps, theme } and pass mergedEditorProps) so the
local variable theme (from useMemo) always wins; update usages around the
CodeEditor render where theme and editorProps are applied (also apply same
change at the other occurrence noted near lines 592-593).
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: f4186b34-70f2-4d20-a806-b72a95095b1f

📥 Commits

Reviewing files that changed from the base of the PR and between 222a78d and e963ba4.

📒 Files selected for processing (4)
  • packages/react-code-editor/src/components/CodeEditor/CodeEditor.tsx
  • packages/react-code-editor/src/components/CodeEditor/examples/CodeEditor.md
  • packages/react-code-editor/src/components/CodeEditor/examples/CodeEditorBasic.tsx
  • packages/react-code-editor/src/components/CodeEditor/examples/CodeEditorConfigurationModal.tsx
✅ Files skipped from review due to trivial changes (1)
  • packages/react-code-editor/src/components/CodeEditor/examples/CodeEditor.md
🚧 Files skipped from review as they are similar to previous changes (2)
  • packages/react-code-editor/src/components/CodeEditor/examples/CodeEditorConfigurationModal.tsx
  • packages/react-code-editor/src/components/CodeEditor/examples/CodeEditorBasic.tsx

Comment on lines +468 to +473
const theme = useMemo(() => {
if (isHighContrastTheme) {
return isDarkTheme ? 'hc-black' : 'hc-light';
}
return isDarkTheme ? 'pf-v6-theme-dark' : 'pf-v6-theme-light';
}, [isHighContrastTheme, isDarkTheme]);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Keep the computed theme from being overridden.

{...editorProps} currently comes after theme={theme}, so any caller-supplied editorProps.theme will win and can silently bypass isHighContrastTheme.

🔧 Suggested fix
 <Editor
   height={height === '100%' ? undefined : height}
   width={width}
   language={language}
   value={value}
   options={editorOptions}
   overrideServices={overrideServices}
   onChange={onModelChange}
   onMount={editorDidMount}
   loading={loading}
-  theme={theme}
   {...editorProps}
+  theme={theme}
   beforeMount={editorBeforeMount}
 />

Also applies to: 592-593

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/react-code-editor/src/components/CodeEditor/CodeEditor.tsx` around
lines 468 - 473, The computed theme in CodeEditor is getting overridden by
caller-supplied editorProps.theme because editorProps is spread after theme; to
fix, ensure the component's computed theme cannot be overridden by moving the
spread before theme or by merging theme into editorProps (e.g., create
mergedEditorProps = { ...editorProps, theme } and pass mergedEditorProps) so the
local variable theme (from useMemo) always wins; update usages around the
CodeEditor render where theme and editorProps are applied (also apply same
change at the other occurrence noted near lines 592-593).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CodeEditor - High contrast support

2 participants