v-autosize Vue Textarea Autosize Directive
v-autosize is a lightweight Vue 3 directive designed to wrap the standalone `autosize` library, enabling `<textarea>` HTML elements to automatically adjust their height to perfectly fit their content. The current stable version, 2.0.1, is built specifically for Vue 3. Users requiring Vue 2 compatibility should refer to `v-autosize@1`. This package differentiates itself from other solutions like `vue-autosize` and `vue-textarea-autosize` by being actively maintained for the current Vue ecosystem (Vue 3) and providing a simple, directive-based integration rather than a component-based one, ensuring minimal boilerplate. Release cadence is typically tied to updates in the core `autosize` library or significant Vue ecosystem changes, prioritizing stability and a small bundle size.
Common errors
-
[Vue warn]: Failed to resolve directive: autosize
cause The `v-autosize` directive has not been correctly registered with your Vue application, either locally in a component or globally.fixRegister the directive. For local use: `import autosize from 'v-autosize'; export default { directives: { autosize } }`. For global use: `import autosizePlugin from 'v-autosize/src/plugin.js'; app.use(autosizePlugin);` -
TypeError: Cannot read properties of null (reading 'style') at autosize.
cause This error often occurs when the `v-autosize` directive is applied to an element that is not a `<textarea>`, or the `<textarea>` element itself is not rendered or accessible at the time of directive binding.fixVerify that the `v-autosize` directive is exclusively used on `<textarea>` HTML elements and that the element is present in the DOM when the directive initializes.
Warnings
- breaking Version 2.x and above of `v-autosize` is exclusively for Vue 3. If you are using Vue 2, you must install `v-autosize@1` instead.
- gotcha The `v-autosize` directive must be applied directly to a `<textarea>` HTML element. Applying it to other elements (e.g., `<div>` or custom components) will not work as intended and may cause runtime errors.
- gotcha When globally registering the plugin, ensure you import from `v-autosize/src/plugin.js`. A direct import from 'v-autosize' typically provides the directive for local registration, not the global plugin.
Install
-
npm install v-autosize -
yarn add v-autosize -
pnpm add v-autosize
Imports
- autosize
const autosize = require('v-autosize');import autosize from 'v-autosize';
- autosizePlugin
import { autosizePlugin } from 'v-autosize';import autosizePlugin from 'v-autosize/src/plugin.js';
- autosize
import autosize from 'v-autosize/dist/v-autosize.esm-browser.js';
import autosize from 'v-autosize';
Quickstart
import { createApp } from 'vue';
import autosizePlugin from 'v-autosize/src/plugin.js';
const app = createApp({
template: `
<div>
<label for="my-textarea">Resizable Textarea:</label>
<textarea id="my-textarea" v-autosize style="width: 300px; padding: 8px; border: 1px solid #ccc;" placeholder="Type something..."></textarea>
<p>The textarea above will automatically adjust its height as you type.</p>
</div>
`,
});
app.use(autosizePlugin);
app.mount('#app');