-
Notifications
You must be signed in to change notification settings - Fork 373
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
Editor does not resize #53
Comments
You are on the right track with calling editor.layout(). You'll need a reference to the 'editor', which can be obtained by implementing the 'editorDidMount' prop on your instance. Then you need to listen for resize events, and call (this.)editor.layout() in your event handler. Here's a very basic ResizableMonacoEditor component which illustrates this: class ResizableMonacoEditor extends Component {
editor = null;
handleEditorDidMount = editor => this.editor = editor;
render() {
return (
<MonacoEditor
{ ...this.props }
editorDidMount={this.handleEditorDidMount}
/>
);
}
handleResize = () => this.editor.layout();
componentDidMount() {
window.addEventListener('resize', this.handleResize);
}
} Of course you are free to 'listen for resize events' in a more elegant way. For example by using one of the many community example React components for detecting resizes for individual elements/components. Note: by spreading { ...this.props } on <MonacoEditor /> you can set any prop on <ResizableMonacoEditor /> intended for <MonacoEditor />. Hope this helps. |
You can also set monaco to autosize. |
Which might cause performance problems as outlined in this issue from the monaco project: So if that might be a concern, I'd agree with the recommendation (in that issue) of handling resizes manually and use something like I suggested. |
So shall we close this issue? |
Closing this issue for now, please feel free to open it again if necessary |
I tried using import React from 'react';
import MonacoEditor from 'react-monaco-editor';
import ReactResizeDetector from 'react-resize-detector';
export default class Editor extends React.Component {
state = {
code: ['function x() {', " console.log('Hello world!');", '}'].join('\n'),
};
onChange = (newValue, e) => {
console.log('onChange', newValue, e);
this.setState({ code: newValue });
};
editorDidMount = (editor, monaco) => {
console.log('editorDidMount', editor, monaco);
this.editor = editor;
editor.focus();
};
render() {
const { code } = this.state;
const options = {
selectOnLineNumbers: true,
};
return (
<ReactResizeDetector
handleWidth
handleHeight
onResize={() => {
if (this.editor) {
this.editor.layout();
}
}}
>
<MonacoEditor
language="javascript"
theme="vs-dark"
value={code}
options={options}
onChange={this.onChange}
editorDidMount={this.editorDidMount}
/>
</ReactResizeDetector>
);
}
} |
This is not working for me when calling .layout() without parameters. Calling .layout({}) happens to work. |
The monaco-editor component does not update to match the dimensions of the container it is within
when the container resizes. This only affects when a percentage height or width is supplied.
I believe this may be accomplished by calling editor.layout() but have not been able to make it work yet.
The text was updated successfully, but these errors were encountered: