Skip to content
WANCRAFT

F.22Forms & Inputs

Floating Label Input

A premium text field whose label floats on focus, with hint, validation and autofill support.

ReactMotionCSSTailwindintermediate

Overview

Floating Label Input keeps a real <label htmlFor> inside the field and floats it up when the input is focused, filled or autofilled. Floating is pure CSS (:placeholder-shown, :focus, :autofill), so server-rendered values and password managers are handled without JavaScript. Errors set aria-invalid, join the aria-describedby chain with the hint, and animate into a persistent live region. Three variants — outline, filled and underline — share the same API, and the ref and every native input prop are forwarded.

01Installation

Method 01 · Registry

CLI verified

Uses the shadcn CLI in a project initialised with shadcn init. Installs the file to components/ui/floating-label-input.tsx and adds its dependencies.

npx shadcn@latest add https://wancraft.my/r/floating-label-input.json

Method 02 · Copy and paste

  1. 1. Install the dependencies below.
  2. 2. Copy the source from the Code tab into components/ui/floating-label-input.tsx.
  3. 3. Import it and adjust props.
npm install motion
Dependencies
motion
Peer
react ≥ 18, tailwindcss ≥ 4
Added
1 Oct 2026
Updated
—

02Usage

The default configuration. Change properties above to generate a tailored example in the Usage tab.

example.tsx
import { FloatingLabelInput } from "@/components/ui/floating-label-input";

export function FloatingLabelInputExample() {
  return (
    <FloatingLabelInput
      label="Email address"
      type="email"
      name="email"
      autoComplete="email"
      required
    />
  );
}

03Props

PropTypeDefaultDescription
labelReactNode—Required. Visible label linked with <label htmlFor>.
hintReactNode—Helper text, linked via aria-describedby.
errorReactNode—Truthy marks the field invalid (aria-invalid). A node/string is shown and announced; `true` only flags.
variant"outline" | "filled" | "underline""outline"Visual style.
accentstring"#FF7448"Focus border, ring and underline colour.
inputClassNamestring—Extra classes for the <input>; className targets the wrapper.
...propsInputHTMLAttributes—All native input props (type, name, value, onChange, autoComplete, required…). Ref goes to the <input>.

04Features

  • 01CSS-only floating: works with SSR values, autofill and controlled or uncontrolled inputs
  • 02Custom placeholders appear only after the label floats
  • 03Animated error message inside a persistent live region
  • 04Outline, filled and underline variants; underline draws from the centre on focus
  • 05Forwards ref and every native input attribute
motion · tweeninput · focusinput · keyboard

05Accessibility

  • Real <label htmlFor> — the label is the accessible name and is clickable.
  • aria-invalid when in error; aria-describedby chains hint and error ids (plus any you pass).
  • Error text is announced politely via a live region; the required asterisk is aria-hidden (use the native required attribute).
  • Visible focus with a border change and ring; label colour changes are paired with position, not colour alone.
  • prefers-reduced-motion removes the float and error transitions.