Skip to content

Commit 701fdce

Browse files
committed
feat(CodeEditor): use custom PatternFly monaco theme
1 parent a53c929 commit 701fdce

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

packages/react-code-editor/src/components/CodeEditor/CodeEditor.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import HelpIcon from '@patternfly/react-icons/dist/esm/icons/help-icon';
2323
import Dropzone, { FileRejection } from 'react-dropzone';
2424
import { CodeEditorContext } from './CodeEditorUtils';
2525
import { CodeEditorControl } from './CodeEditorControl';
26+
import { defineThemes } from './CodeEditorTheme';
2627

2728
export type ChangeHandler = (value: string, event: editor.IModelContentChangedEvent) => void;
2829
export type EditorDidMount = (editor: editor.IStandaloneCodeEditor, monaco: Monaco) => void;
@@ -387,6 +388,7 @@ class CodeEditor extends Component<CodeEditorProps, CodeEditorState> {
387388
};
388389

389390
editorDidMount: EditorDidMount = (editor, monaco) => {
391+
defineThemes(monaco.editor);
390392
// eslint-disable-next-line no-bitwise
391393
editor.addCommand(monaco.KeyMod.Shift | monaco.KeyCode.Tab, () => this.wrapperRef.current.focus());
392394
Array.from(document.getElementsByClassName('monaco-editor')).forEach((editorElement) =>
@@ -652,7 +654,7 @@ class CodeEditor extends Component<CodeEditorProps, CodeEditorState> {
652654
overrideServices={overrideServices}
653655
onChange={this.onChange}
654656
onMount={this.editorDidMount}
655-
theme={isDarkTheme ? 'vs-dark' : 'vs-light'}
657+
theme={isDarkTheme ? 'pf-v6-theme-dark' : 'pf-v6-theme-light'}
656658
loading={loading}
657659
{...editorProps}
658660
/>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import green70 from '@patternfly/react-tokens/dist/esm/t_color_green_70';
2+
import yellow70 from '@patternfly/react-tokens/dist/esm/t_color_yellow_70';
3+
import blue70 from '@patternfly/react-tokens/dist/esm/t_color_blue_70';
4+
import purple70 from '@patternfly/react-tokens/dist/esm/t_color_purple_70';
5+
import green30 from '@patternfly/react-tokens/dist/esm/t_color_green_30';
6+
import blue30 from '@patternfly/react-tokens/dist/esm/t_color_blue_30';
7+
import yellow30 from '@patternfly/react-tokens/dist/esm/t_color_yellow_30';
8+
import purple30 from '@patternfly/react-tokens/dist/esm/t_color_purple_30';
9+
import white from '@patternfly/react-tokens/dist/esm/t_color_white';
10+
import gray20 from '@patternfly/react-tokens/dist/esm/t_color_gray_20';
11+
import gray60 from '@patternfly/react-tokens/dist/esm/t_color_gray_60';
12+
import gray90 from '@patternfly/react-tokens/dist/esm/t_color_gray_90';
13+
import black from '@patternfly/react-tokens/dist/esm/t_color_black';
14+
import type { editor as monacoEditor } from 'monaco-editor/esm/vs/editor/editor.api';
15+
16+
/**
17+
* Define the themes `pf-v6-code-editor-theme--light` and
18+
* `pf-v6-code-editor-theme--dark` for an instance of Monaco editor.
19+
*
20+
* Note that base tokens must be used as Monaco will throw a runtime
21+
* error for CSS variables.
22+
*/
23+
export const defineThemes = (editor: typeof monacoEditor) => {
24+
editor.defineTheme('pf-v6-theme-light', {
25+
base: 'vs',
26+
inherit: true,
27+
colors: {
28+
'editor.background': white.value,
29+
'editorLineNumber.activeForeground': black.value,
30+
'editorLineNumber.foreground': gray60.value
31+
},
32+
rules: [
33+
{ token: 'number', foreground: green70.value },
34+
{ token: 'type', foreground: yellow70.value },
35+
{ token: 'string', foreground: blue70.value },
36+
{ token: 'keyword', foreground: purple70.value }
37+
]
38+
});
39+
40+
editor.defineTheme('pf-v6-theme-dark', {
41+
base: 'vs-dark',
42+
inherit: true,
43+
colors: {
44+
'editor.background': gray90.value,
45+
'editorLineNumber.activeForeground': white.value,
46+
'editorLineNumber.foreground': gray20.value
47+
},
48+
rules: [
49+
{ token: 'number', foreground: green30.value },
50+
{ token: 'type', foreground: blue30.value },
51+
{ token: 'string', foreground: yellow30.value },
52+
{ token: 'keyword', foreground: purple30.value }
53+
]
54+
});
55+
};

0 commit comments

Comments
 (0)