Like button animation: a heart that bursts

A heart that pops when you tap it, sends out six dots and rolls the count up by one. Plus a bookmark that drops into place.

UI loop

A like button that bursts

Sundry Millsundrymill.com/library
Like button animation: a heart that bursts

Everything in the preview is live. Try it.

The code, one by one

Heart with a burst

The heart pops, six dots fly out, and the count rolls up by one. Click again to take the like back.

HTML
<button class="lk-heart" type="button" aria-pressed="false" data-press>
  <svg viewBox="0 0 24 24" aria-hidden="true"><path d="M12 20.5s-7.5-4.6-9.3-9.2C1.5 8 3.6 4.5 7.1 4.5c2 0 3.6 1.1 4.9 2.9 1.3-1.8 2.9-2.9 4.9-2.9 3.5 0 5.6 3.5 4.4 6.8-1.8 4.6-9.3 9.2-9.3 9.2z"/></svg>
  <span class="lk-burst" aria-hidden="true"><i style="--a:0deg"></i><i style="--a:60deg"></i><i style="--a:120deg"></i><i style="--a:180deg"></i><i style="--a:240deg"></i><i style="--a:300deg"></i></span>
  <span class="lk-hidden">Like,</span> <span class="lk-num" data-count="41">41</span>
</button>
CSS
.lk-heart {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 44px;
  padding: 0 16px 0 12px;
  border: 0;
  border-radius: 999px;
  background: #FFFFFF;
  color: #17202C;
  font: inherit;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  cursor: pointer;
  box-shadow: inset 0 0 0 2px #E5DED3;
}
.lk-heart svg {
  width: 24px;
  height: 24px;
  overflow: visible;
  fill: transparent;
  stroke: #17202C;
  stroke-width: 2;
  stroke-linejoin: round;
  transition: fill .2s, stroke .2s;
}
.lk-heart[aria-pressed="true"] svg {
  fill: #D93A4A;
  stroke: #D93A4A;
  animation: lk-pop .4s cubic-bezier(.34, 1.56, .64, 1);
}
.lk-burst { position: absolute; left: 24px; top: 50%; pointer-events: none; }
.lk-burst i {
  position: absolute;
  left: -3px;
  top: -3px;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #FFB23F;
  opacity: 0;
}
.lk-burst i:nth-child(even) { background: #D93A4A; }
.lk-heart[aria-pressed="true"] .lk-burst i { animation: lk-burst .5s cubic-bezier(.33, 1, .68, 1); }
.lk-num { display: inline-block; min-width: 2ch; }
.lk-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}
.lk-heart:focus-visible { outline: 3px solid #E8631A; outline-offset: 3px; }
@keyframes lk-pop {
  0% { transform: scale(.6); }
  60% { transform: scale(1.2); }
  100% { transform: scale(1); }
}
@keyframes lk-burst {
  0% { opacity: 1; transform: rotate(var(--a)) translateY(0) scale(1); }
  100% { opacity: 0; transform: rotate(var(--a)) translateY(-24px) scale(.4); }
}
@media (prefers-reduced-motion: reduce) {
  .lk-heart[aria-pressed="true"] svg, .lk-heart[aria-pressed="true"] .lk-burst i { animation: none; }
}
JavaScript
// one script for every [data-press] button: it flips aria-pressed, and rolls the count if there is one
document.querySelectorAll('[data-press]').forEach((btn) => {
  const count = btn.querySelector('[data-count]');
  const still = matchMedia('(prefers-reduced-motion: reduce)');
  btn.addEventListener('click', () => {
    const on = btn.getAttribute('aria-pressed') !== 'true';
    btn.setAttribute('aria-pressed', String(on));
    btn.dispatchEvent(new CustomEvent('press', { bubbles: true, detail: { on } }));
    if (!count) return;
    const n = Number(count.dataset.count) + (on ? 1 : -1);
    count.dataset.count = n;
    count.textContent = n.toLocaleString('en');
    if (!still.matches) {
      count.animate(
        [{ transform: 'translateY(' + (on ? 60 : -60) + '%)', opacity: 0 }, { transform: 'none', opacity: 1 }],
        { duration: 250, easing: 'cubic-bezier(.33, 1, .68, 1)' }
      );
    }
  });
});

Save to bookmarks

The bookmark fills from the top like a ribbon dropping into place, and the button turns dark. Same script as the heart.

HTML
<button class="lk-save" type="button" aria-pressed="false" data-press>
  <svg viewBox="0 0 24 24" aria-hidden="true"><path class="lk-save-fill" d="M6 3.5h12v17l-6-4.2-6 4.2z"/><path d="M6 3.5h12v17l-6-4.2-6 4.2z"/></svg>
  <span>Save</span>
</button>
CSS
.lk-save {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 44px;
  padding: 0 18px 0 14px;
  border: 0;
  border-radius: 12px;
  background: #FFFFFF;
  color: #17202C;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
  box-shadow: inset 0 0 0 2px #E5DED3;
  transition: background-color .25s, color .25s, box-shadow .25s;
}
.lk-save svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linejoin: round; }
.lk-save-fill {
  fill: currentColor;
  stroke: none;
  opacity: 0;
  transform-box: fill-box;
  transform-origin: 50% 0;
  transform: scaleY(0);
  transition: opacity .15s, transform .3s cubic-bezier(.33, 1, .68, 1);
}
.lk-save[aria-pressed="true"] { background: #17202C; color: #FDF9F1; box-shadow: none; }
.lk-save[aria-pressed="true"] .lk-save-fill { opacity: 1; transform: none; }
.lk-save:focus-visible { outline: 3px solid #E8631A; outline-offset: 3px; }
@media (prefers-reduced-motion: reduce) {
  .lk-save, .lk-save-fill { transition: none; }
}

How to use it

  • Both are real buttons with aria-pressed. A screen reader says "Like, 41, toggle button, not pressed", then "pressed" once it is liked, so the state never depends on the red alone.
  • The label stays the same when the state changes. Changing "Like" to "Liked" as well as flipping aria-pressed would make a screen reader say the state twice.
  • The script sends a press event with detail.on set to true or false. Listen for it to save the like on your server.
  • Reduced motion keeps the colour change and drops the pop, the burst and the rolling number.

When to use it

  • Any feed with posts people can react to: recipes, photos, comments, saved articles. The burst makes a tiny action feel like it counted.
  • Save or bookmark buttons in a shop or a reading app, where people come back to what they kept.
  • Not for actions with consequences, like following someone or buying. Those want a clear label and, where it matters, a confirm step, not confetti.

A worked example: likes that survive a reload

A recipe card whose heart saves to the server, and puts itself back if the save fails.

  1. Render the button with aria-pressed="true" or "false" from your data, and the real count in data-count and in the text.
  2. Listen for the press event on the button and send the new state to your API. The animation has already played, so the tap feels instant.
  3. If the request fails, click the button again from code to undo the change, and tell the person in a toast that the like did not save.
  4. Keep the heart's red, #D93A4A, for the liked state only. It measures 4.5:1 on white, well past the 3:1 a control's parts need, so the filled heart reads on any light card.
Save the like
const heart = document.querySelector('.lk-heart');
heart.addEventListener('press', async (e) => {
  const res = await fetch('/api/likes', { method: e.detail.on ? 'POST' : 'DELETE' });
  if (!res.ok) heart.click(); // put it back the way it was
});

Showing an interaction like this to a client or in a tutorial? Framok records your screen and zooms in on every click for you.

Free to use in personal and commercial work. No credit needed, though a link back is always nice.

More from the library

See the whole library