Skip to content

Commit 2addc2c

Browse files
committed
refactor: add theme condition for monaco
1 parent 6bef3d9 commit 2addc2c

File tree

2 files changed

+44
-20
lines changed

2 files changed

+44
-20
lines changed

src/stories/components/Editor/Editor.tsx

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react';
22

3-
import {RadioButton, Switch, Text} from '@gravity-ui/uikit';
3+
import {RadioButton, Switch, Text, useTheme} from '@gravity-ui/uikit';
44
import noop from 'lodash/noop';
55
import {Form} from 'react-final-form';
66
import MonacoEditor from 'react-monaco-editor';
@@ -32,6 +32,7 @@ export const Editor: React.FC<EditorProps> = ({spec: externalSpec, value, viewMo
3232
const [ready, setReady] = React.useState(true);
3333
const [toggler, setToggler] = React.useState<'form' | 'view' | 'json'>('form');
3434
const [parseJson, setParseJson] = React.useState(false);
35+
const theme = useTheme();
3536

3637
const togglerItems = React.useMemo(
3738
() => [
@@ -70,11 +71,16 @@ export const Editor: React.FC<EditorProps> = ({spec: externalSpec, value, viewMo
7071
},
7172
spec: {viewSpec: {monacoParams: {language: 'json', fontSize: 11}}},
7273
MonacoComponent: (props: MonacoEditorProps) => (
73-
<MonacoEditor {...props} width="640px" height="calc(100% - 49px)" />
74+
<MonacoEditor
75+
{...props}
76+
width="640px"
77+
height="calc(100% - 49px)"
78+
theme={`vs-${theme.includes('dark') ? 'dark' : 'light'}`}
79+
/>
7480
),
7581
withoutDialog: true,
7682
}) as MonacoInputBaseProps,
77-
[],
83+
[theme],
7884
);
7985

8086
const getValuesEditorProps = React.useCallback(
@@ -86,11 +92,16 @@ export const Editor: React.FC<EditorProps> = ({spec: externalSpec, value, viewMo
8692
},
8793
spec: {viewSpec: {monacoParams: {language: 'json', fontSize: 11}, disabled: true}},
8894
MonacoComponent: (props: MonacoEditorProps) => (
89-
<MonacoEditor {...props} width="640px" height="calc(100% - 49px)" />
95+
<MonacoEditor
96+
{...props}
97+
width="640px"
98+
height="calc(100% - 49px)"
99+
theme={`vs-${theme.includes('dark') ? 'dark' : 'light'}`}
100+
/>
90101
),
91102
withoutDialog: true,
92103
}) as MonacoInputBaseProps,
93-
[],
104+
[theme],
94105
);
95106

96107
const getViewProps = React.useCallback(

src/stories/components/InputPreview/InputPreview.tsx

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react';
22

3-
import {RadioButton, TextInput} from '@gravity-ui/uikit';
3+
import {RadioButton, TextInput, useTheme} from '@gravity-ui/uikit';
44
import noop from 'lodash/noop';
55
import {Form} from 'react-final-form';
66
import MonacoEditor from 'react-monaco-editor';
@@ -33,6 +33,7 @@ export const InputPreview: React.FC<InputPreviewProps> = ({
3333
const [searchInput, setSearchInput] = React.useState('');
3434
const [toggler, setToggler] = React.useState<'form' | 'json'>('form');
3535
const [togglerInput, setTogglerInput] = React.useState<'form' | 'view' | 'json'>('form');
36+
const theme = useTheme();
3637

3738
const togglerItems = React.useMemo(
3839
() => [
@@ -61,21 +62,33 @@ export const InputPreview: React.FC<InputPreviewProps> = ({
6162
[setTogglerInput],
6263
);
6364

64-
const renderMonaco = React.useCallback((value: FormValue) => {
65-
const monacoProps = {
66-
input: {
67-
value: JSON.stringify(value, (_, value) => (value === undefined ? null : value), 2),
68-
onChange: noop,
69-
},
70-
spec: {viewSpec: {monacoParams: {language: 'json', fontSize: 11}, disabled: true}},
71-
MonacoComponent: (props: MonacoEditorProps) => (
72-
<MonacoEditor {...props} width="640px" height="calc(100% - 49px)" />
73-
),
74-
withoutDialog: true,
75-
} as MonacoInputBaseProps;
65+
const renderMonaco = React.useCallback(
66+
(value: FormValue) => {
67+
const monacoProps = {
68+
input: {
69+
value: JSON.stringify(
70+
value,
71+
(_, value) => (value === undefined ? null : value),
72+
2,
73+
),
74+
onChange: noop,
75+
},
76+
spec: {viewSpec: {monacoParams: {language: 'json', fontSize: 11}, disabled: true}},
77+
MonacoComponent: (props: MonacoEditorProps) => (
78+
<MonacoEditor
79+
{...props}
80+
width="640px"
81+
height="calc(100% - 49px)"
82+
theme={`vs-${theme.includes('dark') ? 'dark' : 'light'}`}
83+
/>
84+
),
85+
withoutDialog: true,
86+
} as MonacoInputBaseProps;
7687

77-
return <MonacoInput {...monacoProps} />;
78-
}, []);
88+
return <MonacoInput {...monacoProps} />;
89+
},
90+
[theme],
91+
);
7992

8093
const initialValues = React.useMemo(
8194
() => ({

0 commit comments

Comments
 (0)