Textarea
The Textarea component provides a flexible multi-line text input with features like auto-resize, character counting, and size variants. It maintains consistency with other form components using typography tokens and includes comprehensive accessibility support.
Basic Textarea
Multi-line text input for longer content
Enter your message or comments
Share your thoughts or feedback
Character Counts:
Message: 0 characters
Comment: 107 characters
API Reference
Props
| Prop | Type | Default | Description |
|---|---|---|---|
id | string | required | Unique identifier for the textarea |
name | string | id | Form field name |
label | string | required | Field label text |
value | string | '' | Textarea value (bindable) |
placeholder | string | '' | Placeholder text |
required | boolean | false | Whether field is required |
disabled | boolean | false | Disables the textarea |
error | string \| null | null | Error message to display |
helper | string | - | Helper text below textarea |
rows | number | 4 | Initial number of rows |
maxLength | number | - | Maximum character limit |
autoResize | boolean | true | Enable automatic height adjustment |
size | 'sm' \| 'md' \| 'lg' | 'md' | Textarea size variant |
Usage Examples
Basic Textarea
<script>
import Textarea from '$lib/shared/components/forms/Textarea.svelte';
let message = $state('');
</script>
<Textarea
id="message"
label="Your Message"
bind:value={message}
placeholder="Type your message here..."
helper="Share your thoughts or feedback"
/> With Character Limit
<script>
import Textarea from '$lib/shared/components/forms/Textarea.svelte';
let description = $state('');
</script>
<Textarea
id="description"
label="Product Description"
bind:value={description}
placeholder="Describe your product..."
maxLength={500}
helper="Maximum 500 characters"
rows={6}
/> Auto-resize Disabled
<script>
import Textarea from '$lib/shared/components/forms/Textarea.svelte';
let code = $state('');
</script>
<Textarea
id="code"
label="Code Snippet"
bind:value={code}
placeholder="Paste your code here..."
autoResize={false}
rows={10}
helper="Fixed height with scrollbar"
/> Different Sizes
<!-- Compact -->
<Textarea
id="notes"
label="Quick Notes"
size="sm"
placeholder="Brief notes..."
rows={2}
/>
<!-- Large -->
<Textarea
id="essay"
label="Essay"
size="lg"
placeholder="Write your essay..."
rows={8}
/> Features
Auto-resize
The textarea automatically adjusts its height based on content:
- Enabled by default - Grows as user types
- Configurable - Can be disabled for fixed height
- Smooth transition - Uses CSS transitions for smooth resizing
- Minimum height - Respects initial
rowssetting
Character Counting
When maxLength is set:
- Real-time counter - Shows current/max characters
- Visual feedback - Counter appears in top-right
- Native validation - Browser enforces the limit
- Accessible - Screen readers announce character limits
Typography Integration
Uses design system tokens for consistent text rendering:
/* Small size */
--typography-caption-size
--typography-caption-weight
--typography-caption-line
/* Medium size (default) */
--typography-body-size
--typography-body-weight
--typography-body-line
/* Large size */
--typography-heading-size
--typography-heading-weight
--typography-heading-line Styling
Visual Design
- Consistent borders - Matches TextInput styling exactly
- Subtle background tint - Very light color-mix for depth
- Focus states - Clear focus ring and background change
- Dark mode support - Automatically adjusts colors
- Custom scrollbar - Styled for better visual integration
Size Variants
| Size | Min Height | Typography Tokens | Use Case |
|---|---|---|---|
sm | 4rem | caption | Compact forms, notes |
md | 6rem | body (default) | Standard messages |
lg | 8rem | heading | Essays, descriptions |
CSS Classes
.field-textarea- Base textarea styles.field-error- Error state modifier.field-error-message- Error message styling.field-hint- Helper text styling.text-tertiary-minimal- Character counter
Accessibility
ARIA Support
aria-required- Indicates required fieldsaria-invalid- Communicates error statesaria-describedby- Links helper text and errorsrole="alert"- Error messages announced
Character Limits
When using maxLength:
- Visual counter shows progress
- Screen readers announce limits
- Native browser validation
- Clear error states when exceeded
Keyboard Interaction
- Tab - Navigate to textarea
- Shift+Tab - Navigate backwards
- Enter - New line (not submit)
- Ctrl/Cmd+A - Select all text
- Standard editing - Cut, copy, paste, undo
Best Practices
Content Guidelines
- Clear labels - Describe the expected content
- Helpful placeholders - Show format or examples
- Appropriate sizing - Match textarea size to expected content
- Character limits - Set reasonable limits with clear feedback
Layout Considerations
- Auto-resize for forms - Let content determine height
- Fixed height for UI - Use when space is constrained
- Adequate spacing - Ensure breathing room around textareas
- Mobile optimization - Test with virtual keyboards
Validation Patterns
- On blur validation - Check content when user leaves field
- Real-time for limits - Show character count progress
- Submit validation - Final check before form submission
- Clear error messages - Explain what needs to be fixed
Common Use Cases
Content Creation
- Blog post content
- Product descriptions
- User feedback forms
- Comment systems
Communication
- Contact forms
- Support tickets
- Message composition
- Notes and documentation
Configuration
- Code snippets
- JSON/YAML configuration
- Custom CSS/HTML
- API responses
Performance Tips
- Debounce auto-resize - Avoid excessive height calculations
- Limit max height - Prevent extremely tall textareas
- Virtual scrolling - For very long content
- Lazy validation - Don’t validate on every character