{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "floating-label-input",
  "title": "Floating label input",
  "description": "Input and textarea whose label springs up on focus or value, with prefix/suffix slots that fade in, an animated clear button, character counter and a slide-in error.",
  "dependencies": [
    "lucide-react",
    "motion"
  ],
  "registryDependencies": [
    "https://goodui.dev/r/goodui-motion.json",
    "https://goodui.dev/r/locale.json"
  ],
  "files": [
    {
      "path": "registry/components/inputs/floating-label-input.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\nimport { AnimatePresence, motion, useReducedMotion } from \"motion/react\"\nimport { AlertCircle, X } from \"lucide-react\"\nimport { cn } from \"@/lib/utils\"\nimport { useLabels } from \"@/lib/goodui-locale\"\nimport { GooduiTokens, spring, ease as easing } from \"@/lib/goodui-motion\"\n\n// Chrome/Safari autofill fills inputs without a change event; this no-op animation tells us it happened.\nconst CSS = `\n@keyframes pc-floating-label-input-autofill { from { opacity: 1 } to { opacity: 1 } }\n.pc-floating-label-input-field:-webkit-autofill { animation: pc-floating-label-input-autofill 1ms }\n.pc-floating-label-input-field::placeholder { color: transparent; transition: color .15s }\n.pc-floating-label-input-root[data-floating] .pc-floating-label-input-field:focus::placeholder { color: var(--muted-foreground) }\n`\n\nconst SPRING = spring.snap\n\nexport type FloatingLabelInputLabels = {\n  /** aria-label of the clear button; gets the field's label text. */\n  clear: (label: string) => string\n  /** Used as the label text in `clear` when `label` is not a string. */\n  field: string\n}\nconst DEFAULT_LABELS: FloatingLabelInputLabels = {\n  clear: (label) => `Clear ${label}`,\n  field: \"field\",\n}\n\n/** Empties a native field through its value setter and fires a real input event, so React's onChange runs. */\nfunction clearNative(el: HTMLInputElement | HTMLTextAreaElement | null) {\n  if (!el) return false\n  const proto = el instanceof HTMLTextAreaElement ? HTMLTextAreaElement.prototype : HTMLInputElement.prototype\n  const setter = Object.getOwnPropertyDescriptor(proto, \"value\")?.set\n  if (!setter) return false\n  setter.call(el, \"\")\n  el.dispatchEvent(new Event(\"input\", { bubbles: true }))\n  return true\n}\n\ntype FieldBase = {\n  /** Text that sits inside the field and floats up on focus or when there is a value. */\n  label: React.ReactNode\n  /** Error message; slides in under the field and turns it red. */\n  error?: React.ReactNode\n  /** Helper text shown when there is no error. */\n  description?: React.ReactNode\n  /** Show a clear (×) button when the field has a value. */\n  clearable?: boolean\n  /** Called with the new string on typing and on clear. The clear button also fires `onChange` with a real change event. */\n  onValueChange?: (value: string) => void\n  /** Called after the clear button empties the field. */\n  onClear?: () => void\n  /** Classes for the native input/textarea; `className` styles the root. */\n  inputClassName?: string\n  /** Text for built-in controls and screen readers; pass translations here. */\n  labels?: Partial<FloatingLabelInputLabels>\n}\n\nfunction useField(\n  value: React.ComponentProps<\"input\">[\"value\"],\n  defaultValue: React.ComponentProps<\"input\">[\"defaultValue\"],\n  onValueChange?: (v: string) => void,\n) {\n  const [inner, setInner] = React.useState(defaultValue != null ? String(defaultValue) : \"\")\n  const val = value != null ? String(value) : inner\n  const set = (v: string) => {\n    if (value == null) setInner(v)\n    onValueChange?.(v)\n  }\n  return [val, set] as const\n}\n\nfunction Message({ id, error, description, reduce }: { id: string; error?: React.ReactNode; description?: React.ReactNode; reduce: boolean }) {\n  return (\n    <AnimatePresence initial={false} mode=\"wait\">\n      {error ? (\n        <motion.p\n          key=\"error\"\n          id={id}\n          role=\"alert\"\n          initial={reduce ? { opacity: 0 } : { opacity: 0, height: 0, y: -4 }}\n          animate={{ opacity: 1, height: \"auto\", y: 0 }}\n          exit={reduce ? { opacity: 0 } : { opacity: 0, height: 0, y: -4 }}\n          transition={reduce ? { duration: 0.1 } : { duration: 0.2, ease: easing.out }}\n          className=\"overflow-hidden\"\n        >\n          <span className=\"flex items-center gap-1.5 pt-1.5 text-xs font-medium text-destructive\">\n            <AlertCircle className=\"size-3.5 shrink-0\" /> {error}\n          </span>\n        </motion.p>\n      ) : description ? (\n        <motion.p\n          key=\"description\"\n          id={id}\n          initial={{ opacity: 0 }}\n          animate={{ opacity: 1 }}\n          exit={{ opacity: 0, transition: { duration: 0.08 } }}\n          className=\"pt-1.5 text-xs text-muted-foreground\"\n        >\n          {description}\n        </motion.p>\n      ) : null}\n    </AnimatePresence>\n  )\n}\n\nfunction ClearButton({ show, label, onClear, reduce }: { show: boolean; label: string; onClear: () => void; reduce: boolean }) {\n  return (\n    <AnimatePresence initial={false}>\n      {show && (\n        <motion.button\n          type=\"button\"\n          aria-label={label}\n          onMouseDown={(e) => e.preventDefault()}\n          onClick={onClear}\n          initial={reduce ? { opacity: 0 } : { opacity: 0, scale: 0.5, rotate: -90 }}\n          animate={{ opacity: 1, scale: 1, rotate: 0 }}\n          exit={reduce ? { opacity: 0 } : { opacity: 0, scale: 0.5, rotate: 90 }}\n          transition={reduce ? { duration: 0.1 } : SPRING}\n          className=\"pc-press grid size-6 shrink-0 place-items-center rounded-full text-muted-foreground outline-none hover:bg-muted hover:text-foreground focus-visible:ring-3 focus-visible:ring-ring/50\"\n        >\n          <X className=\"size-3.5\" />\n        </motion.button>\n      )}\n    </AnimatePresence>\n  )\n}\n\nconst shell =\n  \"relative flex w-full rounded-lg border border-input bg-background shadow-xs transition-[border-color,box-shadow] dark:bg-input/30 focus-within:border-ring focus-within:ring-3 focus-within:ring-ring/50 data-invalid:border-destructive data-invalid:focus-within:ring-destructive/20 data-disabled:cursor-not-allowed data-disabled:opacity-50\"\n\nconst labelTone = (floating: boolean, focused: boolean, invalid: boolean) =>\n  cn(\n    \"pointer-events-none absolute start-0 origin-top-left truncate rtl:origin-top-right text-sm whitespace-nowrap transition-colors\",\n    invalid ? \"text-destructive\" : focused ? \"text-foreground\" : \"text-muted-foreground\",\n    floating && !invalid && !focused && \"text-muted-foreground\",\n  )\n\nexport type FloatingLabelInputProps = Omit<React.ComponentProps<\"input\">, \"prefix\"> &\n  FieldBase & {\n    /** Shown before the value once the label floats, e.g. \"$\" or \"https://\". */\n    prefix?: React.ReactNode\n    /** Shown after the value once the label floats, e.g. \"/hr\" or \".com\". */\n    suffix?: React.ReactNode\n  }\n\n/** Input whose label floats up on focus or value, with prefix/suffix slots, a clear button and an animated error. */\nexport function FloatingLabelInput({\n  label,\n  error,\n  description,\n  clearable,\n  onValueChange,\n  onClear,\n  prefix,\n  suffix,\n  className,\n  inputClassName,\n  labels,\n  value,\n  defaultValue,\n  onChange,\n  onFocus,\n  onBlur,\n  onAnimationStart,\n  disabled,\n  id: idProp,\n  ref,\n  ...props\n}: FloatingLabelInputProps) {\n  const l = useLabels(\"floating-label-input\", DEFAULT_LABELS, labels)\n  const reduce = useReducedMotion() ?? false\n  const uid = React.useId()\n  const id = idProp ?? uid\n  const msgId = `${id}-msg`\n  const [val, setVal] = useField(value, defaultValue, onValueChange)\n  const [focused, setFocused] = React.useState(false)\n  const [autofilled, setAutofilled] = React.useState(false)\n  const inputRef = React.useRef<HTMLInputElement | null>(null)\n  const floating = focused || val.length > 0 || autofilled\n  const invalid = Boolean(error)\n\n  const setRefs = (el: HTMLInputElement | null) => {\n    inputRef.current = el\n    if (typeof ref === \"function\") ref(el)\n    else if (ref) (ref as React.RefObject<HTMLInputElement | null>).current = el\n  }\n\n  const labelText = typeof label === \"string\" ? label : l.field\n\n  return (\n    <div className={cn(\"pc-floating-label-input-root w-full\", className)} data-floating={floating || undefined}>\n      <GooduiTokens />\n      <style href=\"pc-floating-label-input\" precedence=\"default\">\n        {CSS}\n      </style>\n      <div className={cn(shell, \"h-14 items-center gap-1 px-3\")} data-invalid={invalid || undefined} data-disabled={disabled || undefined}>\n        {prefix != null && (\n          <motion.span\n            aria-hidden=\"true\"\n            className=\"shrink-0 self-end pb-2.5 text-sm text-muted-foreground\"\n            initial={false}\n            animate={{ opacity: floating ? 1 : 0 }}\n            transition={{ duration: 0.15 }}\n          >\n            {prefix}\n          </motion.span>\n        )}\n        {/* The label sits on the shell, so it lines up with the field edge even when a prefix is set. */}\n        <motion.label\n          htmlFor={id}\n          className={cn(labelTone(floating, focused, invalid), \"top-[1.125rem] start-3 max-w-[calc(100%-1.5rem)]\")}\n          initial={false}\n          animate={{ y: floating ? -10 : 0, scale: floating ? 0.78 : 1 }}\n          transition={reduce ? { duration: 0 } : SPRING}\n        >\n          {label}\n        </motion.label>\n        <div className=\"relative h-full min-w-0 flex-1\">\n          <input\n            ref={setRefs}\n            id={id}\n            value={val}\n            disabled={disabled}\n            aria-invalid={invalid || undefined}\n            aria-describedby={error || description ? msgId : undefined}\n            onChange={(e) => {\n              setVal(e.target.value)\n              setAutofilled(false)\n              onChange?.(e)\n            }}\n            onFocus={(e) => {\n              setFocused(true)\n              onFocus?.(e)\n            }}\n            onBlur={(e) => {\n              setFocused(false)\n              onBlur?.(e)\n            }}\n            onAnimationStart={(e) => {\n              if (e.animationName === \"pc-floating-label-input-autofill\") setAutofilled(true)\n              onAnimationStart?.(e)\n            }}\n            className={cn(\n              \"pc-floating-label-input-field h-full w-full bg-transparent pt-5 pb-1.5 text-sm text-foreground outline-none disabled:cursor-not-allowed\",\n              inputClassName,\n            )}\n            {...props}\n          />\n        </div>\n        {suffix != null && (\n          <motion.span\n            aria-hidden=\"true\"\n            className=\"shrink-0 self-end pb-2.5 text-sm text-muted-foreground\"\n            initial={false}\n            animate={{ opacity: floating ? 1 : 0 }}\n            transition={{ duration: 0.15 }}\n          >\n            {suffix}\n          </motion.span>\n        )}\n        {clearable && (\n          <ClearButton\n            show={val.length > 0 && !disabled}\n            label={l.clear(labelText)}\n            reduce={reduce}\n            onClear={() => {\n              // A real input event runs our onChange (setVal + the user's onChange); fall back to setVal if that fails.\n              if (!clearNative(inputRef.current)) setVal(\"\")\n              onClear?.()\n              inputRef.current?.focus()\n            }}\n          />\n        )}\n      </div>\n      <Message id={msgId} error={error} description={description} reduce={reduce} />\n    </div>\n  )\n}\n\nexport type FloatingLabelTextareaProps = React.ComponentProps<\"textarea\"> & FieldBase\n\n/** Textarea with the same floating label; grows with content where `field-sizing` is supported and counts characters. */\nexport function FloatingLabelTextarea({\n  label,\n  error,\n  description,\n  clearable,\n  onValueChange,\n  onClear,\n  className,\n  inputClassName,\n  labels,\n  value,\n  defaultValue,\n  onChange,\n  onFocus,\n  onBlur,\n  disabled,\n  maxLength,\n  id: idProp,\n  ref,\n  ...props\n}: FloatingLabelTextareaProps) {\n  const l = useLabels(\"floating-label-input\", DEFAULT_LABELS, labels)\n  const reduce = useReducedMotion() ?? false\n  const uid = React.useId()\n  const id = idProp ?? uid\n  const msgId = `${id}-msg`\n  const [val, setVal] = useField(value as React.ComponentProps<\"input\">[\"value\"], defaultValue as React.ComponentProps<\"input\">[\"defaultValue\"], onValueChange)\n  const [focused, setFocused] = React.useState(false)\n  const areaRef = React.useRef<HTMLTextAreaElement | null>(null)\n  const floating = focused || val.length > 0\n  const invalid = Boolean(error)\n  const near = maxLength ? val.length / maxLength : 0\n\n  const setRefs = (el: HTMLTextAreaElement | null) => {\n    areaRef.current = el\n    if (typeof ref === \"function\") ref(el)\n    else if (ref) (ref as React.RefObject<HTMLTextAreaElement | null>).current = el\n  }\n\n  return (\n    <div className={cn(\"pc-floating-label-input-root w-full\", className)} data-floating={floating || undefined}>\n      <style href=\"pc-floating-label-input\" precedence=\"default\">\n        {CSS}\n      </style>\n      <div className={cn(shell, \"flex-col px-3 pt-6 pb-2\")} data-invalid={invalid || undefined} data-disabled={disabled || undefined}>\n        <motion.label\n          htmlFor={id}\n          className={cn(labelTone(floating, focused, invalid), \"top-4 start-3 max-w-[calc(100%-1.5rem)]\")}\n          initial={false}\n          animate={{ y: floating ? -8 : 0, scale: floating ? 0.78 : 1 }}\n          transition={reduce ? { duration: 0 } : SPRING}\n        >\n          {label}\n        </motion.label>\n        <textarea\n          ref={setRefs}\n          id={id}\n          value={val}\n          disabled={disabled}\n          maxLength={maxLength}\n          aria-invalid={invalid || undefined}\n          aria-describedby={error || description ? msgId : undefined}\n          onChange={(e) => {\n            setVal(e.target.value)\n            onChange?.(e)\n          }}\n          onFocus={(e) => {\n            setFocused(true)\n            onFocus?.(e)\n          }}\n          onBlur={(e) => {\n            setFocused(false)\n            onBlur?.(e)\n          }}\n          className={cn(\n            \"pc-floating-label-input-field min-h-20 w-full resize-none bg-transparent text-sm leading-6 text-foreground outline-none [field-sizing:content] max-h-48 disabled:cursor-not-allowed\",\n            inputClassName,\n          )}\n          {...props}\n        />\n        {(clearable || maxLength) && (\n          <div className=\"flex items-center justify-end gap-2 pt-1\">\n            {maxLength ? (\n              <span\n                aria-live={near > 0.9 ? \"polite\" : \"off\"}\n                className={cn(\n                  \"text-xs tabular-nums text-muted-foreground transition-colors\",\n                  near > 0.9 && \"text-amber-700 dark:text-amber-400\",\n                  near >= 1 && \"text-destructive\",\n                )}\n              >\n                {val.length}/{maxLength}\n              </span>\n            ) : null}\n            {clearable && (\n              <ClearButton\n                show={val.length > 0 && !disabled}\n                label={l.clear(typeof label === \"string\" ? label : l.field)}\n                reduce={reduce}\n                onClear={() => {\n                  if (!clearNative(areaRef.current)) setVal(\"\")\n                  onClear?.()\n                  areaRef.current?.focus()\n                }}\n              />\n            )}\n          </div>\n        )}\n      </div>\n      <Message id={msgId} error={error} description={description} reduce={reduce} />\n    </div>\n  )\n}\n",
      "type": "registry:component",
      "target": "components/goodui/floating-label-input.tsx"
    }
  ],
  "categories": [
    "components",
    "inputs"
  ],
  "type": "registry:component"
}