UNPKG

react-textra

Version:
93 lines (92 loc) 4.73 kB
var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; // eslint-disable-next-line no-use-before-define import React, { useEffect, useRef, useState, useCallback } from 'react'; import animationStyles from './animationStyles'; var Textra = function (props) { var selectedAnimation = props.effect || 'simple'; var animationRef = useRef(null); var _a = useState(animationStyles[selectedAnimation][0]), style = _a[0], setStyle = _a[1]; var textArrIndex = useRef(0); var previousTime = useRef(null); var animationDuration = props.duration || 500; var stopDuration = props.stopDuration || 3000; var currentRoundStartTime = useRef(0); var singleRoundDuration = stopDuration + 2 * animationDuration; var easeOutQuad = function (t) { return t * (2 - t); }; var text = props.data[textArrIndex.current]; useEffect(function () { setStyle(animationStyles[selectedAnimation][0]); }, [selectedAnimation]); var runShowTextAnimation = function (elapsed) { var showingTranlateInitial = animationStyles[selectedAnimation][1].translate.value; var showingTranlateDiffrence = animationStyles[selectedAnimation][1].translate.value - animationStyles[selectedAnimation][0].translate.value; var showingTiming = easeOutQuad((elapsed - currentRoundStartTime.current) / animationDuration); setStyle(function (s) { return ({ translate: __assign(__assign({}, s.translate), { value: showingTranlateInitial - showingTranlateDiffrence * showingTiming }), opacity: showingTiming }); }); }; var runHideTextAnimation = function (elapsed) { var hidingTraslateInitial = animationStyles[selectedAnimation][0].translate.value; var hidingTranslateDiffrence = animationStyles[selectedAnimation][0].translate.value - animationStyles[selectedAnimation][1].translate.value; var hidingOpacityInitial = animationStyles[selectedAnimation][0].opacity; var hidingTiming = easeOutQuad((elapsed - (currentRoundStartTime.current + singleRoundDuration - animationDuration)) / animationDuration); setStyle(function (s) { return ({ translate: __assign(__assign({}, s.translate), { value: hidingTraslateInitial - hidingTranslateDiffrence * hidingTiming }), opacity: hidingOpacityInitial - hidingTiming }); }); }; var updateTextIndex = function () { if (textArrIndex.current === props.data.length - 1) { textArrIndex.current = 0; } else { textArrIndex.current = textArrIndex.current + 1; } }; var updateRoudStartTime = function () { currentRoundStartTime.current += singleRoundDuration; }; var handlePropEvents = function () { props.onUpdate && props.onUpdate(textArrIndex.current); }; var runAnimation = useCallback(function (timestamps) { if (previousTime.current === null) previousTime.current = timestamps; var elapsed = timestamps - previousTime.current; if (elapsed > currentRoundStartTime.current && elapsed < currentRoundStartTime.current + animationDuration) { runShowTextAnimation(elapsed); } if (elapsed > currentRoundStartTime.current + singleRoundDuration - animationDuration && elapsed < currentRoundStartTime.current + singleRoundDuration) { runHideTextAnimation(elapsed); } if (elapsed > currentRoundStartTime.current + singleRoundDuration) { updateRoudStartTime(); updateTextIndex(); handlePropEvents(); } animationRef.current = window.requestAnimationFrame(runAnimation); }, [selectedAnimation, props.data.length, singleRoundDuration, animationDuration]); useEffect(function () { animationRef.current = window.requestAnimationFrame(runAnimation); return function () { return window.cancelAnimationFrame(animationRef.current); }; }, [runAnimation, props.effect]); return (React.createElement(React.Fragment, null, React.createElement("span", __assign({ style: { display: 'inline-block', transform: style.translate.type + "(" + style.translate.value + style.translate.unit + ")", opacity: style.opacity } }, props), text))); }; export default Textra;