Animated Phosphor Icons (Opt-in)

A separate module that brings to life the Phosphor icons molique already uses: the bell swings, the heart beats. It moves the individual parts of an icon, not just the whole. The animation is triggered by hover, a tap, scrolling into view or a class of your own, and the trigger can sit on the button, card or link that holds the icon.

Framework: molique

The author of molique is Rafał Różacki.

Icons: Phosphor Icons

The icons were designed by Helena Zhang and Tobias Fried. The Phosphor Icons family is released under the MIT License (© Phosphor Icons). This module contains no icon geometry, it only moves the icons you already have. It is not a Phosphor project and is not endorsed by its team.

1. Installation

You link one CSS file. The JS logic is pulled in by molique-script.js as soon as the page has at least one .icon-anim, just like the other widgets.

<link rel="stylesheet" href="css/molique-style-icon-anim.css">

<svg class="icon icon-anim icon-anim-hover" aria-hidden="true">
  <use href="img/icons-sprite.svg#ph-bell"></use>
</svg>

Where the icons come from

Phosphor publishes every weight in two forms, and that decides how much can move.

Source What moves Notes
A sprite <use href="…#ph-bell"> built from the stroke icons (the raw/ folder in phosphor-icons/core) The whole icon and its parts, stroke drawing This is how molique.dev works. The JS copies the symbol’s shapes into the <svg>. The sprite must be on the same domain.
SVG pasted straight into the HTML, from the same source The whole icon and its parts, stroke drawing Add data-icon="bell" - the name cannot be read from the shapes.
Flattened SVG (assets/, the npm package @phosphor-icons/core) The whole icon Every icon is a single outline path. There are no separate parts to move.
The web font <i class="ph ph-bell"> (@phosphor-icons/web) The whole icon The JS reads the name from the ph-bell class. A font glyph is one character.

Parts are numbered the same way in thin, light, regular, bold and duotone, so one preset works in all of them. The duotone background is left out of the count. The fill weight is built differently and only gets the whole-icon animation.

2. Triggers

A trigger class works on the icon itself or on any of its ancestors. The second is the usual case: you hover the whole button and the icon inside it moves.

Loop with a pause
<!-- On the icon itself -->
<svg class="icon icon-anim icon-anim-hover"><use href="…#ph-bell"></use></svg>

<!-- On a parent: hovering the whole button -->
<button class="btn-primary icon-anim-hover">
  <svg class="icon icon-anim"><use href="…#ph-download-simple"></use></svg>
  Download quote
</button>

<!-- Tap: plays to the end, even after a quick tap -->
<button class="btn-outline-danger icon-anim-tap">…</button>

<!-- Always, looping, with an 800 ms pause between laps -->
<span class="icon-anim-play icon-anim-loop" data-icon-anim-loop-delay="800">…</span>
Class / attribute What it does
.icon-anim The icon that should move. On an <svg> or on <i class="ph ph-…">. Animates nothing by itself, it waits for a trigger.
.icon-anim-hover Animates on pointer hover (only on devices that can hover) and on keyboard focus (:focus-visible). On phones, add .icon-anim-tap.
.icon-anim-tap Animates on press (pointer, Enter, Space) and plays to the end. With a loop, it lasts until release. The JS adds .is-tapped for the duration.
.icon-anim-view Animates when the element scrolls into view (the JS adds .is-in-view). Once by default. data-icon-anim-once="false" replays on every entry, data-icon-anim-margin is the observer’s rootMargin.
.icon-anim-play Animates right away and for as long as the class is on the element. For control from your own JS, e.g. a “saving” status icon.

3. Animations

More than a thousand icons have a default animation matched to what they show. The car drives, the flame flickers, the gear turns, and an arrow goes where its name says. Nearly 600 move only a part: the arrow in a frame goes while the frame stays, a slash draws across the icon, letters and digits write themselves, the arcs of wifi spread from the centre, the hands go round the dial. 226 icons give a short pop: brand logos, religious symbols and objects with no natural motion. Hover the tiles:

trash
lock
check-circle
clock
envelope-simple
hammer

A gallery of all 1512 icons, searchable in Polish, English and German, is on Examples: animated icons.

A different animation than the default

data-icon-anim="name" overrides the icon’s preset. The animations below move the whole icon, so they work with every source and every weight. The exceptions are draw and draw-loop, which draw the strokes and need the parts. Without them the icon just pops.

draw draw-loop tada flip float
<svg class="icon icon-anim icon-anim-hover" data-icon-anim="draw">
  <use href="img/icons-sprite.svg#ph-house"></use>
</svg>
data-icon-anim value Motion
popA scale pulse. The default for icons with no family and no preset.
wiggle, shakeRocking around the centre / a horizontal shake.
ringA pendulum hung from the top, like a bell.
bounce, drop, floatA hop / a fall with a rebound / a calm hover (for loops).
beat, tada, flashA heartbeat / a “ta-da” / a flicker.
spin, spin-ccw, flipA full turn clockwise / counter-clockwise / around the vertical axis.
blinkA blink (a vertical squeeze).
press, rock, flapA push of a finger / rocking on water / a flap of wings.
sway, flicker, page-turnLeaning from the foot, like a plant / a flickering flame / a page turning.
zoom-in, zoom-outGrows and comes back / shrinks and comes back.
nudge-right, nudge-left, nudge-up, nudge-downA push in that direction and back.
draw, draw-loopThe strokes draw one after another / draw, hold and erase (for loops).

4. Timing, loops and inheritance

The modifiers are CSS custom properties, so they inherit like the text colour. .icon-anim-loop on a toolbar loops every icon in it, and a single icon can override that with a value of its own.

Class / token What it does
.icon-anim-loop Repeats forever (--icon-anim-iterations: infinite). A looping icon off screen waits (the JS adds .is-offscreen).
data-icon-anim-loop-delay="600" A pause in milliseconds after every lap. On the icon or on an ancestor. During the pause the icon returns to its normal look.
.icon-anim-persist Keeps the last frame when the animation ends (--icon-anim-fill: forwards).
.icon-anim-fast / .icon-anim-slow Twice as fast / twice as slow.
--icon-anim-speed A tempo multiplier, 1 by default. 2 is twice as fast.
--icon-anim-delay A start delay, 0s by default.
<div class="icon-anim-hover">
  <svg class="icon icon-anim">…</svg>
  <svg class="icon icon-anim icon-anim-slow">…</svg>
  <svg class="icon icon-anim" style="--icon-anim-delay: 0.3s">…</svg>
</div>

5. Accessibility and API

With prefers-reduced-motion: reduce the icons stay still. Every rule that moves anything sits inside @media (prefers-reduced-motion: no-preference), so there is nothing to override. Hover also reacts to keyboard focus, and a decorative icon needs aria-hidden="true" like any other.

Icons added later (a toast, a modal filled by fetch) are picked up by the module on its own. From your own JS:

// one lap of the animation, e.g. after a form has been saved
window.moliqueIconAnim.play(document.querySelector('#save-icon'));

// preparing a fragment by hand (usually not needed)
window.moliqueIconAnim.hydrate(container);

6. Pitfalls

1 A sprite from another domain or a CDN

Browsers do not render <use> from another origin, and the module fetches the sprite from the same address as the href. Keep the sprite on the same domain as the page. If the fetch fails, the icon keeps its <use> and only moves as a whole.

2 A pasted SVG from the flattened set has no parts

The files in assets/ (npm) and the font are a single outline. The bin or padlock preset finds nothing to move there, so the icon only gets the whole-icon animation. For part animations use the stroke files from raw/: <line>, <path> and <polyline> elements with stroke="currentColor".

3 .icon-anim-view and hover on the same element

After scrolling into view, .is-in-view stays on the element, so the “animate” state is already on and a hover will not start the animation a second time. If you need both, put the view trigger on the container and the hover on the button inside it.

7. Related pages