Loading component…
Loading component…
One input for every form
To reuse this local React demo, copy all files from the Code tab into the same folder, then import TextField from ./text-field.
import { useState } from "react";
import { TextField } from "@/components/ui/text-field";
export function WorkspaceForm() {
const [slug, setSlug] = useState("");
const valid = /^[a-z0-9-]*$/.test(slug);
return (
<form className="grid gap-4">
<TextField label="Workspace name" name="name" maxLength={32} counter clearable required />
<TextField
label="Workspace URL"
prefix="acme.app/"
value={slug}
onChange={(e) => setSlug(e.target.value)}
hint="Lowercase letters, numbers and dashes."
error={valid ? undefined : "Only lowercase letters, numbers and dashes."}
/>
<TextField label="Password" type="password" name="password" autoComplete="new-password" />
<TextField label="API key" value={apiKey} readOnly copyable />
</form>
);
}| Name | Type | Default | Description |
|---|---|---|---|
| label | ReactNode | Shown above the field; clicking it focuses the input | |
| hint | ReactNode | A quiet line under the field – the error takes its place while there is one | |
| error | ReactNode | Marks the field invalid (red edge and a faint red fill, aria-invalid) and shows the message where the hint was | |
| prefix | ReactNode | Text or a 16px icon inside the field before the value – e.g. "https://" or a search icon | |
| suffix | ReactNode | Text or an icon after the value – e.g. ".acme.app", "USD" or "%" | |
| size | "sm" \| "md" \| "lg" | "md" | 32 · 36 · 40px high; taller on touch screens |
| optional | boolean | false | Adds "Optional" next to the label |
| clearable | boolean | false | A small × (and Esc) that empties the field while it has a value; onChange fires as if the user had deleted the text |
| copyable | boolean | false | A copy button that turns into a green check – pair it with readOnly for keys, links and IDs |
| counter | boolean | false | Shows "12/40" under the field, red once a prefilled value is over the limit; needs maxLength |
| type | string | "text" | Any input type – "password" adds a show/hide button, "search" drops the browser's own × |
| onClear | () => void | Called after the × emptied the field | |
| inputClassName | string | Extra class on the input itself, e.g. a monospace font | |
| className | string | Extra class on the wrapper | |
| ...input props | ComponentProps<"input"> | Everything else – value, defaultValue, onChange, name, placeholder, required, disabled, readOnly, maxLength, autoComplete, ref – goes to the input |
Fills its container; long values, prefixes and messages never push it wider – a long prefix is cut at its start, so the part next to the value stays
On touch screens the field grows to a comfortable tap height, its buttons to 32px with a larger hit area, and it uses 16px text, so iOS doesn't zoom in on focus
Dark mode built in: switches when an ancestor (usually <html>) has the class dark or data-theme="dark"
@import "tailwindcss"; in your main CSS file.