"use client"; import { useCallback, useEffect, useId, useImperativeHandle, useLayoutEffect, useRef, useState, } from "react"; import { motion, useReducedMotion } from "motion/react"; const INSTANT = { duration: 0 } as const; const LIFT = { type: "spring", stiffness: 760, damping: 46, mass: 0.5 } as const; const RAISE = -32; const SLIDE = -12; const SHRINK = 0.92; const useIsomorphicLayoutEffect = typeof window === "undefined" ? useEffect : useLayoutEffect; export type UseFloatingLabelOptions = { value?: string; defaultValue?: string; disabled?: boolean; }; export type UseFloatingLabelReturn = { ref: React.RefObject; raised: boolean; focused: boolean; filled: boolean; length: number; instant: boolean; fieldProps: { onFocus: () => void; onBlur: () => void; onChange: (event: React.ChangeEvent) => void; }; }; type Fill = { length: number; instant: boolean }; export function useFloatingLabel({ value, defaultValue, disabled = false, }: UseFloatingLabelOptions = {}): UseFloatingLabelReturn { const ref = useRef(null); const mounted = useRef(false); const [focused, setFocused] = useState(false); const [fill, setFill] = useState({ length: (value ?? defaultValue ?? "").length, instant: true, }); const settle = useCallback((next: number, instant: boolean) => { setFill((prev) => prev.length === next && prev.instant === instant ? prev : { length: next, instant }, ); }, []); useIsomorphicLayoutEffect(() => { const el = ref.current; const next = value !== undefined ? value.length : el ? el.value.length : 0; settle(next, !mounted.current); mounted.current = true; }, [value, settle]); useEffect(() => { setFill((prev) => (prev.instant ? { ...prev, instant: false } : prev)); }, []); useEffect(() => { const el = ref.current; if (!el || value !== undefined) return; const read = () => settle(el.value.length, false); el.addEventListener("input", read); el.addEventListener("change", read); return () => { el.removeEventListener("input", read); el.removeEventListener("change", read); }; }, [value, settle]); useEffect(() => { if (disabled) setFocused(false); }, [disabled]); const onFocus = useCallback(() => setFocused(true), []); const onBlur = useCallback(() => setFocused(false), []); const onChange = useCallback( (event: React.ChangeEvent) => settle(event.currentTarget.value.length, false), [settle], ); return { ref, raised: focused || fill.length > 0, focused, filled: fill.length > 0, length: fill.length, instant: fill.instant && !focused, fieldProps: { onFocus, onBlur, onChange }, }; } export type FloatingLabelInputProps = { label: string; value?: string; defaultValue?: string; onChange?: (value: string, event: React.ChangeEvent) => void; onFocus?: () => void; onBlur?: () => void; hint?: string; invalid?: boolean; id?: string; name?: string; type?: "text" | "email" | "password" | "search" | "tel" | "url"; autoComplete?: string; inputMode?: React.ComponentProps<"input">["inputMode"]; maxLength?: number; required?: boolean; disabled?: boolean; readOnly?: boolean; spellCheck?: boolean; inputRef?: React.Ref; endAdornment?: React.ReactNode; ariaDescribedBy?: string; reserveHintSpace?: boolean; className?: string; }; export function FloatingLabelInput({ label, value, defaultValue, onChange, onFocus, onBlur, hint, invalid = false, id, name, type = "text", autoComplete, inputMode, maxLength, required = false, disabled = false, readOnly = false, spellCheck, inputRef, endAdornment, ariaDescribedBy, reserveHintSpace = true, className = "", }: FloatingLabelInputProps) { const auto = useId(); const fieldId = id ?? `${auto}-field`; const hintId = `${auto}-hint`; const reduced = useReducedMotion(); const { ref, raised, focused, length, instant, fieldProps } = useFloatingLabel({ value, defaultValue, disabled, }); const move = reduced || instant ? INSTANT : LIFT; useImperativeHandle(inputRef, () => ref.current as HTMLInputElement, [ref]); const attach = useCallback( (node: HTMLInputElement | null) => { ref.current = node; }, [ref], ); const describedBy = [ariaDescribedBy, hint ? hintId : null] .filter(Boolean) .join(" "); return (
{ fieldProps.onFocus(); onFocus?.(); }} onBlur={() => { fieldProps.onBlur(); onBlur?.(); }} onChange={(event) => { fieldProps.onChange(event); onChange?.(event.currentTarget.value, event); }} className={`absolute inset-0 h-full w-full rounded-[9px] bg-transparent px-3 py-0 text-[13px] leading-[20px] text-foreground outline-none focus-visible:outline-none disabled:cursor-not-allowed ${ endAdornment ? "pr-16" : "" }`} /> {endAdornment ? (
{endAdornment}
) : null}
{label} {required ? ( * ) : null}
{reserveHintSpace || hint || maxLength !== undefined ? (

{hint}

{maxLength !== undefined ? ( {maxLength} / {maxLength} {length} / {maxLength} ) : null} {hint ? ( {hint} ) : null}
) : null}
); }