Ir al contenido
Español

Cómo funciona

On like, the heart squashes to 0.72, overshoots to 1.24 and settles in 560ms while the colour grows from the centre; a ring and particles burst outward. Particle angles are deterministic: evenly spaced, with odd ones closer and later, so there is no randomness. Digits align from the right and only changed ones slide. Animation runs only on a user click, never when external data syncs. Works controlled or uncontrolled.

Úsalo para

  • Likes under posts, comments and articles.
  • Favourites on a product or detail page; a star icon works with the same motion.
  • Moments where a small delight encourages interaction.

Busca otra opción cuando

  • Checkboxes or flags in admin tables: celebration looks frivolous in productivity tools.
  • Dense lists with dozens of visible buttons: set particles to 0 and keep only the bounce.
  • Results the server must confirm and may reject: use controlled mode and roll back on failure.

Uso

import { useState } from 'react'
import { LikeButton } from './LikeButton.jsx'

// Uncontrolled: a like adds 1 to count; a new count from the API replaces it, with no double counting
<LikeButton count={post.likes} defaultLiked={post.liked} onChange={(liked) => api.like(post.id, liked)} />

// Controlled
const [liked, setLiked] = useState(false)
const [count, setCount] = useState(1286)
<LikeButton liked={liked} count={count} onChange={(l, c) => { setLiked(l); setCount(c) }} />

Propiedades

PropiedadTipoPor defectoQué hace
likedboolean—Controlled: whether it is liked. Use with onChange.
defaultLikedbooleanfalseUncontrolled: initial liked state.
onChange(liked, count) => void—Called when the state changes; the second argument is the new count.
countnumber—The count. Shown as is when controlled; uncontrolled adds ±1 on top, and a changed count resets that. Omit it to show only the icon.
particlesnumber8Number of burst particles. 0 turns them off but keeps the ring.
colorstring'#f43f5e'Colour when liked. You can also set the CSS variable --ds-like-color.
sizenumber22Heart size in px; the ring and particles scale with it.
labelstring'Like'Accessible name, joined with the count as “Like, 1286”.
formatCount(n) => stringStringCount format, for example showing 12000 as 12k.
...restButtonHTMLAttributes—Other attributes go to the <button>. Call e.preventDefault() in onClick to block a toggle, for example when the user is signed out.

Accesibilidad

  • aria-pressed expresses liked or not.
  • aria-label includes the count (“Like, 1286”); the sliding digits are aria-hidden, so intermediate values are never read.
  • A focus ring shows on :focus-visible; the minimum target is 44×44.
  • Reduced motion removes particles, ring, bounce and digit sliding, leaving an instant state change.

Movimiento

Like bounce 560ms, fill 360ms with spring easing, ring 480ms, particles 640ms. Unlike shrinks in 240ms. Digits enter in 300ms and leave in 210ms.

Añádelo a tu proyecto

Copia la instrucción en Codex o Claude Code. El agente lee el código publicado, te muestra lo que va a escribir y añade el componente junto a los tuyos.

LikeButton.jsxlike-button.csstokens.cssREADME.md

Los dos archivos se instalan juntos. La hoja de estilos lee las variables de tokens.css y lleva valores de reserva, así que también funciona sola.

Botones
Botón de borde aurora viene con Pro. La vista previa aparece al desbloquearlo.
Botón con mordiscos viene con Pro. La vista previa aparece al desbloquearlo.