import { Textarea } from "@chakra-ui/react"
const Demo = () => {
return <Textarea placeholder="Comment..." />
}
Usage
import { Textarea } from "@chakra-ui/react"
<Textarea placeholder="..." />
Examples
Variants
Use the variant
prop to change the appearance of the textarea.
import { Stack, Textarea } from "@chakra-ui/react"
const Demo = () => {
return (
<Stack gap="4">
<Textarea variant="outline" placeholder="outline" />
<Textarea variant="subtle" placeholder="subtle" />
<Textarea variant="flushed" placeholder="flushed" />
</Stack>
)
}
Sizes
Use the size
prop to change the size of the textarea.
import { Stack, Textarea } from "@chakra-ui/react"
const Demo = () => {
return (
<Stack gap="4">
<Textarea size="xs" placeholder="XSmall size" />
<Textarea size="sm" placeholder="Small size" />
<Textarea size="md" placeholder="Medium size" />
<Textarea size="lg" placeholder="Large size" />
<Textarea size="xl" placeholder="XLarge size" />
</Stack>
)
}
Helper Text
Pair the textarea with the Field
component to add helper text.
Max 500 characters.
Max 500 characters.
import { HStack, Textarea } from "@chakra-ui/react"
import { Field } from "@/components/ui/field"
const Demo = () => {
return (
<HStack gap="10" width="full">
<Field label="Comment" required helperText="Max 500 characters.">
<Textarea placeholder="Start typing..." variant="subtle" />
</Field>
<Field label="Comment" required helperText="Max 500 characters.">
<Textarea placeholder="Start typing..." variant="outline" />
</Field>
</HStack>
)
}
Error Text
Pair the textarea with the Field
component to add error text.
Field is required
Field is required
import { HStack, Textarea } from "@chakra-ui/react"
import { Field } from "@/components/ui/field"
const Demo = () => {
return (
<HStack gap="10" width="full">
<Field invalid label="Comment" required errorText="Field is required">
<Textarea placeholder="Start typing..." variant="subtle" />
</Field>
<Field invalid label="Comment" required errorText="Field is required">
<Textarea placeholder="Start typing..." variant="outline" />
</Field>
</HStack>
)
}
Field
Compose the textarea with the Field
component to add a label, helper text, and
error text.
import { HStack, Input } from "@chakra-ui/react"
import { Field } from "@/components/ui/field"
const Demo = () => {
return (
<HStack gap="10" width="full">
<Field label="Email" required>
<Input placeholder="me@example.com" variant="subtle" />
</Field>
<Field label="Email" required>
<Input placeholder="me@example.com" variant="outline" />
</Field>
</HStack>
)
}
Hook Form
Here's an example of how to integrate the textarea with react-hook-form
.
"use client"
import { Button, Input, Stack, Textarea } from "@chakra-ui/react"
import { Field } from "@/components/ui/field"
import { useForm } from "react-hook-form"
interface FormValues {
username: string
bio: string
}
const Demo = () => {
const {
register,
handleSubmit,
formState: { errors },
} = useForm<FormValues>()
const onSubmit = handleSubmit((data) => console.log(data))
return (
<form onSubmit={onSubmit}>
<Stack gap="4" align="flex-start" maxW="sm">
<Field
label="Username"
invalid={!!errors.username}
errorText={errors.username?.message}
>
<Input
placeholder="@username"
{...register("username", { required: "Username is required" })}
/>
</Field>
<Field
label="Profile bio"
invalid={!!errors.bio}
helperText="A short description of yourself"
errorText={errors.bio?.message}
>
<Textarea
placeholder="I am ..."
{...register("bio", { required: "Bio is required" })}
/>
</Field>
<Button type="submit">Submit</Button>
</Stack>
</form>
)
}
Resize
Use the resize
prop to control the resize behavior of the textarea.
import { Stack, Textarea } from "@chakra-ui/react"
const Demo = () => {
return (
<Stack gap="4" maxWidth="250px">
<Textarea resize="none" placeholder="Search the docs…" />
<Textarea resize="vertical" placeholder="Search the docs…" />
<Textarea resize="horizontal" placeholder="Search the docs…" />
<Textarea resize="both" placeholder="Search the docs…" />
</Stack>
)
}
Autoresize
Here's an example of how to integrate the react-textarea-autosize
package to
make the textarea autoresize.
"use client"
import { chakra, useRecipe } from "@chakra-ui/react"
import AutoResize from "react-textarea-autosize"
const StyledAutoResize = chakra(AutoResize)
const Demo = () => {
const recipe = useRecipe({ key: "textarea" })
const styles = recipe({ size: "sm" })
return (
<StyledAutoResize
placeholder="This textarea will autoresize as you type"
minH="initial"
resize="none"
overflow="hidden"
lineHeight="inherit"
css={styles}
/>
)
}
Props
Prop | Default | Type |
---|---|---|
colorPalette | 'gray' | 'gray' | 'red' | 'orange' | 'yellow' | 'green' | 'teal' | 'blue' | 'cyan' | 'purple' | 'pink' | 'accent' The color palette of the component |
size | 'md' | 'lg' | 'md' | 'sm' | 'xs' The size of the component |
variant | 'outline' | 'outline' | 'filled' | 'flushed' The variant of the component |