-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[React@18] useLayoutEffect
when setting value from a prop in react-monaco-editor
#195775
Merged
+360
−40
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8a4f481
simplify license setup
Dosant 87cefca
Merge branch 'main' of github.com:elastic/kibana into react18/codeeditor
Dosant 6cc172e
clean comment
Dosant d2511bd
Merge branch 'main' into react18/codeeditor
elasticmachine 08dd4cc
Merge branch 'main' into react18/codeeditor
elasticmachine File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,9 +8,6 @@ | |
*/ | ||
|
||
import React, { useState, useRef, useCallback, useMemo, useEffect, KeyboardEvent, FC } from 'react'; | ||
import ReactMonacoEditor, { | ||
type MonacoEditorProps as ReactMonacoEditorProps, | ||
} from 'react-monaco-editor'; | ||
import { | ||
htmlIdGenerator, | ||
EuiToolTip, | ||
|
@@ -34,6 +31,10 @@ import { | |
import { i18n } from '@kbn/i18n'; | ||
import { FormattedMessage } from '@kbn/i18n-react'; | ||
import { css } from '@emotion/react'; | ||
import { | ||
MonacoEditor as ReactMonacoEditor, | ||
type MonacoEditorProps as ReactMonacoEditorProps, | ||
} from './react_monaco_editor'; | ||
import './register_languages'; | ||
import { remeasureFonts } from './remeasure_fonts'; | ||
|
||
|
@@ -168,7 +169,7 @@ export interface CodeEditorProps { | |
export const CodeEditor: React.FC<CodeEditorProps> = ({ | ||
languageId, | ||
value, | ||
onChange: _onChange, | ||
onChange, | ||
width, | ||
height, | ||
options, | ||
|
@@ -225,8 +226,6 @@ export const CodeEditor: React.FC<CodeEditorProps> = ({ | |
|
||
const [isHintActive, setIsHintActive] = useState(true); | ||
|
||
const onChange = useBug175684OnChange(_onChange); | ||
|
||
const startEditing = useCallback(() => { | ||
setIsHintActive(false); | ||
_editor?.focus(); | ||
|
@@ -701,23 +700,6 @@ const useFitToContent = ({ | |
}, [editor, isFitToContent, minLines, maxLines, isFullScreen]); | ||
}; | ||
|
||
// https://github.com/elastic/kibana/issues/175684 | ||
// 'react-monaco-editor' has a bug that it always calls the initial onChange callback, so the closure might become stale | ||
// we work this around by calling the latest onChange from props | ||
const useBug175684OnChange = (onChange: CodeEditorProps['onChange']) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this also fixes this bug in the react_monaco_editor, so this workaround in the parent component is no longer needed |
||
const onChangePropRef = useRef<CodeEditorProps['onChange']>(onChange); | ||
useEffect(() => { | ||
onChangePropRef.current = onChange; | ||
}, [onChange]); | ||
const onChangeWrapper = useCallback<NonNullable<CodeEditorProps['onChange']>>((_value, event) => { | ||
if (onChangePropRef.current) { | ||
onChangePropRef.current(_value, event); | ||
} | ||
}, []); | ||
|
||
return onChangeWrapper; | ||
}; | ||
|
||
const UseBug177756ReBroadcastMouseDown: FC<{ children: React.ReactNode }> = ({ children }) => { | ||
const [$codeWrapper, setCodeWrapper] = React.useState<HTMLElement | null>(null); | ||
|
||
|
33 changes: 33 additions & 0 deletions
33
packages/shared-ux/code_editor/impl/react_monaco_editor/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
This is a fork of [react-monaco-editor project](https://github.com/react-monaco-editor/react-monaco-editor) that is a Monaco editor wrapper for React. | ||
This fork is needed to apply a change that fixes the editor behavior in Kibana when running React@18 in Legacy Mode and the bug is described [here](https://github.com/facebook/react/issues/31023) | ||
The change is to replace the `useEffect` hook with `useLayoutEffect` when the editor is in controlled mode and the value is updated from prop. | ||
|
||
```diff | ||
---useEffect(() => { | ||
+++useLayoutEffect(() => { | ||
if (editor.current) { | ||
if (value === editor.current.getValue()) { | ||
return; | ||
} | ||
|
||
const model = editor.current.getModel(); | ||
__prevent_trigger_change_event.current = true; | ||
editor.current.pushUndoStop(); | ||
// pushEditOperations says it expects a cursorComputer, but doesn't seem to need one. | ||
model.pushEditOperations( | ||
[], | ||
[ | ||
{ | ||
range: model.getFullModelRange(), | ||
text: value, | ||
}, | ||
], | ||
undefined, | ||
); | ||
editor.current.pushUndoStop(); | ||
__prevent_trigger_change_event.current = false; | ||
} | ||
}, [value]); | ||
``` | ||
|
||
In addition, the fork only includes functionality that is used in Kibana and removes the rest of the code that is not needed. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should also update this comment to remove the reference to
react-monaco-editor