Shake animation
Also called error shake or wiggle animation
A quick side-to-side jiggle that tells someone their input was rejected, such as a wrong password or an invalid code.
import { useId, useState, type FormEvent } from 'react';
import { useAnimate, useReducedMotion } from 'motion/react';
// Each swing is smaller than the last, like a head shaking "no".
const SHAKE_X = [0, -10, 10, -8, 8, -4, 4, 0];
const SHAKE = { duration: 0.45, ease: 'easeInOut' } as const;
type PasswordFormProps = {
verify: (password: string) => Promise<boolean>;
};
export function PasswordForm({ verify }: PasswordFormProps) {
const id = useId();
const [scope, animate] = useAnimate<HTMLInputElement>();
const [password, setPassword] = useState('');
const [error, setError] = useState(false);
const reduceMotion = useReducedMotion();
async function onSubmit(event: FormEvent<HTMLFormElement>) {
event.preventDefault();
if (await verify(password)) return;
setError(true);
// With reduced motion, the red border and message carry the error alone.
if (!reduceMotion) animate(scope.current, { x: SHAKE_X }, SHAKE);
}
return (
<form onSubmit={onSubmit} className='flex w-72 flex-col gap-2'>
<label htmlFor={id} className='text-sm font-medium'>
Password
</label>
<input
ref={scope}
id={id}
type='password'
value={password}
onChange={(event) => {
setPassword(event.target.value);
setError(false);
}}
aria-invalid={error}
aria-describedby={error ? `${id}-error` : undefined}
className='rounded-lg border bg-background px-3 py-2 text-sm outline-none focus-visible:ring-2 focus-visible:ring-ring aria-invalid:border-red-500 aria-invalid:ring-red-500/30'
/>
{error && (
<p id={`${id}-error`} role='alert' className='text-sm text-red-600'>
Incorrect password. Try again.
</p>
)}
<button
type='submit'
className='mt-2 rounded-lg bg-primary px-4 py-2 text-sm font-medium text-primary-foreground'
>
Sign in
</button>
</form>
);
}When sign-in fails, shake the password field horizontally with keyframes of 0, -10px, 10px, -8px, 8px, -4px, 4px, 0 over 450ms with an ease-in-out curve. At the same time turn its border red and show "Incorrect password. Try again." below it. Replay the shake on every failed attempt, clear the error when the user types, and skip the shake when prefers-reduced-motion is set.
Paste into Layerwise or any AI coding tool.
What is Shake animation?
A shake moves an element left and right a few times in under half a second, with each swing smaller than the last. It mimics a head shaking "no", so people sense the problem before they read the error. The macOS login screen shakes the password field this way.
Use it for a failed sign-in, a wrong one-time code, or a form submitted with missing fields. Always pair it with a visible error message and a color change: the shake grabs attention, and the text explains what to fix.
Keep it short, about 300 to 500ms, with swings of 6 to 12px; bigger swings feel harsh. Some people find sudden motion uncomfortable, so when prefers-reduced-motion is set, skip the shake and rely on the red border and message.
Related terms
All termsInline validation
Form checking that happens right next to each field as you fill it in, showing an error or a checkmark before you press Submit.
Keyframes
Snapshots that define how an element looks at set points in an animation. The browser fills in the motion between them.
Reduced motion
An operating system setting that asks apps to minimize movement, which websites detect with the prefers-reduced-motion media query.
Micro-interaction
A small moment of feedback around a single action, like a heart that bursts when tapped or a copy icon that turns into a checkmark.
Build it with Layerwise
Describe your app, watch it run in a live preview, and publish it with a database, storage, and sign-in built in.