Textarea
<sl-textarea> | SlTextarea
Textareas collect data from the user and allow multiple lines of text.
<sl-textarea></sl-textarea>
import SlTextarea from '@shoelace-style/shoelace/dist/react/textarea'; const App = () => <SlTextarea />;
This component works with standard <form>
elements. Please refer to the section on
form controls to learn more about form submission and
client-side validation.
Examples
Labels
Use the label
attribute to give the textarea an accessible label. For labels that contain HTML,
use the label
slot instead.
<sl-textarea label="Comments"></sl-textarea>
import SlTextarea from '@shoelace-style/shoelace/dist/react/textarea'; const App = () => <SlTextarea label="Comments" />;
Help Text
Add descriptive help text to a textarea with the help-text
attribute. For help texts that
contain HTML, use the help-text
slot instead.
<sl-textarea label="Feedback" help-text="Please tell us what you think."> </sl-textarea>
import SlTextarea from '@shoelace-style/shoelace/dist/react/textarea'; const App = () => <SlTextarea label="Feedback" help-text="Please tell us what you think." />;
Rows
Use the rows
attribute to change the number of text rows that get shown.
<sl-textarea rows="2"></sl-textarea>
import SlTextarea from '@shoelace-style/shoelace/dist/react/textarea'; const App = () => <SlTextarea rows={2} />;
Placeholders
Use the placeholder
attribute to add a placeholder.
<sl-textarea placeholder="Type something"></sl-textarea>
import SlTextarea from '@shoelace-style/shoelace/dist/react/textarea'; const App = () => <SlTextarea placeholder="Type something" />;
Filled Textareas
Add the filled
attribute to draw a filled textarea.
<sl-textarea placeholder="Type something" filled></sl-textarea>
import SlTextarea from '@shoelace-style/shoelace/dist/react/textarea'; const App = () => <SlTextarea placeholder="Type something" filled />;
Disabled
Use the disabled
attribute to disable a textarea.
<sl-textarea placeholder="Textarea" disabled></sl-textarea>
import SlTextarea from '@shoelace-style/shoelace/dist/react/textarea'; const App = () => <SlTextarea placeholder="Textarea" disabled />;
Sizes
Use the size
attribute to change a textarea’s size.
<sl-textarea placeholder="Small" size="small"></sl-textarea> <br /> <sl-textarea placeholder="Medium" size="medium"></sl-textarea> <br /> <sl-textarea placeholder="Large" size="large"></sl-textarea>
import SlTextarea from '@shoelace-style/shoelace/dist/react/textarea'; const App = () => ( <> <SlTextarea placeholder="Small" size="small"></SlTextarea> <br /> <SlTextarea placeholder="Medium" size="medium"></SlTextarea> <br /> <SlTextarea placeholder="Large" size="large"></SlTextarea> </> );
Prevent Resizing
By default, textareas can be resized vertically by the user. To prevent resizing, set the
resize
attribute to none
.
<sl-textarea resize="none"></sl-textarea>
import SlTextarea from '@shoelace-style/shoelace/dist/react/textarea'; const App = () => <SlTextarea resize="none" />;
Expand with Content
Textareas will automatically resize to expand to fit their content when resize
is set to
auto
.
<sl-textarea resize="auto"></sl-textarea>
import SlTextarea from '@shoelace-style/shoelace/dist/react/textarea'; const App = () => <SlTextarea resize="auto" />;
Importing
If you’re using the autoloader or the traditional loader, you can ignore this section. Otherwise, feel free to use any of the following snippets to cherry pick this component.
To import this component from the CDN using a script tag:
<script type="module" src="https://cdn.jsdelivr.net/npm/@shoelace-style/shoelace@2.16.0/cdn/components/textarea/textarea.js"></script>
To import this component from the CDN using a JavaScript import:
import 'https://cdn.jsdelivr.net/npm/@shoelace-style/shoelace@2.16.0/cdn/components/textarea/textarea.js';
To import this component using a bundler:
import '@shoelace-style/shoelace/dist/components/textarea/textarea.js';
To import this component as a React component:
import SlTextarea from '@shoelace-style/shoelace/dist/react/textarea';
Slots
Name | Description |
---|---|
label
|
The textarea’s label. Alternatively, you can use the label attribute. |
help-text
|
Text that describes how to use the input. Alternatively, you can use the
help-text attribute.
|
Learn more about using slots.
Properties
Name | Description | Reflects | Type | Default |
---|---|---|---|---|
name
|
The name of the textarea, submitted as a name/value pair with form data. |
string
|
''
|
|
value
|
The current value of the textarea, submitted as a name/value pair with form data. |
string
|
''
|
|
size
|
The textarea’s size. |
|
'small' | 'medium' | 'large'
|
'medium'
|
filled
|
Draws a filled textarea. |
|
boolean
|
false
|
label
|
The textarea’s label. If you need to display HTML, use the label slot instead. |
string
|
''
|
|
helpText
help-text
|
The textarea’s help text. If you need to display HTML, use the help-text slot instead.
|
string
|
''
|
|
placeholder
|
Placeholder text to show as a hint when the input is empty. |
string
|
''
|
|
rows
|
The number of rows to display by default. |
number
|
4
|
|
resize
|
Controls how the textarea can be resized. |
'none' | 'vertical' | 'auto'
|
'vertical'
|
|
disabled
|
Disables the textarea. |
|
boolean
|
false
|
readonly
|
Makes the textarea readonly. |
|
boolean
|
false
|
form
|
By default, form controls are associated with the nearest containing
<form> element. This attribute allows you to place the form control outside of a
form and associate it with the form that has this id . The form must be in the same
document or shadow root for this to work.
|
|
string
|
''
|
required
|
Makes the textarea a required field. |
|
boolean
|
false
|
minlength
|
The minimum length of input that will be considered valid. |
number
|
- | |
maxlength
|
The maximum length of input that will be considered valid. |
number
|
- | |
autocapitalize
|
Controls whether and how text input is automatically capitalized as it is entered by the user. |
'off' | 'none' | 'on' | 'sentences' | 'words' | 'characters'
|
- | |
autocorrect
|
Indicates whether the browser’s autocorrect feature is on or off. |
string
|
- | |
autocomplete
|
Specifies what permission the browser has to provide assistance in filling out form field values. Refer to this page on MDN for available values. |
string
|
- | |
autofocus
|
Indicates that the input should receive focus on page load. |
boolean
|
- | |
enterkeyhint
|
Used to customize the label or icon of the Enter key on virtual keyboards. |
'enter' | 'done' | 'go' | 'next' | 'previous' | 'search' | 'send'
|
- | |
spellcheck
|
Enables spell checking on the textarea. |
boolean
|
true
|
|
inputmode
|
Tells the browser what type of data will be entered by the user, allowing it to display the appropriate virtual keyboard on supportive devices. |
'none' | 'text' | 'decimal' | 'numeric' | 'tel' | 'search' | 'email' | 'url'
|
- | |
defaultValue
|
The default value of the form control. Primarily used for resetting the form control. |
string
|
''
|
|
validity
|
Gets the validity state object | - | - | |
validationMessage
|
Gets the validation message | - | - | |
updateComplete |
A read-only promise that resolves when the component has finished updating. |
Learn more about attributes and properties.
Events
Name | React Event | Description | Event Detail |
---|---|---|---|
sl-blur |
onSlBlur |
Emitted when the control loses focus. | - |
sl-change |
onSlChange |
Emitted when an alteration to the control’s value is committed by the user. | - |
sl-focus |
onSlFocus |
Emitted when the control gains focus. | - |
sl-input |
onSlInput |
Emitted when the control receives input. | - |
sl-invalid |
onSlInvalid |
Emitted when the form control has been checked for validity and its constraints aren’t satisfied. | - |
Learn more about events.
Methods
Name | Description | Arguments |
---|---|---|
focus() |
Sets focus on the textarea. |
options: FocusOptions
|
blur() |
Removes focus from the textarea. | - |
select() |
Selects all the text in the textarea. | - |
scrollPosition() |
Gets or sets the textarea’s scroll position. |
position: { top?: number; left?: number }
|
setSelectionRange() |
Sets the start and end positions of the text selection (0-based). |
selectionStart: number, selectionEnd: number, selectionDirection: 'forward' | 'backward' | 'none'
|
setRangeText() |
Replaces a range of text with a new string. |
replacement: string, start: number, end: number, selectMode: 'select' | 'start' | 'end' |
'preserve'
|
checkValidity() |
Checks for validity but does not show a validation message. Returns true when valid and
false when invalid.
|
- |
getForm() |
Gets the associated form, if one exists. | - |
reportValidity() |
Checks for validity and shows the browser’s validation message if the control is invalid. | - |
setCustomValidity() |
Sets a custom validation message. Pass an empty string to restore validity. |
message: string
|
Learn more about methods.
Parts
Name | Description |
---|---|
form-control |
The form control that wraps the label, input, and help text. |
form-control-label |
The label’s wrapper. |
form-control-input |
The input’s wrapper. |
form-control-help-text |
The help text’s wrapper. |
base |
The component’s base wrapper. |
textarea |
The internal <textarea> control. |
Learn more about customizing CSS parts.