question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

react-ace in resizable container

See original GitHub issue

I am trying to use react-ace inside a div that can be resized by the user at runtime (to be precise, the size of the div is controlled by a flexbox). By default, the ACE editor requires fixed with and height values in pixels.

By using react-measure and some div nesting, I was able to get the ACE Editor to resize its bounds correctly, at least visually. However, the controls inside the editor (e.g. the width of the line wrapping, the scroll bar, …) behave weirdly, because they did not get properly notified about the change in editor dimensions.

The “vanilla” ACE editor has a resize() method to fix just that problem (see this fiddle).

My request is: please either forward resize() in react-ace, or call resize() internally on componentWillReceiveProps in case that width and/or height has changed.

EDIT: I had a quick look at the source code. You actually do this:

if(nextProps.height !== this.props.height){
  this.editor.resize();
}

… but what about changes in width? Shouldn’t it be this:

if(nextProps.height !== this.props.height || nextProps.width !== this.props.width){
  this.editor.resize();
}

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:18 (3 by maintainers)

github_iconTop GitHub Comments

18reactions
milahucommented, Aug 18, 2021

here is a pure css solution, using CSS resize: both on the editor div

<AceEditor
  onLoad={editorInstance => {
    editorInstance.container.style.resize = "both";
    // mouseup = css resize end
    document.addEventListener("mouseup", e => (
      editorInstance.resize()
    ));
  }}
/>

downside: on resize-larger the editor content is hidden

here is a demo fiddle with vanilla js

7reactions
xgaiacommented, Oct 23, 2019

Anyone figure out how to automatically resize the container based on user-control?

I succeed by using react-resize-detector

import AceEditor from 'react-ace'
import ReactResizeDetector from 'react-resize-detector'

export default class myComponent extends Component {

  constructor (props) {
    super(props)
    this.state = {
      editorHeight: 400,
      editorWidth: "auto"
    }
    this.onResize = this.onResize.bind(this)
  }


  onResize (w, h) {
    this.setState({
      editorHeight: h,
      editorWidth: w
    })
  }

  render () {
    <div className="resizable">
     <ReactResizeDetector handleWidth handleHeight onResize={this.onResize} />
     <AceEditor
       height={this.state.editorHeight}
       width={this.state.editorWidth}
     />
    </div>
  }
}

CSS of resizable class

.resizable {
    overflow: auto;
    resize: both;
    height: 400px;
    width: auto;
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Hold border and Resize two or more DIV simultaneously in the ...
Let's say that div#widget1 is 99px width and div.part are 33px by default. How can I easily resize div.blue by increasing its width...
Read more >
react-resizable - npm
A simple widget that can be resized via one or more handles. You can either use the <Resizable> element directly, or use the...
Read more >
resize - CSS: Cascading Style Sheets - MDN Web Docs - Mozilla
The resize CSS property sets whether an element is resizable, and if so, in which directions.
Read more >
Can We Create a "Resize Hack" With Container Queries?
Moving the cord handle, resized an element which in turn affected the container size. Different container breakpoints would update a CSS ...
Read more >
cap size reducer
How to Resize an Image in Microsoft Paint (with Pictures). ... hence the reactance will be 44Ω and the standard frequency of the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found