Skip to content
English

How it works

The label is absolutely positioned inside the box and rises with translateY and scale(0.75) on focus or when filled, without changing font-size. Whether there is a value is left to CSS :placeholder-shown, so controlled, uncontrolled and autofilled inputs all float correctly with no JS state. Helper and error text share one row that expands with grid rows 0fr to 1fr. Clearing uses the native setter plus an input event, so onChange always fires.

Use it for

  • Sign-in, sign-up and similar short forms where space is tight: the label doubles as the placeholder.
  • Mobile web forms: the 56px field is easy to hit and 16px text avoids iOS zoom.
  • Account-style inputs that need consistent clear and show-password behavior.

Look elsewhere when

  • Long admin forms: shrunken labels are harder to scan, so use labels above the field.
  • Very long labels: they get truncated once floated, so use a label above.
  • Multi-line text: this component only handles a single-line input.

Usage

import { useState } from 'react'
import { FloatingLabelInput } from './FloatingLabelInput.jsx'

export function SignInFields() {
  const [id, setId] = useState('')
  const [pwd, setPwd] = useState('')
  return (
    <>
      <FloatingLabelInput
        label="Member ID"
        type="tel"
        clearable
        value={id}
        onChange={(e) => setId(e.target.value)}
        error={id && id.length !== 8 ? 'Enter your 8-digit member ID' : undefined}
      />
      <FloatingLabelInput
        label="Password"
        type="password"
        helperText="8–20 characters, with letters and numbers"
        value={pwd}
        onChange={(e) => setPwd(e.target.value)}
      />
    </>
  )
}

Props

PropTypeDefaultWhat it does
labelReactNode—Label (required), linked to the input through <label for>.
helperTextReactNode—Helper text under the box.
errorReactNode—Error: text replaces the helper text and turns everything red; true only turns it red.
variant'outline' | 'filled''outline'Look: white with a soft hairline ring, or filled light grey. Both turn white with an accent line and halo on focus.
clearablebooleanfalseShows a clear button when there is a value; Esc also clears. Not available when read-only or disabled.
onClear() => void—Called after clearing; onChange also receives the empty value.
revealablebooleantrue for type passwordWhether a password field has a show / hide toggle.
typestring'text'Native input type; password adds the reveal toggle automatically.
placeholderstring—The real placeholder text, shown only once the label has floated (on focus).
requiredboolean—Required: shows a red * after the label (hidden from screen readers, which read the native required).
labelsobject—Replaces built-in text (clear, showPassword, hidePassword). Shallow-merged over the English defaults.
refRef<HTMLInputElement>—Forwarded to the <input>, so it works with react-hook-form's register.
...restInputHTMLAttributes—className and style go on the outer element; other native attributes (value, onChange, name, autoComplete, inputMode…) go to the <input>.

Accessibility

  • <label for> ties the label to the input, so clicking it focuses the field; the required asterisk is aria-hidden and screen readers read the native required.
  • Errors set aria-invalid, and helper or error text is linked with aria-describedby inside an aria-live="polite" region, so new errors are announced.
  • The show-password button is focusable with aria-pressed and a name that switches between show and hide; the clear button is out of the Tab order and keyboard users press Esc (propagation is stopped only when there is a value to clear).
  • Buttons have a focus ring; a focused field shows an accent line and halo around the whole box.
  • With reduced motion the label jumps, the halo appears at once and messages do not slide; only color and opacity change.

Motion

Label rises in 280ms ease-out; on focus the fill turns white in 160ms and the accent line and 4px halo grow in over 220ms; messages slide in over 240ms; the clear button pops in over 240ms and leaves in 100ms; password icons cross-fade in 240ms; buttons press to 0.92.

Add it to your project

Copy the prompt into Codex or Claude Code. The agent reads the published source, shows you what it will write and adds the component next to your own.

FloatingLabelInput.jsxfloating-label-input.csstokens.cssREADME.md

Both files install together. The stylesheet reads the design tokens in tokens.css and carries fallbacks, so it also works alone.

Form inputs
Availability grid comes with Pro. Its preview shows once you unlock it.
Boolean operations comes with Pro. Its preview shows once you unlock it.
Cascader comes with Pro. Its preview shows once you unlock it.