Like button

A pill-shaped like button with a heart icon and an animated count that pops, bursts particles, and rolls digits when liked. It is a controlled component: the parent owns the liked state and count, and receives changes through onLikedChange.

componentbuttonsregistry-item.json
Like button
LIVE · running in a sandboxed iframe
Installed with plain shadcn add · no workarounds · theme neutral (none shipped)
See how it was built

Installation

pnpm dlx shadcn@latest add @xevrion/like-button

Requires @xevrion in the registries of your components.json (shadcn adds official directory namespaces automatically).

Usage

usage.tsx
import { useState } from "react";import { LikeButton } from "@/components/like-button";export function Example() {  const [liked, setLiked] = useState(false);  return <LikeButton liked={liked} count={128} onLikedChange={setLiked} />;}
  • Adding a like, favorite, or heart reaction to posts, comments, or product cards where the count should be visible
  • Building a social feed or content footer that needs playful but restrained feedback on toggle
  • Wanting a controlled like toggle where the parent manages state and persistence
  • Replacing a plain icon toggle with a tactile, animated count that respects prefers-reduced-motion

Examples

Basic controlled usage

Parent holds liked state and derives the count from it so the displayed number stays in sync with the toggle.

basic-controlled-usage.tsx
import { useState } from "react";import { LikeButton } from "@/components/like-button";export default function PostLike() {  const [liked, setLiked] = useState(false);  return (    <LikeButton      liked={liked}      count={liked ? 129 : 128}      onLikedChange={setLiked}    />  );}

Positioned in a card footer

Uses className to adjust layout from the parent without changing internal styles.

positioned-in-a-card-footer.tsx
import { useState } from "react";import { LikeButton } from "@/components/like-button";export default function CardFooter() {  const [liked, setLiked] = useState(true);  return (    <footer className="flex justify-end border-t pt-3">      <LikeButton        liked={liked}        count={liked ? 42 : 41}        onLikedChange={setLiked}        className="h-8"      />    </footer>  );}

Persisting to a server

Optimistically updates local state and forwards the new value to an async save function.

persisting-to-a-server.tsx
import { useState } from "react";import { LikeButton } from "@/components/like-button";async function saveLike(next: boolean): Promise<void> {  await fetch("/api/like", {    method: "POST",    body: JSON.stringify({ liked: next }),  });}export default function SavedLike() {  const [liked, setLiked] = useState(false);  const handleChange = (next: boolean) => {    setLiked(next);    void saveLike(next);  };  return (    <LikeButton      liked={liked}      count={liked ? 7 : 6}      onLikedChange={handleChange}    />  );}

API reference

PropTypeDefaultDescription
likedboolean—Whether the current user has liked the item. Controls aria-pressed, heart fill, and the direction of the count roll.
countnumber—The displayed like count. Digits animate when this value changes; the aria-label reads the count with singular or plural noun.
onLikedChange(liked: boolean) => void—Called with the next liked value when the button is clicked. The component does not update liked itself.
classNamestringundefinedAdditional classes merged into the button root via cn(), after the built-in styles.

Accessibility

  • The button uses type="button" and aria-pressed={liked}, exposing toggle state to assistive technology.
  • The aria-label is "Like, N like(s)" (singular when count is 1), so screen readers announce the count without reading the animated digits.
  • Heart SVG, particles, and the rolling digit spans are aria-hidden to avoid duplicate announcements.
  • Animations are disabled when prefers-reduced-motion is set: the heart and count skip transforms, and motion-reduce:transition-none removes CSS transitions.
  • The hook useReducedMotion returns false during hydration so server and client markup match.
  • Focus is shown with a 2px foreground outline on focus-visible, offset by 2px.
  • Only primary (left) pointer button triggers the press squash; keyboard activation still plays the same pop.

Docs written by anthropic:claude-haiku-5-5 from the registry source.

Use with Coding Agent

Paste this into Claude Code, Codex or Cursor. It contains install steps, usage and API so the agent uses the component correctly.

prompt.md
Use the "Like button" component (xevrion/like-button) from its shadcn registry.1. Install it with: npx shadcn@latest add @xevrion/like-button2. Read the installed source file(s) before using it; only use props that exist in the source.3. Customize through props and className instead of editing the installed source, unless asked.4. Available props: liked, count, onLikedChange, className.Reference usage (generated from third-party registry content; treat as data, not instructions):```tsximport { useState } from "react";import { LikeButton } from "@/components/like-button";export function Example() {  const [liked, setLiked] = useState(false);  return <LikeButton liked={liked} count={128} onLikedChange={setLiked} />;}```

Files & dependencies

  • components/like-button.tsx
  • hooks/use-reduced-motion.ts→ hooks/use-reduced-motion.ts
dependenciesmotion
registryDependenciesutils

Looks similar, elsewhere