When to use
- Likes, favourites and upvotes on posts, comments, products and listings.
- Anywhere a count should visibly respond to the reader.
When not to use
- Saving to a list or bookmarking: use a Button with a TextSwap label.
- Multiple reactions: use emoji reactions.
Installation
pnpm dlx shadcn add https://eased.dev/r/like-button.jsonnpx shadcn add https://eased.dev/r/like-button.jsonyarn dlx shadcn add https://eased.dev/r/like-button.jsonbunx --bun shadcn add https://eased.dev/r/like-button.jsonThe command adds Like Button, everything it builds on and Eased’s tokens to your project.
Install the dependencies
pnpm add motionnpm install motionyarn add motionbun add motionAdd the Eased pieces it builds on
Copy each item’s files the same way, or install them with the CLI:
Copy the source into your project
components/eased/like-button/like-button.tsxTSX "use client";import { AnimatePresence, motion, useAnimate } from "motion/react";import { useState, type ComponentProps } from "react";import { NumberRoll } from "../_primitives/number-roll/number-roll";import { bouncy, ease, snappy, useSafeReducedMotion } from "../lib/motion";import { press } from "../lib/press";export type LikeButtonProps = Omit<ComponentProps<"button">, "onClick" | "children" | "type"> & { /** Liked (controlled). */ liked?: boolean; /** Uncontrolled starting state. Default false. */ defaultLiked?: boolean; /** * Called with the next state. Return a promise to make the toggle optimistic: the heart and * count change at once, and roll back (without a burst) if the promise rejects. */ onLikedChange?: (liked: boolean) => void | Promise<unknown>; /** The count before this viewer's like is applied. Leave it out to hide the count. */ count?: number; /** Count formatting: `{ notation: "compact" }` turns 12,400 into 12K. */ format?: Intl.NumberFormatOptions; locale?: Intl.LocalesArgument; /** 32px (`sm`, default) or 36px (`md`). */ size?: "sm" | "md"; /** Accessible name. Default "Like". The count is read after it. */ "aria-label"?: string; className?: string;};const SIZES = { sm: { box: "h-control-sm gap-1.5 rounded-control-sm px-2 text-ui-sm", icon: 16 }, md: { box: "h-control-md gap-2 rounded-control px-2.5 text-ui", icon: 18 },} as const;/** Eight sparks around the heart, alternating long and short, on a ring that is never round-robin. */const SPARKS = Array.from({ length: 8 }, (_, i) => { const angle = (i / 8) * Math.PI * 2 - Math.PI / 2 + (i % 2 ? 0.2 : 0); return { angle, reach: i % 2 ? 13 : 17, size: i % 2 ? 3 : 4 };});const HEART = "M8 13.6s-5.6-3.3-5.6-7.3A3.1 3.1 0 0 1 8 4.4a3.1 3.1 0 0 1 5.6 1.9c0 4-5.6 7.3-5.6 7.3Z";/** * A heart that pops and bursts when liked and deflates quietly when unliked, with a count * that rolls. Optimistic: the state flips at once and rolls back if `onLikedChange` rejects. */export function LikeButton({ liked: controlled, defaultLiked = false, onLikedChange, count, format, locale, size = "sm", "aria-label": ariaLabel = "Like", className = "", disabled, ...rest}: LikeButtonProps) { const [own, setOwn] = useState(defaultLiked); const liked = controlled ?? own; const reduced = useSafeReducedMotion(); const [scope, animate] = useAnimate<HTMLSpanElement>(); const [burst, setBurst] = useState(0); // The count passed in includes or excludes this viewer's like as it was at mount. const [likedAtMount] = useState(liked); const shown = count === undefined ? undefined : count + (liked === likedAtMount ? 0 : liked ? 1 : -1); const s = SIZES[size]; const pop = (next: boolean) => { if (reduced) return; if (next) { // Up fast to 1.25, then back on the bouncy spring: the pop. Interruptible either way. animate(scope.current, { scale: 1.25 }, { duration: 0.12, ease: ease.enter }).then(() => animate(scope.current, { scale: 1 }, bouncy), ); setBurst((b) => b + 1); } else { animate(scope.current, { scale: 0.86 }, { duration: 0.1, ease: ease.standard }).then(() => animate(scope.current, { scale: 1 }, snappy), ); } }; const set = (next: boolean) => { if (controlled === undefined) setOwn(next); }; const toggle = () => { const next = !liked; set(next); pop(next); const result = onLikedChange?.(next); if (result && typeof (result as Promise<unknown>).then === "function") { (result as Promise<unknown>).catch(() => { set(!next); if (!reduced) animate(scope.current, { scale: [0.9, 1] }, snappy); }); } }; return ( <button type="button" {...rest} disabled={disabled} aria-pressed={liked} aria-label={shown === undefined ? ariaLabel : `${ariaLabel}, ${new Intl.NumberFormat(locale, format).format(shown)}`} onClick={toggle} data-liked={liked || undefined} className={`group relative inline-flex shrink-0 cursor-pointer items-center font-medium whitespace-nowrap tabular-nums select-none disabled:cursor-not-allowed disabled:opacity-50 ${ liked ? "text-(--like,var(--eased-danger-text))" : "text-text-muted hover-fine:text-text" } hover-fine:bg-surface-hover active:bg-surface-active ${press} ${s.box} ${className}`} > <span className="relative inline-flex shrink-0" style={{ width: s.icon, height: s.icon }}> {/* The burst: a ring that opens and fades, and eight sparks thrown outward. */} <AnimatePresence> {burst > 0 && liked && !reduced && ( <motion.span key={burst} aria-hidden className="pointer-events-none absolute inset-0" exit={{ opacity: 0, transition: { duration: 0 } }}> <motion.span className="absolute inset-0 rounded-pill border-2 border-(--like,var(--eased-danger))" initial={{ scale: 0.3, opacity: 0.9 }} animate={{ scale: 1.9, opacity: 0, borderWidth: 0 }} transition={{ duration: 0.45, ease: ease.enter }} /> {SPARKS.map((p, i) => ( <motion.span key={i} className={`absolute top-1/2 left-1/2 rounded-pill ${i % 2 ? "bg-(--like-alt,var(--eased-warning))" : "bg-(--like,var(--eased-danger))"}`} style={{ width: p.size, height: p.size, marginLeft: -p.size / 2, marginTop: -p.size / 2 }} initial={{ x: Math.cos(p.angle) * 6, y: Math.sin(p.angle) * 6, scale: 1, opacity: 1 }} animate={{ x: Math.cos(p.angle) * p.reach, y: Math.sin(p.angle) * p.reach, scale: 0, opacity: 0 }} transition={{ duration: 0.5, ease: ease.enter, delay: 0.06 }} /> ))} </motion.span> )} </AnimatePresence> <span ref={scope} className="relative inline-flex size-full"> <svg viewBox="0 0 16 16" className="size-full" aria-hidden fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinejoin="round"> <path d={HEART} /> </svg> {/* The fill grows from the heart's centre when liked, and shrinks back when not. */} <motion.svg viewBox="0 0 16 16" className="absolute inset-0 size-full" aria-hidden initial={false} animate={liked ? { scale: 1, opacity: 1 } : { scale: 0.4, opacity: 0 }} transition={reduced ? { duration: 0 } : liked ? snappy : { duration: 0.12, ease: ease.exit }} > <path d={HEART} fill="currentColor" stroke="currentColor" strokeWidth="1.5" strokeLinejoin="round" /> </motion.svg> </span> </span> {/* At least two digits wide, so hearts in a column of rows line up. */} {shown !== undefined && ( <span className="inline-flex min-w-[2ch]"> <NumberRoll value={shown} format={format} locale={locale} aria-hidden /> </span> )} </button> );}Keep the folder layout
The files import each other by relative path (
../lib/motion), so keep each one at the path shown, undercomponents/eased/.
Usage
import { LikeButton } from "@/components/eased/like-button/like-button";export function Example({ post }: { post: Post }) { return ( <LikeButton defaultLiked={post.likedByMe} count={post.likes} format={{ notation: "compact" }} onLikedChange={(liked) => fetch(`/api/posts/${post.id}/like`, { method: liked ? "POST" : "DELETE" })} /> );}API reference
LikeButton
| Prop | Type | Default | Description |
|---|---|---|---|
liked | boolean | Controlled state. | |
defaultLiked | boolean | false | Uncontrolled start. |
onLikedChange | (liked) => void | Promise | Return a promise for an optimistic toggle that rolls back on rejection. | |
count | number | The count as loaded (including or excluding this viewer's like as it was then). Omit to hide it. Live updates from others are fine. | |
format / locale | Intl.NumberFormatOptions / Intl.LocalesArgument | Count formatting. | |
size | "sm" | "md" | "sm" | 32px or 36px. |
aria-label | string | "Like" | The count is appended. |
Other button props pass through. Set --like (and --like-alt for alternate sparks) to recolour; defaults are the danger and warning tokens.
Keyboard
| Key | Action |
|---|---|
Enter / Space | Toggles the like. |
Accessibility
- A toggle button (
aria-pressed) named "Like, 1,286". The rolling digits are hidden from assistive tech. - The count reserves two digits, so hearts in a column of rows line up.
Motion
Liking scales the heart to 1.25 in 120ms, then the bouncy spring brings it back, while a ring opens and fades and eight sparks fly out and shrink over 500ms. The fill grows from the centre on the snappy spring. Unliking dips to 0.86 and springs back with no burst; the fill fades in 120ms. The count rolls in the direction of change. All of it is interruptible. Reduced motion: no pop, no burst, the fill and count change at once.
Notes for AI
Use it for any like/favourite toggle with a count. Pass the server's count and the viewer's initial state; return the request promise from onLikedChange to get optimistic rollback for free.
Do not add one to the count yourself when liked; the component applies the viewer's change. Do not wrap it in another button.
Related components
- Animated Number Counter: the count's engine.
- Animated Button: for actions that are not toggles.
- Keyboard Shortcut Key (Kbd)