仕組み
The label is absolutely positioned inside the box and rises with translateY and scale(0.75) on focus or when filled, without changing font-size. Whether there is a value is left to CSS :placeholder-shown, so controlled, uncontrolled and autofilled inputs all float correctly with no JS state. Helper and error text share one row that expands with grid rows 0fr to 1fr. Clearing uses the native setter plus an input event, so onChange always fires.
向いている場面
- Sign-in, sign-up and similar short forms where space is tight: the label doubles as the placeholder.
- Mobile web forms: the 56px field is easy to hit and 16px text avoids iOS zoom.
- Account-style inputs that need consistent clear and show-password behavior.
向かない場面
- Long admin forms: shrunken labels are harder to scan, so use labels above the field.
- Very long labels: they get truncated once floated, so use a label above.
- Multi-line text: this component only handles a single-line input.
使い方
import { useState } from 'react'
import { FloatingLabelInput } from './FloatingLabelInput.jsx'
export function SignInFields() {
const [id, setId] = useState('')
const [pwd, setPwd] = useState('')
return (
<>
<FloatingLabelInput
label="Member ID"
type="tel"
clearable
value={id}
onChange={(e) => setId(e.target.value)}
error={id && id.length !== 8 ? 'Enter your 8-digit member ID' : undefined}
/>
<FloatingLabelInput
label="Password"
type="password"
helperText="8–20 characters, with letters and numbers"
value={pwd}
onChange={(e) => setPwd(e.target.value)}
/>
</>
)
}プロパティ
| プロパティ | 型 | 既定値 | 説明 |
|---|---|---|---|
label | ReactNode | — | Label (required), linked to the input through <label for>. |
helperText | ReactNode | — | Helper text under the box. |
error | ReactNode | — | Error: text replaces the helper text and turns everything red; true only turns it red. |
variant | 'outline' | 'filled' | 'outline' | Look: white with a soft hairline ring, or filled light grey. Both turn white with an accent line and halo on focus. |
clearable | boolean | false | Shows a clear button when there is a value; Esc also clears. Not available when read-only or disabled. |
onClear | () => void | — | Called after clearing; onChange also receives the empty value. |
revealable | boolean | true for type password | Whether a password field has a show / hide toggle. |
type | string | 'text' | Native input type; password adds the reveal toggle automatically. |
placeholder | string | — | The real placeholder text, shown only once the label has floated (on focus). |
required | boolean | — | Required: shows a red * after the label (hidden from screen readers, which read the native required). |
labels | object | — | Replaces built-in text (clear, showPassword, hidePassword). Shallow-merged over the English defaults. |
ref | Ref<HTMLInputElement> | — | Forwarded to the <input>, so it works with react-hook-form's register. |
...rest | InputHTMLAttributes | — | className and style go on the outer element; other native attributes (value, onChange, name, autoComplete, inputMode…) go to the <input>. |
アクセシビリティ
- <label for> ties the label to the input, so clicking it focuses the field; the required asterisk is aria-hidden and screen readers read the native required.
- Errors set aria-invalid, and helper or error text is linked with aria-describedby inside an aria-live="polite" region, so new errors are announced.
- The show-password button is focusable with aria-pressed and a name that switches between show and hide; the clear button is out of the Tab order and keyboard users press Esc (propagation is stopped only when there is a value to clear).
- Buttons have a focus ring; a focused field shows an accent line and halo around the whole box.
- With reduced motion the label jumps, the halo appears at once and messages do not slide; only color and opacity change.
モーション
Label rises in 280ms ease-out; on focus the fill turns white in 160ms and the accent line and 4px halo grow in over 220ms; messages slide in over 240ms; the clear button pops in over 240ms and leaves in 100ms; password icons cross-fade in 240ms; buttons press to 0.92.
プロジェクトに追加
プロンプトを Codex または Claude Code に貼り付けます。エージェントが公開ソースを読み、書き込む内容を示したうえで、既存のコンポーネントの隣に追加します。
FloatingLabelInput.jsxfloating-label-input.csstokens.cssREADME.md
2 つのファイルを一緒に導入します。スタイルは tokens.css の変数を読み、予備の値も持つため、単体でも表示できます。





