@infinityfx/lively
Version:
Feature complete, lightweight react animation library.
4 lines (3 loc) • 2.85 kB
JavaScript
"use client";
import{jsx as e}from"react/jsx-runtime";import{createContext as r,useRef as n,use as t,useMemo as i,useCallback as a,useImperativeHandle as o,useEffect as s,useLayoutEffect as c,Children as u,isValidElement as d,cloneElement as m}from"react";import l from"./core/clip.js";import p from"./core/timeline.js";import{merge as f,pick as h,combineRefs as g}from"./core/utils.js";const v=r(null);function y(r){const y=n(null),b=n([]),x=t(v),w=r.inherit&&x?f({},r,h(x,["group","animations","triggers","animate","initial","stagger","staggerLimit","deform","disabled","paused"])):r,{id:k="",inherit:E,triggers:L=[],disabled:j,adaptive:z=!1,manual:A=!1,paused:C}=w,M=void 0!==r.order?r.order:(E&&void 0!==x?.index?x.index:-1)+1,D=n([]),O=i((()=>{const e={animate:l.from(w.animate,w.initial)};for(const r in w.animations)e[r]=l.from(w.animations[r],w.initial);return e}),[]),T=n(new p({...w,mountClips:L.reduce(((e,{name:r,on:n})=>("mount"===n&&e.push(O[r||"animate"]),e)),[])})),V=a(((e,n={},t=1)=>{const i=O[e];if(j||E&&t<2)return 0;f(n,{reverse:i?.reverse});let a=0,o=n.delay||0,s=i?T.current.time(i):0;for(const r of b.current)r.current?.inherit&&(a=Math.max(r.current.play(e,f({delay:o+s},n),t+1),a));const c=(n.reverse?a:o)*(M/t+1/t);return i&&T.current.add(i,f({delay:c},n)),r.onAnimationEnd&&setTimeout(r.onAnimationEnd.bind({},e),1e3*(s+c)),s+c}),[j,E,M]);function q(e,r={}){let n=0;for(const{name:t,on:i,...a}of L)i===e&&(n=Math.max(V(t||"animate",f(a,r)),n));return n}return o(g(y,r.ref),(()=>({play:V,trigger:q,stop:()=>T.current.tracks.forEach((e=>e.clear())),timeline:T.current,children:b.current,inherit:E,adaptive:z,group:w.group,id:k})),[L]),s((()=>T.current.pause(!(!C&&!j))),[C,j]),s((()=>{for(let e=0;e<L.length;e++){let{name:r,on:n,...t}=L[e];if("string"==typeof n)continue;const i="boolean"==typeof n?n:n.called,a=D.current[e];void 0!==a&&i&&i!==a&&V(r||"animate",t),D.current[e]=i}}),[L]),c((()=>{const e=()=>T.current.cache();return T.current.link(w.animate),window.addEventListener("resize",e),x?.add(y),document.fonts.ready.then((()=>{A||T.current.mounted||q("mount"),T.current.mount()})),()=>{window.removeEventListener("resize",e),T.current.unlink(),x?.remove(y)}}),[]),e(v,{value:{...r.passthrough?x:{index:M,...w},add:e=>{r.passthrough&&x?.add(e),b.current.includes(e)||b.current.push(e)},remove:e=>{r.passthrough&&x?.remove(e);const n=b.current.indexOf(e)||-1;n>=0&&b.current.splice(n,1)}},children:u.map(r.children,(e=>d(e)?m(e,{ref:g((e=>T.current.insert(e)),e.props.ref),pathLength:1,style:f({backfaceVisibility:"hidden",willChange:"transform"},e.props.style,O.animate.initial,{strokeDasharray:1}),"data-lively-offset-boundary":!(!["x","y"].some((e=>r.cachable?.includes(e)))&&void 0!==r.cachable)||void 0}):e))})}y.isLively=!0;export{v as AnimatableContext,y as default};
//# sourceMappingURL=animatable.js.map