TextInput
TextInput is used to set a value that is a single line of text.
Page navigation navigation
Use a TextInput to allow users to input short free-form text. It may also be used as a number input when type="number" is passed to the component.
React examples
Default
Required
Custom size
Adjust input size based on context; use smaller sizes in compact spaces, larger sizes in open spaces or feels out of proportion with adjacent large elements.
Monospace value
Use a monospace font for code-related inputs, like configuration tokens.
In a loading state
When loading, the spinner replaces a visual without shifting layout. Control which side displays the loading indicator as needed.
The TextInput component has internal logic to decide which visual to replace, but it can be overridden and explicitly set to show at the start or end of the input.
With a caption
With a validation message
With a leading visual
Leading and trailing visuals, typically icons, clarify input purpose. For context, prefer icons but use text (e.g., “$” or “%”) if helpful.
Avoid overwhelming the TextInput by using both a leading and trailing visual. If you're trying to provide more context about the input, consider using text in the input caption.
With a trailing visual
With a trailing action
Use a trailing action button (e.g., clear button) without a label when possible. Avoid using it alongside a trailing visual.
More code examples
See the TextInput Storybook stories
Props
TextInput
| Name | Default | Description |
|---|---|---|
aria-label | stringGives the input an accessible name. | |
autoComplete | stringAllows input to autofill based on value given. | |
block | false | booleanCreates a full-width input element |
contrast | false | booleanChanges background color to a higher contrast color |
size | 'small' | 'medium' | 'large'Creates a smaller or larger input than the default. | |
loading | booleanWhether to show a loading indicator in the input | |
loaderPosition | 'auto' | 'leading' | 'trailing'Which position to render the loading indicator
| |
loaderText | Loading | stringText for screen readers to convey the loading state, should be descriptive and explain what is loading. This prop should only be used if there is visible context explaining what is loading, to ensure that context is provided to all users. |
leadingVisual | string | React.ComponentTypeVisual positioned on the left edge inside the input | |
monospace | false | booleanShows input in monospace font |
trailingVisual | string | React.ComponentTypeVisual positioned on the right edge inside the input | |
trailingAction | React.ReactElement<HTMLProps<HTMLButtonElement>>A visual that renders inside the input after the typing area | |
validationStatus | 'error' | 'success'Style the input to match the status | |
variant Deprecated | 'small' | 'medium' | 'large'(Use size) Creates a smaller or larger input than the default. | |
width Deprecated | string | number | Array<string | number>(Use sx prop) Set the width of the input | |
maxWidth Deprecated | string | number | Array<string | number>(Use sx prop) Set the maximum width of the input | |
minWidth Deprecated | string | number | Array<string | number>(Use sx prop) Set the minimum width of the input | |
icon Deprecated | React.ComponentType(Use leadingVisual or trailingVisual) An Octicon React component. To be used inside of input. Positioned on the left edge. | |
sx Deprecated | SystemStyleObject | |
ref | React.RefObject<HTMLInputElement> |
TextInput.Action
| Name | Default | Description |
|---|---|---|
aria-label | stringText that appears in a tooltip. If an icon is passed, this is also used as the label used by assistive technologies. | |
tooltipDirection | s | 'n' | 'ne' | 'e' | 'se' | 's' | 'sw' | 'w' | 'nw'Sets where the tooltip renders in relation to the target. |
icon | React.FunctionComponentThe icon to render inside the button | |
variant Deprecated | 'default' | 'primary' | 'invisible' | 'danger'(Deprecated so that only the 'invisible' variant is used) Determine's the styles on a button | |
ref | React.RefObject<HTMLButtonElement> | |
as | "button" | React.ElementType |
sx Deprecated | SystemStyleObject |