跳到主要內容

useTextareaAutosize

Category
匯出大小
779 B
Last Changed
5 days ago

Automatically update the height of a textarea depending on the content.

Demo

Type, the textarea will grow

Usage

Simple example

vue
<script setup lang="ts">
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.

css
textarea {
  -ms-overflow-style: none;
  scrollbar-width: none;
}

textarea::-webkit-scrollbar {
  display: none;
}

With rows attribute

If you need support for the rows attribute on a textarea element, then you should set the styleProp option to minHeight.

vue
<script setup lang="ts">
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>

Type Declarations

Show Type Declarations
typescript
export interface UseTextareaAutosizeOptions extends ConfigurableWindow {
  /** Textarea element to autosize. */
  element?: MaybeRef<HTMLTextAreaElement | undefined>
  /** Textarea content. */
  input?: MaybeRef<string>
  /** Watch sources that should trigger a textarea resize. */
  watch?: WatchSource | Array<WatchSource>
  /** Function called when the textarea size changes. */
  onResize?: () => void
  /** Specify style target to apply the height based on textarea content. If not provided it will use textarea it self.  */
  styleTarget?: MaybeRef<HTMLElement | undefined>
  /** Specify the style property that will be used to manipulate height. Can be `height | minHeight`. Default value is `height`. */
  styleProp?: "height" | "minHeight"
}
export declare function useTextareaAutosize(
  options?: UseTextareaAutosizeOptions,
): {
  textarea: Ref<
    HTMLTextAreaElement | undefined,
    HTMLTextAreaElement | undefined
  >
  input: Ref<string, string>
  triggerResize: () => void
}
export type UseTextareaAutosizeReturn = ReturnType<typeof useTextareaAutosize>

Source

原始碼示範文件

貢獻者

Anthony Fu
IlyaL
貢獻者
Mutter
Mutter
huiliangShen
yakudik
leex
JD Solanki
Dominik Pschenitschni
Jelf
Enzo Innocenzi

更新日誌

v12.8.0 on 2025/3/5
7432f - feat(types): deprecate MaybeRef and MaybeRefOrGetter in favor of Vue's native (#4636)
v12.7.0 on 2025/2/15
e1a7e - fix: improve resize handling with requestAnimationFrame (
v12.3.0 on 1/2/2025
59f75 - feat(toValue): deprecate toValue from @vueuse/shared in favor of Vue's native
v12.1.0 on 12/22/2024
25ed2 - fix: make input required (#4129)
v12.0.0-beta.1 on 11/21/2024
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)
v11.0.0-beta.2 on 7/17/2024
06c6f - fix: improve triggerResize triggering (#4074)
v10.10.0 on 5/27/2024
a6ede - fix: onResize callback fires not only on resize (#3887)
Contributors
5025e - feat: allow configuring styleProp to support native rows attribute (#3552)
v10.2.0 on 6/16/2023
1b0ec - fix: autosize error when changing input asynchronously (#3118)
v10.0.0-beta.0 on 3/14/2023
a3e95 - feat: added styleTarget option to style other element (#2312)

Released under the MIT License.