Text Field
Inputs for words, numbers and codes, each with its label, hint and error handled.
Standard
Label, input and one message row for hints and errors. Errors shake once; confirmed values get a check.
Try “admin” to see an error.
Installation
$npx shadcn@latest add @stillui/text-fieldOr copy the source into your project. It needs the shadcn color tokens and the cn helper.
"use client";
// stillui.dev/components/text-field
import { AnimatePresence, motion, useAnimationControls, useReducedMotion } from "motion/react";
import { forwardRef, useEffect, useId, type ComponentProps, type ReactNode } from "react";
import { cn } from "@/lib/utils";
const REVEAL = { duration: 0.18, ease: [0.16, 1, 0.3, 1] } as const;
type TextFieldProps = Omit<ComponentProps<"input">, "size"> & {
label: ReactNode;
/** Help text under the field. Replaced by the error while one is shown. */
hint?: ReactNode;
/** Error message. Setting it marks the field invalid and gives it a short shake. */
error?: ReactNode;
/** Marks the value as confirmed (e.g. a username that is available). */
valid?: boolean;
};
export const TextField = forwardRef<HTMLInputElement, TextFieldProps>(function TextField(
{ label, hint, error, valid, id, className, disabled, ...props },
ref,
) {
const reduce = useReducedMotion();
const controls = useAnimationControls();
const autoId = useId();
const inputId = id ?? autoId;
const messageId = `${inputId}-message`;
const invalid = Boolean(error);
const message = error ?? hint;
// Shake once when an error appears. Reduced motion keeps the color change only.
useEffect(() => {
if (invalid && !reduce) controls.start({ x: [0, -5, 5, -3, 3, 0], transition: { duration: 0.32 } });
}, [invalid, error, reduce, controls]);
return (
<div className={cn("grid gap-1.5", disabled && "opacity-50", className)}>
<label htmlFor={inputId} className="text-sm font-medium">
{label}
</label>
<motion.div animate={controls} className="relative">
<input
ref={ref}
id={inputId}
disabled={disabled}
aria-invalid={invalid || undefined}
aria-describedby={message ? messageId : undefined}
className={cn(
"h-10 w-full rounded-md bg-card px-3 text-sm shadow-[inset_0_0_0_1px_var(--input)]",
"placeholder:text-faint-foreground transition-shadow duration-150 ease-out",
"focus-visible:outline-none focus-visible:shadow-[inset_0_0_0_1px_var(--ring),0_0_0_3px_oklch(from_var(--ring)_l_c_h/0.2)]",
"aria-invalid:shadow-[inset_0_0_0_1px_var(--destructive)] aria-invalid:focus-visible:shadow-[inset_0_0_0_1px_var(--destructive),0_0_0_3px_oklch(from_var(--destructive)_l_c_h/0.18)]",
"disabled:cursor-not-allowed",
valid && !invalid && "pr-9",
)}
{...props}
/>
<AnimatePresence>
{valid && !invalid ? (
<motion.svg
key="valid"
viewBox="0 0 16 16"
fill="none"
stroke="currentColor"
strokeWidth="1.75"
strokeLinecap="round"
strokeLinejoin="round"
className="pointer-events-none absolute right-3 top-1/2 size-4 -translate-y-1/2 text-success"
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
transition={REVEAL}
aria-hidden
>
<motion.path
d="M3.5 8.5l3 3 6-7"
initial={{ pathLength: reduce ? 1 : 0 }}
animate={{ pathLength: 1 }}
transition={{ duration: 0.24, ease: [0.16, 1, 0.3, 1], delay: 0.04 }}
/>
</motion.svg>
) : null}
</AnimatePresence>
</motion.div>
{/* Fixed-height row so the layout below never jumps when a message appears. */}
<div className="min-h-5">
<AnimatePresence mode="wait" initial={false}>
{message ? (
<motion.p
key={invalid ? "error" : "hint"}
id={messageId}
role={invalid ? "alert" : undefined}
className={cn("text-[13px] leading-5", invalid ? "text-destructive" : "text-muted-foreground")}
initial={{ opacity: 0, y: reduce ? 0 : -3 }}
animate={{ opacity: 1, y: 0 }}
exit={{ opacity: 0, transition: { duration: 0.1 } }}
transition={REVEAL}
>
{message}
</motion.p>
) : null}
</AnimatePresence>
</div>
</div>
);
});States
- Empty
- Placeholder uses the faint text role and never replaces the label.
- Focus
- A soft ring appears around the field; no layout change.
- Invalid
- Red edge, one short shake, error replaces the hint and is announced.
- Valid
- A check draws in at the right edge.
- Disabled
- Label and field dim together.
Notes
The message row keeps its height, so errors never push the rest of the form down.
The label sits closer to its field than to the field above, so each pair reads as one unit.
Floating label
The label sits inside the field, then floats up when you focus or type. Compact forms without losing the label.
Installation
$npx shadcn@latest add @stillui/text-field-floatingOr copy the source into your project. It needs the shadcn color tokens and the cn helper.
"use client";
// stillui.dev/components/text-field#floating
import { forwardRef, useId, type ComponentProps } from "react";
import { cn } from "@/lib/utils";
type TextFieldFloatingProps = Omit<ComponentProps<"input">, "placeholder" | "size"> & {
label: string;
hint?: string;
};
/**
* The label sits inside the field like a placeholder, then floats up when you type.
* Pure CSS: an empty placeholder lets the label know when the field is empty.
*/
export const TextFieldFloating = forwardRef<HTMLInputElement, TextFieldFloatingProps>(function TextFieldFloating(
{ label, hint, id, className, ...props },
ref,
) {
const autoId = useId();
const inputId = id ?? autoId;
return (
<div className={cn("grid gap-1.5", className)}>
<div className="relative">
<input
ref={ref}
id={inputId}
placeholder=" "
aria-describedby={hint ? `${inputId}-hint` : undefined}
className={cn(
"peer h-14 w-full rounded-lg bg-card px-3.5 pb-2 pt-6 text-[15px] shadow-[inset_0_0_0_1px_var(--input)] transition-shadow duration-150",
"focus-visible:outline-none focus-visible:shadow-[inset_0_0_0_1px_var(--ring),0_0_0_3px_oklch(from_var(--ring)_l_c_h/0.2)]",
)}
{...props}
/>
<label
htmlFor={inputId}
className={cn(
"pointer-events-none absolute left-3.5 top-2 origin-left text-[12px] font-medium text-muted-foreground",
"transition-[transform,top,font-size,color] duration-200 ease-[cubic-bezier(0.16,1,0.3,1)] motion-reduce:transition-none",
// Empty and unfocused: the label sits where text would go.
"peer-[:placeholder-shown:not(:focus)]:top-1/2 peer-[:placeholder-shown:not(:focus)]:-translate-y-1/2 peer-[:placeholder-shown:not(:focus)]:text-[15px] peer-[:placeholder-shown:not(:focus)]:font-normal",
"peer-focus:text-foreground",
)}
>
{label}
</label>
</div>
{hint ? (
<p id={`${inputId}-hint`} className="px-0.5 text-[13px] leading-5 text-muted-foreground">
{hint}
</p>
) : null}
</div>
);
});States
- Empty
- The label fills the field like a placeholder, at full text size.
- Focused
- The label shrinks to the top-left and darkens; a soft ring surrounds the field.
- Filled
- The label stays small at the top so the value and its name are both visible.
- Hint
- Optional help text under the field, linked with aria-describedby.
- Reduced motion
- The label moves without a transition.
Notes
No JavaScript: it relies on the :placeholder-shown selector, so autofilled values float the label too.
Unlike a placeholder, the label never disappears, which matters for people who return to a form later.
Password
A password field with a show button and, for new passwords, a strength meter that says exactly what’s missing.
- 12 or more characters(missing)
- Upper and lower case(missing)
- A number(missing)
- A symbol(missing)
Installation
$npx shadcn@latest add @stillui/text-field-passwordOr copy the source into your project. It needs the shadcn color tokens and the cn helper.
"use client";
// stillui.dev/components/text-field#password
import { motion, useReducedMotion } from "motion/react";
import { useId, useState } from "react";
import { cn } from "@/lib/utils";
const FILL = { type: "spring", stiffness: 260, damping: 30 } as const;
const rules = [
{ test: (v: string) => v.length >= 12, text: "12 or more characters" },
{ test: (v: string) => /[a-z]/.test(v) && /[A-Z]/.test(v), text: "Upper and lower case" },
{ test: (v: string) => /\d/.test(v), text: "A number" },
{ test: (v: string) => /[^A-Za-z0-9]/.test(v), text: "A symbol" },
];
const levels = [
{ label: "Too short", color: "bg-destructive" },
{ label: "Weak", color: "bg-destructive" },
{ label: "Fair", color: "bg-[oklch(72%_0.15_70)]" },
{ label: "Good", color: "bg-success" },
{ label: "Strong", color: "bg-success" },
];
type TextFieldPasswordProps = {
label?: string;
value?: string;
onValueChange?: (value: string) => void;
/** Show the strength meter and checklist, for new passwords. */
meter?: boolean;
className?: string;
};
/** A password field with a show button and, for new passwords, a strength meter that says what’s missing. */
export function TextFieldPassword({ label = "Password", value, onValueChange, meter = true, className }: TextFieldPasswordProps) {
const reduce = useReducedMotion();
const id = useId();
const [internal, setInternal] = useState("");
const [visible, setVisible] = useState(false);
const current = value ?? internal;
const passed = rules.filter((rule) => rule.test(current)).length;
const level = current.length === 0 ? null : levels[current.length < 8 ? 0 : passed];
const change = (next: string) => {
if (value === undefined) setInternal(next);
onValueChange?.(next);
};
return (
<div className={cn("grid gap-1.5", className)}>
<label htmlFor={id} className="text-sm font-medium">
{label}
</label>
<div className="relative">
<input
id={id}
type={visible ? "text" : "password"}
value={current}
onChange={(e) => change(e.target.value)}
autoComplete={meter ? "new-password" : "current-password"}
aria-describedby={meter ? `${id}-strength` : undefined}
className={cn(
"h-10 w-full rounded-md bg-card pl-3 pr-10 text-sm shadow-[inset_0_0_0_1px_var(--input)] transition-shadow duration-150",
"focus-visible:outline-none focus-visible:shadow-[inset_0_0_0_1px_var(--ring),0_0_0_3px_oklch(from_var(--ring)_l_c_h/0.2)]",
)}
/>
<button
type="button"
onClick={() => setVisible((v) => !v)}
aria-label={visible ? "Hide password" : "Show password"}
aria-pressed={visible}
className="absolute right-1 top-1/2 grid size-8 -translate-y-1/2 place-items-center rounded-md text-muted-foreground transition-[color,background-color,box-shadow,scale] active:scale-[0.9] hover:bg-secondary hover:text-foreground"
>
<svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" className="size-4" aria-hidden>
<path d="M1.75 8S4 3.75 8 3.75 14.25 8 14.25 8 12 12.25 8 12.25 1.75 8 1.75 8z" />
<circle cx="8" cy="8" r="2" />
<motion.path d="M2.5 2.5l11 11" initial={false} animate={{ pathLength: visible ? 1 : 0, opacity: visible ? 1 : 0 }} transition={{ duration: reduce ? 0 : 0.2 }} />
</svg>
</button>
</div>
{meter ? (
<div id={`${id}-strength`} className="grid gap-2 pt-1">
<div className="flex items-center gap-3">
<div className="h-1 flex-1 overflow-hidden rounded-full bg-secondary">
<motion.div
className={cn("h-full rounded-full transition-colors duration-300", level?.color ?? "bg-transparent")}
initial={false}
animate={{ width: level ? `${Math.max(passed, 1) * 25}%` : "0%" }}
transition={reduce ? { duration: 0 } : FILL}
/>
</div>
<span className="w-16 text-right text-[12px] font-medium text-muted-foreground" aria-live="polite">
{level?.label ?? ""}
</span>
</div>
<ul className="grid grid-cols-2 gap-x-4 gap-y-1">
{rules.map((rule) => {
const ok = rule.test(current);
return (
<li key={rule.text} className={cn("flex items-center gap-1.5 text-[12px] transition-colors duration-200", ok ? "text-foreground" : "text-muted-foreground")}>
<svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.75" strokeLinecap="round" strokeLinejoin="round" className={cn("size-3.5 shrink-0", ok ? "text-success" : "text-faint-foreground")} aria-hidden>
{ok ? <path d="M3.5 8.5l3 3 6-7" /> : <circle cx="8" cy="8" r="2" fill="currentColor" stroke="none" />}
</svg>
{rule.text}
<span className="sr-only">{ok ? "(done)" : "(missing)"}</span>
</li>
);
})}
</ul>
</div>
) : null}
</div>
);
}States
- Hidden / shown
- The eye button toggles the text; a slash draws across it while the password is visible.
- Strength
- A bar fills in quarters and changes from red to amber to green, with a word beside it.
- Checklist
- Each rule turns from a dot into a green check as it is met.
- Sign-in mode
- Pass meter={false} for existing passwords; it also switches autocomplete to current-password.
- Screen readers
- The strength word is announced politely; each rule says done or missing.
- Reduced motion
- The bar and slash change without animating.
Notes
This meter only checks the shape of a password. Check it against breached-password lists on the server too.
Code
A one-time code in separate boxes, backed by a single real input so typing, pasting and autofill all just work.
Enter the 6-digit code we sent you.
Installation
$npx shadcn@latest add @stillui-pro/text-field-codeA paid component. The command needs the token from your account in STILLUI_TOKEN; your account page shows the one-time setup.
This is a paid component. Its source and install come with access.
States
- Empty
- Boxes with a hint underneath saying how many digits to expect.
- Typing
- The focused box gets the ring and a blinking caret; each digit rises into place.
- Paste and autofill
- Pasting a full code or tapping the phone’s suggestion fills every box at once.
- Complete
- onComplete fires as soon as the last digit is in, so there’s no submit button to find.
- Error
- All boxes outline in red and the message is announced.
- Reduced motion
- Digits appear without rising and the caret doesn’t blink.
Notes
Only digits are accepted. Letters and spaces from a pasted message are dropped.
Search
A rounded search field with a clear button, Escape to clear, and a shortcut key to jump to it from anywhere.
- Amara Okafor
- Bruno Costa
- Chen Wei
- Dana Levi
- Elif Şahin
- Farah Haddad
Installation
$npx shadcn@latest add @stillui/text-field-searchOr copy the source into your project. It needs the shadcn color tokens and the cn helper.
"use client";
// stillui.dev/components/text-field#search
import { AnimatePresence, motion, useReducedMotion } from "motion/react";
import { useEffect, useRef, useState } from "react";
import { cn } from "@/lib/utils";
type TextFieldSearchProps = {
label?: string;
placeholder?: string;
value?: string;
onValueChange?: (value: string) => void;
/** A key that focuses the field from anywhere on the page. */
shortcut?: string;
className?: string;
};
/** A search field with a clear button, Escape to clear, and a shortcut key to jump to it. */
export function TextFieldSearch({ label = "Search", placeholder = "Search", value, onValueChange, shortcut = "/", className }: TextFieldSearchProps) {
const reduce = useReducedMotion();
const ref = useRef<HTMLInputElement>(null);
const [internal, setInternal] = useState("");
const [focused, setFocused] = useState(false);
const current = value ?? internal;
const change = (next: string) => {
if (value === undefined) setInternal(next);
onValueChange?.(next);
};
// The shortcut is ignored while someone is typing in another field.
useEffect(() => {
const onKey = (event: KeyboardEvent) => {
const target = event.target as HTMLElement;
if (event.key !== shortcut || target.isContentEditable || ["INPUT", "TEXTAREA", "SELECT"].includes(target.tagName)) return;
event.preventDefault();
ref.current?.focus();
};
window.addEventListener("keydown", onKey);
return () => window.removeEventListener("keydown", onKey);
}, [shortcut]);
const pop = { initial: { opacity: 0, scale: reduce ? 1 : 0.8 }, animate: { opacity: 1, scale: 1 }, exit: { opacity: 0, scale: reduce ? 1 : 0.8 } };
return (
<div role="search" className={cn("relative", className)}>
<svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" className="pointer-events-none absolute left-3 top-1/2 size-4 -translate-y-1/2 text-muted-foreground" aria-hidden>
<circle cx="7" cy="7" r="4.25" />
<path d="M10.25 10.25L13.5 13.5" />
</svg>
<input
ref={ref}
type="search"
aria-label={label}
placeholder={placeholder}
value={current}
onChange={(e) => change(e.target.value)}
onFocus={() => setFocused(true)}
onBlur={() => setFocused(false)}
onKeyDown={(e) => {
if (e.key === "Escape" && current) {
e.preventDefault();
change("");
}
}}
className={cn(
"h-10 w-full rounded-full bg-muted pl-9 pr-10 text-sm transition-[background-color,box-shadow] duration-150 placeholder:text-muted-foreground [&::-webkit-search-cancel-button]:appearance-none",
"focus-visible:bg-card focus-visible:outline-none focus-visible:shadow-[inset_0_0_0_1px_var(--ring),0_0_0_3px_oklch(from_var(--ring)_l_c_h/0.2)]",
)}
/>
<div className="absolute right-1.5 top-1/2 -translate-y-1/2">
<AnimatePresence initial={false} mode="popLayout">
{current ? (
<motion.button
key="clear"
type="button"
onClick={() => {
change("");
ref.current?.focus();
}}
aria-label="Clear search"
className="grid size-7 place-items-center rounded-full text-muted-foreground transition-[color,background-color,box-shadow,scale] active:scale-[0.9] hover:bg-secondary hover:text-foreground"
transition={{ duration: 0.12 }}
{...pop}
>
<svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" className="size-3.5" aria-hidden>
<path d="M4.5 4.5l7 7m0-7l-7 7" />
</svg>
</motion.button>
) : !focused ? (
<motion.kbd
key="hint"
aria-hidden
className="mr-1 grid h-6 min-w-6 place-items-center rounded-md bg-card px-1.5 font-mono text-[12px] text-muted-foreground shadow-raised"
transition={{ duration: 0.12 }}
{...pop}
>
{shortcut}
</motion.kbd>
) : null}
</AnimatePresence>
</div>
</div>
);
}States
- Resting
- A soft muted field with a search icon and the shortcut key shown on the right.
- Focused
- The field lifts to the card color with a ring; the shortcut hint steps aside.
- Has text
- A clear button pops in. Escape also clears, without leaving the field.
- Shortcut
- Pressing / anywhere focuses it, unless someone is typing in another field.
- Screen readers
- Wrapped in a search landmark, with a label even though there is no visible one.