packages/core/useTextareaAutosize/index.md
Automatically update the height of a textarea depending on the content.
<script setup lang="ts">
import { useTextareaAutosize } from '@vueuse/core'
const { textarea, input } = useTextareaAutosize()
</script>
<template>
<textarea
ref="textarea"
v-model="input"
class="resize-none"
placeholder="What's on your mind?"
/>
</template>
::: info
It's recommended to reset the scrollbar styles for the textarea element to avoid incorrect height values for large amounts of text.
textarea {
-ms-overflow-style: none;
scrollbar-width: none;
}
textarea::-webkit-scrollbar {
display: none;
}
:::
rows attributeIf you need support for the rows attribute on a textarea element, then you should set the styleProp option to minHeight.
<script setup lang="ts">
import { useTextareaAutosize } from '@vueuse/core'
const { textarea, input } = useTextareaAutosize({ styleProp: 'minHeight' })
</script>
<template>
<textarea
ref="textarea"
v-model="input"
class="resize-none"
placeholder="What's on your mind?"
rows="3"
/>
</template>
maxHeightUse the maxHeight option to cap the textarea height in pixels while keeping autosize behavior.
<script setup lang="ts">
import { useTextareaAutosize } from '@vueuse/core'
const { textarea, input } = useTextareaAutosize({
maxHeight: 180,
styleProp: 'minHeight',
})
</script>
<template>
<textarea
ref="textarea"
v-model="input"
class="resize-none"
placeholder="What's on your mind?"
rows="3"
/>
</template>