Forms
One-time code
A code received by SMS or email, one box per digit.
Example
The boxes fill one by one while typing; when the code is complete, they turn green one after another, in a wave, and onCrComplete gives the code in event.detail. length changes the number of digits (6 by default).
'use client';
import { useState } from 'react';
import { CrOtp } from '@cristal-ui/react';
export default function OtpBasic() {
const [code, setCode] = useState('');
return (
<>
<CrOtp label="Code sent by text message" name="code" hint="6 digits" onCrComplete={(event) => setCode(event.detail)} />
<p role="status">{code && `Full code: ${code}`}</p>
</>
);
}Animations
animation picks the effect for typing and for the complete code.
'use client';
import { useState } from 'react';
import { CrBox, CrButton, CrOtp, CrSegmented } from '@cristal-ui/react';
const animations = [
['pop', 'Pop'],
['bounce', 'Bounce'],
['stretch', 'Stretch'],
['spin', 'Spin'],
['merge', 'Merge'],
['zoom', 'Zoom'],
['tint', 'Tint'],
] as const;
export default function OtpAnimations() {
const [animation, setAnimation] = useState(0);
const [verified, setVerified] = useState('');
return (
<CrBox display="flex" direction="column" gap="xl" align="center">
<CrSegmented label="Animation" fit value={String(animation)} onCrChange={(event) => setAnimation(Number(event.detail))}>
{animations.map(([value, label], index) => (
<option key={value} value={index}>
{label}
</option>
))}
</CrSegmented>
<form
style={{ display: 'grid', gap: 24, padding: 32, borderRadius: 'var(--cr-radius-l)', background: 'var(--cr-color-surface)', boxShadow: 'var(--cr-shadow-m)' }}
onSubmit={(event) => {
event.preventDefault();
setVerified(String(new FormData(event.currentTarget).get('code')));
}}
>
<div>
<h3 style={{ margin: 0 }}>Enter the code we sent</h3>
<p style={{ margin: '4px 0 0', color: 'var(--cr-color-text-muted)' }}>We sent it to •• •• •• 42.</p>
</div>
{/* key: switching animation starts again from an empty code, to see it from the first digit. */}
<CrOtp key={animation} label="Verification code" name="code" required animation={animations[animation][0]} onCrInput={() => setVerified('')} />
<CrButton type="submit" fullWidth>
Verify
</CrButton>
<p role="status" style={{ margin: 0 }}>
{verified && `Code ${verified} verified.`}
</p>
</form>
</CrBox>
);
}animation | Effect |
|---|---|
pop | the digit grows in; when complete, the boxes turn green in a wave (default) |
bounce | the digit drops into its box, which squashes and bounces back; when complete, the boxes hop one after another |
stretch | a single focus ring stretches from box to box while typing (the leading edge moves first); when complete, it wraps the whole code, in green |
spin | digits roll like a slot-machine reel before stopping; when complete, every digit spins once more, in a wave |
merge | when complete, the boxes slide together into a single rounded green bar |
zoom | the digit zooms in big and lands; when complete, a shine sweeps across the boxes as they turn green |
tint | the box fills with colour from the bottom as its digit lands; when complete, the fill turns green, starting from the last box |
A pasted code arrives as a quick cascade, digit after digit; deleting a digit undoes the complete state. When the user asks for less motion (prefers-reduced-motion), nothing moves: digits and colours change instantly.
error-animation animates the error when it appears and on every rejected submit (zigzag, badge, echo…): see Animated error.
Accessibility
- A single real field, invisible and laid over the boxes: one label for screen readers, and both pasting ("123 456") and autofill of the code received by SMS (
autocomplete="one-time-code") work. - Only digits are kept; the numeric keyboard opens on mobile.
- An incomplete code blocks form submission: "Enter the 6 digits of the code.".
- Animations only touch the drawn boxes, hidden from screen readers: whatever
animationis, the field that is read stays the same. Withstretch, the ring following the active box is the focus indicator; in forced colours it takes the system highlight colour, and a complete code gets a thicker border.
API
Properties
| Property | Type | Default | Description |
|---|---|---|---|
animation | "bounce" | "merge" | "pop" | "spin" | "stretch" | "tint" | "zoom" | 'pop' | Animation de la saisie : pop (le chiffre apparaît, vague verte à la fin, par défaut), bounce (le chiffre tombe, la case s'écrase et rebondit), stretch (un seul anneau de focus s'étire de case en case, puis entoure tout le code), spin (les chiffres défilent comme une machine à sous), merge (à la fin, les cases se rejoignent en une seule barre), zoom (le chiffre arrive en grand, un reflet balaie le code complet) ou tint (la case se remplit de couleur par le bas, verte à la fin). |
disabled | boolean | false | Désactive le champ. |
error | string | — | Message d'erreur ; remplace le message automatique. |
errorAnimation | "drop" | "echo" | "jitter" | "none" | "pop" | "wobble" | "zigzag" | 'none' | Animation de l'erreur quand elle apparaît et à chaque envoi refusé : none (par défaut), zigzag, pop, drop, echo, jitter ou wobble. |
hint | string | — | Texte d'aide, lu par les lecteurs d'écran. |
label (obligatoire) | string | — | Libellé affiché au-dessus des cases. |
length | number | 6 | Nombre de chiffres. |
name | string | — | Nom envoyé avec le formulaire. |
required | boolean | false | Rend la saisie obligatoire. |
value | string | '' | Code saisi (chiffres). |
Events
| React | event.detail | Description |
|---|---|---|
onCrComplete | string | Émis quand toutes les cases sont remplies ; detail : le code. |
onCrInput | string | Émis à chaque chiffre ; detail : le code. |
CSS parts
To style it from outside: cr-otp::part(name) { … }
| Part | Description |
|---|---|
box | Une case. |
label | Le libellé. |