Textarea
Textarea component gives you a textarea HTML element that automatically adjusts its height to match the length of the content within.
Introduction
Joy UI's textarea component is built on top of the Base UI TextareaAutoSize
component.
<Textarea minRows={2} />
Playground
Component
After installation, you can start building with this component using the following basic elements:
<span class="token keyword">import</span> Textarea <span class="token keyword">from</span> <span class="token string">'@mui/joy/Textarea'</span><span class="token punctuation">;</span>
<span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token keyword">function</span> <span class="token function">MyApp</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">Textarea</span></span> <span class="token attr-name">placeholder</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>Type anything…<span class="token punctuation">"</span></span> <span class="token punctuation">/></span></span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
Variants
The textarea component supports the four global variants: solid (default), soft, outlined, and plain.
Focused ring
Provide these CSS variables to sx
prop to control the focused ring appearance:
--Textarea-focusedInset
: the focused ring's position, either inside(inset
) or outside(var(--any, )
) of the Textarea.--Textarea-focusedThickness
: the size of the focused ring.--Textarea-focusedHighlight
: the color of the focused ring.
Debugging the focus ring
To display the Textarea's focus ring by simulating user's focus, inspect the Textarea element and toggle the pseudostate panel.
- If you inspect the Textarea's root element, with
.MuiTextarea-root
class, you have to toggle on the:focus-within
state. - If you inspect the
<input>
element, you have to toggle on the:focus
state.
Triggering the focus ring
To trigger the focus ring programmatically, set the CSS variable --Textarea-focused: 1
.
Note that using the color
prop with danger as value gets the same result:
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">Textarea</span></span> <span class="token attr-name">color</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>danger<span class="token punctuation">"</span></span> <span class="token punctuation">/></span></span>
Rows
Use the minRows
to set the minimum number of lines to show and maxRows
to limit the number of lines that users will see.
Decorators
Use the startDecorator
and/or endDecorator
props to add supporting icons or elements to the textarea.
It's usually more common to see textarea components using decorators at the top and bottom.
0 character(s)
Accessibility
In order for the textarea to be accessible, it should be linked to a label.
The FormControl
automatically generates a unique id that links the textarea with the FormLabel
component:
Alternatively, you can do it manually by targeting the textarea slot:
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>label</span> <span class="token attr-name">htmlFor</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>unique-id<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>Label<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>label</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span><span class="token class-name">Textarea</span></span>
<span class="token attr-name">slotProps</span><span class="token script language-javascript"><span class="token script-punctuation punctuation">=</span><span class="token punctuation">{</span><span class="token punctuation">{</span>
<span class="token literal-property property">textarea</span><span class="token operator">:</span> <span class="token punctuation">{</span>
<span class="token literal-property property">id</span><span class="token operator">:</span> <span class="token string">'unique-id'</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">}</span></span>
<span class="token punctuation">/></span></span>
Common examples
Focus outline
This example shows how to replace the default focus ring appearance with CSS outline.
Floating label
To create a floating label textarea, a custom component (combination of <textarea>
and <label>
) is required to replace the default textarea slot.