Animated icons JS behaviour

State morph icons

Lordicon-style button icons morphing between two states on click: the heart fills with a particle burst, the bell swings a springy shake while its badge counts, play and pause glide through a sliding window, the bookmark fills and scatters — all mirrored into aria-pressed.

Documentation

Real scenarios

The component as it is used in a product — not a specimen grid.

Icons that earn their click

Lordicon-style state morphs on real buttons: the heart bursts into particles as it fills, the bell swings a springy shake while its badge counts, play and pause glide through a sliding window, the bookmark fills and scatters. Every state is mirrored into aria-pressed.

Signal · launch post

people liked this

Meridian · EUR/USD watch

alerts today

Onboarding · chapter 1

4:12 · product tour

Field notes · Q4 report

Goes to your reading list

Important props

Important props
Prop Type Default What it does
aria-pressed state true | false Each button is a real toggle; the state rides aria-pressed while the card mirrors it through data-on, so screen reader and eye see the same thing.
burst particles '8 · 24–36px' Eight dots fly out on even-but-jittered angles and fade over 700ms; their timers are cleared on destroy.
pop keyframe 'scale 0 → 1.28 → 1' Fills for heart, bookmark and badge spring with a 28% overshoot; transform-box: fill-box pivots each shape at its own centre.
ring keyframe '0 → 15 → −12 → 8 → −5 → 0°' The bell shakes on a springy rotation anchored at its handle; cubic-bezier(.36,.07,.19,.97) damps the swings.
reduced-motion media 'static' With motion off, the final state (filled or hollow) lands instantly and the particles are dropped.

Snippet

<div class="ims" x-data="{
    dots: [], t: null,
    burst() {
        this.dots = Array.from({ length: 8 }, (_, i) => {
            const a = (Math.PI * 2 * i) / 8 + Math.random() * .6;
            const r = 24 + Math.random() * 12;
            return { k: Date.now() + '-' + i, dx: Math.round(Math.cos(a) * r), dy: Math.round(Math.sin(a) * r) };
        });
        clearTimeout(this.t);
        this.t = setTimeout(() => (this.dots = []), 700);
    },
}" x-init="$cleanup(() => clearTimeout(t))">
    {{-- heart: fill morph + particle burst --}}
    <button type="button" class="ims-btn" x-data="{ on: false, count: 2481 }"
            x-bind:aria-pressed="on ? 'true' : 'false'"
            x-on:click="on = !on; if (on) { count++; $data.burst(); }">
        <span class="ims-ico">
            <svg viewBox="0 0 24 24" aria-hidden="true">
                <path class="ims-line" d="M19 14c1.49-1.46 3-3.21 3-5.5A5.5 5.5 0 0 0 16.5 3.5c-1.76 0-3 .5-4.5 2-1.5-1.5-2.74-2-4.5-2A5.5 5.5 0 0 0 2 8.5c0 2.3 1.5 4.05 3 5.5l7 7Z"/>
                <path class="ims-fill" d="…the same heart path…"/>
            </svg>
            <template x-for="dot in dots" :key="dot.k">
                <span class="ims-dot" :style="`--dx: ${dot.dx}px; --dy: ${dot.dy}px`"></span>
            </template>
        </span>
        <span x-text="on ? 'Liked' : 'Like'"></span>
    </button>

    {{-- play/pause: the sliding window --}}
    <button type="button" class="ims-btn" x-data="{ on: false }"
            x-bind:aria-pressed="on ? 'true' : 'false'" x-on:click="on = !on">
        <span class="ims-window" dir="ltr">
            <span class="ims-track" x-bind:data-on="on ? 'true' : 'false'">
                <svg class="ims-half" viewBox="0 0 24 24"><path class="ims-line" d="m7 4 13 8-13 8Z"/></svg>
                <svg class="ims-half" viewBox="0 0 24 24"><path class="ims-line" d="M7 4h3v16H7zM14 4h3v16h-3z"/></svg>
            </span>
        </span>
        <span x-text="on ? 'Playing' : 'Play video'"></span>
    </button>
</div>

<style>
.ims-btn { display: inline-grid; justify-items: center; gap: .55rem; padding: .8rem 1.4rem;
           border: 1px solid #e7e5e4; border-radius: 1rem; background: #fff; cursor: pointer; }
.ims-ico { position: relative; display: grid; place-items: center; }
.ims-line { fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.ims-fill { fill: currentColor; scale: 0; transform-box: fill-box; transform-origin: center; }
.ims-btn[aria-pressed='true'] .ims-fill { scale: 1; animation: ims-pop .5s cubic-bezier(.2,.9,.3,1.35); }
.ims-dot { position: absolute; inline-size: 4px; block-size: 4px; border-radius: 50%; background: currentColor;
           animation: ims-burst .65s ease-out forwards; }
.ims-window { inline-size: 1.7rem; overflow: clip; }
.ims-track { display: flex; inline-size: 3.4rem; transition: translate .35s cubic-bezier(.3,.7,.2,1); }
.ims-track[data-on='true'] { translate: -1.7rem 0; }
.ims-half { flex: none; inline-size: 1.7rem; }
@keyframes ims-pop { 0% { scale: 0; } 62% { scale: 1.28; } 100% { scale: 1; } }
@keyframes ims-burst { from { opacity: 1; translate: 0 0; } to { opacity: 0; translate: var(--dx) var(--dy); scale: .2; } }
@media (prefers-reduced-motion: reduce) {
    .ims-fill { animation: none; } .ims-track { transition: none; } .ims-dot { display: none; }
}
</style>
// app/Pages/IconMotion.jsx — Inertia (React) page; renders the React
// snippet below unchanged, with Inertia's Link wired into the provider.
import { Link } from '@inertiajs/react';
import { NabuXUIProvider } from '@nabuxai/ui-react';
import StateMorphs from '@/components/StateMorphs';

export default function IconMotion() {
  return (
    <NabuXUIProvider locale="en" linkComponent={Link}>
      <main className="nx-page">
        <StateMorphs />
      </main>
    </NabuXUIProvider>
  );
}
// StateMorphs.jsx — Lordicon-style state morphs on real toggle buttons
import { useEffect, useRef, useState } from 'react';
import '@nabuxai/ui-core/css';

const HEART = 'M19 14c1.49-1.46 3-3.21 3-5.5A5.5 5.5 0 0 0 16.5 3.5c-1.76 0-3 .5-4.5 2-1.5-1.5-2.74-2-4.5-2A5.5 5.5 0 0 0 2 8.5c0 2.3 1.5 4.05 3 5.5l7 7Z';

function useBurst() {
  const [dots, setDots] = useState([]);
  const timers = useRef([]);
  useEffect(() => () => timers.current.forEach(clearTimeout), []);
  const burst = () => {
    const nonce = Date.now();
    setDots(Array.from({ length: 8 }, (_, i) => {
      const a = (Math.PI * 2 * i) / 8 + Math.random() * 0.6;
      const r = 24 + Math.random() * 12;
      return { k: `${nonce}-${i}`, dx: Math.round(Math.cos(a) * r), dy: Math.round(Math.sin(a) * r) };
    }));
    timers.current.push(setTimeout(() => setDots([]), 700));
  };
  return { dots, burst };
}

export default function StateMorphs() {
  const [liked, setLiked] = useState(false);
  const [count, setCount] = useState(2481);
  const [playing, setPlaying] = useState(false);
  const like = useBurst();

  return (
    <>
      <style>{`
        .ims-btn { display: inline-grid; justify-items: center; gap: .55rem; padding: .8rem 1.4rem;
                   border: 1px solid #e7e5e4; border-radius: 1rem; background: #fff; color: #1c1917; cursor: pointer; }
        .ims-ico { position: relative; display: grid; place-items: center; }
        .ims-line { fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
        .ims-fill { fill: currentColor; scale: 0; transform-box: fill-box; transform-origin: center; }
        .ims-btn[aria-pressed='true'] .ims-fill { scale: 1; animation: ims-pop .5s cubic-bezier(.2,.9,.3,1.35); }
        .ims-dot { position: absolute; inline-size: 4px; block-size: 4px; border-radius: 50%; background: currentColor;
                   animation: ims-burst .65s ease-out forwards; }
        .ims-window { inline-size: 1.7rem; overflow: clip; }
        .ims-track { display: flex; inline-size: 3.4rem; transition: translate .35s cubic-bezier(.3,.7,.2,1); }
        .ims-track[data-on='true'] { translate: -1.7rem 0; }
        .ims-half { flex: none; inline-size: 1.7rem; }
        @keyframes ims-pop { 0% { scale: 0; } 62% { scale: 1.28; } 100% { scale: 1; } }
        @keyframes ims-burst { from { opacity: 1; translate: 0 0; } to { opacity: 0; translate: var(--dx) var(--dy); scale: .2; } }
        @media (prefers-reduced-motion: reduce) {
          .ims-fill { animation: none; } .ims-track { transition: none; } .ims-dot { display: none; }
        }
      `}</style>
      {/* Heart — fills with a particle burst; aria-pressed carries the state. */}
      <button type="button" className="ims-btn" aria-pressed={liked}
        onClick={() => { setLiked(!liked); if (!liked) { setCount((c) => c + 1); like.burst(); } }}>
        <span className="ims-ico">
          <svg viewBox="0 0 24 24" aria-hidden="true">
            <path className="ims-line" d={HEART} />
            <path className="ims-fill" d={HEART} />
          </svg>
          {like.dots.map((dot) => (
            <span key={dot.k} className="ims-dot" style={{ '--dx': `${dot.dx}px`, '--dy': `${dot.dy}px` }} />
          ))}
        </span>
        <span>{liked ? 'Liked' : 'Like'}</span>
      </button>
      <p>{count} people liked this</p>

      {/* Play/pause — the two icons glide through a sliding window. */}
      <button type="button" className="ims-btn" aria-pressed={playing} onClick={() => setPlaying(!playing)}>
        <span className="ims-window" dir="ltr">
          <span className="ims-track" data-on={playing}>
            <svg className="ims-half" viewBox="0 0 24 24" aria-hidden="true"><path className="ims-line" d="m7 4 13 8-13 8Z" /></svg>
            <svg className="ims-half" viewBox="0 0 24 24" aria-hidden="true"><path className="ims-line" d="M7 4h3v16H7zM14 4h3v16h-3z" /></svg>
          </span>
        </span>
        <span>{playing ? 'Playing' : 'Play video'}</span>
      </button>
    </>
  );
}
<!-- StateMorphs.vue — Lordicon-style state morphs on real toggle buttons -->
<script setup lang="ts">
import { onBeforeUnmount, ref } from 'vue';
import '@nabuxai/ui-core/css';

const HEART = 'M19 14c1.49-1.46 3-3.21 3-5.5A5.5 5.5 0 0 0 16.5 3.5c-1.76 0-3 .5-4.5 2-1.5-1.5-2.74-2-4.5-2A5.5 5.5 0 0 0 2 8.5c0 2.3 1.5 4.05 3 5.5l7 7Z';
const liked = ref(false);
const count = ref(2481);
const playing = ref(false);
const dots = ref<{ k: string; dx: number; dy: number }[]>([]);
const timers: number[] = [];

// Eight particles on even-but-jittered angles; swept away after 700ms.
const burst = () => {
  const nonce = Date.now();
  dots.value = Array.from({ length: 8 }, (_, i) => {
    const a = (Math.PI * 2 * i) / 8 + Math.random() * 0.6;
    const r = 24 + Math.random() * 12;
    return { k: `${nonce}-${i}`, dx: Math.round(Math.cos(a) * r), dy: Math.round(Math.sin(a) * r) };
  });
  timers.push(window.setTimeout(() => (dots.value = []), 700));
};
const toggleLike = () => {
  liked.value = !liked.value;
  if (liked.value) { count.value++; burst(); }
};
onBeforeUnmount(() => timers.forEach(clearTimeout));
</script>

<template>
  <!-- Heart — fills with a particle burst; aria-pressed carries the state. -->
  <button type="button" class="ims-btn" :aria-pressed="liked" @click="toggleLike">
    <span class="ims-ico">
      <svg viewBox="0 0 24 24" aria-hidden="true">
        <path class="ims-line" :d="HEART" />
        <path class="ims-fill" :d="HEART" />
      </svg>
      <span v-for="dot in dots" :key="dot.k" class="ims-dot" :style="{ '--dx': dot.dx + 'px', '--dy': dot.dy + 'px' }" />
    </span>
    <span>{{ liked ? 'Liked' : 'Like' }}</span>
  </button>
  <p>{{ count }} people liked this</p>

  <!-- Play/pause — the two icons glide through a sliding window. -->
  <button type="button" class="ims-btn" :aria-pressed="playing" @click="playing = !playing">
    <span class="ims-window" dir="ltr">
      <span class="ims-track" :data-on="playing">
        <svg class="ims-half" viewBox="0 0 24 24" aria-hidden="true"><path class="ims-line" d="m7 4 13 8-13 8Z" /></svg>
        <svg class="ims-half" viewBox="0 0 24 24" aria-hidden="true"><path class="ims-line" d="M7 4h3v16H7zM14 4h3v16h-3z" /></svg>
      </span>
    </span>
    <span>{{ playing ? 'Playing' : 'Play video' }}</span>
  </button>
</template>

<style scoped>
.ims-btn { display: inline-grid; justify-items: center; gap: .55rem; padding: .8rem 1.4rem;
           border: 1px solid #e7e5e4; border-radius: 1rem; background: #fff; color: #1c1917; cursor: pointer; }
.ims-ico { position: relative; display: grid; place-items: center; }
.ims-line { fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.ims-fill { fill: currentColor; scale: 0; transform-box: fill-box; transform-origin: center; }
.ims-btn[aria-pressed='true'] .ims-fill { scale: 1; animation: ims-pop .5s cubic-bezier(.2,.9,.3,1.35); }
.ims-dot { position: absolute; inline-size: 4px; block-size: 4px; border-radius: 50%; background: currentColor;
           animation: ims-burst .65s ease-out forwards; }
.ims-window { inline-size: 1.7rem; overflow: clip; }
.ims-track { display: flex; inline-size: 3.4rem; transition: translate .35s cubic-bezier(.3,.7,.2,1); }
.ims-track[data-on='true'] { translate: -1.7rem 0; }
.ims-half { flex: none; inline-size: 1.7rem; }
@keyframes ims-pop { 0% { scale: 0; } 62% { scale: 1.28; } 100% { scale: 1; } }
@keyframes ims-burst { from { opacity: 1; translate: 0 0; } to { opacity: 0; translate: var(--dx) var(--dy); scale: .2; } }
@media (prefers-reduced-motion: reduce) {
  .ims-fill { animation: none; } .ims-track { transition: none; } .ims-dot { display: none; }
}
</style>
<!-- StateMorphs.svelte — Lordicon-style state morphs on real toggle buttons -->
<script lang="ts">
  import { onMount } from 'svelte';
  import '@nabuxai/ui-core/css';

  const HEART = 'M19 14c1.49-1.46 3-3.21 3-5.5A5.5 5.5 0 0 0 16.5 3.5c-1.76 0-3 .5-4.5 2-1.5-1.5-2.74-2-4.5-2A5.5 5.5 0 0 0 2 8.5c0 2.3 1.5 4.05 3 5.5l7 7Z';
  let liked = $state(false);
  let count = $state(2481);
  let playing = $state(false);
  let dots = $state<{ k: string; dx: number; dy: number }[]>([]);
  const timers: number[] = [];

  // Eight particles on even-but-jittered angles; swept away after 700ms.
  const burst = () => {
    const nonce = Date.now();
    dots = Array.from({ length: 8 }, (_, i) => {
      const a = (Math.PI * 2 * i) / 8 + Math.random() * 0.6;
      const r = 24 + Math.random() * 12;
      return { k: `${nonce}-${i}`, dx: Math.round(Math.cos(a) * r), dy: Math.round(Math.sin(a) * r) };
    });
    timers.push(window.setTimeout(() => (dots = []), 700));
  };
  const toggleLike = () => {
    liked = !liked;
    if (liked) { count++; burst(); }
  };
  onMount(() => () => timers.forEach(clearTimeout));
</script>

<!-- Heart — fills with a particle burst; aria-pressed carries the state. -->
<button type="button" class="ims-btn" aria-pressed={liked} onclick={toggleLike}>
  <span class="ims-ico">
    <svg viewBox="0 0 24 24" aria-hidden="true">
      <path class="ims-line" d={HEART} />
      <path class="ims-fill" d={HEART} />
    </svg>
    {#each dots as dot (dot.k)}
      <span class="ims-dot" style="--dx: {dot.dx}px; --dy: {dot.dy}px"></span>
    {/each}
  </span>
  <span>{liked ? 'Liked' : 'Like'}</span>
</button>
<p>{count} people liked this</p>

<!-- Play/pause — the two icons glide through a sliding window. -->
<button type="button" class="ims-btn" aria-pressed={playing} onclick={() => (playing = !playing)}>
  <span class="ims-window" dir="ltr">
    <span class="ims-track" data-on={playing}>
      <svg class="ims-half" viewBox="0 0 24 24" aria-hidden="true"><path class="ims-line" d="m7 4 13 8-13 8Z" /></svg>
      <svg class="ims-half" viewBox="0 0 24 24" aria-hidden="true"><path class="ims-line" d="M7 4h3v16H7zM14 4h3v16h-3z" /></svg>
    </span>
  </span>
  <span>{playing ? 'Playing' : 'Play video'}</span>
</button>

<style>
.ims-btn { display: inline-grid; justify-items: center; gap: .55rem; padding: .8rem 1.4rem;
           border: 1px solid #e7e5e4; border-radius: 1rem; background: #fff; color: #1c1917; cursor: pointer; }
.ims-ico { position: relative; display: grid; place-items: center; }
.ims-line { fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.ims-fill { fill: currentColor; scale: 0; transform-box: fill-box; transform-origin: center; }
.ims-btn[aria-pressed='true'] .ims-fill { scale: 1; animation: ims-pop .5s cubic-bezier(.2,.9,.3,1.35); }
.ims-dot { position: absolute; inline-size: 4px; block-size: 4px; border-radius: 50%; background: currentColor;
           animation: ims-burst .65s ease-out forwards; }
.ims-window { inline-size: 1.7rem; overflow: clip; }
.ims-track { display: flex; inline-size: 3.4rem; transition: translate .35s cubic-bezier(.3,.7,.2,1); }
.ims-track[data-on='true'] { translate: -1.7rem 0; }
.ims-half { flex: none; inline-size: 1.7rem; }
@keyframes ims-pop { 0% { scale: 0; } 62% { scale: 1.28; } 100% { scale: 1; } }
@keyframes ims-burst { from { opacity: 1; translate: 0 0; } to { opacity: 0; translate: var(--dx) var(--dy); scale: .2; } }
@media (prefers-reduced-motion: reduce) {
  .ims-fill { animation: none; } .ims-track { transition: none; } .ims-dot { display: none; }
}
</style>