Floating Label Input
React floating label input and textarea whose label springs onto the top border on focus or fill, with helper text, an error state and a leading icon.
Installation
npx shadcn@latest add @minidev/floating-label-input
MiniDev UI is in the shadcn registry directory, so the short form works with no setup. Older CLI versions can use the full URL: npx shadcn@latest add https://ui.minidev.pro/r/floating-label-input.json
Usage
import { FloatingLabelInput } from "@/components/ui/floating-label-input"
export function Example() {
return (
<FloatingLabelInput />
)
}
API reference
| Prop | Type | Default |
|---|---|---|
| labelThe field label. When omitted, the `fields` form is rendered instead. | string | - |
| multilineRender a textarea instead of an input. | boolean | false |
| rowsTextarea rows when `multiline`. | number | 4 |
| typeInput type, e.g. "email", "tel", "url". Ignored when `multiline`. | string | "text" |
| valueControlled text. | string | - |
| defaultValueInitial text when uncontrolled. | string | "" |
| onChangeCalled with the new text on every keystroke. | (value: string) => void | - |
| onBlurCalled when the field loses focus, e.g. React Hook Form's `field.onBlur` to mark it touched. | () => void | - |
| iconIcon shown at the start of the field (inputs only). | React.ReactNode | - |
| hintHelper text under the field, linked with aria-describedby. | React.ReactNode | - |
| errorError message. Marks the field invalid and replaces the hint. | React.ReactNode | - |
| disabled | boolean | false |
| required | boolean | false |
| id | string | - |
| name | string | - |
| autoComplete | string | - |
| fieldsFields of the sample form rendered when no `label` is passed. | FloatingLabelField[] | FIELDS |
| onSubmitCalled with the form values when the sample form is submitted and valid. | (values: Record<string, string>) => void | - |
| className | string | - |
When to use
Pass label for one field (input, or textarea with multiline). Without label it renders a small sign-up form built from fields. The label is a real <label>; error sets aria-invalid and is announced. For a classic label-above field use Input inside FormField; for passwords use PasswordInput; for formatted values use MaskedInput.
Accessibility
- The label is a real
<label htmlFor>. It moves with a spring transform only; under reduced motionuseReducedMotionsets the duration to 0, so it jumps into place. errorsetsaria-invalid. The hint or error line is linked witharia-describedbyand isaria-live="polite", so a new error is announced.- The
requiredasterisk isaria-hidden.requiredalso sets the native attribute, which screen readers announce.
Frequently asked questions
Why do I get a whole form instead of one field?
Without label, FloatingLabelInput renders a sample sign-up form built from fields. Pass label and you get one input, or a textarea with multiline.
Does the label move out of the way when the browser autofills?
Yes. Autofill does not fire onChange, so the field checks :autofill 400ms after mount and on animationstart, and floats the label when it matches.
How do I use it with React Hook Form?
Use a Controller, because it does not forward a ref and register() needs one. Pass value={field.value}, onChange={field.onChange}, onBlur={field.onBlur} and error={errors.email?.message}. onBlur lets touched and blur-mode validation work.
Dependencies
npm
Built from
Examples
Single field
Pass label to render one field; the label floats as soon as there is a value or focus.
Controlled with validation
Control the value, add a leading icon and pass error to replace the hint and mark the field invalid.
Multiline
multiline renders a textarea with the same floating label; rows sets the starting height.
Disabled and error
A disabled field keeps its value readable; error shows the message with an icon and sets aria-invalid.