UNPKG

animare

Version:

Advanced animation library for modern JavaScript.

18 lines 919 B
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.useAnimare=useAnimare;var _hooks=require("preact/hooks");/** * `useAnimare` custom React hook. * * @example * * import { useAnimare } from 'animare/react'; * * useAnimare(() => { * return animare(...params); * // or * return animare.single(...params); * }, []); * */function useAnimare(callback){let deps=arguments.length>1&&arguments[1]!==undefined?arguments[1]:[];const[animation,setAnimation]=(0,_hooks.useState)();(0,_hooks.useEffect)(()=>{// clean previous if(animation){animation.clearEvents();if(animation.timelineInfo.isPlaying)animation.pause();}// set new const newAnimation=callback();setAnimation(newAnimation);// clean current return()=>{newAnimation.clearEvents();if(newAnimation.timelineInfo.isPlaying)newAnimation.pause();};// eslint-disable-next-line react-hooks/exhaustive-deps },deps);return animation;}