# Floating label input

> The label sits inside the field and slides up to the border when you click or start typing.

- Category: [Inputs](https://library.aimran.es/components/inputs)
- Built with: React + Motion
- Live editor: https://library.aimran.es/blocks/floating-label-input
- Library: AIMRAN Library (https://library.aimran.es), free, no sign-up, by Aimran Oukham

## Where to use it

Floating labels save space without losing context: the hint stays visible after you type, unlike a plain placeholder. They look right in sign-up and login forms, the checkout of an online shop or the booking form of a dental clinic. This floating label input in React is a small detail that makes forms feel finished.

Keep labels short so they still fit once they shrink, “Email” rather than “Your email address”. Adjust the height to match your buttons, and pick a focus color that stands out from the border so people always know where they are typing.

## Code

### React (Motion)

```tsx
import { useState } from "react";
import { motion } from "framer-motion";

interface FloatingLabelInputProps {
  color?: string;
  size?: number;
  label?: string;
  type?: string;
  value?: string;
  defaultValue?: string;
  onChange?: (value: string) => void;
}

export default function FloatingLabelInput({
  color = "#0071e3",
  size = 48,
  label = "Email",
  type = "text",
  value,
  defaultValue = "",
  onChange,
}: FloatingLabelInputProps) {
  const [internal, setInternal] = useState(defaultValue);
  const [focused, setFocused] = useState(false);
  const current = value ?? internal;
  const up = focused || current.length > 0;

  return (
    <div className="relative w-64">
      <input
        type={type}
        aria-label={label}
        value={current}
        onChange={(e) => {
          if (value === undefined) setInternal(e.target.value);
          onChange?.(e.target.value);
        }}
        onFocus={() => setFocused(true)}
        onBlur={() => setFocused(false)}
        className="w-full rounded-[14px] border bg-white px-3 outline-none transition-colors dark:bg-[#1c1c1e]"
        style={{
          height: size,
          fontSize: size / 3.2,
          borderColor: focused ? color : "oklch(1 0 0 / 14%)",
          boxShadow: focused ? `0 0 0 3px ${color}22` : "none",
        }}
      />
      <motion.label
        className={`pointer-events-none absolute left-3 px-1 ${up ? "bg-white dark:bg-[#1c1c1e]" : "bg-transparent"}`}
        animate={{
          top: up ? -8 : size / 2 - size / 6.4,
          fontSize: up ? 11 : size / 3.2,
          color: up ? color : "oklch(0.62 0.015 260)",
        }}
        transition={{ duration: 0.16 }}
      >
        {label}
      </motion.label>
    </div>
  );
}
```

## Prompt for AI assistants

Paste this into ChatGPT, Claude, Cursor, v0 or Lovable to adapt the block to your stack (Tailwind, Vue, Svelte…):

````text
I want to add the “Floating label input” block (Inputs) from AIMRAN Library to my project.

## What it is
The label sits inside the field and slides up to the border when you click or start typing.

## Chosen settings
- Color: #0071e3
- Size: 48px (from 28 to 64px)

## What I need
1. Add it to my project with the same look and animation as the reference code: same sizes, colours, radii, shadows, timings and easing.
2. It is built with React + TypeScript, with framer-motion for the animation and lucide-react for the icons. If my project uses something else (Vue, Svelte, Astro, plain HTML…), translate it without losing any behaviour. If I use Tailwind, turn the styles into classes; if not, keep the CSS in its own file.
3. Expose the settings above as props (or CSS variables) with those values as defaults.
4. Make it work well on phones: nothing that depends only on hover, touch targets of at least 44 px and no overflow on 320 px screens.
5. Make it look right in light and dark mode.
6. Keep it accessible: semantic HTML, roles and aria-* where needed, visible focus and keyboard support.
7. Respect prefers-reduced-motion: with reduced motion, no big animations but the same end result.

## Reference code
```tsx
import { useState } from "react";
import { motion } from "framer-motion";

interface FloatingLabelInputProps {
  color?: string;
  size?: number;
  label?: string;
  type?: string;
  value?: string;
  defaultValue?: string;
  onChange?: (value: string) => void;
}

export default function FloatingLabelInput({
  color = "#0071e3",
  size = 48,
  label = "Email",
  type = "text",
  value,
  defaultValue = "",
  onChange,
}: FloatingLabelInputProps) {
  const [internal, setInternal] = useState(defaultValue);
  const [focused, setFocused] = useState(false);
  const current = value ?? internal;
  const up = focused || current.length > 0;

  return (
    <div className="relative w-64">
      <input
        type={type}
        aria-label={label}
        value={current}
        onChange={(e) => {
          if (value === undefined) setInternal(e.target.value);
          onChange?.(e.target.value);
        }}
        onFocus={() => setFocused(true)}
        onBlur={() => setFocused(false)}
        className="w-full rounded-[14px] border bg-white px-3 outline-none transition-colors dark:bg-[#1c1c1e]"
        style={{
          height: size,
          fontSize: size / 3.2,
          borderColor: focused ? color : "oklch(1 0 0 / 14%)",
          boxShadow: focused ? `0 0 0 3px ${color}22` : "none",
        }}
      />
      <motion.label
        className={`pointer-events-none absolute left-3 px-1 ${up ? "bg-white dark:bg-[#1c1c1e]" : "bg-transparent"}`}
        animate={{
          top: up ? -8 : size / 2 - size / 6.4,
          fontSize: up ? 11 : size / 3.2,
          color: up ? color : "oklch(0.62 0.015 260)",
        }}
        transition={{ duration: 0.16 }}
      >
        {label}
      </motion.label>
    </div>
  );
}
```

Give me the complete final code, ready to paste, and tell me in one line where to put it in my project.
````
