Textarea
A multiline text field with character counts, automatic height, resize controls, form validation, and public focus methods.
Import
import { HuiTextarea } from '@nscode/handy-ui'
import type {
TextareaProps,
TextareaEmits,
TextareaExpose,
TextareaAutosize,
TextareaResize,
TextareaSize
} from '@nscode/handy-ui'
Examples
Internal and Controlled Values
Without modelValue, the component stores its own value across parent updates. An update listener alone does not make it controlled. Passing the prop controls it, including explicit undefined or null, which display empty text. Rejected updates restore the bound value. There is no defaultValue prop. Adding or removing the binding at runtime emits a development warning and follows the new mode.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiSpace, HuiTextarea } from '@nscode/handy-ui'
const redraw = ref(0)
const lastInput = ref('No input yet')
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiButton @click="redraw++">Parent update: {{ redraw }}</HuiButton>
<HuiTextarea
:data-parent-render="redraw"
:maxlength="30"
show-word-limit
placeholder="Internal value, update listener only"
@update:model-value="lastInput = $event"
/>
<span>Last update: {{ lastInput }}</span>
<HuiTextarea model-value="Fixed content" placeholder="Controlled, updates rejected" />
<HuiTextarea
:model-value="undefined"
placeholder="Explicit undefined: controlled empty value"
/>
<HuiTextarea :model-value="null" placeholder="Explicit null: controlled empty value" />
</HuiSpace>
</template>
Binding and Rows
Use v-model for a string binding. The default is two rows; rows sets the native initial row count.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiSpace, HuiTextarea } from '@nscode/handy-ui'
const one = ref('')
const two = ref('')
const five = ref('')
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiTextarea v-model="one" :rows="1" placeholder="rows = 1" />
<HuiTextarea v-model="two" placeholder="rows = 2 (default)" />
<HuiTextarea v-model="five" :rows="5" placeholder="rows = 5" />
<span>Lengths: {{ one.length }}, {{ two.length }}, {{ five.length }}</span>
</HuiSpace>
</template>
Character Count
maxlength limits native input length. Add show-word-limit to show the current count; the count requires a positive maxlength.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiSpace, HuiTextarea } from '@nscode/handy-ui'
const limited = ref('')
const counted = ref('')
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiTextarea v-model="limited" :maxlength="30" placeholder="Limit only: 30" />
<HuiTextarea
v-model="counted"
:maxlength="100"
show-word-limit
placeholder="Limit and count: 100"
/>
</HuiSpace>
</template>
Sizes
large, default, and small use font sizes 16, 14, and 12px. A one-row textarea is 40, 32, or 24px high, matching the corresponding input and button. Omitted sizes follow FormItem, Form, then ConfigProvider.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiInput, HuiSpace, HuiTextarea } from '@nscode/handy-ui'
import type { TextareaSize } from '@nscode/handy-ui'
const sizes: TextareaSize[] = ['large', 'default', 'small']
const value = ref('Text')
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace v-for="size in sizes" :key="size" wrap>
<span>{{ size }}</span>
<HuiTextarea v-model="value" :size="size" :rows="1" :maxlength="40" show-word-limit />
<HuiInput v-model="value" :size="size" />
<HuiButton :size="size">{{ size }}</HuiButton>
</HuiSpace>
</HuiSpace>
</template>
Automatic Height
autosize grows and shrinks to fit content. An object sets minRows and maxRows. Explicit minRows can be smaller than rows; omitted minRows uses rows. There is no maximum unless specified. Native dragging is disabled while autosize is enabled.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiSpace, HuiTextarea } from '@nscode/handy-ui'
const automatic = ref('')
const bounded = ref('')
const compact = ref('One line')
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiTextarea v-model="automatic" autosize placeholder="Automatic height" />
<HuiTextarea
v-model="bounded"
:autosize="{ minRows: 2, maxRows: 4 }"
placeholder="Two to four rows"
/>
<HuiTextarea
v-model="compact"
:rows="4"
:autosize="{ minRows: 1, maxRows: 4 }"
placeholder="Can shrink below rows"
/>
</HuiSpace>
</template>
Resize Directions
resize controls native dragging. Compare all four directions with autosize disabled.
<script setup lang="ts">
import { HuiSpace, HuiTextarea } from '@nscode/handy-ui'
import type { TextareaResize } from '@nscode/handy-ui'
const directions: TextareaResize[] = ['none', 'both', 'horizontal', 'vertical']
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiTextarea
v-for="direction in directions"
:key="direction"
:resize="direction"
:placeholder="direction"
/>
</HuiSpace>
</template>
Disabled and Readonly
Disabled fields cannot receive focus or input. Readonly fields retain selection and focus but cannot be edited.
<script setup lang="ts">
import { HuiSpace, HuiTextarea } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiTextarea model-value="Disabled content" disabled />
<HuiTextarea model-value="Readonly content" readonly />
</HuiSpace>
</template>
Events, Modifiers, and Native Attributes
update:modelValue precedes input. IME composition emits neither until the final text is committed, then emits once. change follows the native change event. v-model.trim preserves intermediate whitespace while typing and removes leading/trailing whitespace from the displayed text on change.
class and style belong to the wrapper. The reserved data-hui-field, data-invalid, data-disabled, data-open and data-filled markers share the actual border on the native textarea. Other attributes and undeclared listeners still go to the native textarea, including id, name, autocomplete, form, tabindex, aria-*, @keydown, and @paste.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiSpace, HuiTextarea } from '@nscode/handy-ui'
const value = ref('')
const event = ref('No events yet')
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiTextarea
v-model.trim="value"
name="notes"
autocomplete="off"
aria-label="Notes"
:tabindex="0"
placeholder="Type, paste, then leave the field"
@input="event = 'input: ' + $event"
@change="event = 'change: ' + $event"
@focus="event = 'focus'"
@blur="event = 'blur'"
@keydown.enter="event = 'Enter'"
@paste="event = 'paste'"
/>
<span>Bound value: '{{ value }}'; {{ event }}</span>
</HuiSpace>
</template>
Form Validation
FormItem links its label to the native textarea. Value changes validate change rules; blur validates blur rules. Disable automatic validation with validate-event="false". Use form to associate the native textarea with an external form element.
<script setup lang="ts">
import { reactive, ref } from 'vue'
import { HuiButton, HuiForm, HuiFormItem, HuiSpace, HuiTextarea } from '@nscode/handy-ui'
import type { FormRule } from '@nscode/handy-ui'
const model = reactive({ notes: '' })
const rules: Record<string, FormRule[]> = {
notes: [{ required: true, message: 'Enter notes', trigger: 'blur' }]
}
const event = ref('Waiting')
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiForm
id="textarea-example-form"
:model="model"
:rules="rules"
label-width="100px"
@submit.prevent="event = 'Submitted: ' + model.notes"
>
<HuiFormItem prop="notes" label="Notes">
<HuiTextarea
v-model="model.notes"
form="textarea-example-form"
:maxlength="200"
show-word-limit
/>
</HuiFormItem>
<HuiButton native-type="submit">Submit</HuiButton>
</HuiForm>
<HuiTextarea :validate-event="false" placeholder="No automatic validation" />
<span>{{ event }}</span>
</HuiSpace>
</template>
Public Focus Methods
Use TextareaExpose for component refs. focus() ignores disabled or unmounted fields; readonly fields remain focusable. blur() only blurs this field when it is active in its owner document and preserves external focus. Neither method changes the value.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiTextarea, HuiButton, HuiSpace } from '@nscode/handy-ui'
import type { TextareaExpose } from '@nscode/handy-ui'
const control = ref<TextareaExpose>()
const value = ref('Notes')
const event = ref('Waiting')
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace wrap>
<HuiButton @mousedown.prevent @click="control?.focus()">focus()</HuiButton>
<HuiButton @mousedown.prevent @click="control?.blur()">blur()</HuiButton>
</HuiSpace>
<HuiTextarea ref="control" v-model="value" @focus="event = 'focus'" @blur="event = 'blur'" />
<span>{{ event }}</span>
</HuiSpace>
</template>
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| modelValue | string | null | undefined | Passing the property controls it; omitted: internally stored empty string. Explicit null/undefined displays empty text |
| modelModifiers | Record<string, boolean> | — | Supplied by Vue for v-model modifiers; not forwarded to the native field |
| placeholder | string | — | Placeholder text |
| size | 'large' | 'default' | 'small' | — | Inherits FormItem, Form, ConfigProvider, then default |
| disabled | boolean | false | Disable the field |
| readonly | boolean | false | Prevent editing while retaining focus and selection |
| maxlength | number | — | Native maximum text length |
| showWordLimit | boolean | false | Show the character count when maxlength is positive |
| rows | number | 2 | Initial number of rows |
| autosize | boolean | TextareaAutosize | false | Size to content, optionally bounded by row counts |
| resize | 'none' | 'both' | 'horizontal' | 'vertical' | 'vertical' | Native resize direction; forced to none with autosize |
| autocomplete | string | — | Native autocomplete value |
| form | string | — | ID of the associated native form |
| validateEvent | boolean | true | Validate on value changes and blur |
Events
| Event | Payload | Description |
|---|---|---|
| update:modelValue | (value: string) | Input update, once after IME composition completes |
| input | (value: string) | Follows update:modelValue |
| change | (value: string) | Native change after modified text loses focus |
| focus | (event: FocusEvent) | Native textarea receives focus |
| blur | (event: FocusEvent) | Native textarea loses focus |
Slots
No slots.
Methods
| Name | Type | Description |
|---|---|---|
| focus | () => void | Focus the current field unless disabled or unmounted |
| blur | () => void | Blur only this field in its owner document |
| textareaRef | HTMLTextAreaElement | undefined | Current native element; undefined after unmount |
Types
import type {
TextareaProps,
TextareaEmits,
TextareaExpose,
TextareaAutosize,
TextareaResize,
TextareaSize
} from '@nscode/handy-ui'
type TextareaSize = 'large' | 'default' | 'small'
type TextareaResize = 'none' | 'both' | 'horizontal' | 'vertical'
interface TextareaAutosize {
minRows?: number
maxRows?: number
}
interface TextareaProps {
modelValue?: string | null
modelModifiers?: Record<string, boolean>
placeholder?: string
size?: TextareaSize
disabled?: boolean
readonly?: boolean
maxlength?: number
showWordLimit?: boolean
rows?: number
autosize?: boolean | TextareaAutosize
resize?: TextareaResize
autocomplete?: string
form?: string
validateEvent?: boolean
}
interface TextareaEmits {
'update:modelValue': [value: string]
input: [value: string]
change: [value: string]
focus: [event: FocusEvent]
blur: [event: FocusEvent]
}
interface TextareaExpose {
focus: () => void
blur: () => void
textareaRef: HTMLTextAreaElement | undefined
}