Text field
Labelled text entry on a soft filled surface: a primary ring and halo on focus, inset or floating labels, icons, clear with undo, a rolling counter and messages that rise in.
npx @swipeui/cli add text-fieldWhen to use it
- Names, emails, usernames, addresses and any single line of text in a form.
- Fields that need a helper line, an error and a success message in the same place, such as checking a username is free.
- Inputs with a leading icon, a prefix or suffix, or a clear button that can undo itself.
- Multi-field forms where Return should move to the next field, and compact forms with inset or floating labels.
When not to use it
- Several lines of text. Textarea grows with its content.
- Passwords. Password field adds a reveal action and password manager hints.
- Search. Search field adds a search glyph, Cancel and a Search key.
- Numbers with bounds. Number field adds steppers; Money input handles currency exactly.
- Picking from fixed options. Use Select, Combobox or Radio group.
Installation
Run this from your app's root. If SwipeUI isn't set up yet, run npx @swipeui/cli init first; it sets up Uniwind, the theme tokens and the cn helper.
npx @swipeui/cli add text-field- Adds
components/swipeui/ui/text-field.tsxand 12 shared helpers. - Installs react-native-reanimated, uniwind, react-native-worklets, clsx, tailwind-merge, expo-haptics, class-variance-authority, react-native-svg. In Expo apps it uses
npx expo installso versions match your SDK.
Usage
import { TextField } from '@/components/swipeui/ui/text-field';
<TextField
label="Email"
placeholder="name@company.com"
autoComplete="email"
keyboardType="email-address"
clearButton="while-editing"
/>;Examples
A floating label with an icon and a counter that appears near the limit:
<TextField
label="Display name"
labelPlacement="floating"
leadingIcon={({ color, size }) => <UserIcon color={color} size={size} />}
counter="near-limit"
maxLength={30}
/>Checking a username as it's typed. The spinner shows while validating, a check draws on when valid, and a new error shakes the field once with an error haptic:
<TextField
label="Username"
value={name}
onValueChange={setName}
status={checking ? 'validating' : available ? 'valid' : 'idle'}
success={available ? 'Username available' : undefined}
error={taken ? 'That username is taken' : undefined}
/>Icons take a node, or a function given { color, size }. The colour is muted at rest, primary while focused, red while invalid and muted while disabled.
API reference
| Prop | Type | Default | Description |
|---|---|---|---|
label? | | — | Visible label, or inherit an enclosing Field. |
helper? | | — | Instructions under the field; an error or success message replaces it. |
error? | | — | Validation message: a destructive ring, an alert glyph, one shake and an error haptic. |
success? | | — | Confirmation under the field, such as "Username available", in the success colour. |
status? | | |
|
required? | | | Required; "Required" is spoken, and |
requiredIndicator? | | | How a required label is marked: a muted asterisk, the word, or nothing. |
optionalIndicator? | | | Show a muted "Optional" after the label. |
disabled? | | | Disable editing and accessory actions. |
disabledStyle? | | |
|
appearance? | | | The surface: |
size? | | | Height: |
shape? | | |
|
labelPlacement? | | |
|
focusRing? | | | The focus indicator: a primary ring with a soft halo, the ring alone, or none. |
messageMorph? | | | How a changed message arrives: rises in, letter by letter, or at once. |
reserveMessageSpace? | | | Keep one line of room under the field so forms don't move as messages appear. |
value? | | — | Controlled text. |
defaultValue? | | | Initial uncontrolled text. |
onValueChange? | | — | Reports changed text, including clearing and undo. |
onChangeText? | | — | Native-compatible text callback, in addition to onValueChange. |
displayValue? | | — | Decorative rendering for an unfocused value; the native text remains the accessible/editable source. |
prefix? | | — | Decorative prefix or an accessible accessory, drawn first. |
suffix? | | — | Accessible trailing accessory, drawn last. |
leadingIcon? | | — | A leading icon: a node, or a function given the state colour and size. It turns primary while focused. |
trailingIcon? | | — | A trailing icon: a node, or a function given the state colour and size. |
clearable? | | | 1.x switch: |
clearButton? | | | When a "Clear text" button shows. |
undoClear? | | | After clearing, the button becomes "Undo clear" for a moment. Phones have no ⌘Z. |
undoClearWindow? | | | How long undo stays, in milliseconds. |
counter? | | | A rolling character counter under the field, using |
tintCaret? | | | Caret and selection in the primary colour. |
iconTint? | | | The leading icon turns primary while focused ( |
shakeOnError? | | | Shake once for each new error. Reduce Motion keeps it still. |
messages? | | | Built-in strings, for translation. |
nextRef? | | — | Ref focused when Return is pressed; otherwise a single-line field dismisses. |
haptics? | | | All feedback, or each part: |
motion? | | | Ring, label and message motion. |
className? | | — | Classes on the outer field. |
controlClassName? | | — | Classes on the field surface. |
inputClassName? | | — | Classes on the editable text. |
style? | | — | Outer field style. |
inputStyle? | | — | Native editable text style. |
Native TextInput props such as autoComplete, keyboardType and maxLength pass through, and the ref is the native input.
Composition and behavior
- appearance picks the surface.
filledis a soft fill that reads on backgrounds and cards in every preset;outlineis a hairline;underlinegrows a bar from the centre on focus;ghosthas no fill until focused. Corners follow your preset's control radius; shape="pill" makes a capsule. - Focusing shows a primary ring and a soft halo just outside it, drawn as a border so it looks the same on iOS, Android and the web. Leave 4 points around the field so a clipping parent doesn't cut the halo.
- labelPlacement
insetdraws a small label inside the field.floatingrests the label on the field's centre, like a hint, and raises it on focus or once there's a value, so autofill raises it too. - clearButton shows a Clear text button
while-editing,unless-editingoralways. After a clear, it becomes Undo clear for 4 seconds (undoClear, undoClearWindow); phones have no ⌘Z. - counter rolls a count under the field using
maxLength. It turns amber at 90% and red at the limit, plays a warning haptic there, and announces when 10% remains. - Messages under the field rise in while the row's height springs. An error replaces the helper and success text, rings the field in red and shakes it once.
- Return moves to nextRef; otherwise a single-line field closes the keyboard. On an enclosing Field, the look, label and messages come from the Field.
- For the 1.x look, pass
appearance="outline" disabledStyle="dim". Every 1.x prop still works.
Text field settings
| Prop | Does | Default |
|---|---|---|
| appearance | filled, outline, underline or ghost | filled |
| size / shape | sm, md, lg; preset or pill | md, preset |
| labelPlacement | above, inset, floating or hidden | above |
| leadingIcon / trailingIcon | A node, or a function given { color, size } | none |
| iconTint | The leading icon turns primary while focused | focus |
| clearButton | never, while-editing, unless-editing or always | never |
| undoClear / undoClearWindow | Undo after clearing, and for how long (ms) | true, 4000 |
| counter | true, near-limit (from 80%) or false | false |
| status | idle, validating, valid or invalid | idle |
| helper / error / success | Messages under the field | none |
| required / requiredIndicator | Required, marked by asterisk, word or none | false, asterisk |
| optionalIndicator | "Optional" after the label | false |
| focusRing | halo, ring or none | halo |
| shakeOnError | One shake per new error | true |
| tintCaret | Caret and selection in the primary colour | true |
| messageMorph | fade, characters or none | fade |
| reserveMessageSpace | Keep a line for messages so forms don't move | false |
| disabledStyle | muted (no opacity) or dim (50%) | muted |
| haptics | All, or { clear, error, valid, limit, focus } | clear, error, limit |
| messages | Built-in strings, for translation | English |
| motion | A local motion feel or override | the app's feel |
Behavior and gestures
- Tapping the field focuses it: a primary ring fades in with a soft halo, and the leading icon turns primary (iconTint).
- appearance switches the surface: filled (default), outline, underline (a bar grows from the centre) or ghost (no fill until focused).
- labelPlacement inset draws a small label inside the field; floating rests the label on the text line and raises it as you focus or once there's a value, including autofill.
- clearButton shows Clear text never, while editing, unless editing or always (clearable is the 1.x switch for always). After clearing, Undo clear restores the text for undoClearWindow milliseconds (undoClear).
- counter rolls a count under the field using maxLength: always, or from 80% with near-limit. It turns amber at 90% and red at the limit, with a warning haptic.
- status validating shows a spinner; valid draws a check on. success shows a green message; error shows a red one, rings the field in red, shakes it once (shakeOnError) and plays an error haptic.
- Return moves focus to nextRef if you pass one; otherwise a single-line field closes the keyboard.
- haptics switches feedback for clear, error, valid, limit and focus separately; false turns all of it off.
Accessibility
- The visible label is the field's name, linked to the input; inset and floating labels are drawn inside but spoken only through the input.
- Helper, error and success text are read as the field's hint; new errors are announced without moving focus. "Required" is spoken; the asterisk isn't.
- Clear text and Undo clear are separate 44pt buttons, and clearing announces that undo is available.
- Validating reports busy; reaching valid is announced once unless a success message already says so.
- The counter is hidden from screen readers; the field announces when 10% remains and when the limit is reached.
- Text grows with Dynamic Type; fields use minimum heights only, and RTL mirrors icons, accessories and the floating label's origin.
Motion
- Uses the
toggle,morph,layout,number,pressandambientmotion roles, so it follows your app's feel. Passmotionto tune this one instance. - The ring and halo fade in on the toggle role; the halo settles from 98% to full size.
- The floating label travels and shrinks on the morph role; messages rise in on morph and the row height springs on layout.
- Counter digits roll on the number role; the validating spinner turns on the ambient role.
- Accessories press in on the press role, and a new error shakes the field once on its timing.
- Reduce Motion: the ring cross-fades, the label moves at once and fades in place, the shake and halo scale go, and the spinner holds still.
Performance
- A native TextInput underneath; the ring, halo, label and message row animate in their own leaves on the UI thread.
- Works controlled with value and onValueChange, or uncontrolled with defaultValue.
Notes for AI
- Always pass label (or accessibilityLabel when the label is hidden); use helper for instructions, error for validation and success for confirmation.
- Chain fields with nextRef for faster form entry.
- Pick one appearance per form; set it once on each field or on an enclosing Field.
- Use clearButton="while-editing" for search-like fields and keep undoClear on.
- Translate built-in strings with messages.
Compatibility
| Runs on | Supported |
|---|---|
| Expo SDK 57 | Yes |
| Expo SDK 58 | Yes |
| React Native CLI (with Expo modules) | Yes |
| Expo Go | Yes |
| iOS | Yes |
| Android | Yes |
This table describes API support and static bundle checks. Native runtime, device accessibility and bare Uniwind compatibility remain pending.