# TextInput
**📖 Live documentation:** https://cds.coinbase.com/components/inputs/TextInput/
A control for entering text.
## Import
```tsx
import { TextInput } from '@coinbase/cds-web/controls/TextInput'
```
## Examples
**Note** TextField extends props from [HTMLInputElement](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attributes) on web. On mobile, it extends [TextInputProps](https://reactnative.dev/docs/textinput#props) from react-native.
#### Input Label
Default composition of Inputs.
```jsx live
COPY
}
/>
Use the compact variant when space is tight.
```
#### Accessible Text Inputs
TextInput comes with an accessibilityLabel prop. If no accessibilityLabel is passed, it will use the label as the accessibilityLabel. If you want an accessibilityLabel that differs from the Label, you can set this prop.
Here, since no accessibilityLabel is passed, the accessibilityLabel will be "Email".
```jsx
```
Example of passing an accessibilityLabel. For web, this will set aria-label="Enter a Coinbase Email" under the hood
```jsx
```
:::tip Accessibility tip
Like any component system, much of the responsibility for building accessible UIs is in your hands as the consumer to properly implement the component composition. We'll do our best to provide sane fallbacks, but here are the biggest gotchas for `TextInput`s you can watch out for.
##### `aria-*` attr overrides
Any time you use `variant='negative'`, we assume you're showing an error state. If for some reason this is _not_ the case, you will want to use `aria-invalid={false}` to override the default configuration.
##### Message format
It's also advised you always format `helperText` with `Error: ${errorMessage}`. We'd do that for you, but _i18n_ isn't baked into CDS. Take a look at the example below:
:::
```jsx live
```
#### Placeholder Text
```jsx live
```
#### Borderless
For borderless TextInput usage, prefer adding a focus border with `focusedBorderWidth`.
If you need a fully borderless input (including focus), use that pattern with a TypeAhead
composition.
```jsx live
```
#### Helper Text
##### Default Sentiment
```jsx live
Default sentiment
Positive sentiment
}
/>
Negative Sentiment
}
/>
```
#### Color Surge Enabled
```jsx live
```
#### Content Alignment
```jsx live
Left aligned (default): Right aligned (with compact):
```
#### Label Variants
TextInput supports two label variants: `outside` (default) and `inside`. Note that the `compact` prop, when set to true, will override label variant preference.
:::warning
When using the `inside` label variant, you should always include a `placeholder` prop.
:::
```jsx live
Outside label (default):Inside label:Inside label (with start content):}
placeholder="Search for anything"
/>
Inside label (with end content):}
placeholder="Enter your password"
/>
```
#### Custom Label
You can pass a ReactNode to `labelNode` to render a custom label. If you want to include a tooltip, ensure the touch target is at least 24x24 for accessibility compliance.
```jsx live
Display name
{/* Add padding to ensure 24x24 tooltip tap target for a11y compliance */}
}
placeholder="Satoshi Nakamoto"
/>
Amount
*
}
placeholder="0.00"
suffix="USD"
/>
Bio
(optional)
}
placeholder="Tell us about yourself"
/>
Notes
}
placeholder="Add a note"
start={}
/>
```
#### StartContent & EndContent
##### Examples of Input Objects placed at the Start
```jsx live
function StartContentExamples() {
return (
Asset: Asset objects are not interactive
}
placeholder="HaeJiWplJohn6W42eCq0Qqft0"
/>
Icon: Icon objects are not interactive.
} placeholder="1234" />
IconButton: The most common use case for Icon Button at the start of a
Text Field is search.
}
placeholder="Search for anything"
/>
);
}
```
#### Read Only
TextInput supports a read-only state which is visually distinct from the disabled state. Read-only inputs have a secondary background color and can still be focused.
```jsx live
}
/>
```
### Example of Input Objects placed at the End
Here are some examples and best practices when using end content in a TextField.
```jsx live
Icon: Icon objects are not interactive.
}
/>
The most common use case for placing a text object at the end of an input is currency. This
object is not interactive.
You can add a Text Button object at the end of an Input. "Copy" is a great example of this.
COPY
}
/>
```
#### Password input
Password Input - Use Icon Buttons at the end for actions like showing a password or clearing text from an input.
> a11y tip: Always provide an `accessibilityLabel` to start/end nodes to clearly communicate state/actions
```jsx live
function PasswordInput() {
const [isVisible, setIsVisible] = useState(false);
const type = useMemo(() => (isVisible ? 'text' : 'password'), [isVisible]);
const iconName = useMemo(() => (isVisible ? 'visible' : 'invisible'), [isVisible]);
return (
setIsVisible((isVisible) => !isVisible)}
accessibilityLabel={isVisible ? 'Hide password' : 'Show password'}
/>
}
/>
);
}
```
#### Link + Icon Button
If needed, you can add a Link + Icon Button like this example here. Use this sparingly and only at the End of an Input.
```jsx live
function CopyTextField() {
const [copied, setCopied] = useState(false);
const [variant, setVariant] = useState('foregroundMuted');
const [helperText, setHelperText] = useState('');
useEffect(() => {
if (copied) {
setVariant('positive');
setHelperText('Your token has been copied!');
} else {
setVariant('foregroundMuted');
setHelperText('');
}
}, [copied]);
const handleOnChange = useCallback(() => {
setVariant('foregroundMuted');
setCopied(false);
setHelperText('');
}, []);
return (
setCopied(true)} variant="caption" color={variant}>
{copied ? 'copied' : 'copy'}
}
onChange={handleOnChange}
variant={variant}
helperText={helperText}
label="API Access Token"
/>
);
}
```
#### Disabled
```jsx live
```
#### Example of a Form
We recommend that you use spacing 3 when building stacked forms.
```jsx live
function FormExample() {
const gap = 3;
const onSubmit = useCallback((e) => {
e.preventDefault();
console.log(e.currentTarget.nodeValue);
alert('Submitted');
}, []);
return (
);
}
```
#### Example of a Sign Up Form
```jsx live
```
### Testing
#### Testing different parts of the input
You can also use the testIDMap to test different parts
of the TextInput. If you use testID, it will add the testID to the root
of the TextInput.
```jsx live
function testExample() {
const testIDMap = useMemo(() => {
return {
input: 'input-id',
helperText: 'helperText-id',
label: 'label-id',
start: 'start-id',
end: 'end-id',
};
}, []);
return (
}
end={}
/>
);
}
```
## Props
| Prop | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `align` | `center \| start \| end \| justify` | No | `start` | Text Align Input |
| `alignContent` | `ResponsiveProp
` | No | `-` | - |
| `as` | `input` | No | `-` | The underlying element or component the polymorphic component will render. Changing as also changes the inherited native props (e.g. href for as=a) and the expected ref type. |
| `aspectRatio` | `ResponsiveProp` | No | `-` | - |
| `background` | `ResponsiveProp` | No | `-` | - |
| `borderBottomLeftRadius` | `ResponsiveProp` | No | `-` | - |
| `borderBottomRightRadius` | `ResponsiveProp` | No | `-` | - |
| `borderBottomWidth` | `ResponsiveProp` | No | `-` | - |
| `borderColor` | `ResponsiveProp` | No | `-` | - |
| `borderEndWidth` | `ResponsiveProp` | No | `-` | - |
| `borderRadius` | `ResponsiveProp` | No | `200` | Leverage one of the borderRadius styles we offer to round the corners of the input. |
| `borderStartWidth` | `ResponsiveProp` | No | `-` | - |
| `borderTopLeftRadius` | `ResponsiveProp` | No | `-` | - |
| `borderTopRightRadius` | `ResponsiveProp` | No | `-` | - |
| `borderTopWidth` | `ResponsiveProp` | No | `-` | - |
| `borderWidth` | `ResponsiveProp` | No | `-` | - |
| `bordered` | `boolean` | No | `true` | Add a border around all sides of the box. Adds border to input. When set to false, focus border styling is disabled by default. |
| `borderedBottom` | `boolean` | No | `-` | Add a border to the bottom side of the box. |
| `borderedEnd` | `boolean` | No | `-` | Add a border to the trailing side of the box. |
| `borderedHorizontal` | `boolean` | No | `-` | Add a border to the leading and trailing sides of the box. |
| `borderedStart` | `boolean` | No | `-` | Add a border to the leading side of the box. |
| `borderedTop` | `boolean` | No | `-` | Add a border to the top side of the box. |
| `borderedVertical` | `boolean` | No | `-` | Add a border to the top and bottom sides of the box. |
| `bottom` | `ResponsiveProp>` | No | `-` | - |
| `color` | `ResponsiveProp` | No | `-` | - |
| `columnGap` | `ResponsiveProp` | No | `-` | - |
| `compact` | `boolean` | No | `false` | Enables compact variation |
| `containerSpacing` | `string` | No | `-` | Custom container spacing if needed. This will add to the existing spacing |
| `dangerouslySetBackground` | `string` | No | `-` | - |
| `disabled` | `boolean` | No | `false` | Toggles input interactability and opacity |
| `display` | `ResponsiveProp` | No | `-` | - |
| `elevation` | `ResponsiveProp` | No | `-` | - |
| `enableColorSurge` | `boolean` | No | `-` | Enable Color Surge motion |
| `end` | `ReactNode` | No | `-` | Adds content to the end of the inner input. Refer to diagram for location of endNode in InputStack component |
| `flexBasis` | `ResponsiveProp>` | No | `-` | - |
| `flexDirection` | `ResponsiveProp` | No | `-` | - |
| `flexGrow` | `ResponsiveProp` | No | `-` | - |
| `flexShrink` | `ResponsiveProp` | No | `-` | - |
| `flexWrap` | `ResponsiveProp` | No | `-` | - |
| `focusedBorderWidth` | `BorderWidth` | No | `-` | Additional border width when focused. Set this when bordered={false} to opt into a focus border style. |
| `font` | `ResponsiveProp` | No | `-` | - |
| `fontFamily` | `ResponsiveProp` | No | `-` | - |
| `fontSize` | `ResponsiveProp` | No | `-` | - |
| `fontWeight` | `ResponsiveProp` | No | `-` | - |
| `gap` | `ResponsiveProp` | No | `-` | - |
| `grid` | `ResponsiveProp` | No | `-` | - |
| `gridArea` | `ResponsiveProp` | No | `-` | - |
| `gridAutoColumns` | `ResponsiveProp>` | No | `-` | - |
| `gridAutoFlow` | `ResponsiveProp` | No | `-` | - |
| `gridAutoRows` | `ResponsiveProp>` | No | `-` | - |
| `gridColumn` | `ResponsiveProp` | No | `-` | - |
| `gridColumnEnd` | `ResponsiveProp` | No | `-` | - |
| `gridColumnStart` | `ResponsiveProp` | No | `-` | - |
| `gridRow` | `ResponsiveProp` | No | `-` | - |
| `gridRowEnd` | `ResponsiveProp` | No | `-` | - |
| `gridRowStart` | `ResponsiveProp` | No | `-` | - |
| `gridTemplate` | `ResponsiveProp` | No | `-` | - |
| `gridTemplateAreas` | `ResponsiveProp` | No | `-` | - |
| `gridTemplateColumns` | `ResponsiveProp>` | No | `-` | - |
| `gridTemplateRows` | `ResponsiveProp>` | No | `-` | - |
| `height` | `ResponsiveProp>` | No | `-` | Height of input |
| `helperText` | `ReactNode` | No | `-` | For cases where label is not enough information to describe what the text input is for. Can also be used for showing positive/negative messages |
| `helperTextErrorIconAccessibilityLabel` | `string` | No | `'error'` | Accessibility label for helper text error icon when variant=negative |
| `inputBackground` | `Color` | No | `'bg'` | Background color of the input. |
| `inputNode` | `ReactElement` | No | `-` | Customize the element which the input area will be rendered as. Adds ability to render the input area as a , etc... By default, the input area will be rendered as an . |
| `justifyContent` | `ResponsiveProp` | No | `-` | - |
| `key` | `Key \| null` | No | `-` | - |
| `label` | `string` | No | `-` | Short messageArea indicating purpose of input |
| `labelNode` | `ReactNode` | No | `-` | React node to render label. Takes precedence over label. |
| `labelVariant` | `inside \| outside` | No | `'outside'` | The variant of the label. Only used when compact is not true. |
| `left` | `ResponsiveProp>` | No | `-` | - |
| `lineHeight` | `ResponsiveProp` | No | `-` | - |
| `margin` | `ResponsiveProp<0 \| -5 \| -10 \| -0.25 \| -0.5 \| -0.75 \| -1 \| -1.5 \| -2 \| -3 \| -4 \| -6 \| -7 \| -8 \| -9>` | No | `-` | - |
| `marginBottom` | `ResponsiveProp<0 \| -5 \| -10 \| -0.25 \| -0.5 \| -0.75 \| -1 \| -1.5 \| -2 \| -3 \| -4 \| -6 \| -7 \| -8 \| -9>` | No | `-` | - |
| `marginEnd` | `ResponsiveProp<0 \| -5 \| -10 \| -0.25 \| -0.5 \| -0.75 \| -1 \| -1.5 \| -2 \| -3 \| -4 \| -6 \| -7 \| -8 \| -9>` | No | `-` | - |
| `marginStart` | `ResponsiveProp<0 \| -5 \| -10 \| -0.25 \| -0.5 \| -0.75 \| -1 \| -1.5 \| -2 \| -3 \| -4 \| -6 \| -7 \| -8 \| -9>` | No | `-` | - |
| `marginTop` | `ResponsiveProp<0 \| -5 \| -10 \| -0.25 \| -0.5 \| -0.75 \| -1 \| -1.5 \| -2 \| -3 \| -4 \| -6 \| -7 \| -8 \| -9>` | No | `-` | - |
| `marginX` | `ResponsiveProp<0 \| -5 \| -10 \| -0.25 \| -0.5 \| -0.75 \| -1 \| -1.5 \| -2 \| -3 \| -4 \| -6 \| -7 \| -8 \| -9>` | No | `-` | - |
| `marginY` | `ResponsiveProp<0 \| -5 \| -10 \| -0.25 \| -0.5 \| -0.75 \| -1 \| -1.5 \| -2 \| -3 \| -4 \| -6 \| -7 \| -8 \| -9>` | No | `-` | - |
| `maxHeight` | `ResponsiveProp>` | No | `-` | - |
| `maxWidth` | `ResponsiveProp>` | No | `-` | - |
| `minHeight` | `ResponsiveProp>` | No | `-` | - |
| `minWidth` | `ResponsiveProp>` | No | `-` | - |
| `onChange` | `FormEventHandler` | No | `-` | - |
| `opacity` | `ResponsiveProp` | No | `-` | - |
| `overflow` | `ResponsiveProp` | No | `-` | - |
| `padding` | `ResponsiveProp` | No | `-` | - |
| `paddingBottom` | `ResponsiveProp` | No | `-` | - |
| `paddingEnd` | `ResponsiveProp` | No | `-` | - |
| `paddingStart` | `ResponsiveProp` | No | `-` | - |
| `paddingTop` | `ResponsiveProp` | No | `-` | - |
| `paddingX` | `ResponsiveProp` | No | `-` | - |
| `paddingY` | `ResponsiveProp` | No | `-` | - |
| `pin` | `PinningDirection` | No | `-` | Direction in which to absolutely pin the box. |
| `placeholder` | `string` | No | `-` | Placeholder text displayed inside of the input. Will be replaced if there is a value. |
| `position` | `ResponsiveProp` | No | `-` | - |
| `ref` | `((instance: HTMLInputElement \| null) => void) \| RefObject \| null` | No | `-` | - |
| `right` | `ResponsiveProp>` | No | `-` | - |
| `rowGap` | `ResponsiveProp` | No | `-` | - |
| `start` | `ReactNode` | No | `-` | Adds content to the start of the inner input. Refer to diagram for location of startNode in InputStack component |
| `style` | `CSSProperties` | No | `-` | - |
| `suffix` | `string` | No | `-` | Adds suffix text to the end of input |
| `testID` | `string` | No | `-` | Used to locate this element in unit and end-to-end tests. Under the hood, testID translates to data-testid on Web. On Mobile, testID stays the same - testID |
| `testIDMap` | `{ start?: string; end?: string \| undefined; label?: string \| undefined; helperText?: string \| undefined; } \| undefined` | No | `-` | Add ability to test individual parts of the input |
| `textAlign` | `ResponsiveProp
` | No | `-` | - |
| `textDecoration` | `ResponsiveProp` | No | `-` | - |
| `textTransform` | `ResponsiveProp` | No | `-` | - |
| `top` | `ResponsiveProp>` | No | `-` | - |
| `transform` | `ResponsiveProp` | No | `-` | - |
| `type` | `button \| reset \| submit` | No | `-` | - |
| `userSelect` | `ResponsiveProp` | No | `-` | - |
| `value` | `string \| number \| readonly string[]` | No | `-` | - |
| `variant` | `InputVariant` | No | `foregroundMuted` | Determines the sentiment of the input. Because we allow startContent and endContent to be custom ReactNode, the content placed inside these slots will not change colors according to the variant. You will have to add that yourself |
| `visibility` | `ResponsiveProp` | No | `-` | - |
| `width` | `ResponsiveProp>` | No | `100%` | Width of input as a percentage string. |
| `zIndex` | `ResponsiveProp` | No | `-` | - |