Textarea
Displays a form textarea or a component that looks like a textarea.
import { Textarea } from "~/components/ui/textarea"
export default function TextareaDemo() {
return <Textarea class="max-w-xs" placeholder="Type your message here." />
}Installation
Copy and paste the following code into your project.
import { type ComponentProps } from "@solidjs/web"
import { omit } from "solid-js"
import { cn } from "~/lib/utils"
type TextareaProps = ComponentProps<"textarea">
const Textarea = (props: TextareaProps) => {
const others = omit(props, "class", "rows")
return (
<textarea
data-slot="textarea"
class={cn(
"rounded-lg border border-input bg-transparent px-2.5 py-2 text-base transition-colors focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50 disabled:bg-input/50 aria-invalid:border-destructive aria-invalid:ring-3 aria-invalid:ring-destructive/20 md:text-sm dark:bg-input/30 dark:disabled:bg-input/80 dark:aria-invalid:border-destructive/50 dark:aria-invalid:ring-destructive/40 flex min-h-16 w-full outline-none placeholder:text-muted-foreground disabled:cursor-not-allowed disabled:opacity-50",
props.rows == null && "field-sizing-content",
props.class
)}
rows={props.rows}
{...others}
/>
)
}
export { Textarea, type TextareaProps }Update the import paths to match your project setup.
Usage
import { Textarea } from "~/components/ui/textarea"<Textarea />Field
Use Field, FieldLabel, and FieldDescription to create a textarea with a label and description.
Max 500 characters.
import { Textarea } from "~/components/ui/textarea"
export default function TextareaField() {
return (
<div class="grid w-full max-w-xs gap-2">
<label class="text-sm font-medium" for="field-message">
Message
</label>
<Textarea
id="field-message"
placeholder="Type your message here."
rows={4}
/>
<p class="text-sm text-muted-foreground">Max 500 characters.</p>
</div>
)
}Disabled
Use the disabled prop to disable the textarea. To style the disabled state, add the data-disabled attribute to the Field component.
import { Field, FieldLabel } from "~/components/ui/field"
import { Textarea } from "~/components/ui/textarea"
export default function TextareaDisabled() {
return (
<Field class="w-full max-w-xs" data-disabled="true">
<FieldLabel for="textarea-disabled">Message</FieldLabel>
<Textarea
disabled
id="textarea-disabled"
placeholder="Type your message here."
/>
</Field>
)
}Invalid
Use the aria-invalid prop to mark the textarea as invalid. To style the invalid state, add the data-invalid attribute to the Field component.
Please enter a valid message.
import { Field, FieldDescription, FieldLabel } from "~/components/ui/field"
import { Textarea } from "~/components/ui/textarea"
export default function TextareaInvalid() {
return (
<Field data-invalid>
<FieldLabel for="textarea-invalid">Message</FieldLabel>
<Textarea
id="textarea-invalid"
placeholder="Type your message here."
aria-invalid="true"
/>
<FieldDescription>Please enter a valid message.</FieldDescription>
</Field>
)
}Button
Pair with Button to create a textarea with a submit button.
import { Button } from "~/components/ui/button"
import { Textarea } from "~/components/ui/textarea"
export default function TextareaButton() {
return (
<div class="grid w-full max-w-xs gap-2">
<Textarea placeholder="Type your message here." />
<Button>Send message</Button>
</div>
)
}API Reference
All native textarea attributes are forwarded to the root element.