Number 86: Label that steps into the outline
Specification
- Catalogue
- No. 86 of 110
- Section
- Forms and inputs
- Interaction
- Type
- Element
- Input, Form
- Preview
- Live component
- Imports
- react 19.3.1
- Added
Notes
The label sits inside the field until it has focus or a value, then lifts into a gap that opens in the top edge of the outline.
Forms are on every client site and this is the cleanest version of the pattern: the outline parts from the middle outward as the label arrives, which is the detail most copies miss, so the label never sits on top of a line.
The label lifts 26px and scales to 0.78 from its left edge over 420ms cubic-bezier(.22, 1, .36, 1); its colour and the outline's ink change over 220ms. The outline is an SVG drawn whole; only the notch moves, as two short paths retracted from the middle by a dash offset, and the notch is measured from the label's own width. Showing the password passes the text through a 3px blur over 260ms instead of snapping from dots to letters, and the eye's two icons trade places with a fade, a 0.7 scale and a blur. The eye is a 44px target and keeps focus in the field. Inputs are 16px, with proper email and password types and autocomplete. Under reduced motion the label move shortens to 160ms rather than vanishing, because the move says where the label went.
Code
React and plain CSS"use client";
import { useId, useLayoutEffect, useRef, useState, type CSSProperties } from "react";
import type { LivePreviewProps } from "@/components/live/types";
import { PieceStage } from "@/components/pieces/stage/PieceStage";
import type { Tour } from "@/components/pieces/stage/tour";
import "./floating-label.css";
const W = 300;
const H = 52;
const R = 14;
/** The lifted label's size against its resting one. */
const LIFT = 0.78;
/** The stroke sits half a stroke inside the box, so the svg never clips it. */
const IN = 0.75;
/** The outline as three parts: the two sides from the notch round to the bottom middle, and the notch as two halves. */
function outline(labelWidth: number) {
const lx = R + 4;
const x0 = lx - 5;
const x1 = lx + labelWidth * LIFT + 5;
const a = R - IN;
const right = W - IN;
const bottom = H - IN;
const mid = W / 2;
const nm = (x0 + x1) / 2;
return {
lx,
right: `M${x1},${IN} L${W - R},${IN} A${a},${a} 0 0 1 ${right},${R} L${right},${H - R} A${a},${a} 0 0 1 ${W - R},${bottom} L${mid},${bottom}`,
left: `M${x0},${IN} L${R},${IN} A${a},${a} 0 0 0 ${IN},${R} L${IN},${H - R} A${a},${a} 0 0 0 ${R},${bottom} L${mid},${bottom}`,
gapL: `M${nm},${IN} L${x0},${IN}`,
gapR: `M${nm},${IN} L${x1},${IN}`,
};
}
function Field({ kind }: { kind: "email" | "password" }) {
const id = useId();
const secret = kind === "password";
const label = secret ? "Password" : "Email";
const [focus, setFocus] = useState(false);
const [value, setValue] = useState("");
const [show, setShow] = useState(false);
const [flip, setFlip] = useState(0);
const lab = useRef<HTMLLabelElement>(null);
const [lw, setLw] = useState(40);
// The notch is the label's own width, so it is measured.
useLayoutEffect(() => {
if (lab.current) setLw(lab.current.offsetWidth);
}, [label]);
const up = focus || value.length > 0;
const o = outline(lw);
return (
<div className="fl" data-up={up} data-focus={focus} data-filled={value.length > 0}>
<div className="fl-box" style={{ width: W, height: H, borderRadius: R, "--fl-x": `${o.lx}px` } as CSSProperties}>
<svg className="fl-ring" width={W} height={H} viewBox={`0 0 ${W} ${H}`} aria-hidden="true">
<path d={o.right} />
<path d={o.left} />
<path className="fl-gap" d={o.gapL} pathLength={1} />
<path className="fl-gap" d={o.gapR} pathLength={1} />
</svg>
<label ref={lab} className="fl-label" htmlFor={id}>
{label}
</label>
<input
id={id}
className="fl-input"
data-flip={flip === 0 ? undefined : flip % 2}
type={secret && !show ? "password" : secret ? "text" : "email"}
autoComplete={secret ? "current-password" : "email"}
autoCapitalize="off"
autoCorrect="off"
spellCheck={false}
value={value}
onChange={(e) => setValue(e.target.value)}
onFocus={() => setFocus(true)}
onBlur={() => setFocus(false)}
style={{ paddingRight: secret ? 52 : o.lx }}
/>
{secret && (
<button
type="button"
className="fl-eye"
data-show={show}
aria-label={show ? "Hide password" : "Show password"}
aria-pressed={show}
// The eye toggles what is being typed; focus stays in the field.
onPointerDown={(e) => e.preventDefault()}
onClick={() => {
setShow((s) => !s);
setFlip((f) => f + 1);
}}
>
<svg viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
<path d="M2 12s3.5-7 10-7 10 7 10 7-3.5 7-10 7S2 12 2 12z" />
<circle cx="12" cy="12" r="3" />
</svg>
<svg viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
<path d="M10.6 5.1A10.4 10.4 0 0 1 12 5c6.5 0 10 7 10 7a17.6 17.6 0 0 1-2.6 3.6M6.6 6.6A17.4 17.4 0 0 0 2 12s3.5 7 10 7a9.7 9.7 0 0 0 5.4-1.6M9.9 9.9a3 3 0 0 0 4.2 4.2M3 3l18 18" />
</svg>
</button>
)}
</div>
</div>
);
}
function FloatingLabelDemo() {
return (
<div className="dm fl-demo">
<Field kind="email" />
<Field kind="password" />
</div>
);
}
// What a hovered card shows: an address typed, a password typed, and the password shown.
const TOUR: Tour = [
{ input: ".fl-input[type='email']", value: "ana@studio.pt", wait: 900 },
{ input: ".fl-input[type='password']", value: "kiln-41-glaze", wait: 900 },
{ click: ".fl-eye", wait: 1100 },
];
export default function FloatingLabelEntry(props: LivePreviewProps) {
return (
<PieceStage {...props} label="Label that steps into the outline" tour={TOUR}>
<FloatingLabelDemo />
</PieceStage>
);
}