Components / Inputs
Floating Label Input
A text field whose label floats up as you type, with an animated error shake.
Installation
Requires React 18+ and Tailwind CSS.
- 1
Install dependencies
$ npm install motion - 2
Add the component
Copy the code from the Code tab above into
components/ui/floating-input.tsx. - 3
Use it
example.tsx"use client"; import { useState } from "react"; import { FloatingInput } from "@/components/ui/floating-input"; export default function FloatingInputDemo() { const [email, setEmail] = useState(""); const [error, setError] = useState<string>(); return ( <form className="flex w-full max-w-xs flex-col gap-3" noValidate onSubmit={(e) => { e.preventDefault(); setError(/^\S+@\S+\.\S+$/.test(email) ? undefined : "Please enter a valid email address."); }} > <FloatingInput label="Full name" autoComplete="name" /> <FloatingInput label="Email address" type="email" value={email} onChange={(e) => setEmail(e.target.value)} error={error} /> <button type="submit" className="h-11 rounded-xl bg-zinc-900 text-sm font-semibold text-white transition hover:bg-zinc-700 dark:bg-white dark:text-zinc-900 dark:hover:bg-zinc-200" > Subscribe </button> </form> ); }
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| label | string | — | Field label. |
| error | string | — | Error message; shows below with a shake. |
| ...props | ComponentProps<"input"> | — | Any native input prop (value, onChange, type…). |