Textarea
A textarea allows users to enter text on multiple lines
- Status
- production
- Version
- 3.0.4
- Import from
@twilio-paste/core/textarea— or —@twilio-paste/textarea
Guidelines#
A textarea allows users to enter text on multiple lines.
About Textarea#
Use a textarea when you want to allow a user to enter text on multiple lines. A textarea allows for any text to be entered and can't be restricted as other inputs can.
Accessibility#
- Include a visible label on all form fields.
- Each label must use the
htmlForprop that equals theidof the associated textarea. - Don't use placeholder text as a replacement for labels. It can be used to provide an example to users, but will disappear from the field when a user enters text. It's also not broadly supported by assistive technologies and won't display in older browsers.
- Use one of 3 ways to label a form field:
- Visible label with Label (preferred)
- Visible label that's associated to the textarea with
aria-labelledby - Label directly using
aria-label
- Provide clear identification of required fields in the label or at the start of a form. If you use the required field indicator, include the form key at the start of the form.
- Use the
requiredprop to programatically indicate they are required to browsers.
- Use the
- Include inline error text with the error icon on any field that errors to make it visually clear that the field changed.
- If the textarea has associated help text or error text, include the
aria-describedbyprop on the textarea. This should match theidof the help or error text.
Examples#
Textarea#
The textarea should include the base textarea, along with supporting elements to compose a textarea field that gives the user the context they need to successfully fill it out:
- Label — A label should be included for every form field and provide a short title for its associated textarea.
- Required field indicator — In a form where there are fewer or as many required fields as optional, use a required indicator to show users which fields are required to be filled out.
- Help text — Text that's placed below the field to help users prevent an error and describe what makes the form field successful
Hot accessibility tip
Make sure to connect the Label to the TextArea. This is done with the htmlFor prop on the label, and the id prop on the textarea. Those two need to match.
If the textarea has any associated help text, the textarea should also use the aria-describedby prop that equals the id of the help text. This ensures screen readers know the help text ties directly to the textarea.
Textarea with add-ons (prefix/suffix text or icons)#
- Prefix/suffix text — Text that can be used as a prefix and/or suffix to the value that is entered. Use prefix/suffix to help users format text.
- Icon — Icons can be placed in the same area as the prefix and suffix text. Icons should trigger an action (e.g., clearing a field).
States#
Textarea with inline error#
Change a form field to its error state and add an inline error if the value entered doesn't pass validation requirements.
An inline error is placed below the field to inform a user of any errors in their value. If help text exists, the error text should replace and repeat the help text.
Disabled textarea#
Use a disabled form field to show users that they can't interact with the field.
Read-only textarea#
Use a read-only form field when a field's value can't be changed, but users should be able to read or apply focus on the field. For example, use a read-only form field if a user needs to copy the value.
Composition notes#
A textarea field must have at least a label and a textarea.
Positioning form fields#
Stack form fields vertically with $space-80 spacing between each field.
Avoid placing multiple form fields on the same horizontal row to help make it easier to scan a page vertically. Use the Grid component if you need to place form fields horizontally.
Labels and help text#
Labels should clearly describe the value being requested. They should be concise and descriptive, not instructional. To do this:
- Use help text to provide instruction if needed. For example, don't use "Enter the registration code on the back of your SIM card below" as label text. Instead, use "SIM registration code" as a label and "Find the registration code on the back of your SIM card" as help text.
- Avoid articles ("a", "the") and punctuation. For example, use "SIM registration code" rather than "The SIM registration code".
To support internationalization, avoid starting a sentence with the label and using the field to finish it since sentence structures vary across languages. For example, use "Call log expiration date" or "How long should logs be stored?". Don't use "Remove logs after:".
Give users enough information in help text to prevent textarea and formatting errors. Keep it concise and scoped to information that will help with validation. For example, use help text if a password field has specific requirements that a user should know prior to filling it out.
Required field indicator#
Ask for information only when needed. Consider removing the field otherwise.
Use required indicators to show users which fields they must fill out.
Validation#
Validate form fields on form submission.
Validating a form field when the user leaves the current field (on blur) can be helpful to check for syntax errors. However, this can be frustrating to users who tab through controls to navigate a page, and to screen reader users, who might not be able to detect that an error occurred on blur.
Don't prevent form submission by disabling the submit button. An error message can give more information than a disabled button can to help users figure out which fields are invalid.
Errors#
Use inline error text to explain how to fix an error.
Ideally, help text should have enough information to help users prevent errors. If help text exists and you need to show an error, the error text should replace and repeat the help text until the error has been resolved.
Error text should:
- Be actionable. Explain how to fix an error and if reasonable, why it happened so that it might also be prevented in the future.
- Be concise and simple, maybe even more than normal. Avoid jargon. Try to keep error text to 2 sentences or fewer.
- Use the passive voice for textarea errors to avoid placing blame on the user. For example, "A friendly name is required."
- Use the active voice for system errors. For example, "Our systems are currently down. Please contact our support team."
Optional compositional elements#
- Prefix/suffix — Use a prefix or suffix to help users format their textarea and to provide more context about the value a user is entering. For example, you could prefix or suffix a field with a currency symbol, or use suffix to append a character counter. Make sure to consider internationalization when using prefixes or suffixes since formatting may differ across languages. For example, currency symbols are placed on the left in American English, while they're placed on the right in French. Don't use prefix/suffix text as a replacement for a label.
- Icon — Use an icon if you need to give the user additional controls for the field. For example, use an icon to clear a field on click, removing the need for users to manually delete their entered value. Place icon buttons that trigger an action on the right side of the field. If you need 2 actions on a field (e.g., popover trigger and clear field), place the icon button that affects the field value on the right, and the other icon on the left.
- Placeholder — Use a placeholder when you want to give an example of the type of data a user should enter, mainly to help with recall. For example, when asking for links to social media profiles, you could give examples of a few social media sites. Placeholders should be used in addition to the label and not as a replacement since they'll disappear when a user begins entering their own value. If you need to show supporting instructions, use help text instead.
When to use an textarea#
Use a textarea when users are expected to enter text that exceeds a single line, usually longer than a sentence.
Do
Use a textarea to encourage longer text entry.
Don't
Don't use a textarea when text entry is expected to be short since it could confuse users. Use an input instead.
Do
If you limit the length of text entry, show a character counter and explain to users in help text why their entry is restricted.
Don't
Don't have a character limit if you can't explain to the user why their text entry is restricted.
Do
Use help text to help users prevent errors and fill out a form field correctly.
Don't
Don't use placeholder text for validation instructions.
Do
Keep help text and error text concise and simple. If you need to use more than 2 sentences to explain a field, link out to supporting docs or trigger a popover instead.
Don't
Don't use more than 2 sentences in help text or error text.
Do
Include a visible label on every form field.
Don't
Don't use prefix/suffix text as a replacement for a label.
Do
Use a disabled form field to show users that they can't interact with the field, but that it could potentially be enabled through another UI element.
Don't
Don't use a disabled form field to show information that can't be edited. Instead, use a read-only form field or consider another way of showing static information.
Anatomy#
| Property | Default token | Modifiable? |
|---|---|---|
| Label text |
| No |
| Required field indicator |
| No |
| Box Shadow |
| No |
| Background |
| No |
| Prefix/suffix |
| No |
| Prefix/suffix text |
| No |
| Value text |
| No, but it can take any children |
| Placeholder text | $color-text-weak, italic | No |
| Help text | $color-text-weak, $font-size-30 | No |
| Inline error |
| No |
| Spacing |
| No |
Usage Guide#
API#
Installation#
yarn add @twilio-paste/textarea - or - yarn add @twilio-paste/coreUsage#
import {TextArea, Label, HelpText} from '@twilio-paste/core';const Component = () => ( <> <Label htmlFor="foo" required> foo </Label> <TextArea onChange={() => {}} aria-describedby="foo_text" id="foo" name="foo" placeholder="foo" required /> <HelpText id="foo_text">Please enter some text</HelpText> </>);TextArea Props#
All the valid HTML attributes for textarea are supported including the following props:
| Prop | Type | Description | Default |
|---|---|---|---|
| id? | string | Sets the id of the textarea. Should match the htmlFor of Label. | null |
| name? | string | Sets the name of the textarea. | null |
| placeholder? | string | Sets the placeholder of the textarea. | null |
| disabled? | boolean | Disables the textarea. | false |
| readOnly? | boolean | Sets the textarea to readonly. | false |
| required? | boolean | Sets the textarea as required. | false |
| hasError? | boolean | Sets the textarea to an error state. | false |
| onChange? | (event: React.MouseEvent<HTMLElement>) | null | |
| onFocus? | (event: React.MouseEvent<HTMLElement>) | null | |
| onBlur? | (event: React.MouseEvent<HTMLElement>) | null | |
| variant? | 'default', 'inverse' | 'default' |
Label Props#
All the valid HTML attributes for label are supported including the following props:
| Prop | Type | Description | Default |
|---|---|---|---|
| children? | ReactNode | null | |
| htmlFor | string | Sets the for of the label. Should match the id of TextArea. Required. | null |
| disabled? | boolean | Shows the textarea is disabled. | false |
| required? | boolean | Shows the textarea is required. | false |
| marginBottom? | 'space0', 'space10' | 'space10' | |
| variant ? | 'default', 'inverse' | 'default' |
HelpText Props#
All the valid HTML attributes (role, aria-*, type, and so on) including the following props:
| Prop | Type | Description | Default |
|---|---|---|---|
| children? | ReactNode | null | |
| marginTop? | 'space0', 'space20' | 'space20' | |
| variant? | 'default', 'error', 'error_inverse', 'inverse' | Changes the state. | 'default' |