# 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.

- Kind: Component (Forms)
- Docs: https://ui.minidev.pro/docs/floating-label-input
- Source (registry JSON with the full file): https://ui.minidev.pro/r/floating-label-input.json
- License: MIT. One file, yours to edit after install.

## 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`](https://ui.minidev.pro/docs/input) inside [`FormField`](https://ui.minidev.pro/docs/form-field); for passwords use [`PasswordInput`](https://ui.minidev.pro/docs/password-input); for formatted values use [`MaskedInput`](https://ui.minidev.pro/docs/masked-input).

## Installation

### shadcn CLI (copies the source into your project)

```bash
npx shadcn@latest add @minidev/floating-label-input
```

Writes `components/ui/floating-label-input.tsx` plus the MiniDev files it is built from (button) and installs its npm dependencies. Older CLI versions: `npx shadcn@latest add https://ui.minidev.pro/r/floating-label-input.json`.

### npm package

```bash
npm i minidev-ui-kit lucide-react motion
```

Add the tokens to your global stylesheet once:

```css
@import "tailwindcss";
@import "minidev-ui-kit/styles.css";
@source "../node_modules/minidev-ui-kit";
```

Next.js also needs `transpilePackages: ["minidev-ui-kit"]` in next.config.

## Import

After the shadcn install:

```tsx
import { FloatingLabelInput } from "@/components/ui/floating-label-input"
```

From the npm package:

```tsx
import { FloatingLabelInput } from "minidev-ui-kit/ui/floating-label-input"
```

## Usage

```tsx
import { FloatingLabelInput } from "@/components/ui/floating-label-input"

export function Example() {
  return (
    <FloatingLabelInput />
  )
}
```

## Props

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| `label` | `string` | - | no | The field label. When omitted, the `fields` form is rendered instead. |
| `multiline` | `boolean` | `false` | no | Render a textarea instead of an input. |
| `rows` | `number` | `4` | no | Textarea rows when `multiline`. |
| `type` | `string` | `"text"` | no | Input type, e.g. "email", "tel", "url". Ignored when `multiline`. |
| `value` | `string` | - | no | Controlled text. |
| `defaultValue` | `string` | `""` | no | Initial text when uncontrolled. |
| `onChange` | `(value: string) => void` | - | no | Called with the new text on every keystroke. |
| `onBlur` | `() => void` | - | no | Called when the field loses focus, e.g. React Hook Form's `field.onBlur` to mark it touched. |
| `icon` | `React.ReactNode` | - | no | Icon shown at the start of the field (inputs only). |
| `hint` | `React.ReactNode` | - | no | Helper text under the field, linked with aria-describedby. |
| `error` | `React.ReactNode` | - | no | Error message. Marks the field invalid and replaces the hint. |
| `disabled` | `boolean` | `false` | no |  |
| `required` | `boolean` | `false` | no |  |
| `id` | `string` | - | no |  |
| `name` | `string` | - | no |  |
| `autoComplete` | `string` | - | no |  |
| `fields` | `FloatingLabelField[]` | `FIELDS` | no | Fields of the sample form rendered when no `label` is passed. |
| `onSubmit` | `(values: Record<string, string>) => void` | - | no | Called with the form values when the sample form is submitted and valid. |
| `className` | `string` | - | no |  |

## Examples

### Single field

Pass label to render one field; the label floats as soon as there is a value or focus.

```tsx
<FloatingLabelInput label="Company" defaultValue="Northwind Traders" hint="Shown on every invoice you send." />
```

### Controlled with validation

Control the value, add a leading icon and pass error to replace the hint and mark the field invalid.

```tsx
function ControlledDemo() {
  const [email, setEmail] = React.useState("maya@northwind")
  const valid = /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)
  return (
    <FloatingLabelInput
      label="Work email"
      type="email"
      icon={<MailIcon />}
      value={email}
      onChange={setEmail}
      error={email && !valid ? "That email looks incomplete." : undefined}
      hint="We send the Lumen invite here."
      autoComplete="email"
      required
    />
  )
}
```

### Multiline

multiline renders a textarea with the same floating label; rows sets the starting height.

```tsx
<FloatingLabelInput label="Notes for finance" multiline rows={3} hint="Optional, a sentence or two is plenty." />
```

### Disabled and error

A disabled field keeps its value readable; error shows the message with an icon and sets aria-invalid.

```tsx
<FloatingLabelInput label="Plan" defaultValue="Business, annual" disabled hint="Change plans from Billing." />
<FloatingLabelInput label="Invoice number" defaultValue="INV-20" error="Invoice numbers have four digits." required />
```

## Accessibility

- The label is a real `<label htmlFor>`. It moves with a spring transform only; under reduced motion `useReducedMotion` sets the duration to 0, so it jumps into place.
- `error` sets `aria-invalid`. The hint or error line is linked with `aria-describedby` and is `aria-live="polite"`, so a new error is announced.
- The `required` asterisk is `aria-hidden`. `required` also sets the native attribute, which screen readers announce.

## FAQ

### 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.

## Rules for generating code

- Use semantic tokens only (bg, surface, sunken, border, fg, fg-muted, fg-subtle, ink, accent, success, warning, danger). Never raw hex or palette steps like blue-500.
- Shadows only from the set: shadow-xs, shadow-sm, shadow-raised, shadow-md, shadow-lg, shadow-overlay.
- Numbers use tabular-nums. Dark mode is the `dark` class on <html>; components never branch on theme.

## Dependencies

- npm: `lucide-react`, `motion`
- Built from: [button](https://ui.minidev.pro/docs/button/md)

## Related

- [Input](https://ui.minidev.pro/docs/input/md): The base text input with sm, default and lg sizes and designed hover, focus, disabled, read only and invalid states. A drop in shadcn style input for React.
- [Form Field](https://ui.minidev.pro/docs/form-field/md): A wrapper that pairs any input with its label, required marker, help text and an error message announced with role alert. Keeps React forms consistent.
- [Password Input](https://ui.minidev.pro/docs/password-input/md): A password field with a show and hide toggle inside the input, an eye icon that swaps state and an accessible label. Supports sm, default and lg sizes in React.
- [Masked Input](https://ui.minidev.pro/docs/masked-input/md): React masked input that formats phones, dates, plates and IBANs as you type, keeps the caret steady on edits and pastes, and returns masked and raw values.
- [Autocomplete](https://ui.minidev.pro/docs/autocomplete/md): Text input that filters string options as you type, using ARIA combobox and listbox roles. Works controlled or uncontrolled and lists up to eight matches.
- [Calendar Month](https://ui.minidev.pro/docs/calendar-month/md): Month calendar grid with weekday headers and clickable day cells that highlight the selected date. A simple Tailwind base for date views and schedulers.

---

All 557 components: https://ui.minidev.pro/llms.txt (full text: https://ui.minidev.pro/llms-full.txt).
