import {Textarea} from '@primer/react'
Textarea components must always be accompanied by a corresponding label to improve support for assistive technologies. Examples below are provided for conciseness and may not reflect accessibility best practices.
Use the FormControl component to render a Textarea with a corresponding label.
const TextareaExample = () => {// Running in controlled mode (recommended)const [value, setValue] = React.useState('')const handleChange = event => {setValue(event.target.value)}return <Textarea placeholder="Enter a description" onChange={handleChange} value={value} />}render(<TextareaExample />)
const TextareaExample = () => {const ref = React.useRef()const handleSubmit = event => {event.preventDefault()if (!ref.current.value) {alert(`Enter a value into the Textarea and press submit`)return}alert(`Current Textarea value: ${ref.current.value}`)}return (<form onSubmit={handleSubmit}><Textareacols={40}rows={8}ref={ref}defaultValue="Set the initial state in uncontrolled mode using the defaultValue prop"/><br /><Button type="submit">Submit</Button></form>)}render(<TextareaExample />)
<><Box as="label" htmlFor="success-state" sx={{display: 'block'}}>Success state:</Box><Textarea id="success-state" validationStatus="success" /><Box as="label" htmlFor="error-state" sx={{display: 'block', mt: 5}}>Error state:</Box><Textarea id="error-state" validationStatus="error" /></>
<><Textarea disabled>This text is inactive</Textarea></>
By default, Textarea
can be resized by the user vertically and horizontally. Resizing can be prevented by setting resize
to none
<Textarea cols={40} resize="none" />
<><Textarea sx={{marginBottom: 2}} /><p>Custom styles like `margin` and `padding` can be applied using the `sx` prop</p></>
Name | Type | Default | Description |
---|---|---|---|
required | boolean | Indicates to the user and assistive technologies that the field value is required | |
cols | number | Specifies the visible width of a text area. | |
rows | number | Specifies the visible height of a text area. | |
block | boolean | false | Expands with width of the component to fill the parent elements |
resize | 'both' | 'horizontal' | 'vertical' | 'none' | 'both' | Changes background color to a higher contrast color |
validationStatus | 'success' | 'error' | undefined | Style the textarea to match the current form validation status | |
ref | React.RefObject<HTMLTextAreaElement> | A ref to the element rendered by this component. | |
sx | SystemStyleObject | Style overrides to apply to the component. See also overriding styles. | |
ref | React.RefObject<HTMLTextareaElement> | A ref to the element rendered by this component. | |
Additional props are passed to the <input> element. See MDN for a list of props accepted by the <input> element. |