@ant-design/x-markdown
Version:
placeholder for @ant-design/x-markdown
33 lines • 1.13 kB
JavaScript
import React, { useEffect, useMemo, useRef, useState } from 'react';
const AnimationText = /*#__PURE__*/React.memo(props => {
const {
text,
animationConfig
} = props;
const {
fadeDuration = 200,
easing = 'ease-in-out'
} = animationConfig || {};
const [chunks, setChunks] = useState([]);
const prevTextRef = useRef('');
useEffect(() => {
if (text === prevTextRef.current) return;
if (!(prevTextRef.current && text.indexOf(prevTextRef.current) === 0)) {
setChunks([text]);
prevTextRef.current = text;
return;
}
const newText = text.slice(prevTextRef.current.length);
if (!newText) return;
setChunks(prev => [...prev, newText]);
prevTextRef.current = text;
}, [text]);
const animationStyle = useMemo(() => ({
animation: `x-markdown-fade-in ${fadeDuration}ms ${easing} forwards`
}), [fadeDuration, easing]);
return /*#__PURE__*/React.createElement(React.Fragment, null, chunks.map((text, index) => /*#__PURE__*/React.createElement("span", {
style: animationStyle,
key: `animation-text-${index}`
}, text)));
});
export default AnimationText;