Skip to content
WebJs
Menu

Displays a form textarea or a component that looks like a textarea.

import { html } from '@webjsdev/core';
import { textareaClass } from '#components/ui/textarea.ts';

export default function TextareaDemo() {
  return html`
    <textarea data-slot="textarea" placeholder="Type your message here." aria-label="Message" class=${textareaClass()}></textarea>
  `;
}
View Code

Installation

pnpm dlx @webjsdev/ui add textarea

Usage

import { textareaClass } from '#components/ui/textarea.ts';
<textarea class=${textareaClass()}></textarea>

Field

Use fieldClass(), fieldLabelClass(), and fieldDescriptionClass() to create a textarea with a label and description.

Enter your message below.

import { html } from '@webjsdev/core';
import { fieldClass, fieldLabelClass, fieldDescriptionClass } from '#components/ui/field.ts';
import { textareaClass } from '#components/ui/textarea.ts';

export default function TextareaField() {
  return html`
    <div role="group" data-slot="field" data-orientation="vertical" class=${fieldClass()}>
      <label data-slot="field-label" for="textarea-message" class=${fieldLabelClass()}>Message</label>
      <p data-slot="field-description" class=${fieldDescriptionClass()}>Enter your message below.</p>
      <textarea data-slot="textarea" id="textarea-message" placeholder="Type your message here." class=${textareaClass()}></textarea>
    </div>
  `;
}
View Code

Disabled

Use the disabled attribute to disable the textarea. To style the disabled state, add the data-disabled attribute to the field.

import { html } from '@webjsdev/core';
import { fieldClass, fieldLabelClass } from '#components/ui/field.ts';
import { textareaClass } from '#components/ui/textarea.ts';

export default function TextareaDisabled() {
  return html`
    <div role="group" data-slot="field" data-orientation="vertical" data-disabled="true" class=${fieldClass()}>
      <label data-slot="field-label" for="textarea-disabled" class=${fieldLabelClass()}>Message</label>
      <textarea data-slot="textarea" id="textarea-disabled" placeholder="Type your message here." disabled class=${textareaClass()}></textarea>
    </div>
  `;
}
View Code

Invalid

Use the aria-invalid attribute to mark the textarea as invalid. To style the invalid state, add the data-invalid attribute to the field.

Please enter a valid message.

import { html } from '@webjsdev/core';
import { fieldClass, fieldLabelClass, fieldDescriptionClass } from '#components/ui/field.ts';
import { textareaClass } from '#components/ui/textarea.ts';

export default function TextareaInvalid() {
  return html`
    <div role="group" data-slot="field" data-orientation="vertical" data-invalid="true" class=${fieldClass()}>
      <label data-slot="field-label" for="textarea-invalid" class=${fieldLabelClass()}>Message</label>
      <textarea data-slot="textarea" id="textarea-invalid" placeholder="Type your message here." aria-invalid="true" class=${textareaClass()}></textarea>
      <p data-slot="field-description" class=${fieldDescriptionClass()}>Please enter a valid message.</p>
    </div>
  `;
}
View Code

Button

Pair with buttonClass() to create a textarea with a submit button.

import { html } from '@webjsdev/core';
import { buttonClass } from '#components/ui/button.ts';
import { textareaClass } from '#components/ui/textarea.ts';

export default function TextareaButton() {
  return html`
    <div class="grid w-full gap-2">
      <textarea data-slot="textarea" placeholder="Type your message here." aria-label="Message" class=${textareaClass()}></textarea>
      <button type="button" data-slot="button" data-variant="default" data-size="default" class=${buttonClass()}>Send message</button>
    </div>
  `;
}
View Code

RTL

To enable RTL support, see the RTL configuration guide.

شاركنا أفكارك حول خدمتنا.

import { html } from '@webjsdev/core';
import { cn } from '#lib/utils/cn.ts';
import { fieldClass, fieldLabelClass, fieldDescriptionClass } from '#components/ui/field.ts';
import { textareaClass } from '#components/ui/textarea.ts';

const translations = {
  en: {
    dir: 'ltr',
    values: {
      label: 'Feedback',
      placeholder: 'Your feedback helps us improve...',
      description: 'Share your thoughts about our service.',
    },
  },
  ar: {
    dir: 'rtl',
    values: {
      label: 'التعليقات',
      placeholder: 'تعليقاتك تساعدنا على التحسين...',
      description: 'شاركنا أفكارك حول خدمتنا.',
    },
  },
  he: {
    dir: 'rtl',
    values: {
      label: 'משוב',
      placeholder: 'המשוב שלך עוזר לנו להשתפר...',
      description: 'שתף את מחשבותיך על השירות שלנו.',
    },
  },
};

// The provider is the dir attribute: the field styles itself with logical
// properties, so dir="rtl" on it flips the whole layout.
export default function TextareaRtl() {
  const lang = 'ar';
  const { dir, values: t } = translations[lang];

  return html`
    <div role="group" data-slot="field" data-orientation="vertical" dir=${dir} lang=${lang} class=${cn(fieldClass(), 'w-full max-w-xs')}>
      <label data-slot="field-label" for="feedback" class=${fieldLabelClass()}>${t.label}</label>
      <textarea data-slot="textarea" id="feedback" placeholder=${t.placeholder} rows="4" class=${textareaClass()}></textarea>
      <p data-slot="field-description" class=${fieldDescriptionClass()}>${t.description}</p>
    </div>
  `;
}
View Code