@zhenre/animation
v0.1.0
Published
@zhenre animation — Angular-model enter/leave orchestration (literal CSS classes, framework-triggered, leave holds removal), FLIP reorder, stagger. Zero dependencies.
Readme
@zhenre/animation
Angular-model enter/leave orchestration — animations are defined in your CSS
(classes + keyframes, or transitions paired with @starting-style); JS only
decides when they play. The library adds exactly the classes you hand it,
waits for the longest transition/animation, removes them. It invents no class
names, ships no styles, no tweening, no per-frame JS. Zero dependencies.
<style>
.fade-in { animation: fade .2s ease; }
.fade-out { animation: fade .2s ease reverse; }
/* transition form — @starting-style is the from-state, no extra classes */
.slide-in { translate: 0 0; transition: translate .2s ease;
@starting-style { translate: 0 -100%; } }
</style>import { animateEnter, animateLeave, animateSwap, stagger, flip, eachAnim } from '@zhenre/animation';
import { each, mountEach } from '@zhenre/core';
// standalone
await animateEnter(el, 'fade-in'); // literal class → wait → removed
await animateLeave(el, ['a', 'b']); // array / space-separated both fine
await animateLeave(el, 'fade-out'); el.remove();
// JS-library escape hatch (GSAP / anime.js / WAAPI) — call done() when finished;
// an auto-done guard fires after `timeout` (default 4000, Angular's MAX_ANIMATION_TIMEOUT)
await animateEnter(el, (el, done) => {
gsap.fromTo(el, { opacity: 0 }, { opacity: 1, onComplete: () => done() });
});
// framework-triggered (the Angular way): rows entering/leaving a keyed list
// are animated automatically — leave HOLDS the DOM removal until it settles
const view = mountEach(tbody, each(rows, r => r.id, rowTpl, {
anim: eachAnim('fade-in', 'fade-out'),
}));
each(rows, r => r.id, rowTpl, { anim: eachAnim({ enter: 'in', leave: gsapOut, timeout: 6000 }) });Pair it with the router for animated route changes:
router.outlet(el, render, {
transition: (a, b) => animateSwap(a, b, 'fade-out', 'fade-in'),
});Semantics (Angular animate.enter / animate.leave)
- Literal classes — what you give is what gets added. No
{name}-enter-fromprotocol, no prefix invention. Your CSS is the single source of truth. - Longest-wait completion — the runner reads the element's computed
transition/animation durations, listens for
transitionend/animationend(capture, first hit wins) and keeps a duration-derived timeout as the ceiling: it can neither hang (no event) nor cut off early (event lost). Zero-duration CSS (tests) resolves immediately. - leave holds removal — inside
each(..., { anim }), a removed row's node stays in place (fading "ghost") until its leave settles, then the framework removes it — no matter what (throw, rejection, timeout). The list ledger splices immediately; keys stay coherent. - fn receivers —
(el, done) => voidfor GSAP-style libraries; the auto-done timeout (default 4s, configurable per spec) guarantees completion. - Scope — enter fires when a row enters the DOM (initial fill included);
leave plays for rows the list drops while alive. Whole-view teardown
(
dispose()) is instant, mirroring Angular's same-template rule.
stagger() (incremental inline delays, restored after) and flip() (FLIP
reorder via native WAAPI transforms — keyframes are necessarily built at
runtime from measured deltas) are the two imperative helpers that complement
the declarative layer.
Class protocol summary: none. Bring your own class names.
