Color Schemes for Textarea
are not implemented in the default theme. You can extend the theme to implement them.
Textarea
The Textarea component allows you to easily create multi-line text inputs.
Import#
import { Textarea } from '@chakra-ui/react'
Usage#
<Textarea placeholder='Here is a sample placeholder' />
Controlled Textarea#
function Example() {let [value, setValue] = React.useState('')let handleInputChange = (e) => {let inputValue = e.target.valuesetValue(inputValue)}return (<><Text mb='8px'>Value: {value}</Text><Textareavalue={value}onChange={handleInputChange}placeholder='Here is a sample placeholder'size='sm'/></>)}
Resize behavior#
You can add resize
prop to change the Textarea
resize behavior.
function ResizeExample() {const [resize, setResize] = React.useState('horizontal')return (<><RadioGroup defaultValue={resize} onChange={setResize} mb={6}><Stack direction='row' spacing={5}><Radio value='horizontal'>Horizontal</Radio><Radio value='vertical'>Vertical</Radio><Radio value='none'>None</Radio></Stack></RadioGroup><Textareaplaceholder='Here is a sample placeholder'size='sm'resize={resize}/></>)}
Disabled Textarea#
<Textarea isDisabled placeholder='Here is a sample placeholder' />
Invalid Textarea#
<Textarea isInvalid placeholder='Here is a sample placeholder' />
Props#
The Textarea composes the Input component.
colorScheme
colorScheme
string
errorBorderColor
errorBorderColor
The border color when the textarea is invalid. Use color keys in `theme.colors` @example errorBorderColor = "red.500"
string
focusBorderColor
focusBorderColor
The border color when the textarea is focused. Use color keys in `theme.colors` @example focusBorderColor = "blue.500"
string
isDisabled
isDisabled
If true
, the form control will be disabled. This has 2 side effects:
- The FormLabel
will have `data-disabled` attribute
- The form element (e.g, Input) will be disabled
boolean
isInvalid
isInvalid
If true
, the form control will be invalid. This has 2 side effects:
- The FormLabel
and FormErrorIcon
will have `data-invalid` set to true
- The form element (e.g, Input) will have `aria-invalid` set to true
boolean
isReadOnly
isReadOnly
If true
, the form control will be readonly
boolean
isRequired
isRequired
If true
, the form control will be required. This has 2 side effects:
- The FormLabel
will show a required indicator
- The form element (e.g, Input) will have `aria-required` set to true
boolean
size
size
"xs" | "sm" | "md" | "lg"
"md"
variant
variant
"outline" | "flushed" | "filled" | "unstyled"
"outline"
The Textarea
component is a single part component. All of the styling is applied directly to the textarea element.
To learn more about styling single part components, visit the Component Style page.
Theming properties#
The properties that affect the theming of the Textarea
component are:
variant
: The visual variant of the textarea. Defaults tooutline
.size
: The size of the textarea. Defaults tomd
.
Theming utilities#
defineStyle
: a function used to create style objects.defineStyleConfig
: a function used to define the style configuration for a single part component.
import { defineStyle, defineStyleConfig } from '@chakra-ui/react'
Customizing the default theme#
import { defineStyle, defineStyleConfig } from '@chakra-ui/react'const outline = defineStyle({border: '2px dashed', // change the appearance of the borderborderRadius: 0, // remove the border radiusfontWeight: 'semibold', // change the font weight})export const textareaTheme = defineStyleConfig({variants: { outline },})
After the customization of the textarea theme, we can import it in our theme file and add
it in the components
property:
import { extendTheme } from '@chakra-ui/react'import { textareaTheme } from './components/textarea'export const theme = extendTheme({components: { Textarea: textareaTheme },})
Adding a custom size#
Let's assume you want to add a custom size of your textarea. Here's how we can do that:
import { defineStyle, defineStyleConfig } from '@chakra-ui/react'const xl = defineStyle({fontSize: 'xl',px: '6',h: '16',borderRadius: 'md',})export const textareaTheme = defineStyleConfig({sizes: { xl },})// Now we can use the new `xl` size<Textarea size="xl">...</Textarea>
Every time you're adding anything new to the theme, you'd need to run the CLI command to get proper autocomplete in your IDE. You can learn more about the CLI tool here.
Adding a custom variant#
Let's assume we want to include a custom branded variant. Here's how we can do that:
import { defineStyle, defineStyleConfig } from '@chakra-ui/react'const brandPrimary = defineStyle({background: 'orange.500',color: 'white',fontFamily: 'serif',fontWeight: 'normal',// let's also provide dark mode alternatives_dark: {background: 'orange.300',color: 'orange.800',}})export const textareaTheme = defineStyleConfig({variants: { brandPrimary },})// Now we can use the new `brandPrimary` variant<Textarea variant="brandPrimary">...</Textarea>
Changing the default properties#
Let's assume we want to change the default size or variant of every textarea in our app. Here's how we can do that:
import { defineStyleConfig } from '@chakra-ui/react'export const textareaTheme = defineStyleConfig({defaultProps: {size: 'lg',variant: 'outline',colorScheme: 'brand',},})// This saves you time, instead of manually setting the size and// variant every time you use the textarea component:<Textarea size="lg" variant="outline">...</Textarea>
Showcase#
import { Box, SimpleGrid, IconButton, Textarea, useColorMode } from "@chakra-ui/react"; import { FaMoon, FaSun } from "react-icons/fa"; export default function App() { const { toggleColorMode, colorMode } = useColorMode(); return ( <Box position="relative" h="100vh"> <SimpleGrid gap={12} p={12} columns={2}> <Textarea variant="flushed" placeholder="Flushed variant" /> <Textarea variant="filled" placeholder="Filled variant" /> <Textarea variant="outline" placeholder="Outlined variant" /> <Textarea variant="brandPrimary" placeholder="Custom variant" /> </SimpleGrid> <IconButton aria-label="toggle theme" rounded="full" size="xs" position="absolute" bottom={4} left={4} onClick={toggleColorMode} icon={colorMode === "dark" ? <FaSun /> : <FaMoon />} /> </Box> ); }