Skip to content

Commit 0313078

Browse files
author
Loïc Mangeonjean
committed
fix: replace useColorTheme by useThemeColors
BREAKING CHANGE: the useColorTheme was returning a mutable instance which breaks the react contract
1 parent daeac7b commit 0313078

File tree

3 files changed

+22
-18
lines changed

3 files changed

+22
-18
lines changed

src/MonacoEditor.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { ForwardedRef, forwardRef, ReactElement, useEffect, useMemo, useR
22
import debounce from 'lodash.debounce'
33
import { monaco, createEditor, getMonacoLanguage, updateEditorKeybindingsMode, registerEditorOpenHandler } from '@codingame/monaco-editor-wrapper'
44
import { IEditorOptions } from 'vscode/service-override/modelEditor'
5-
import { useDeepMemo, useLastValueRef, useLastVersion, useColorTheme } from './hooks'
5+
import { useDeepMemo, useLastValueRef, useLastVersion, useThemeColors } from './hooks'
66
import './style'
77

88
const STATUS_BAR_HEIGHT = 20
@@ -123,7 +123,7 @@ function MonacoEditor ({
123123
const preventTriggerChangeEventRef = useRef<boolean>(false)
124124

125125
const [height, setHeight] = useState<number | string>(requestedHeight !== 'auto' ? requestedHeight : 50)
126-
const colorTheme = useColorTheme()
126+
const [statusBarBackground, statusBarForeground, statusBarBorder] = useThemeColors(['statusBar.background', 'statusBar.foreground', 'statusBar.border'])
127127

128128
const containerRef = useRef<HTMLDivElement>(null)
129129
const statusBarRef = useRef<HTMLDivElement>(null)
@@ -334,11 +334,11 @@ function MonacoEditor ({
334334

335335
const statusBarStyle = useMemo(() => {
336336
return {
337-
backgroundColor: colorTheme.getColor('statusBar.background')?.toString() ?? '#007ACC',
338-
color: colorTheme.getColor('statusBar.foreground')?.toString() ?? '#FFFFFF',
339-
borderTop: `1px solid ${colorTheme.getColor('statusBar.border')?.toString() ?? '#FFFFFF'}`
337+
backgroundColor: statusBarBackground ?? '#007ACC',
338+
color: statusBarForeground ?? '#FFFFFF',
339+
borderTop: `1px solid ${statusBarBorder ?? '#FFFFFF'}`
340340
}
341-
}, [colorTheme])
341+
}, [statusBarBackground, statusBarBorder, statusBarForeground])
342342

343343
return (
344344
<div className='react-monaco-editor-react-container' style={{ height }}>

src/hooks.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
1-
import { MutableRefObject, useCallback, useEffect, useMemo, useRef, useState } from 'react'
1+
import { MutableRefObject, useCallback, useEffect, useRef, useState } from 'react'
22
import deepEqual from 'deep-equal'
33
import { getConfiguration, onConfigurationChanged, monaco } from '@codingame/monaco-editor-wrapper'
4-
import { StandaloneServices, IThemeService, IColorTheme } from 'vscode/services'
4+
import { StandaloneServices, IThemeService } from 'vscode/services'
55

6-
export function useColorTheme (): IColorTheme {
7-
const themeService = useMemo(() => StandaloneServices.get(IThemeService), [])
8-
const [theme, setTheme] = useState(themeService.getColorTheme())
6+
function getCurrentThemeColor (color: string): string | undefined {
7+
const themeService = StandaloneServices.get(IThemeService)
8+
return themeService.getColorTheme().getColor(color)?.toString()
9+
}
10+
11+
export function useThemeColors (colors: string[]): (string | undefined)[] {
12+
const [colorValues, setColorValues] = useState(colors.map(getCurrentThemeColor))
913
useEffect(() => {
10-
const disposable = themeService.onDidColorThemeChange(() => {
11-
setTheme(themeService.getColorTheme())
14+
const disposable = StandaloneServices.get(IThemeService).onDidColorThemeChange(() => {
15+
setColorValues(colors.map(getCurrentThemeColor))
1216
})
1317
// Since useEffect is asynchronous, the theme may have changed between the initialization of state and now
1418
// Let's update the state just in case
15-
setTheme(themeService.getColorTheme())
19+
setColorValues(colors.map(getCurrentThemeColor))
1620
return () => {
1721
disposable.dispose()
1822
}
19-
}, [themeService])
23+
}, [colors])
2024

21-
return theme
25+
return colorValues
2226
}
2327

2428
export function useUserConfiguration (programmingLanguageId?: string): Partial<monaco.editor.IEditorOptions> {

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { loadLanguage, monaco, updateKeybindings, updateUserConfiguration } from '@codingame/monaco-editor-wrapper'
2-
import { useColorTheme, useUserConfiguration } from './hooks'
2+
import { useThemeColors, useUserConfiguration } from './hooks'
33
import MonacoEditor, { MonacoEditorProps } from './MonacoEditor'
44

55
export default MonacoEditor
66
export {
77
monaco,
8-
useColorTheme,
8+
useThemeColors,
99
useUserConfiguration,
1010
updateKeybindings,
1111
updateUserConfiguration,

0 commit comments

Comments
 (0)