opensourceui
GitHubTwitter/X
  1. Home
  2. Components
  3. Inputs
  4. Floating Label Field Input
Back to Inputs

Components / Inputs

Floating Label Field Input

Free Inputs React component — FloatingLabelFieldInput. MIT licensed, copy-paste ready for Next.js and Tailwind CSS.

Outlined floating label that rests on the border notch when focused or filled — matches Material-style fields with autofill detection.

Preview

We never share your email.

Setup

How to use

Free for personal and commercial use. No UI attribution required. Include the MIT copyright notice when copying component source into your project. Read the MIT license.

  1. 1

    Run in your terminal:

    $npm install clsx tailwind-merge lucide-react
  2. 2

    Copy lib/cn.ts below. Skip if you already have cn().

  3. 3

    Copy the code below and create components/inputs/floating-label-field-input.tsx in your project.

  4. 4

    Import and render:

    Example

    import { FloatingLabelFieldInput } from "@/components/inputs/floating-label-field-input";

    <FloatingLabelFieldInput label="Work email" type="email" value={email} onChange={(e) => setEmail(e.target.value)} />

lib/cn.ts

1
2
3
4
5
6
import { type ClassValue, clsx } from "clsx";
import { twMerge } from "tailwind-merge";
 
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}

components/inputs/floating-label-field-input.tsx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
"use client";
 
import {
forwardRef,
useCallback,
useEffect,
useId,
useRef,
useState,
type ChangeEvent,
type ComponentPropsWithoutRef,
type FocusEvent,
type MutableRefObject,
type Ref,
} from "react";
 
import { cn } from "@/lib/cn";
 
export type FloatingLabelFieldInputProps = Readonly<
{
label?: string;
hint?: string;
error?: boolean;
errorMessage?: string;
containerClassName?: string;
} & Omit<ComponentPropsWithoutRef<"input">, "size" | "placeholder">
>;
 
function mergeRefs<T>(...refs: Array<Ref<T> | undefined>) {
return (node: T | null) => {
for (const ref of refs) {
if (!ref) continue;
if (typeof ref === "function") ref(node);
else (ref as MutableRefObject<T | null>).current = node;
}
};
}
 
export const FloatingLabelFieldInput = forwardRef<
HTMLInputElement,
FloatingLabelFieldInputProps
>(function FloatingLabelFieldInput(
{
className,
containerClassName,
id,
label = "Email address",
hint,
error = false,
errorMessage = "Enter a valid email address.",
disabled,
readOnly,
required,
type = "email",
value,
defaultValue = "",
onChange,
onFocus,
onBlur,
...props
},
ref,
) {
const generatedId = useId();
const inputId = id ?? generatedId;
const hintId = `${inputId}-hint`;
const errorId = `${inputId}-error`;
const localRef = useRef<HTMLInputElement | null>(null);
 
const isControlled = value !== undefined;
const [internal, setInternal] = useState(() => String(defaultValue));
const [focused, setFocused] = useState(false);
const [autofilled, setAutofilled] = useState(false);
 
const current = isControlled ? String(value ?? "") : internal;
const floated = focused || current.length > 0 || autofilled;
 
useEffect(() => {
if (isControlled) return;
setInternal(String(defaultValue));
}, [defaultValue, isControlled]);
 
useEffect(() => {
const node = localRef.current;
if (!node) return;
 
const syncFromDom = () => {
const domValue = node.value;
if (domValue.length > 0) {
if (!isControlled) setInternal(domValue);
setAutofilled(true);
}
};
 
syncFromDom();
const timers = [
window.setTimeout(syncFromDom, 100),
window.setTimeout(syncFromDom, 500),
];
 
return () => {
for (const timer of timers) window.clearTimeout(timer);
};
}, [isControlled]);
 
const handleChange = useCallback(
(event: ChangeEvent<HTMLInputElement>) => {
const next = event.target.value;
if (!isControlled) setInternal(next);
setAutofilled(false);
onChange?.(event);
},
[isControlled, onChange],
);
 
const handleFocus = useCallback(
(event: FocusEvent<HTMLInputElement>) => {
setFocused(true);
onFocus?.(event);
},
[onFocus],
);
 
const handleBlur = useCallback(
(event: FocusEvent<HTMLInputElement>) => {
setFocused(false);
const next = event.currentTarget.value;
if (!isControlled) setInternal(next);
if (next.length > 0) setAutofilled(true);
onBlur?.(event);
},
[isControlled, onBlur],
);
 
return (
<div
data-slot="floating-label-field-input"
data-error={error || undefined}
data-floated={floated || undefined}
className={cn("w-full max-w-sm font-sans", containerClassName)}
>
<div className="relative">
<input
ref={mergeRefs(ref, localRef)}
id={inputId}
type={type}
disabled={disabled}
readOnly={readOnly}
required={required}
value={isControlled ? current : undefined}
defaultValue={isControlled ? undefined : String(defaultValue)}
aria-invalid={error || undefined}
aria-describedby={
error ? errorId : hint ? hintId : undefined
}
onChange={handleChange}
onFocus={handleFocus}
onBlur={handleBlur}
className={cn(
"h-12 w-full rounded-lg border bg-white px-3.5 pt-4 pb-2 font-sans text-sm text-neutral-900 outline-none ring-0 transition-[border-color] duration-200 focus:ring-0 disabled:cursor-not-allowed disabled:bg-neutral-50 disabled:text-neutral-400 read-only:cursor-default read-only:bg-neutral-50",
error
? "border-rose-400 focus:border-rose-500"
: focused
? "border-neutral-900"
: "border-neutral-300",
className,
)}
{...props}
/>
 
<label
htmlFor={inputId}
className={cn(
"pointer-events-none absolute left-3 max-w-[calc(100%-1.5rem)] origin-left truncate transition-all duration-200 ease-out",
floated
? "top-0 -translate-y-1/2 bg-white px-1 text-xs font-medium leading-none"
: "top-1/2 -translate-y-1/2 bg-transparent px-0 text-sm leading-none",
floated && !error && (focused ? "text-neutral-900" : "text-neutral-600"),
!floated && "text-neutral-500",
error && "text-rose-500",
disabled && "text-neutral-400",
)}
>
{label}
{required ? (
<span className="ml-0.5 text-rose-500" aria-hidden>
*
</span>
) : null}
</label>
</div>
 
{error ? (
<p id={errorId} role="alert" className="mt-1.5 text-xs text-rose-600">
{errorMessage}
</p>
) : hint ? (
<p id={hintId} className="mt-1.5 text-xs text-neutral-500">
{hint}
</p>
) : null}
</div>
);
});
 
FloatingLabelFieldInput.displayName = "FloatingLabelFieldInput";
PreviousNext

On this

page

Jump to a section on this page.

  • lib/cn.ts
  • components/inputs/floating-label-field-input.tsx

Sponsor spot · demo preview

Logo

Your brand name

Your tagline or category

A short pitch about your product or service goes here. This is a preview of how sponsor cards will look in this sidebar.

Your call to action

This slot is available. Sponsor Opensource UI and reach developers browsing components every day.