React + MotionEingabefelder

Floating-Label-Eingabefeld

Das Label sitzt im Feld und wandert an den Rand, sobald du hineinklickst oder zu tippen beginnst.

Einstellungen

48px

Code

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 = "E-Mail",
  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 für KI

Füg ihn in ChatGPT, Claude oder Cursor ein und der Baustein wird mit diesen Einstellungen an dein Projekt angepasst, auch ohne React.

Markdown-Version mit dem ganzen Code, für KI-Assistenten

Mehr aus Eingabefelder

Alle ansehen: Eingabefelder

Wofür du ihn nutzen kannst

Floating Labels sparen Platz, ohne dass der Kontext verloren geht: Der Hinweis bleibt nach dem Tippen sichtbar, anders als ein normaler Platzhalter. Sie passen zu Registrierungs- und Login-Formularen, zum Checkout eines Onlineshops oder zur Terminbuchung einer Zahnarztpraxis. Dieses Input mit Floating Label in React lässt Formulare fertiger wirken.

Halte Labels kurz, damit sie auch verkleinert passen: „E-Mail“ statt „Deine E-Mail-Adresse“. Stimm die Höhe auf deine Buttons ab und wähl eine Fokusfarbe, die sich klar vom Rand abhebt. Dann weiß man immer, wo man gerade tippt.