Vue Autosize Directive (Vue 2)
The `vue-autosize` package provides a simple Vue 2 directive that integrates the `autosize.js` library into Vue applications, allowing `textarea` elements to automatically adjust their height to fit content. While `autosize.js` itself is actively maintained (latest v6.0.1, published February 2023), this specific Vue 2 wrapper (v1.0.2) is not actively maintained and does not support Vue 3. For new projects or Vue 3, developers should consider modern alternatives like `v-autosize` (a Vue 3 wrapper), `vue-textarea-autosize` (a component-based solution), or `useTextareaAutosize` from VueUse. Its key differentiator was its straightforward directive-based approach for Vue 2, simplifying textarea resizing without complex component structures.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'directive') OR [Vue warn]: Failed to resolve directive: autosize
cause The `vue-autosize` plugin was not correctly registered with Vue, or you are attempting to use it in a Vue 3 project where the directive API has changed.fixEnsure `Vue.use(VueAutosize)` is called before your Vue application mounts. If using Vue 3, this package is incompatible; switch to a Vue 3 specific alternative. -
Textarea not resizing initially or after programmatic content change.
cause The textarea might be `display: none` on mount, or its value was updated by JavaScript without `autosize.js` being notified to recalculate.fixEnsure the textarea is visible when the component mounts. If content changes programmatically, and resizing doesn't occur, you may need to force a re-render or find a way to manually trigger an `autosize.update` if the wrapper provides access, or consider alternatives that better handle dynamic updates.
Warnings
- breaking This package is explicitly noted as not supporting Vue 3. Attempting to use it in a Vue 3 project will lead to errors due to API changes in Vue's directive system.
- deprecated The `vue-autosize` package is not actively maintained and is considered abandoned. While it works for Vue 2, new feature development or bug fixes are unlikely.
- gotcha The underlying `autosize.js` library, and thus this directive, may not correctly initialize or update the textarea height if the element is initially hidden (e.g., `display: none`) or its content is changed programmatically after initial rendering without triggering an update.
- gotcha Conflicting CSS properties like `height`, `min-height`, or `max-height` can interfere with the autosizing behavior. While `autosize.js` supports `min-height` and `max-height` via CSS, directly setting `height` will override its functionality.
Install
-
npm install vue-autosize -
yarn add vue-autosize -
pnpm add vue-autosize
Imports
- VueAutosize
const { VueAutosize } = require('vue-autosize');import VueAutosize from 'vue-autosize';
- Vue.use(VueAutosize)
Vue.use(require('vue-autosize').default);import Vue from 'vue'; import VueAutosize from 'vue-autosize'; Vue.use(VueAutosize);
- v-autosize
<textarea autosize></textarea>
<textarea v-autosize></textarea>
Quickstart
import Vue from 'vue';
import VueAutosize from 'vue-autosize';
Vue.use(VueAutosize);
new Vue({
el: '#app',
data: {
message: 'This textarea will automatically adjust its height as you type. Try typing a lot of text here to see it expand vertically. The underlying autosize.js library handles the resizing logic efficiently.',
anotherMessage: 'Short message.'
},
template: `
<div id="app">
<h3>Autosizing Textareas</h3>
<textarea v-autosize v-model="message" style="width: 100%; border: 1px solid #ccc; padding: 8px;"></textarea>
<p>Current message length: {{ message.length }}</p>
<br>
<textarea v-autosize v-model="anotherMessage" style="width: 100%; border: 1px solid #ccc; padding: 8px;"></textarea>
</div>
`
});