Keyframes
Also called keyframe animation or @keyframes
Snapshots that define how an element looks at set points in an animation. The browser fills in the motion between them.
import { useEffect, useRef } from 'react';
import { useAnimate, useReducedMotion } from 'motion/react';
// The angle at each keyframe, and when (0 to 1) each one is reached.
const ROTATE = [0, 14, -12, 8, -4, 0, 0];
const TIMES = [0, 0.15, 0.3, 0.45, 0.6, 0.75, 1];
export function NotificationBell({ unread }: { unread: number }) {
const [scope, animate] = useAnimate<HTMLSpanElement>();
const reduceMotion = useReducedMotion();
const previous = useRef(unread);
// Ring once each time the unread count goes up.
useEffect(() => {
const hasNew = unread > previous.current;
previous.current = unread;
if (!hasNew || reduceMotion) return;
animate(
scope.current,
{ rotate: ROTATE },
{ duration: 0.9, times: TIMES, ease: 'easeInOut' }
);
}, [unread, reduceMotion, animate, scope]);
return (
<button
type='button'
aria-label={`Notifications, ${unread} unread`}
className='grid size-10 place-items-center rounded-full hover:bg-muted'
>
{/* Swing from the top, where a bell hangs. */}
<span ref={scope} className='block origin-top'>
<svg
viewBox='0 0 24 24'
fill='none'
stroke='currentColor'
strokeWidth={2}
className='size-5'
aria-hidden
>
<path d='M6 8a6 6 0 0 1 12 0c0 7 3 9 3 9H3s3-2 3-9' />
<path d='M10.3 21a1.94 1.94 0 0 0 3.4 0' />
</svg>
</span>
</button>
);
}Add a CSS @keyframes animation that rings the notification bell when a new message arrives: rotate it to 14deg at 15%, -12deg at 30%, 8deg at 45%, -4deg at 60%, and back to 0 at 75%, over 900ms with ease-in-out, rotating from the top center. Play it once per message and skip it when prefers-reduced-motion is set.
Paste into Layerwise or any AI coding tool.
Keyframes explained
A keyframe pins down an element’s style at one moment of an animation, such as its position, rotation, or opacity at 0%, 50%, and 100% of the way through. The browser works out every frame in between, so a handful of keyframes can describe a path with several turns.
In CSS you write keyframes in an @keyframes rule and attach them with the animation property. In Motion for React you pass an array of values, like x: [0, 100, 0], and an optional times array that says when each value is reached.
Reach for keyframes when an animation needs more than a start and an end: a bounce, a shake, a pulse, or a path with turns. For a simple change from one state to another, a transition is shorter to write and easier to interrupt.
Often confused with
- Tweening
- Keyframes are the fixed points you define. Tweening is the in-between motion the computer generates from them.
Related terms
All termsTweening
Generating the in-between frames of an animation from a start and an end state, so an element moves smoothly from one to the other.
Shake animation
A quick side-to-side jiggle that tells someone their input was rejected, such as a wrong password or an invalid code.
Looping animation
An animation that plays more than once, either a set number of times or forever, set in CSS with animation-iteration-count.
Easing
A curve that controls how an animation’s speed changes over time, so motion can start slow, land softly, or keep one steady pace.
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.