Skip to content

TextArea

A TextArea is a multi-line text input that allows manual resizing if needed.

NameValue
Props
status
autosize
startIcon
endIcon
placeholder
disabled
readonly
rows
View
Reading direction
Note: For icon properties, the relevant icon also needs to be imported from the @wikimedia/codex-icons package. See the usage documentation for more information.

Overview

When to use TextArea

Use the TextArea component to let users enter long text that exceeds one line.

This form element is useful when the user needs to enter a sizable amount of free-form text, for example comments, reviews, feedback, or short essay responses. If the text is expected to be short, use TextInput instead.

About TextArea

TextArea includes the following elements.

Start icon (optional)

A start icon can be included to visually enhance the TextArea's purpose.

  • Use a simple icon that is easily understandable to users.

Textarea

The textarea element's has an optional grabber that allows vertical resizing, and a scrollbar will appear if the text exceeds the height of the textarea before resizing. If the autosize property is enabled, the height of the textarea adjusts automatically according to the length of the text.

Placeholder text (optional)

Placeholder text provides an example of what type of information is being requested in the TextArea.

  • Placeholder text should further illustrate and support the TextArea's label.
  • Placeholder text should never replace the label nor be the input's only description.

End icon (optional)

A secondary end icon can be included if needed.

Examples

Basic usage

By default, the TextArea has a grabber that can be used to manually resize it.

Browser Support Warning

While Codex strives to provide a consistent experience across different platforms and browsers, it's important to be aware that the vertical resizing feature may not be available on iOS devices, including iPhones and iPads. This limitation is due to the default behavior of iOS Safari.

Developer notes

The TextArea component uses v-model to two-way bind the reactive reference to the value of <textarea>. The reactive reference will automatically update when the value changes in the <textarea>.

Custom rows

You can set a number of rows of text to display initially.

  • The number of rows shown should reflect the amount of text expected to be entered.

With autosize

When the autosize property is enabled, the TextArea's height will automatically grow with the length of the text. The grabber and scrollbar are removed since the TextArea expands automatically and shows all of the text at once.

  • Use a TextArea with `autosize` when there is ample space in the layout for the TextArea to expand.

With icons

A TextArea can have a start icon, end icon, or both. Any Codex icon can be used.

  • Use simple icons that are easily understandable to users.

Form field

A TextArea can be wrapped in the Field component to add features like a semantic label, description and help text, and validation messages. A character counter can also be added via the Field component. Refer to the Field page for more information.

Native validation

The TextArea component exposes native constraint validation methods. Refer to the methods below to review all of the supported features.

This demo sets the required attribute on the textarea and validates it when the form is submitted. When the textarea is invalid, it sets the Field's status to "error" and passes the native validation message to the Field component for display. Try submitting the form while leaving the TextArea blank.

Technical implementation

Vue usage

v-model is used to track the current value of the textarea. Refer to the events docs for details on emitted events and their properties.

Attributes passed to textarea

This component will pass any HTML attributes applied to it, except for CSS class, to the <textarea> element within the component.

Props

Prop nameDescriptionTypeValuesDefault
modelValueCurrent value of the textarea.

Provided by v-model binding in the parent component.
string-''
statusstatus attribute of the textarea.ValidationStatusType-'default'
disabledWhether the textarea is disabled.boolean-false
autosizeDescribes whether the textarea grows vertically to show all text.

When autosize is true, the textarea automatically grows in height (vertically). The height of the textarea expands while the user types in the textarea. The content inside the textarea is visible and there's no scroll.
booleantrue, falsefalse
startIconAn icon at the start of the textarea element. Similar to a ::before pseudo-element.Icon|undefined-undefined
endIconAn icon at the end of the textarea element. Similar to an ::after pseudo-element.Icon|undefined-undefined

Methods

Method nameDescriptionSignature
focusFocus the component's textarea element.Returns: void
blurBlur the component's textarea element.Returns: void
checkValidityCheck the validity of the textarea element according to its constraint attributes. Emits an 'invalid' event if the textarea is invalid. See: https://developer.mozilla.org/en-US/docs/Web/API/HTMLTextAreaElement/checkValidityReturns: boolean Whether the textarea is valid
reportValidityCheck the validity of the textarea element and report it as a pop up on the UI. See: https://developer.mozilla.org/en-US/docs/Web/API/HTMLTextAreaElement/reportValidityReturns: boolean Whether the textarea is valid
setCustomValiditySet custom validity and message for the textarea element. See: https://developer.mozilla.org/en-US/docs/Web/API/HTMLTextAreaElement/setCustomValidityParams:
  • message string - The custom validation message to set
Returns: void

Events

Event namePropertiesDescription
update:modelValuemodelValue string - The new model valueWhen the textarea value changes.
inputevent InputEventWhen the input value changes via direct use of the input
changeevent EventWhen an input value change is committed by the user (e.g. on blur)
focusevent FocusEventWhen the input comes into focus
blurevent FocusEventWhen the input loses focus
invalidevent EventWhen the textarea value is invalid according to the textarea's constraint attributes (e.g. minlength, maxlength, or required). See: https://developer.mozilla.org/en-US/docs/Web/HTML/Constraint_validation#validation-related_attributes

CSS-only version

Markup structure

The CSS-only TextArea component consists of a <div> wrapping a <textarea> element.

With icons

You can use CSS-only icons to add start and end icons to the textarea.

Using CSS-only icons

I. Add classes to elements.

You'll need the following CSS classes on the root element:

  • Start icon: .cdx-text-area--has-start-icon
  • End icon: .cdx-text-area--has-end-icon

The icons will be <span> elements with the .cdx-text-area__icon class, plus:

  • Start icon: .cdx-text-area__start-icon
  • End icon: .cdx-text-area__end-icon

You will need to add your own CSS classes to set the icon styles and background image.

II. Choose an icon (list of all icons) to set the background image.

You'll set the background image of the <span> with the icon of your choosing by utilizing a Less mixin built into Codex called .cdx-mixin-css-icon-background-image.

In the example below, we've chosen cdxIconNotBright as the start icon and added a class called cdx-demo-css-icon--not-bright to the <span>. In your stylesheet, the selector, cdx-demo-css-icon--not-bright, will call the background image mixin and pass in the icon name of your choice. The icon name is passed in the mixin as a argument and is in lower-case and separated by hyphens: .cdx-mixin-css-icon-background-image( @cdx-icon-not-bright );.

Disabled

Add the disabled attribute to the <textarea> element for a disabled textarea. This also works for the readonly attribute.

Error state

Add the cdx-text-area--status-error class to the root element to show error styles.

Form field

The CSS-only Field component can wrap the CSS-only TextArea to add features like a semantic label, description, optional flag, and help text. Refer to the Field for more information.

Briefly describe your changes
By saving changes, you agree to the Terms of Use, and you irrevocably agree to release your contribution under the CC BY-SA 4.0 License and the GFDL. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.

Keyboard navigation

KeyFunction
Left arrow / Right arrowThe left and right arrows navigate through the text content within the input.
Up arrow / Down arrowUp arrow moves the focus from the last position of the input to the first one, while down arrow moves it from the first position to the last.