Floating label form
A sign-up form whose labels float up out of the way, validates kindly as you leave each field, and shakes only the field that needs attention.
"use client";
import { useState, type FormEvent } from "react";
import "./signup-form.css";
type Field = "name" | "email" | "company";
const RULES: Record<Field, (v: string) => string> = {
name: (v) =>
v.trim().length < 2 ? "Add your name so we know what to call you." : "",
email: (v) =>
!/^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/.test(v)
? "That email looks incomplete. Check for a missing @ or dot."
: "",
company: () => "",
};
/**
* SignupForm
* - Labels sit inside the field and float up on focus or when filled (:placeholder-shown trick).
* - Each field is checked when you leave it, not while you type, so errors never nag mid-word.
* - On submit, invalid fields shake once and focus jumps to the first one.
*/
export function SignupForm({ theme = "light" }: { theme?: "light" | "dark" }) {
const [values, setValues] = useState<Record<Field, string>>({
name: "",
email: "",
company: "",
});
const [errors, setErrors] = useState<Record<Field, string>>({
name: "",
email: "",
company: "",
});
const [shake, setShake] = useState(0);
const [sent, setSent] = useState(false);
const check = (f: Field, v = values[f]) =>
setErrors((e) => ({ ...e, [f]: RULES[f](v) }));
const submit = (e: FormEvent<HTMLFormElement>) => {
e.preventDefault();
const next = {
name: RULES.name(values.name),
email: RULES.email(values.email),
company: "",
};
setErrors(next);
const first = (Object.keys(next) as Field[]).find((f) => next[f]);
if (first) {
setShake((s) => s + 1);
e.currentTarget
.querySelector<HTMLInputElement>(`[name="${first}"]`)
?.focus();
return;
}
setSent(true);
};
if (sent) {
return (
<div className="gu-form gu-form--done" data-theme={theme} role="status">
<svg viewBox="0 0 48 48" aria-hidden="true">
<circle cx="24" cy="24" r="22" />
<path d="m14 25 7 7 13-15" />
</svg>
<h3>Check your inbox, {values.name.split(" ")[0]}.</h3>
<p>We sent a sign-in link to {values.email}.</p>
</div>
);
}
const input = (
f: Field,
label: string,
type: string,
auto: string,
optional = false,
) => (
<div
className="gu-form__field"
data-error={errors[f] ? shake : undefined}
key={f + (errors[f] ? shake : "")}
>
<input
id={`gu-form-${f}`}
name={f}
type={type}
autoComplete={auto}
placeholder=" "
value={values[f]}
aria-invalid={Boolean(errors[f])}
aria-describedby={errors[f] ? `gu-form-${f}-err` : undefined}
onChange={(e) => {
setValues((v) => ({ ...v, [f]: e.target.value }));
if (errors[f]) check(f, e.target.value); // clear the error as soon as it is fixed
}}
onBlur={() => values[f] && check(f)}
/>
<label htmlFor={`gu-form-${f}`}>
{label}
{optional && <span> (optional)</span>}
</label>
<p id={`gu-form-${f}-err`} className="gu-form__error">
{errors[f]}
</p>
</div>
);
return (
<form className="gu-form" data-theme={theme} onSubmit={submit} noValidate>
<h3>Create your account</h3>
<p className="gu-form__lead">
Two fields and you are in. No password needed.
</p>
{input("name", "Full name", "text", "name")}
{input("email", "Work email", "email", "email")}
{input("company", "Company", "text", "organization", true)}
<button type="submit">Send my sign-in link</button>
</form>
);
}
Paste this into ChatGPT, Claude, Cursor, v0, Lovable or Bolt. It describes the layout, motion, colors and accessibility and includes the code, so the AI can rebuild it in any framework.
# Build this UI piece: Floating label form
You are a senior front-end engineer. Recreate the piece below in my project. If I don't say which framework I use, ask once, then build it in that framework (React, Vue, Svelte, Angular, Astro, plain HTML/CSS/JS or anything else). Keep the behavior, motion, colors, spacing and accessibility exactly as described. Keep the code clean and commented for beginners.
## What it is
A friendly sign-up card form with floating labels, validation that runs when you leave a field (never while typing), a shake on the offending field when submitting, and an animated success state.
## Layout and look
- Card max 420px, padding 28px, radius 24px, 1px border, #fbf8f2 (dark #18181b). Title (26px, weight 900) and a muted lead line.
- Inputs: 58px tall, radius 14px, 1.5px border, background #f3eee3 (dark #0e0e10), padding 22px 16px 6px so the floated label has room.
- Labels sit inside the input; focused or filled they move up 11px and scale to 75% (weight 700); focus color #2b35ff (dark #d7ff3a) with a 4px soft ring.
- Errors: orange-red text under the field, red border and label. Valid filled fields get a faint green border.
- Submit: 56px orange (#ff5a1f) button with dark text.
- Success: an orange circle that pops in with a check that draws itself, "Check your inbox, {first name}." and a muted line.
## Behavior and motion
1. Labels float with transform (250ms cubic-bezier(0.16, 1, 0.3, 1)), using the input:not(:placeholder-shown) trick with placeholder=" ".
2. Validate on blur if the field has content; once a field shows an error, re-validate on every keystroke so the error disappears as soon as it is fixed.
3. Submit: validate all, focus the first invalid field and shake invalid fields (400ms, ±6px).
## Touch
Correct input types and autocomplete; 56px+ targets.
## Accessibility
- Every input has a real <label>, aria-invalid and aria-describedby pointing to its error text. The success panel has role="status".
- noValidate on the form so the custom messages are used.
- prefers-reduced-motion: no label animation, no shake, check shown immediately.
## Props
theme. Fields and rules are easy to edit at the top of the component.
## Reference implementation
The original is written in React with one plain CSS file. Dependencies: none (plain React and CSS). Translate it faithfully; do not add new libraries unless my framework needs them.
### SignupForm.tsx
```tsx
"use client";
import { useState, type FormEvent } from "react";
import "./signup-form.css";
type Field = "name" | "email" | "company";
const RULES: Record<Field, (v: string) => string> = {
name: (v) =>
v.trim().length < 2 ? "Add your name so we know what to call you." : "",
email: (v) =>
!/^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/.test(v)
? "That email looks incomplete. Check for a missing @ or dot."
: "",
company: () => "",
};
/**
* SignupForm
* - Labels sit inside the field and float up on focus or when filled (:placeholder-shown trick).
* - Each field is checked when you leave it, not while you type, so errors never nag mid-word.
* - On submit, invalid fields shake once and focus jumps to the first one.
*/
export function SignupForm({ theme = "light" }: { theme?: "light" | "dark" }) {
const [values, setValues] = useState<Record<Field, string>>({
name: "",
email: "",
company: "",
});
const [errors, setErrors] = useState<Record<Field, string>>({
name: "",
email: "",
company: "",
});
const [shake, setShake] = useState(0);
const [sent, setSent] = useState(false);
const check = (f: Field, v = values[f]) =>
setErrors((e) => ({ ...e, [f]: RULES[f](v) }));
const submit = (e: FormEvent<HTMLFormElement>) => {
e.preventDefault();
const next = {
name: RULES.name(values.name),
email: RULES.email(values.email),
company: "",
};
setErrors(next);
const first = (Object.keys(next) as Field[]).find((f) => next[f]);
if (first) {
setShake((s) => s + 1);
e.currentTarget
.querySelector<HTMLInputElement>(`[name="${first}"]`)
?.focus();
return;
}
setSent(true);
};
if (sent) {
return (
<div className="gu-form gu-form--done" data-theme={theme} role="status">
<svg viewBox="0 0 48 48" aria-hidden="true">
<circle cx="24" cy="24" r="22" />
<path d="m14 25 7 7 13-15" />
</svg>
<h3>Check your inbox, {values.name.split(" ")[0]}.</h3>
<p>We sent a sign-in link to {values.email}.</p>
</div>
);
}
const input = (
f: Field,
label: string,
type: string,
auto: string,
optional = false,
) => (
<div
className="gu-form__field"
data-error={errors[f] ? shake : undefined}
key={f + (errors[f] ? shake : "")}
>
<input
id={`gu-form-${f}`}
name={f}
type={type}
autoComplete={auto}
placeholder=" "
value={values[f]}
aria-invalid={Boolean(errors[f])}
aria-describedby={errors[f] ? `gu-form-${f}-err` : undefined}
onChange={(e) => {
setValues((v) => ({ ...v, [f]: e.target.value }));
if (errors[f]) check(f, e.target.value); // clear the error as soon as it is fixed
}}
onBlur={() => values[f] && check(f)}
/>
<label htmlFor={`gu-form-${f}`}>
{label}
{optional && <span> (optional)</span>}
</label>
<p id={`gu-form-${f}-err`} className="gu-form__error">
{errors[f]}
</p>
</div>
);
return (
<form className="gu-form" data-theme={theme} onSubmit={submit} noValidate>
<h3>Create your account</h3>
<p className="gu-form__lead">
Two fields and you are in. No password needed.
</p>
{input("name", "Full name", "text", "name")}
{input("email", "Work email", "email", "email")}
{input("company", "Company", "text", "organization", true)}
<button type="submit">Send my sign-in link</button>
</form>
);
}
```
### signup-form.css
```css
/* SignupForm: change these colors to match your brand. */
.gu-form {
--card: #fbf8f2;
--field: #f3eee3;
--text: #0e0e10;
--muted: #57534b;
--line: rgb(14 14 16 / 0.18);
--accent: #ff5a1f;
--accent-text: #0e0e10;
--focus: #2b35ff;
--error: #c2410c;
--ok: #16a34a;
--ease: cubic-bezier(0.16, 1, 0.3, 1);
display: grid;
gap: 14px;
width: min(100%, 420px);
padding: 28px;
border: 1px solid var(--line);
border-radius: 24px;
background: var(--card);
color: var(--text);
font-family: system-ui, sans-serif;
}
.gu-form[data-theme="dark"] {
--card: #18181b;
--field: #0e0e10;
--text: #f3eee3;
--muted: #aea89c;
--line: rgb(243 238 227 / 0.18);
--focus: #d7ff3a;
--error: #fb923c;
--ok: #4ade80;
}
.gu-form h3 {
margin: 0;
font-size: 1.6rem;
font-weight: 900;
letter-spacing: -0.035em;
}
.gu-form__lead {
margin: -8px 0 6px;
color: var(--muted);
font-size: 0.95rem;
}
.gu-form__field {
position: relative;
}
.gu-form input {
width: 100%;
height: 58px;
padding: 22px 16px 6px;
border: 1.5px solid var(--line);
border-radius: 14px;
outline: none;
background: var(--field);
color: inherit;
font: inherit;
font-size: 1rem;
transition:
border-color 0.2s,
box-shadow 0.2s;
}
.gu-form input:focus {
border-color: var(--focus);
box-shadow: 0 0 0 4px color-mix(in srgb, var(--focus) 18%, transparent);
}
.gu-form label {
position: absolute;
top: 19px;
left: 17px;
color: var(--muted);
font-size: 1rem;
pointer-events: none;
transform-origin: left top;
transition:
transform 0.25s var(--ease),
color 0.2s;
}
.gu-form label span {
font-size: 0.85em;
}
/* Float when focused or filled */
.gu-form input:focus + label,
.gu-form input:not(:placeholder-shown) + label {
transform: translateY(-11px) scale(0.75);
font-weight: 700;
}
.gu-form input:focus + label {
color: var(--focus);
}
/* Errors */
.gu-form__error {
min-height: 0;
margin: 6px 4px 0;
color: var(--error);
font-size: 0.82rem;
font-weight: 600;
}
.gu-form__error:empty {
display: none;
}
.gu-form__field[data-error] input {
border-color: var(--error);
}
.gu-form__field[data-error] label {
color: var(--error);
}
.gu-form__field[data-error] {
animation: gu-form-shake 0.4s;
}
@keyframes gu-form-shake {
20%,
60% {
transform: translateX(-6px);
}
40%,
80% {
transform: translateX(6px);
}
}
.gu-form__field:has(input:valid:not(:placeholder-shown)):not([data-error])
input:not(:focus) {
border-color: color-mix(in srgb, var(--ok) 60%, var(--line));
}
.gu-form button {
min-height: 56px;
margin-top: 4px;
border: 0;
border-radius: 14px;
background: var(--accent);
color: var(--accent-text);
font: inherit;
font-weight: 800;
cursor: pointer;
transition:
transform 0.2s,
filter 0.2s;
}
.gu-form button:hover {
filter: brightness(1.05);
}
.gu-form button:active {
transform: scale(0.98);
}
.gu-form button:focus-visible {
outline: 3px solid var(--focus);
outline-offset: 3px;
}
/* Success state */
.gu-form--done {
justify-items: center;
padding: 40px 28px;
text-align: center;
}
.gu-form--done svg {
width: 64px;
height: 64px;
fill: none;
stroke-width: 3;
stroke-linecap: round;
stroke-linejoin: round;
}
.gu-form--done circle {
fill: var(--accent);
stroke: none;
animation: gu-form-pop 0.5s cubic-bezier(0.34, 1.56, 0.64, 1);
}
.gu-form--done path {
stroke: var(--accent-text);
stroke-dasharray: 40;
stroke-dashoffset: 40;
animation: gu-form-draw 0.5s 0.25s var(--ease) forwards;
}
.gu-form--done p {
margin: 0;
color: var(--muted);
}
@keyframes gu-form-pop {
from {
transform: scale(0);
transform-origin: center;
}
}
@keyframes gu-form-draw {
to {
stroke-dashoffset: 0;
}
}
@media (prefers-reduced-motion: reduce) {
.gu-form label {
transition: none;
}
.gu-form__field[data-error],
.gu-form--done circle {
animation: none;
}
.gu-form--done path {
animation: none;
stroke-dashoffset: 0;
}
}
```
You might also want
Next piece: Glide navOne-time code input
Components, intermediate
Password strength meter
Components, beginner
Date range picker
Components, advanced