Detect – Titus Nov 01 '19 at 21:00

  • Looks like there's a `ResizeObserver` class that's implemented in Firefox and Chrome. I'll use that. – dx_over_dt Nov 01 '19 at 21:01
  • [`ResizeObserver`](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver) is not supported in Safari, there are a lot of Safari users out there. – Titus Nov 01 '19 at 21:08
  • It's a best-effort sort of thing. A nice-to-have. – dx_over_dt Nov 01 '19 at 21:08
  • 1 Answers1

    0

    The solution appears to be the experimental (as of 11/2019) ResizeObserver class. It's currently implemented in Chrome, Firefox, and Opera, but not in Edge or Safari.

    export default class MyInput extends PureComponent {
      constructor(props) {
        super(props);
        this.state = { 
          autosize: true,
          canResize: true,
          value: ''
        };
    
        this.inputRef = react.createRef();
      }
    
      componentDidMount() {
        if (window.ResizeObserver) {
          const input = this.inputRef.current,
            observer = new ResizeObserver(() => {
              this.setState({ autosize: false });
              observer.disconnect();
            });
    
          observer.observe(input);
        } else {
          this.setState({ canResize: false });
        }
      }
    
      // ...
    
      render() {
        return <textarea {...} ref={this.inputRef} 
                 style={{ resize: this.state.canResize ? 'vertical' : 'none' }} />
      }
    }
    
    dx_over_dt
    • 13,240
    • 17
    • 54
    • 102