React Input Autosize

3.0.0 · maintenance · verified Sun Apr 19

React-Input-Autosize is a React component designed to provide an `<input>` field that automatically adjusts its width to fit its content. The current stable version is 3.0.0, supporting React versions 16.3.0 and 17.0.0. While there's no explicit release cadence, the package has maintained stability across major React versions. Its key differentiator lies in abstracting away the complexities of dynamic input sizing, typically achieved by wrapping the native input element within a `div` to accurately measure content width. It aims to replicate the behavior of a standard React input while handling resizing automatically, including managing custom font sizes and injecting necessary styles for cross-browser compatibility (e.g., hiding IE/Edge's clear indicator), although this style injection can be disabled for CSP-restricted environments. It functions exclusively as a controlled component.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic usage of `AutosizeInput` as a controlled component, showing how to manage its value and apply inline styles for proper sizing.

import React, { useState, useEffect } from 'react';
import AutosizeInput from 'react-input-autosize';

function MyForm() {
  const [inputValue, setInputValue] = useState('Hello World');

  useEffect(() => {
    // This effect is just to show the value in console for demonstration
    // console.log('Current input value:', inputValue);
  }, [inputValue]);

  const handleChange = (event) => {
    setInputValue(event.target.value);
  };

  return (
    <div>
      <label>
        Auto-resizing Input:
        <AutosizeInput
          name="form-field-name"
          value={inputValue}
          onChange={handleChange}
          inputStyle={{
            border: '1px solid #ccc',
            borderRadius: 4,
            padding: '5px 8px',
            fontSize: 16
          }}
          placeholder="Type something here..."
        />
      </label>
      <p>The input above resizes automatically as you type.</p>
    </div>
  );
}

// Example of how to render this component in a typical React application:
// import { createRoot } from 'react-dom/client';
// const container = document.getElementById('root');
// if (container) {
//   const root = createRoot(container);
//   root.render(<MyForm />);
// }

view raw JSON →