Skip to main content

Textarea

A multi-line text input component with auto-resize, character limits, and typography token integration

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

PropTypeDefaultDescription
idstringrequiredUnique identifier for the textarea
namestringidForm field name
labelstringrequiredField label text
valuestring''Textarea value (bindable)
placeholderstring''Placeholder text
requiredbooleanfalseWhether field is required
disabledbooleanfalseDisables the textarea
errorstring \| nullnullError message to display
helperstring-Helper text below textarea
rowsnumber4Initial number of rows
maxLengthnumber-Maximum character limit
autoResizebooleantrueEnable 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 rows setting

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

SizeMin HeightTypography TokensUse Case
sm4remcaptionCompact forms, notes
md6rembody (default)Standard messages
lg8remheadingEssays, 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 fields
  • aria-invalid - Communicates error states
  • aria-describedby - Links helper text and errors
  • role="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

  1. Clear labels - Describe the expected content
  2. Helpful placeholders - Show format or examples
  3. Appropriate sizing - Match textarea size to expected content
  4. Character limits - Set reasonable limits with clear feedback

Layout Considerations

  1. Auto-resize for forms - Let content determine height
  2. Fixed height for UI - Use when space is constrained
  3. Adequate spacing - Ensure breathing room around textareas
  4. Mobile optimization - Test with virtual keyboards

Validation Patterns

  1. On blur validation - Check content when user leaves field
  2. Real-time for limits - Show character count progress
  3. Submit validation - Final check before form submission
  4. 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

  1. Debounce auto-resize - Avoid excessive height calculations
  2. Limit max height - Prevent extremely tall textareas
  3. Virtual scrolling - For very long content
  4. Lazy validation - Don’t validate on every character