Skip to content

Commit e963ba4

Browse files
committed
feat(CodeEditor): add isHighContrastTheme
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
1 parent 3392662 commit e963ba4

4 files changed

Lines changed: 38 additions & 2 deletions

File tree

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { HTMLProps, ReactNode, useEffect, useRef, useState } from 'react';
1+
import { HTMLProps, ReactNode, useEffect, useMemo, useRef, useState } from 'react';
22
import { css } from '@patternfly/react-styles';
33
import styles from '@patternfly/react-styles/css/components/CodeEditor/code-editor';
44
import fileUploadStyles from '@patternfly/react-styles/css/components/FileUpload/file-upload';
@@ -157,6 +157,8 @@ export interface CodeEditorProps extends Omit<HTMLProps<HTMLDivElement>, 'onChan
157157
isCopyEnabled?: boolean;
158158
/** Flag indicating the editor is styled using monaco's dark theme. */
159159
isDarkTheme?: boolean;
160+
/** Flag indicating the editor is styled using monaco's high contrast themes. */
161+
isHighContrastTheme?: boolean;
160162
/** Flag that enables component to consume the available height of its container. If `height` prop is set to 100%, this will also become enabled. */
161163
isFullHeight?: boolean;
162164
/** Flag indicating the editor has a plain header. */
@@ -286,6 +288,7 @@ export const CodeEditor = ({
286288
height,
287289
isCopyEnabled = false,
288290
isDarkTheme = false,
291+
isHighContrastTheme = false,
289292
isDownloadEnabled = false,
290293
isFullHeight = false,
291294
isHeaderPlain = false,
@@ -462,6 +465,13 @@ export const CodeEditor = ({
462465
headerMainContent ||
463466
!!shortcutsPopoverProps.bodyContent;
464467

468+
const theme = useMemo(() => {
469+
if (isHighContrastTheme) {
470+
return isDarkTheme ? 'hc-black' : 'hc-light';
471+
}
472+
return isDarkTheme ? 'pf-v6-theme-dark' : 'pf-v6-theme-light';
473+
}, [isHighContrastTheme, isDarkTheme]);
474+
465475
return (
466476
<Dropzone multiple={false} onDropAccepted={onDropAccepted} onDropRejected={onDropRejected}>
467477
{({ getRootProps, getInputProps, isDragActive, open }) => {
@@ -579,7 +589,7 @@ export const CodeEditor = ({
579589
onChange={onModelChange}
580590
onMount={editorDidMount}
581591
loading={loading}
582-
theme={isDarkTheme ? 'pf-v6-theme-dark' : 'pf-v6-theme-light'}
592+
theme={theme}
583593
{...editorProps}
584594
beforeMount={editorBeforeMount}
585595
/>

packages/react-code-editor/src/components/CodeEditor/examples/CodeEditor.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import MapIcon from '@patternfly/react-icons/dist/esm/icons/map-icon';
1515
import MoonIcon from '@patternfly/react-icons/dist/esm/icons/moon-icon';
1616
import PlayIcon from '@patternfly/react-icons/dist/esm/icons/play-icon';
1717
import FontIcon from '@patternfly/react-icons/dist/esm/icons/font-icon';
18+
import AdjustIcon from '@patternfly/react-icons/dist/esm/icons/adjust-icon';
1819

1920
## Examples
2021

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Checkbox } from '@patternfly/react-core';
44

55
export const CodeEditorBasic: React.FunctionComponent = () => {
66
const [isDarkTheme, setIsDarkTheme] = useState(false);
7+
const [isHighContrastTheme, setIsHighContrastTheme] = useState(false);
78
const [isLineNumbersVisible, setIsLineNumbersVisible] = useState(true);
89
const [isReadOnly, setIsReadOnly] = useState(false);
910
const [isMinimapVisible, setIsMinimapVisible] = useState(false);
@@ -12,6 +13,10 @@ export const CodeEditorBasic: React.FunctionComponent = () => {
1213
setIsDarkTheme(checked);
1314
};
1415

16+
const toggleHighContrastTheme = (checked) => {
17+
setIsHighContrastTheme(checked);
18+
};
19+
1520
const toggleLineNumbers = (checked) => {
1621
setIsLineNumbersVisible(checked);
1722
};
@@ -43,6 +48,14 @@ export const CodeEditorBasic: React.FunctionComponent = () => {
4348
id="toggle-theme"
4449
name="toggle-theme"
4550
/>
51+
<Checkbox
52+
label="High contrast theme"
53+
isChecked={isHighContrastTheme}
54+
onChange={(_event, checked) => toggleHighContrastTheme(checked)}
55+
aria-label="high contrast theme checkbox"
56+
id="toggle-high-contrast-theme"
57+
name="toggle-high-contrast-theme"
58+
/>
4659
<Checkbox
4760
label="Line numbers"
4861
isChecked={isLineNumbersVisible}
@@ -69,6 +82,7 @@ export const CodeEditorBasic: React.FunctionComponent = () => {
6982
/>
7083
<CodeEditor
7184
isDarkTheme={isDarkTheme}
85+
isHighContrastTheme={isHighContrastTheme}
7286
isLineNumbersVisible={isLineNumbersVisible}
7387
isReadOnly={isReadOnly}
7488
isMinimapVisible={isMinimapVisible}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import MapIcon from '@patternfly/react-icons/dist/esm/icons/map-icon';
33
import MoonIcon from '@patternfly/react-icons/dist/esm/icons/moon-icon';
44
import HashtagIcon from '@patternfly/react-icons/dist/esm/icons/hashtag-icon';
55
import FontIcon from '@patternfly/react-icons/dist/esm/icons/font-icon';
6+
import AdjustIcon from '@patternfly/react-icons/dist/esm/icons/adjust-icon';
67
import { CodeEditor, CodeEditorControl } from '@patternfly/react-code-editor';
78
import {
89
Flex,
@@ -156,6 +157,7 @@ export const CodeEditorConfigurationModal: React.FunctionComponent = () => {
156157

157158
const [isMinimapVisible, setIsMinimapVisible] = useState(true);
158159
const [isDarkTheme, setIsDarkTheme] = useState(false);
160+
const [isHighContrastTheme, setIsHighContrastTheme] = useState(false);
159161
const [isLineNumbersVisible, setIsLineNumbersVisible] = useState(true);
160162
const [fontSize, setFontSize] = useState(14);
161163

@@ -181,6 +183,14 @@ export const CodeEditorConfigurationModal: React.FunctionComponent = () => {
181183
onChange={(_e, checked) => setIsDarkTheme(checked)}
182184
icon={<MoonIcon />}
183185
/>
186+
<ConfigModalSwitch
187+
key="high-contrast-theme-switch"
188+
title="High contrast theme"
189+
description="Switch the editor to a high contrast color theme"
190+
isChecked={isHighContrastTheme}
191+
onChange={(_e, checked) => setIsHighContrastTheme(checked)}
192+
icon={<AdjustIcon />}
193+
/>
184194
<ConfigModalSwitch
185195
key="line-numbers-switch"
186196
title="Line numbers"
@@ -221,6 +231,7 @@ export const CodeEditorConfigurationModal: React.FunctionComponent = () => {
221231
customControls={customControl}
222232
height="400px"
223233
isDarkTheme={isDarkTheme}
234+
isHighContrastTheme={isHighContrastTheme}
224235
isLineNumbersVisible={isLineNumbersVisible}
225236
isMinimapVisible={isMinimapVisible}
226237
onChange={onChange}

0 commit comments

Comments
 (0)