本文へ移動
日本語

仕組み

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.

向いている場面

  • 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.

向かない場面

  • 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.

使い方

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) }} />

プロパティ

プロパティ型既定値説明
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.

アクセシビリティ

  • 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.

モーション

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

プロジェクトに追加

プロンプトを Codex または Claude Code に貼り付けます。エージェントが公開ソースを読み、書き込む内容を示したうえで、既存のコンポーネントの隣に追加します。

LikeButton.jsxlike-button.csstokens.cssREADME.md

2 つのファイルを一緒に導入します。スタイルは tokens.css の変数を読み、予備の値も持つため、単体でも表示できます。

ボタン
「オーロラ縁ボタン」は Pro に含まれます。解除するとプレビューが表示されます。
「かじりボタン」は Pro に含まれます。解除するとプレビューが表示されます。