Back to Buttons
Components / Buttons
Like Button
Free Buttons React component — LikeButton. MIT licensed, copy-paste ready for Next.js and Tailwind CSS.
Heart toggle that pops and scatters particles on the way up, with a live count. Pill fills rose when active — no glow, no gradient.
Preview
Setup
How to use
Free for personal and commercial use. No UI attribution required. Include the MIT copyright notice when copying component source into your project. Read the MIT license.
- 1
Run in your terminal:
$npm install clsx tailwind-merge lucide-react - 2
Copy
lib/cn.tsbelow. Skip if you already havecn(). - 3
Copy the code below and create
components/buttons/like-button.tsxin your project. - 4
Import and render:
Exampleimport { LikeButton } from "@/components/buttons/like-button";
lib/cn.ts
import { type ClassValue, clsx } from "clsx";import { twMerge } from "tailwind-merge";export function cn(...inputs: ClassValue[]) {return twMerge(clsx(inputs));}
components/buttons/like-button.tsx
"use client";import {forwardRef,useEffect,useState,type ComponentPropsWithoutRef,} from "react";import { cn } from "@/lib/cn";import { Heart } from "lucide-react";const PARTICLES = [{ x: 0, y: -22 },{ x: 18, y: -14 },{ x: 22, y: 4 },{ x: 13, y: 20 },{ x: -13, y: 20 },{ x: -22, y: 4 },{ x: -18, y: -14 },];type BurstProps = Readonly<{ color: string }>;// Particles fly outward once on mount using a rAF-triggered transition.function Burst({ color }: BurstProps) {const [out, setOut] = useState(false);useEffect(() => {const id = globalThis.requestAnimationFrame(() => setOut(true));return () => globalThis.cancelAnimationFrame(id);}, []);return (<spanaria-hiddenclassName="pointer-events-none absolute inset-0 flex items-center justify-center">{PARTICLES.map((particle, index) => (<spankey={index}className="absolute top-1/2 left-1/2 size-1.5 rounded-full transition-all duration-500 ease-out"style={{backgroundColor: color,opacity: out ? 0 : 1,transform: out? `translate(calc(-50% + ${particle.x}px), calc(-50% + ${particle.y}px)) scale(0.2)`: "translate(-50%, -50%) scale(1)",}}/>))}</span>);}export type LikeButtonProps = Readonly<{label?: string;likedLabel?: string;defaultLiked?: boolean;count?: number;} & ComponentPropsWithoutRef<"button">>;// Like — heart pops and scatters particles when toggled on.export const LikeButton = forwardRef<HTMLButtonElement, LikeButtonProps>(({className,label = "Like",likedLabel = "Liked",defaultLiked = false,count = 128,onClick,...props},ref,) => {const [liked, setLiked] = useState(defaultLiked);const [burstKey, setBurstKey] = useState(0);const handleClick = (event: React.MouseEvent<HTMLButtonElement>) => {onClick?.(event);setLiked((prev) => {if (!prev) setBurstKey((key) => key + 1);return !prev;});};const shown = count + (liked && !defaultLiked ? 1 : 0);return (<buttonref={ref}type="button"aria-pressed={liked}aria-label={liked ? likedLabel : label}data-slot="like-button"onClick={handleClick}className={cn("inline-flex h-10 cursor-pointer items-center gap-2 rounded-full border pr-4 pl-3 font-sans text-sm font-medium transition-colors duration-200 select-none",liked? "border-rose-200 bg-rose-50 text-rose-600": "border-neutral-200 bg-white text-neutral-600 hover:border-neutral-300",className,)}{...props}><span className="relative flex size-5 items-center justify-center">{liked && <Burst key={burstKey} color="#FB7185" />}<Heartsize={16}strokeWidth={2}className={cn("relative z-10 transition-transform duration-200 ease-out",liked ? "scale-110 text-rose-500" : "scale-100",)}fill={liked ? "#F43F5E" : "none"}/></span><span className="tabular-nums">{shown.toLocaleString()}</span></button>);},);LikeButton.displayName = "LikeButton";