UNPKG

3.39 kBSource Map (JSON)View Raw
1{"version":3,"sources":["useRequestAnimationFrame.js"],"names":["defaultOptions","increment","startAt","finishAt","errorMessage","useRequestAnimationFrame","func","options","isClient","console","warn","opts","progress","onFinish","setOnFinish","next","window","requestAnimationFrame","step","current"],"mappings":";;;;;;;AAAA;;AACA;;AACA;;AACA;;AACA;;;;;;;;;;;;;;;;;;AAEA,IAAMA,cAAc,GAAG;AAAEC,EAAAA,SAAS,EAAE,CAAb;AAAgBC,EAAAA,OAAO,EAAE,CAAzB;AAA4BC,EAAAA,QAAQ,EAAE;AAAtC,CAAvB;AAEA,IAAMC,YAAY,GAAG,4EACjB,qGADiB,GAEjB,6DAFJ;;AAOA,IAAMC,wBAAwB,GAAG,SAA3BA,wBAA2B,CAACC,IAAD,EAAoC;AAAA,MAA7BC,OAA6B,uEAAnBP,cAAmB;;AACnE,MAAI,CAACQ,oBAAD,IAAa,CAAC,gCAAe,uBAAf,CAAlB,EAA2D;AAEzDC,IAAAA,OAAO,CAACC,IAAR,CAAaN,YAAb;AACA,WAAO,0CAAyBA,YAAzB,CAAP;AACD;;AAED,MAAMO,IAAI,qBAAQX,cAAR,MAA2BO,OAA3B,CAAV;;AACA,MAAMK,QAAQ,GAAG,mBAAOD,IAAI,CAACT,OAAZ,CAAjB;;AARmE,6BASnC,uCATmC;AAAA;AAAA,MAS5DW,QAT4D;AAAA,MASlDC,WATkD;;AAYnE,MAAMC,IAAI,GAAG,SAAPA,IAAO;AAAA,WAAMC,MAAM,CAACC,qBAAP,CAA6BC,IAA7B,CAAN;AAAA,GAAb;;AAEA,MAAMA,IAAI,GAAG,wBAAY,YAAM;AAC7B,QAAIN,QAAQ,CAACO,OAAT,IAAoBR,IAAI,CAACR,QAAzB,IAAqCQ,IAAI,CAACR,QAAL,KAAkB,CAAC,CAA5D,EAA+D;AAC7DG,MAAAA,IAAI,CAACM,QAAQ,CAACO,OAAV,EAAmBJ,IAAnB,CAAJ;AACAH,MAAAA,QAAQ,CAACO,OAAT,IAAoBR,IAAI,CAACV,SAAzB;AACD,KAHD,MAGO,IAAIY,QAAQ,CAACM,OAAb,EAAsB;AAC3BN,MAAAA,QAAQ,CAACM,OAAT;AACD;AACF,GAPY,EAOV,CAACb,IAAD,EAAOK,IAAI,CAACR,QAAZ,EAAsBQ,IAAI,CAACV,SAA3B,EAAsCW,QAAQ,CAACO,OAA/C,EAAwDN,QAAQ,CAACM,OAAjE,CAPU,CAAb;;AASA,MAAIP,QAAQ,CAACO,OAAT,IAAoBR,IAAI,CAACT,OAA7B,EAAsC;AACpCa,IAAAA,IAAI;AACL;;AAED,SAAOD,WAAP;AACD,CA5BD;;eA8BeT,wB","sourcesContent":["import { useCallback, useRef } from 'react';\nimport createHandlerSetter from './utils/createHandlerSetter';\nimport isClient from './utils/isClient';\nimport isAPISupported from './utils/isAPISupported';\nimport createCbSetterErrorProxy from './utils/createCbSetterErrorProxy';\n\nconst defaultOptions = { increment: 1, startAt: 0, finishAt: 100 };\n\nconst errorMessage = 'requestAnimationFrame is not supported, this could happen both because '\n + 'window.requestAnimationFrame is not supported by your current browser version or you\\'re using the '\n + 'useRequestAnimationFrame hook whilst server side rendering.';\n\n/**\n * Takes care of running an animating function, provided as the first argument, while keeping track of its progress.\n */\nconst useRequestAnimationFrame = (func, options = defaultOptions) => {\n if (!isClient || !isAPISupported('requestAnimationFrame')) {\n // eslint-disable-next-line no-console\n console.warn(errorMessage);\n return createCbSetterErrorProxy(errorMessage);\n }\n\n const opts = { ...defaultOptions, ...options };\n const progress = useRef(opts.startAt);\n const [onFinish, setOnFinish] = createHandlerSetter();\n\n // eslint-disable-next-line no-use-before-define\n const next = () => window.requestAnimationFrame(step);\n\n const step = useCallback(() => {\n if (progress.current <= opts.finishAt || opts.finishAt === -1) {\n func(progress.current, next);\n progress.current += opts.increment;\n } else if (onFinish.current) {\n onFinish.current();\n }\n }, [func, opts.finishAt, opts.increment, progress.current, onFinish.current]);\n\n if (progress.current <= opts.startAt) {\n next();\n }\n\n return setOnFinish;\n};\n\nexport default useRequestAnimationFrame;\n"],"file":"useRequestAnimationFrame.js"}
\No newline at end of file