Skip to content
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
merged 5 commits into from
Oct 17, 2024

Conversation

Dosant
Copy link
Contributor

@Dosant Dosant commented Oct 10, 2024

Summary

This PR is part of https://github.com/elastic/kibana-team/issues/1016#issuecomment-2399310175 and needed to upgrade Kibana to React@18 in Legacy mode.

We've found a problem in react-monaco-editor component which is a very thin wrapper around monaco where it doesn't play nicely with React@18 in legacy mode. You can read on details around the issue here elastic/eui#8018 where we've found a similar issue in EUI's search bar component. The workaround we've found is to change useEffect -> useLayouEffect where the value that is coming from the prop is set into the model. The issue and a fix might be a bit controversal and the component is very thin, so I decided that it might be better to bring the fork into Kibana with only what's needed and with a workaround.

(fyi @elastic/kibana-management @elastic/kibana-esql as the largest users of CodeEditor)

@Dosant Dosant changed the title codeeditor fix [React@18] useLayouEffect when setting value from a prop in react-monaco-editor Oct 14, 2024
@Dosant Dosant changed the title [React@18] useLayouEffect when setting value from a prop in react-monaco-editor [React@18] useLayoutEffect when setting value from a prop in react-monaco-editor Oct 14, 2024
.eslintrc.js Outdated Show resolved Hide resolved
// 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']) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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

useEffect(initMonaco, []);

// useLayoutEffect instead of useEffect to mitigate https://github.com/facebook/react/issues/31023 in React@18 Legacy Mode
useLayoutEffect(() => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the main change was to change from useEffect -> useLayoutEffect here

import { monaco } from '@kbn/monaco';
// TODO: circular dependency
// import type { MonacoEditorProps } from '@kbn/code-editor/react_monaco_editor';
type MonacoEditorProps = any;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a bummer, but I think it is an OK tradeoff as this seem to be private and doesn't affect consumers. Otherwise this would require a larger restructure and another package for react-monaco-editor folder and there is no need for that

@Dosant Dosant added release_note:skip Skip the PR/issue when compiling release notes Team:SharedUX Team label for AppEx-SharedUX (formerly Global Experience) backport:prev-minor Backport to (8.x) the previous minor version (i.e. one version back from main) labels Oct 14, 2024
@Dosant Dosant marked this pull request as ready for review October 14, 2024 10:31
@Dosant Dosant requested review from a team as code owners October 14, 2024 10:31
@elasticmachine
Copy link
Contributor

Pinging @elastic/appex-sharedux (Team:SharedUX)

@Dosant Dosant requested a review from eokoneyo October 14, 2024 11:32
@@ -0,0 +1,93 @@
/*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we follow the @notice pattern, example in https://github.com/elastic/kibana/blob/main/kbn_pm/src/lib/normalize_path.mjs#L3-L28

After we'll have to regenerate the notice file with node scripts/notice

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, so I understand with this approach I can simplify and don't do a separate header with linting and license file? will do, thanks

@stratoula
Copy link
Contributor

I am quite positive with the CI been green but I am going to do a thorough test too Anton.

Copy link
Contributor

@stratoula stratoula left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ES|QL editor looks good Anton

@Dosant Dosant force-pushed the react18/codeeditor branch from 90b9ff7 to 8a4f481 Compare October 15, 2024 09:08
@Dosant Dosant requested a review from jbudz October 15, 2024 09:09
Copy link
Contributor

@eokoneyo eokoneyo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code changes LGTM

@@ -107,7 +107,7 @@ module.exports = {
transformIgnorePatterns: [
// ignore all node_modules except monaco-editor, monaco-yaml and react-monaco-editor which requires babel transforms to handle dynamic import()
Copy link
Contributor

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

@Dosant
Copy link
Contributor Author

Dosant commented Oct 16, 2024

@elasticmachine merge upstream

Copy link
Member

@pheyos pheyos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

packages/kbn-test/jest-preset.js changes LGTM

@Dosant
Copy link
Contributor Author

Dosant commented Oct 17, 2024

@elasticmachine merge upstream

@elasticmachine
Copy link
Contributor

💚 Build Succeeded

Metrics [docs]

Unknown metric groups

ESLint disabled in files

id before after diff
@kbn/code-editor 0 1 +1

ESLint disabled line counts

id before after diff
@kbn/code-editor 3 5 +2

Total ESLint disabled count

id before after diff
@kbn/code-editor 3 6 +3

History

@Dosant Dosant merged commit dc3dda7 into elastic:main Oct 17, 2024
38 checks passed
@kibanamachine
Copy link
Contributor

Starting backport for target branches: 8.x

https://github.com/elastic/kibana/actions/runs/11383942184

kibanamachine pushed a commit to kibanamachine/kibana that referenced this pull request Oct 17, 2024
…-monaco-editor` (elastic#195775)

## Summary

This PR is part of
elastic/kibana-team#1016 (comment)
and needed to upgrade Kibana to React@18 in Legacy mode.

We've found a problem in `react-monaco-editor` component which is a very
thin wrapper around `monaco` where it doesn't play nicely with React@18
in legacy mode. You can read on details around the issue here
elastic/eui#8018 where we've found a similar
issue in EUI's search bar component. The workaround we've found is to
change `useEffect` -> `useLayouEffect` where the value that is coming
from the prop is set into the model. The issue and a fix might be a bit
controversal and the component is very thin, so I decided that it might
be better to bring the fork into Kibana with only what's needed and with
a workaround.

(cherry picked from commit dc3dda7)
@kibanamachine
Copy link
Contributor

💚 All backports created successfully

Status Branch Result
8.x

Note: Successful backport PRs will be merged automatically after passing CI.

Questions ?

Please refer to the Backport tool documentation

kibanamachine added a commit that referenced this pull request Oct 17, 2024
…a prop in `react-monaco-editor` (#195775) (#196671)

# Backport

This will backport the following commits from `main` to `8.x`:
- [[React@18] `useLayoutEffect` when setting value from a prop
in `react-monaco-editor`
(#195775)](#195775)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Anton
Dosov","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-10-17T11:24:06Z","message":"[React@18]
`useLayoutEffect` when setting value from a prop in
`react-monaco-editor` (#195775)\n\n## Summary\r\n\r\nThis PR is part
of\r\nhttps://github.com/elastic/kibana-team/issues/1016#issuecomment-2399310175\r\nand
needed to upgrade Kibana to React@18 in Legacy mode.\r\n\r\nWe've found
a problem in `react-monaco-editor` component which is a very\r\nthin
wrapper around `monaco` where it doesn't play nicely with React@18\r\nin
legacy mode. You can read on details around the issue
here\r\nhttps://github.com/elastic/eui/issues/8018 where we've found a
similar\r\nissue in EUI's search bar component. The workaround we've
found is to\r\nchange `useEffect` -> `useLayouEffect` where the value
that is coming\r\nfrom the prop is set into the model. The issue and a
fix might be a bit\r\ncontroversal and the component is very thin, so I
decided that it might\r\nbe better to bring the fork into Kibana with
only what's needed and with\r\na
workaround.","sha":"dc3dda7d12662f3d7b5cb6c6c6366e07eae138fa","branchLabelMapping":{"^v9.0.0$":"main","^v8.17.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","v9.0.0","Team:SharedUX","backport:prev-minor"],"title":"[React@18]
`useLayoutEffect` when setting value from a prop in
`react-monaco-editor`","number":195775,"url":"https://github.com/elastic/kibana/pull/195775","mergeCommit":{"message":"[React@18]
`useLayoutEffect` when setting value from a prop in
`react-monaco-editor` (#195775)\n\n## Summary\r\n\r\nThis PR is part
of\r\nhttps://github.com/elastic/kibana-team/issues/1016#issuecomment-2399310175\r\nand
needed to upgrade Kibana to React@18 in Legacy mode.\r\n\r\nWe've found
a problem in `react-monaco-editor` component which is a very\r\nthin
wrapper around `monaco` where it doesn't play nicely with React@18\r\nin
legacy mode. You can read on details around the issue
here\r\nhttps://github.com/elastic/eui/issues/8018 where we've found a
similar\r\nissue in EUI's search bar component. The workaround we've
found is to\r\nchange `useEffect` -> `useLayouEffect` where the value
that is coming\r\nfrom the prop is set into the model. The issue and a
fix might be a bit\r\ncontroversal and the component is very thin, so I
decided that it might\r\nbe better to bring the fork into Kibana with
only what's needed and with\r\na
workaround.","sha":"dc3dda7d12662f3d7b5cb6c6c6366e07eae138fa"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/195775","number":195775,"mergeCommit":{"message":"[React@18]
`useLayoutEffect` when setting value from a prop in
`react-monaco-editor` (#195775)\n\n## Summary\r\n\r\nThis PR is part
of\r\nhttps://github.com/elastic/kibana-team/issues/1016#issuecomment-2399310175\r\nand
needed to upgrade Kibana to React@18 in Legacy mode.\r\n\r\nWe've found
a problem in `react-monaco-editor` component which is a very\r\nthin
wrapper around `monaco` where it doesn't play nicely with React@18\r\nin
legacy mode. You can read on details around the issue
here\r\nhttps://github.com/elastic/eui/issues/8018 where we've found a
similar\r\nissue in EUI's search bar component. The workaround we've
found is to\r\nchange `useEffect` -> `useLayouEffect` where the value
that is coming\r\nfrom the prop is set into the model. The issue and a
fix might be a bit\r\ncontroversal and the component is very thin, so I
decided that it might\r\nbe better to bring the fork into Kibana with
only what's needed and with\r\na
workaround.","sha":"dc3dda7d12662f3d7b5cb6c6c6366e07eae138fa"}}]}]
BACKPORT-->

Co-authored-by: Anton Dosov <[email protected]>
@Dosant Dosant added the React@18 label Nov 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport:prev-minor Backport to (8.x) the previous minor version (i.e. one version back from main) React@18 release_note:skip Skip the PR/issue when compiling release notes Team:SharedUX Team label for AppEx-SharedUX (formerly Global Experience) v8.17.0 v9.0.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants