@chayns-components/typewriter 5.0.0-beta.60 → 5.0.0-beta.61
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -25,9 +25,7 @@ const Typewriter = _ref => {
|
|
|
25
25
|
const [shownCharCount, setShownCharCount] = (0, _react.useState)(0);
|
|
26
26
|
const [shouldStopAnimation, setShouldStopAnimation] = (0, _react.useState)(false);
|
|
27
27
|
const textContent = /*#__PURE__*/_react.default.isValidElement(children) ? (0, _server.renderToString)(children) : children;
|
|
28
|
-
|
|
29
|
-
// const textLength = useMemo(() => getCharactersCount(textContent), [textContent]);
|
|
30
|
-
const textLength = textContent.length;
|
|
28
|
+
const textLength = (0, _react.useMemo)(() => (0, _utils.getCharactersCount)(textContent), [textContent]);
|
|
31
29
|
const isAnimatingText = shownCharCount !== textLength;
|
|
32
30
|
const handleClick = (0, _react.useCallback)(() => {
|
|
33
31
|
setShouldStopAnimation(true);
|
|
@@ -35,14 +33,21 @@ const Typewriter = _ref => {
|
|
|
35
33
|
(0, _react.useEffect)(() => {
|
|
36
34
|
let interval;
|
|
37
35
|
if (shouldStopAnimation) {
|
|
38
|
-
setShownCharCount(
|
|
36
|
+
setShownCharCount(textContent.length);
|
|
39
37
|
} else {
|
|
40
38
|
setShownCharCount(0);
|
|
41
39
|
interval = window.setInterval(() => {
|
|
42
40
|
setShownCharCount(prevState => {
|
|
43
|
-
|
|
41
|
+
let nextState = prevState + 1;
|
|
44
42
|
if (nextState === textLength) {
|
|
45
43
|
window.clearInterval(interval);
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* At this point, the next value for "shownCharCount" is deliberately set to
|
|
47
|
+
* the length of the textContent in order to correctly display HTML elements
|
|
48
|
+
* after the last letter.
|
|
49
|
+
*/
|
|
50
|
+
nextState = textContent.length;
|
|
46
51
|
}
|
|
47
52
|
return nextState;
|
|
48
53
|
});
|
|
@@ -51,7 +56,7 @@ const Typewriter = _ref => {
|
|
|
51
56
|
return () => {
|
|
52
57
|
window.clearInterval(interval);
|
|
53
58
|
};
|
|
54
|
-
}, [shouldStopAnimation, speed, textLength]);
|
|
59
|
+
}, [shouldStopAnimation, speed, textContent.length, textLength]);
|
|
55
60
|
const shownText = (0, _react.useMemo)(() => (0, _utils.getSubTextFromHTML)(textContent, shownCharCount), [shownCharCount, textContent]);
|
|
56
61
|
return /*#__PURE__*/_react.default.createElement(_Typewriter.StyledTypewriter, {
|
|
57
62
|
onClick: handleClick
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Typewriter.js","names":["TypewriterSpeed","Typewriter","children","speed","Medium","shownCharCount","setShownCharCount","useState","shouldStopAnimation","setShouldStopAnimation","textContent","React","isValidElement","renderToString","textLength","
|
|
1
|
+
{"version":3,"file":"Typewriter.js","names":["TypewriterSpeed","Typewriter","children","speed","Medium","shownCharCount","setShownCharCount","useState","shouldStopAnimation","setShouldStopAnimation","textContent","React","isValidElement","renderToString","textLength","useMemo","getCharactersCount","isAnimatingText","handleClick","useCallback","useEffect","interval","length","window","setInterval","prevState","nextState","clearInterval","shownText","getSubTextFromHTML","__html","displayName"],"sources":["../../../src/components/typewriter/Typewriter.tsx"],"sourcesContent":["import React, { FC, ReactElement, useCallback, useEffect, useMemo, useState } from 'react';\nimport { renderToString } from 'react-dom/server';\nimport {\n StyledTypewriter,\n StyledTypewriterPseudoText,\n StyledTypewriterText,\n} from './Typewriter.styles';\nimport { getCharactersCount, getSubTextFromHTML } from './utils';\n\nexport enum TypewriterSpeed {\n Slow = 40,\n Medium = 30,\n Fast = 20,\n}\n\nexport type TypewriterProps = {\n /**\n * The text to type\n */\n children: ReactElement | string;\n speed?: TypewriterSpeed;\n};\n\nconst Typewriter: FC<TypewriterProps> = ({ children, speed = TypewriterSpeed.Medium }) => {\n const [shownCharCount, setShownCharCount] = useState(0);\n const [shouldStopAnimation, setShouldStopAnimation] = useState(false);\n\n const textContent = React.isValidElement(children) ? renderToString(children) : children;\n\n const textLength = useMemo(() => getCharactersCount(textContent), [textContent]);\n\n const isAnimatingText = shownCharCount !== textLength;\n\n const handleClick = useCallback(() => {\n setShouldStopAnimation(true);\n }, []);\n\n useEffect(() => {\n let interval: number | undefined;\n\n if (shouldStopAnimation) {\n setShownCharCount(textContent.length);\n } else {\n setShownCharCount(0);\n\n interval = window.setInterval(() => {\n setShownCharCount((prevState) => {\n let nextState = prevState + 1;\n\n if (nextState === textLength) {\n window.clearInterval(interval);\n\n /**\n * At this point, the next value for \"shownCharCount\" is deliberately set to\n * the length of the textContent in order to correctly display HTML elements\n * after the last letter.\n */\n nextState = textContent.length;\n }\n\n return nextState;\n });\n }, speed);\n }\n\n return () => {\n window.clearInterval(interval);\n };\n }, [shouldStopAnimation, speed, textContent.length, textLength]);\n\n const shownText = useMemo(\n () => getSubTextFromHTML(textContent, shownCharCount),\n [shownCharCount, textContent]\n );\n\n return (\n <StyledTypewriter onClick={handleClick}>\n <StyledTypewriterText\n dangerouslySetInnerHTML={{ __html: shownText }}\n isAnimatingText={isAnimatingText}\n />\n {isAnimatingText && <StyledTypewriterPseudoText>{children}</StyledTypewriterPseudoText>}\n </StyledTypewriter>\n );\n};\n\nTypewriter.displayName = 'Typewriter';\n\nexport default Typewriter;\n"],"mappings":";;;;;;AAAA;AACA;AACA;AAKA;AAAiE;AAAA;AAAA,IAErDA,eAAe;AAAA;AAAA,WAAfA,eAAe;EAAfA,eAAe,CAAfA,eAAe;EAAfA,eAAe,CAAfA,eAAe;EAAfA,eAAe,CAAfA,eAAe;AAAA,GAAfA,eAAe,+BAAfA,eAAe;AAc3B,MAAMC,UAA+B,GAAG,QAAkD;EAAA,IAAjD;IAAEC,QAAQ;IAAEC,KAAK,GAAGH,eAAe,CAACI;EAAO,CAAC;EACjF,MAAM,CAACC,cAAc,EAAEC,iBAAiB,CAAC,GAAG,IAAAC,eAAQ,EAAC,CAAC,CAAC;EACvD,MAAM,CAACC,mBAAmB,EAAEC,sBAAsB,CAAC,GAAG,IAAAF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cAAc,CAAC,EACrD,CAACA,cAAc,EAAEK,WAAW,CAAC,CAChC;EAED,oBACI,6BAAC,4BAAgB;IAAC,OAAO,EAAEQ;EAAY,gBACnC,6BAAC,gCAAoB;IACjB,uBAAuB,EAAE;MAAEY,MAAM,EAAEF;IAAU,CAAE;IAC/C,eAAe,EAAEX;EAAgB,EACnC,EACDA,eAAe,iBAAI,6BAAC,sCAA0B,QAAEf,QAAQ,CAA8B,CACxE;AAE3B,CAAC;AAEDD,UAAU,CAAC8B,WAAW,GAAG,YAAY;AAAC,eAEvB9B,UAAU;AAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chayns-components/typewriter",
|
|
3
|
-
"version": "5.0.0-beta.
|
|
3
|
+
"version": "5.0.0-beta.61",
|
|
4
4
|
"description": "A set of beautiful React components for developing your own applications with chayns.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"chayns",
|
|
@@ -63,5 +63,5 @@
|
|
|
63
63
|
"publishConfig": {
|
|
64
64
|
"access": "public"
|
|
65
65
|
},
|
|
66
|
-
"gitHead": "
|
|
66
|
+
"gitHead": "f76b5e0dc95bc6d34733c758db6e33f03c91f568"
|
|
67
67
|
}
|