{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "floating-label",
  "type": "registry:ui",
  "title": "Floating Label Field",
  "description": "Label rests inside as the placeholder and floats up on focus, value or autofill.",
  "categories": [
    "inputs"
  ],
  "dependencies": [
    "@base-ui/react"
  ],
  "registryDependencies": [
    "https://www.stealth.pm/r/tokens.json",
    "https://www.stealth.pm/r/cn.json",
    "https://www.stealth.pm/r/icons.json",
    "https://www.stealth.pm/r/use-controllable-state.json"
  ],
  "files": [
    {
      "path": "components/ui/floating-label.tsx",
      "content": "\"use client\";\nimport { Field } from \"@base-ui/react/field\";\nimport { Input as BaseInput } from \"@base-ui/react/input\";\nimport { useState } from \"react\";\nimport { cn } from \"@/lib/cn\";\nimport { Alert } from \"@/lib/icons\";\nimport { useControllableState } from \"@/lib/use-controllable-state\";\n\nexport type FloatingLabelFieldProps = Omit<React.ComponentProps<\"input\">, \"value\" | \"defaultValue\" | \"size\" | \"children\"> & {\n  /** Sits inside the field like a placeholder, then floats above the text. */\n  label: string;\n  value?: string;\n  defaultValue?: string;\n  onValueChange?: (value: string) => void;\n  /** A hint under the field. The error takes its place while there is one. */\n  description?: React.ReactNode;\n  /** An error message. Setting it marks the field invalid and slides the message in. */\n  error?: string;\n  /** Trailing content inside the field, e.g. a reveal button or a status icon. */\n  suffix?: React.ReactNode;\n  /** Passed to the Base UI Field for its own validation, when you don't manage `error` yourself. */\n  validate?: React.ComponentProps<typeof Field.Root>[\"validate\"];\n  validationMode?: React.ComponentProps<typeof Field.Root>[\"validationMode\"];\n  /** Classes for the outer Field root; `inputClassName` styles the input. */\n  inputClassName?: string;\n};\n\nexport function FloatingLabelField({\n  label,\n  value: valueProp,\n  defaultValue = \"\",\n  onValueChange,\n  description,\n  error,\n  suffix,\n  validate,\n  validationMode,\n  disabled,\n  readOnly,\n  name,\n  placeholder,\n  className,\n  inputClassName,\n  ref,\n  ...rest\n}: FloatingLabelFieldProps) {\n  const [value, setValue] = useControllableState({ value: valueProp, defaultValue, onChange: onValueChange });\n  // Keep the last message on screen while it animates out, instead of emptying mid-exit.\n  const [lastError, setLastError] = useState(error);\n  if (error && error !== lastError) setLastError(error);\n\n  return (\n    <Field.Root\n      name={name}\n      disabled={disabled}\n      invalid={error ? true : undefined}\n      validate={validate}\n      validationMode={validationMode}\n      className={cn(\"flex w-full min-w-0 flex-col\", className)}\n    >\n      <div\n        data-slot=\"floating-label\"\n        className={cn(\n          \"group/float relative h-13 w-full rounded-lg border border-line-2 bg-raised text-fg shadow-[var(--shadow)]\",\n          \"transition-[border-color,box-shadow,background-color] duration-150 ease-out\",\n          \"hover:border-fg-4 focus-within:border-fg-3 focus-within:ring-3 focus-within:ring-fg/8 hover:focus-within:border-fg-3\",\n          \"has-[input[data-invalid]]:border-danger/70 has-[input[data-invalid]]:hover:border-danger has-[input[data-invalid]]:focus-within:border-danger has-[input[data-invalid]]:focus-within:ring-danger/15\",\n          \"has-[input:disabled]:cursor-not-allowed has-[input:disabled]:opacity-50 has-[input:disabled]:shadow-none has-[input:disabled]:hover:border-line-2\",\n          \"has-[input:read-only]:bg-frame has-[input:read-only]:shadow-none has-[input:read-only]:hover:border-line-2\",\n        )}\n      >\n        <BaseInput\n          ref={ref}\n          value={value}\n          onValueChange={(next) => setValue(next)}\n          readOnly={readOnly}\n          // A real placeholder is required for :placeholder-shown, which is how the\n          // label knows the field is empty without JavaScript (and through autofill).\n          placeholder={placeholder ?? \" \"}\n          className={cn(\n            \"peer absolute inset-0 size-full rounded-[inherit] bg-transparent px-3 pb-1.5 pt-[21px] text-base leading-5 text-fg outline-none sm:text-[14px]\",\n            \"placeholder:text-transparent placeholder:transition-colors placeholder:duration-150 focus:placeholder:text-fg-4 focus:placeholder:delay-75\",\n            \"disabled:cursor-not-allowed\",\n            \"autofill:shadow-[inset_0_0_0_1000px_var(--raised)] autofill:[-webkit-text-fill-color:var(--fg)]\",\n            suffix != null && \"pr-11\",\n            inputClassName,\n          )}\n          {...rest}\n        />\n        <Field.Label\n          className={cn(\n            \"pointer-events-none absolute left-3 top-4 max-w-[calc(100%-1.5rem)] origin-top-left truncate text-base leading-5 text-fg-3 sm:text-[14px]\",\n            // Transform only: the label moves and shrinks, nothing around it reflows.\n            \"transition-[translate,scale,color] duration-200 ease-out-quart motion-reduce:transition-[color]\",\n            \"peer-focus:-translate-y-[9px] peer-focus:scale-[0.85] peer-focus:text-fg-2\",\n            \"peer-[:not(:placeholder-shown)]:-translate-y-[9px] peer-[:not(:placeholder-shown)]:scale-[0.85]\",\n            \"peer-autofill:-translate-y-[9px] peer-autofill:scale-[0.85]\",\n            \"data-invalid:text-danger peer-focus:data-invalid:text-danger\",\n            suffix != null && \"max-w-[calc(100%-3.5rem)]\",\n          )}\n        >\n          {label}\n        </Field.Label>\n        {suffix != null && <div className=\"absolute inset-y-0 right-1.5 flex items-center text-fg-3\">{suffix}</div>}\n      </div>\n\n      {/* The message row grows from nothing on the first error, so the form below\n          eases down instead of jumping; a hint and an error share one slot. */}\n      <div className=\"grid grid-rows-[0fr] transition-[grid-template-rows] duration-200 ease-out-quart has-[[data-msg]]:grid-rows-[1fr] motion-reduce:transition-none\">\n        <div className=\"min-h-0 overflow-hidden\">\n          <div className=\"grid pl-3 pt-1.5\">\n            {description != null && (\n              <Field.Description\n                data-msg=\"\"\n                className=\"col-start-1 row-start-1 text-[12px] leading-4 text-fg-3 transition-opacity duration-150 data-invalid:opacity-0\"\n              >\n                {description}\n              </Field.Description>\n            )}\n            <Field.Error\n              data-msg=\"\"\n              match={error ? true : undefined}\n              className={cn(\n                \"col-start-1 row-start-1 flex items-start gap-1.5 text-[12px] leading-4 text-danger\",\n                \"transition-[opacity,translate] duration-200 ease-out-expo\",\n                \"data-starting-style:-translate-y-1 data-starting-style:opacity-0 data-ending-style:opacity-0 data-ending-style:duration-100\",\n                \"motion-reduce:translate-y-0\",\n              )}\n            >\n              <Alert size={14} className=\"mt-px size-3.5 shrink-0\" />\n              <span className=\"min-w-0\">{error || lastError || <Field.Validity>{(s) => s.error}</Field.Validity>}</span>\n            </Field.Error>\n          </div>\n        </div>\n      </div>\n    </Field.Root>\n  );\n}\n",
      "type": "registry:ui"
    }
  ],
  "docs": "https://www.stealth.pm/ui/floating-label"
}
