GooseUI

Components

Floating Label Input

New

An input and a textarea whose label rests inside the field and moves up into the frame on focus or once there is a value. The state comes from CSS alone (:placeholder-shown and peer), so it also works with browser autofill and without client JavaScript.

Browser Support

:placeholder-shown

Tracks whether the field is empty

Your browser:
:has()

Styles elements based on their contents

Your browser:
:focus-within

Highlights the focused field frame

Your browser:

Preview

Focus a field or start typing. The email field shows its error after you leave it.

We only use it for receipts

A few lines are enough

Installation

npx shadcn@latest add @gooseui/floating-label-input
pnpm dlx shadcn@latest add @gooseui/floating-label-input
yarn dlx shadcn@latest add @gooseui/floating-label-input
bunx --bun shadcn@latest add @gooseui/floating-label-input

Usage

import {
  FloatingLabelInput,
  FloatingLabelTextarea,
} from "@/components/ui/floating-label-input"

export default function Example() {
  return (
    <form className="grid gap-4">
      <FloatingLabelInput label="Email" type="email" name="email" />
      <FloatingLabelTextarea label="Message" name="message" />
    </form>
  )
}

Examples

Two variants

outline puts the floated label on the border, inset keeps it at the top inside the frame.

<FloatingLabelInput label="City" />
<FloatingLabelInput label="City" variant="inset" />

Prefix and suffix

Slots take icons, currency signs, units or a button. A password visibility toggle is a regular button in the suffix.

$
USD
import { Button } from "@/components/ui/button"

<FloatingLabelInput label="Search the docs" prefix={<Search />} />
<FloatingLabelInput label="Budget" prefix="$" suffix="USD" inputMode="decimal" />
<FloatingLabelInput
  label="Password"
  type={visible ? "text" : "password"}
  suffix={
    <Button type="button" onClick={() => setVisible(!visible)} aria-label="Show password">
      {visible ? <EyeOff /> : <Eye />}
    </Button>
  }
/>

Hint and error

hint and error render under the field and are linked to it through aria-describedby. An error replaces the hint and sets aria-invalid. Clear the field to see the hint again.

This username is taken

kg
<FloatingLabelInput
  variant="inset"
  label="Username"
  prefix={<AtSign />}
  value={username}
  onChange={(e) => setUsername(e.target.value)}
  hint="Letters, digits and dots"
  error={taken ? "This username is taken" : undefined}
/>

Props

PropTypeDefaultDescription
labelReactNode-Label text. Also the accessible name of the field
hintReactNode-Helper text under the field
errorReactNode-Error text. Marks the field invalid and replaces the hint
prefixReactNode-Content before the value
suffixReactNode-Content after the value
variant"outline" | "inset""outline"Where the floated label goes
placeholderstring" "Shown only on focus. A single space keeps :placeholder-shown working when there is none
classNamestring-Classes for the outer wrapper
frameClassNamestring-Classes for the bordered frame
...propsComponentProps<"input" | "textarea">-Any input or textarea attribute, including ref

Components

ComponentDescription
FloatingLabelInputSingle-line field
FloatingLabelTextareaMultiline field with the same props, rows defaults to 4

Features

  • CSS only: no focus or value state in JavaScript; works in Server Components
  • Autofill: the :autofill selector floats the label when the browser fills the field
  • Accessibility: a real label element, aria-describedby for hint and error, aria-invalid on error
  • Reduced motion: the label moves without a transition