TextArea
A TextArea is a multi-line text input that allows manual resizing if needed.
Guidelines
When to use text areas
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.
Use TextArea to let users enter long text that exceeds one line. Avoid using TextArea if the text to be entered is short. In this case use TextInput instead.
Specifications
TextArea may include the following optional elements:
- Start icon (optional)
Icons can simplify the identification of specific user inputs. - Placeholder (optional)
The placeholder text provides an example of what type of information is being requested in the input. - End icon (optional)
A secondary end icon can be included if needed. - Grabber (optional)
Optionally, the grabber may be displayed to allow users the ability to vertically resize the text area as needed.
Component limitations
The base min-width for the TextArea component is @size-1600
(equivalent to 256px
in the default Codex theme), but it's customizable to a smaller width if necessary. There's no maximum width limit.
The min-height is set at @size-400
(equivalent to 64px
in the default Codex theme). The grabber 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.
Refer to the TextArea component in Codex Figma.
Types
According to the size properties, there are the following text area types:
With grabber
By default, a grabber will be visible, signaling to users that they can vertically resize the text area field. In this scenario, if the content cannot fit within the visible area, a scrollbar will appear to to navigate through the text that exceeds the visible area.
Use this type of text area when you require it to have a more compact size, especially in situations where it is positioned within other components like dialogs.
Autosize
When the autosize property is enabled, the text area’s height will be automatically resized with the length of the text. There is no need for the grabber indicator in this case, and scrolling within the content is unnecessary since the entire content will be fully visible. Choose this type of text area when there is ample space available on the page where the text area is situated.
Interaction states
Text areas have the following visually separate states:
- Default
- Hover
- Active - Focus
- Filled
- Disabled
- Read-only
- Error default
- Error hover
- Error filled
Best practices
Consider the following recommendations when using textareas.
Usage
- Use a TextArea when the intention is to compose lengthy text entries.
- Integrate the TextArea within a Field component to use all available properties of Field, such as label, helper text, and validation messages.
- Use TextArea for single-line inputs where a simple TextInput is sufficient.
- Use TextArea for source editing.
- Use a standalone TextArea outside of a Field component.
- Use TextArea without a label, as the label is essential for accessibility and ease of scanning.
Icon
- Use a start icon to visually reinforce the purpose or context of the TextArea.
- Use a start icon that meets the input's requirements.
- Use icons that are difficult to understand or do not clearly convey their purpose.
Character counter
- Use the character counter when users need to input text within a specific character limit.
- Use character counter when users are unlikely to reach the limit.
Keyboard navigation
Key | Function |
---|---|
Left arrow / Right arrow | The left and right arrows navigate through the text content within the input. |
Up arrow / Down arrow | Up 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. |
Demos
Configurable
Name | Value |
---|---|
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. |
Default
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>
. The value updates due to an emitted input
event.
By default, the initial value of the autosize
prop is false. When autosize
is false, the textarea will display a grabber/resize tool and a scrollbar to view the overflow content. The <textarea>
can be manually resized vertically to increase height of the element.
Note: The initial min-height of the <textarea>
is set to 64px.
Browser Support Warning
While our design system strives to provide a consistent experience across different platforms and browsers, it's important to be aware that the vertical resizing feature will not be available on iOS devices, including iPhones and iPads. This limitation is due to the default behavior of iOS Safari.
With placeholder
We passed in a native attribute, placeholder
, to hint to users what to enter into the control.
Note: Placeholders are not a substitute for a proper <label>
element tied to the control.
With rows
This example demonstrates how to pass in the native attribute, rows
, to the <textarea>
.
The rows
attribute takes a positive number which represents the number of text lines to display in the control.
With autosize
When the autosize
prop is set to true
, the TextArea automatically grows with the height of the content inside <textarea>
.
The grabber/resize tool is not displayed when autosize
is set to true
.
With icons
TextArea can pass in a start icon and end icon as props. This example shows how to add icons to the component. Refer to Icon.
Disabled
You can disable the component by adding the disabled
attribute.
When <textarea>
is disabled
, the user cannot interact with the control. Users cannot click or select in the control and the form cannot be submitted.
Readonly
You can make the component readonly
by adding the readonly
attribute.
When <textarea>
is readonly
, the user cannot modify the value of the control. Some key differences between disabled
and readonly
is that readonly
does not prevent users from selecting and clicking in the form. Users can highlight and copy content in readonly. Readonly is tabbable and the form can be submitted.
One example usage of readonly
textarea is when you want to prevent a user from typing into the textarea until a condition is met like selecting a a checkbox. In this situation, when the condition is met we can use JavaScript to remove the readonly
to make the textarea editable.
Form field
A TextArea can be wrapped in the Field component to add features like a semantic label, description and help text, validation messages, and more. Refer to the Field page for more information.
Native validation
The TextArea component exposes native constraint validation methods. Refer to the methods below to see 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.
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 name | Description | Type | Values | Default |
---|---|---|---|---|
modelValue | Current value of the textarea. Provided by v-model binding in the parent component. | string | - | '' |
status | status attribute of the textarea. | ValidationStatusType | - | 'default' |
disabled | Whether the textarea is disabled. | boolean | - | false |
autosize | Describes 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. | boolean | true , false | false |
startIcon | An icon at the start of the textarea element. Similar to a ::before pseudo-element. | Icon|undefined | - | undefined |
endIcon | An icon at the end of the textarea element. Similar to an ::after pseudo-element. | Icon|undefined | - | undefined |
Methods
Method name | Description | Signature |
---|---|---|
focus | Focus the component's textarea element. | Returns: void |
blur | Blur the component's textarea element. | Returns: void |
checkValidity | Check 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/checkValidity | Returns: boolean Whether the textarea is valid |
reportValidity | Check 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/reportValidity | Returns: boolean Whether the textarea is valid |
setCustomValidity | Set custom validity and message for the textarea element. See: https://developer.mozilla.org/en-US/docs/Web/API/HTMLTextAreaElement/setCustomValidity | Params:
void |
Events
Event name | Properties | Description |
---|---|---|
update:modelValue | modelValue string - The new model value | When the textarea value changes. |
input | event InputEvent | When the input value changes via direct use of the input |
change | event Event | When an input value change is committed by the user (e.g. on blur) |
focus | event FocusEvent | When the input comes into focus |
blur | event FocusEvent | When the input loses focus |
invalid | event Event | When 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.
Readonly
Add the readonly
attribute to the <textarea>
element for a readonly textarea.
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.