@chayns-components/typewriter 5.0.0-beta.102 → 5.0.0-beta.103
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.
|
@@ -50,7 +50,7 @@ const Typewriter = _ref => {
|
|
|
50
50
|
const [isResetAnimationActive, setIsResetAnimationActive] = (0, _react.useState)(false);
|
|
51
51
|
const [shownCharCount, setShownCharCount] = (0, _react.useState)(charactersCount > 0 ? 0 : textContent.length);
|
|
52
52
|
const [shouldStopAnimation, setShouldStopAnimation] = (0, _react.useState)(false);
|
|
53
|
-
const isAnimatingText = shownCharCount !== textContent.length || areMultipleChildrenGiven;
|
|
53
|
+
const isAnimatingText = shownCharCount !== textContent.length || areMultipleChildrenGiven || textContent.length === 0;
|
|
54
54
|
const handleClick = (0, _react.useCallback)(() => {
|
|
55
55
|
setShouldStopAnimation(true);
|
|
56
56
|
}, []);
|
|
@@ -129,7 +129,7 @@ const Typewriter = _ref => {
|
|
|
129
129
|
isAnimatingText: true
|
|
130
130
|
}) : /*#__PURE__*/_react.default.createElement(_Typewriter.StyledTypewriterText, null, sortedChildren), isAnimatingText && /*#__PURE__*/_react.default.createElement(_Typewriter.StyledTypewriterPseudoText, {
|
|
131
131
|
dangerouslySetInnerHTML: {
|
|
132
|
-
__html: textContent
|
|
132
|
+
__html: textContent || '​'
|
|
133
133
|
}
|
|
134
134
|
}));
|
|
135
135
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Typewriter.js","names":["_react","_interopRequireWildcard","require","_server","_Typewriter","_utils","_getRequireWildcardCache","nodeInterop","WeakMap","cacheBabelInterop","cacheNodeInterop","obj","__esModule","default","cache","has","get","newObj","hasPropertyDescriptor","Object","defineProperty","getOwnPropertyDescriptor","key","prototype","hasOwnProperty","call","desc","set","TypewriterResetDelay","exports","TypewriterSpeed","Typewriter","_ref","children","resetDelay","Medium","shouldSortChildrenRandomly","shouldUseResetAnimation","speed","currentChildrenIndex","setCurrentChildrenIndex","useState","sortedChildren","useMemo","Array","isArray","shuffleArray","areMultipleChildrenGiven","childrenCount","length","textContent","currentChildren","React","isValidElement","renderToString","charactersCount","getCharactersCount","isResetAnimationActive","setIsResetAnimationActive","shownCharCount","setShownCharCount","shouldStopAnimation","setShouldStopAnimation","isAnimatingText","handleClick","useCallback","handleSetNextChildrenIndex","newIndex","useEffect","interval","window","setInterval","prevState","nextState","clearInterval","setTimeout","shownText","getSubTextFromHTML","createElement","StyledTypewriter","onClick","StyledTypewriterText","dangerouslySetInnerHTML","__html","StyledTypewriterPseudoText","displayName","_default"],"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, shuffleArray } from './utils';\n\nexport enum TypewriterResetDelay {\n Slow = 4000,\n Medium = 2000,\n Fast = 1000,\n}\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 | ReactElement[] | string | string[];\n /**\n * Waiting time before the typewriter resets the content if multiple texts are given\n */\n resetDelay?: TypewriterResetDelay;\n /**\n * Specifies whether the children should be sorted randomly if there are multiple texts.\n * This makes the typewriter start with a different text each time and also changes them\n * in a random order.\n */\n shouldSortChildrenRandomly?: boolean;\n /**\n * Specifies whether the reset of the text should be animated with a backspace animation for multiple texts.\n */\n shouldUseResetAnimation?: boolean;\n /**\n * The speed of the animation. Use the TypewriterSpeed enum for this prop.\n */\n speed?: TypewriterSpeed;\n};\n\nconst Typewriter: FC<TypewriterProps> = ({\n children,\n resetDelay = TypewriterResetDelay.Medium,\n shouldSortChildrenRandomly = false,\n shouldUseResetAnimation = false,\n speed = TypewriterSpeed.Medium,\n}) => {\n const [currentChildrenIndex, setCurrentChildrenIndex] = useState(0);\n\n const sortedChildren = useMemo(\n () =>\n Array.isArray(children) && shouldSortChildrenRandomly\n ? shuffleArray<ReactElement | string>(children)\n : children,\n [children, shouldSortChildrenRandomly]\n );\n\n const areMultipleChildrenGiven = Array.isArray(sortedChildren);\n const childrenCount = areMultipleChildrenGiven ? sortedChildren.length : 1;\n\n const textContent = useMemo(() => {\n if (areMultipleChildrenGiven) {\n const currentChildren = sortedChildren[currentChildrenIndex];\n\n if (currentChildren) {\n return React.isValidElement(currentChildren)\n ? renderToString(currentChildren)\n : currentChildren;\n }\n\n return '';\n }\n\n return React.isValidElement(sortedChildren)\n ? renderToString(sortedChildren)\n : sortedChildren;\n }, [areMultipleChildrenGiven, currentChildrenIndex, sortedChildren]);\n\n const charactersCount = useMemo(() => getCharactersCount(textContent), [textContent]);\n\n const [isResetAnimationActive, setIsResetAnimationActive] = useState(false);\n const [shownCharCount, setShownCharCount] = useState(\n charactersCount > 0 ? 0 : textContent.length\n );\n const [shouldStopAnimation, setShouldStopAnimation] = useState(false);\n\n const isAnimatingText = shownCharCount !== textContent.length || areMultipleChildrenGiven;\n\n const handleClick = useCallback(() => {\n setShouldStopAnimation(true);\n }, []);\n\n const handleSetNextChildrenIndex = useCallback(\n () =>\n setCurrentChildrenIndex(() => {\n let newIndex = currentChildrenIndex + 1;\n\n if (newIndex > childrenCount - 1) {\n newIndex = 0;\n }\n\n return newIndex;\n }),\n [childrenCount, currentChildrenIndex]\n );\n\n useEffect(() => {\n let interval: number | undefined;\n\n if (shouldStopAnimation || charactersCount === 0) {\n setShownCharCount(textContent.length);\n } else if (isResetAnimationActive) {\n interval = window.setInterval(() => {\n setShownCharCount((prevState) => {\n const nextState = prevState - 1;\n\n if (nextState === 0) {\n window.clearInterval(interval);\n\n if (areMultipleChildrenGiven) {\n setTimeout(() => {\n setIsResetAnimationActive(false);\n handleSetNextChildrenIndex();\n }, resetDelay);\n }\n }\n\n return nextState;\n });\n }, speed);\n } else {\n interval = window.setInterval(() => {\n setShownCharCount((prevState) => {\n let nextState = prevState + 1;\n\n if (nextState === charactersCount) {\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 if (areMultipleChildrenGiven) {\n setTimeout(() => {\n if (shouldUseResetAnimation) {\n setIsResetAnimationActive(true);\n } else {\n setShownCharCount(0);\n setTimeout(handleSetNextChildrenIndex, resetDelay / 2);\n }\n }, resetDelay);\n }\n }\n\n return nextState;\n });\n }, speed);\n }\n\n return () => {\n window.clearInterval(interval);\n };\n }, [\n shouldStopAnimation,\n speed,\n textContent.length,\n charactersCount,\n isResetAnimationActive,\n areMultipleChildrenGiven,\n resetDelay,\n childrenCount,\n handleSetNextChildrenIndex,\n shouldUseResetAnimation,\n ]);\n\n useEffect(() => {\n if (charactersCount) {\n setIsResetAnimationActive(false);\n setShownCharCount(0);\n }\n }, [charactersCount]);\n\n const shownText = useMemo(\n () => getSubTextFromHTML(textContent, shownCharCount),\n [shownCharCount, textContent]\n );\n\n return (\n <StyledTypewriter onClick={handleClick}>\n {isAnimatingText ? (\n <StyledTypewriterText\n dangerouslySetInnerHTML={{ __html: shownText }}\n isAnimatingText\n />\n ) : (\n <StyledTypewriterText>{sortedChildren}</StyledTypewriterText>\n )}\n {isAnimatingText && (\n <StyledTypewriterPseudoText dangerouslySetInnerHTML={{ __html: textContent }} />\n )}\n </StyledTypewriter>\n );\n};\n\nTypewriter.displayName = 'Typewriter';\n\nexport default Typewriter;\n"],"mappings":";;;;;;AAAA,IAAAA,MAAA,GAAAC,uBAAA,CAAAC,OAAA;AACA,IAAAC,OAAA,GAAAD,OAAA;AACA,IAAAE,WAAA,GAAAF,OAAA;AAKA,IAAAG,MAAA,GAAAH,OAAA;AAA+E,SAAAI,yBAAAC,WAAA,eAAAC,OAAA,kCAAAC,iBAAA,OAAAD,OAAA,QAAAE,gBAAA,OAAAF,OAAA,YA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mB,EACnBvB,KAAK,EACLY,WAAW,CAACD,MAAM,EAClBM,eAAe,EACfE,sBAAsB,EACtBV,wBAAwB,EACxBb,UAAU,EACVc,aAAa,EACbkB,0BAA0B,EAC1B7B,uBAAuB,CAC1B,CAAC;EAEF,IAAA+B,gBAAS,EAAC,MAAM;IACZ,IAAIb,eAAe,EAAE;MACjBG,yBAAyB,CAAC,KAAK,CAAC;MAChCE,iBAAiB,CAAC,CAAC,CAAC;IACxB;EACJ,CAAC,EAAE,CAACL,eAAe,CAAC,CAAC;EAErB,MAAMqB,SAAS,GAAG,IAAAjC,cAAO,EACrB,MAAM,IAAAkC,yBAAkB,EAAC3B,WAAW,EAAES,cAAc,CAAC,EACrD,CAACA,cAAc,EAAET,WAAW,CAAC,CAChC;EAED,oBACIlD,MAAA,CAAAa,OAAA,CAAAiE,aAAA,CAAC1E,WAAA,CAAA2E,gBAAgB;IAACC,OAAO,EAAEhB;EAAY,GAClCD,eAAe,gBACZ/D,MAAA,CAAAa,OAAA,CAAAiE,aAAA,CAAC1E,WAAA,CAAA6E,oBAAoB;IACjBC,uBAAuB,EAAE;MAAEC,MAAM,EAAEP;IAAU,CAAE;IAC/Cb,eAAe;EAAA,EACjB,gBAEF/D,MAAA,CAAAa,OAAA,CAAAiE,aAAA,CAAC1E,WAAA,CAAA6E,oBAAoB,QAAEvC,cAAc,CACxC,EACAqB,eAAe,iBACZ/D,MAAA,CAAAa,OAAA,CAAAiE,aAAA,CAAC1E,WAAA,CAAAgF,0BAA0B;IAACF,uBAAuB,EAAE;MAAEC,MAAM,EAAEjC;IAAY;EAAE,EAChF,CACc;AAE3B,CAAC;AAEDnB,UAAU,CAACsD,WAAW,GAAG,YAAY;AAAC,IAAAC,QAAA,GAEvBvD,UAAU;AAAAF,OAAA,CAAAhB,OAAA,GAAAyE,QAAA"}
|
|
1
|
+
{"version":3,"file":"Typewriter.js","names":["_react","_interopRequireWildcard","require","_server","_Typewriter","_utils","_getRequireWildcardCache","nodeInterop","WeakMap","cacheBabelInterop","cacheNodeInterop","obj","__esModule","default","cache","has","get","newObj","hasPropertyDescriptor","Object","defineProperty","getOwnPropertyDescriptor","key","prototype","hasOwnProperty","call","desc","set","TypewriterResetDelay","exports","TypewriterSpeed","Typewriter","_ref","children","resetDelay","Medium","shouldSortChildrenRandomly","shouldUseResetAnimation","speed","currentChildrenIndex","setCurrentChildrenIndex","useState","sortedChildren","useMemo","Array","isArray","shuffleArray","areMultipleChildrenGiven","childrenCount","length","textContent","currentChildren","React","isValidElement","renderToString","charactersCount","getCharactersCount","isResetAnimationActive","setIsResetAnimationActive","shownCharCount","setShownCharCount","shouldStopAnimation","setShouldStopAnimation","isAnimatingText","handleClick","useCallback","handleSetNextChildrenIndex","newIndex","useEffect","interval","window","setInterval","prevState","nextState","clearInterval","setTimeout","shownText","getSubTextFromHTML","createElement","StyledTypewriter","onClick","StyledTypewriterText","dangerouslySetInnerHTML","__html","StyledTypewriterPseudoText","displayName","_default"],"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, shuffleArray } from './utils';\n\nexport enum TypewriterResetDelay {\n Slow = 4000,\n Medium = 2000,\n Fast = 1000,\n}\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 | ReactElement[] | string | string[];\n /**\n * Waiting time before the typewriter resets the content if multiple texts are given\n */\n resetDelay?: TypewriterResetDelay;\n /**\n * Specifies whether the children should be sorted randomly if there are multiple texts.\n * This makes the typewriter start with a different text each time and also changes them\n * in a random order.\n */\n shouldSortChildrenRandomly?: boolean;\n /**\n * Specifies whether the reset of the text should be animated with a backspace animation for multiple texts.\n */\n shouldUseResetAnimation?: boolean;\n /**\n * The speed of the animation. Use the TypewriterSpeed enum for this prop.\n */\n speed?: TypewriterSpeed;\n};\n\nconst Typewriter: FC<TypewriterProps> = ({\n children,\n resetDelay = TypewriterResetDelay.Medium,\n shouldSortChildrenRandomly = false,\n shouldUseResetAnimation = false,\n speed = TypewriterSpeed.Medium,\n}) => {\n const [currentChildrenIndex, setCurrentChildrenIndex] = useState(0);\n\n const sortedChildren = useMemo(\n () =>\n Array.isArray(children) && shouldSortChildrenRandomly\n ? shuffleArray<ReactElement | string>(children)\n : children,\n [children, shouldSortChildrenRandomly]\n );\n\n const areMultipleChildrenGiven = Array.isArray(sortedChildren);\n const childrenCount = areMultipleChildrenGiven ? sortedChildren.length : 1;\n\n const textContent = useMemo(() => {\n if (areMultipleChildrenGiven) {\n const currentChildren = sortedChildren[currentChildrenIndex];\n\n if (currentChildren) {\n return React.isValidElement(currentChildren)\n ? renderToString(currentChildren)\n : currentChildren;\n }\n\n return '';\n }\n\n return React.isValidElement(sortedChildren)\n ? renderToString(sortedChildren)\n : sortedChildren;\n }, [areMultipleChildrenGiven, currentChildrenIndex, sortedChildren]);\n\n const charactersCount = useMemo(() => getCharactersCount(textContent), [textContent]);\n\n const [isResetAnimationActive, setIsResetAnimationActive] = useState(false);\n const [shownCharCount, setShownCharCount] = useState(\n charactersCount > 0 ? 0 : textContent.length\n );\n const [shouldStopAnimation, setShouldStopAnimation] = useState(false);\n\n const isAnimatingText =\n shownCharCount !== textContent.length ||\n areMultipleChildrenGiven ||\n textContent.length === 0;\n\n const handleClick = useCallback(() => {\n setShouldStopAnimation(true);\n }, []);\n\n const handleSetNextChildrenIndex = useCallback(\n () =>\n setCurrentChildrenIndex(() => {\n let newIndex = currentChildrenIndex + 1;\n\n if (newIndex > childrenCount - 1) {\n newIndex = 0;\n }\n\n return newIndex;\n }),\n [childrenCount, currentChildrenIndex]\n );\n\n useEffect(() => {\n let interval: number | undefined;\n\n if (shouldStopAnimation || charactersCount === 0) {\n setShownCharCount(textContent.length);\n } else if (isResetAnimationActive) {\n interval = window.setInterval(() => {\n setShownCharCount((prevState) => {\n const nextState = prevState - 1;\n\n if (nextState === 0) {\n window.clearInterval(interval);\n\n if (areMultipleChildrenGiven) {\n setTimeout(() => {\n setIsResetAnimationActive(false);\n handleSetNextChildrenIndex();\n }, resetDelay);\n }\n }\n\n return nextState;\n });\n }, speed);\n } else {\n interval = window.setInterval(() => {\n setShownCharCount((prevState) => {\n let nextState = prevState + 1;\n\n if (nextState === charactersCount) {\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 if (areMultipleChildrenGiven) {\n setTimeout(() => {\n if (shouldUseResetAnimation) {\n setIsResetAnimationActive(true);\n } else {\n setShownCharCount(0);\n setTimeout(handleSetNextChildrenIndex, resetDelay / 2);\n }\n }, resetDelay);\n }\n }\n\n return nextState;\n });\n }, speed);\n }\n\n return () => {\n window.clearInterval(interval);\n };\n }, [\n shouldStopAnimation,\n speed,\n textContent.length,\n charactersCount,\n isResetAnimationActive,\n areMultipleChildrenGiven,\n resetDelay,\n childrenCount,\n handleSetNextChildrenIndex,\n shouldUseResetAnimation,\n ]);\n\n useEffect(() => {\n if (charactersCount) {\n setIsResetAnimationActive(false);\n setShownCharCount(0);\n }\n }, [charactersCount]);\n\n const shownText = useMemo(\n () => getSubTextFromHTML(textContent, shownCharCount),\n [shownCharCount, textContent]\n );\n\n return (\n <StyledTypewriter onClick={handleClick}>\n {isAnimatingText ? (\n <StyledTypewriterText\n dangerouslySetInnerHTML={{ __html: shownText }}\n isAnimatingText\n />\n ) : (\n <StyledTypewriterText>{sortedChildren}</StyledTypewriterText>\n )}\n {isAnimatingText && (\n <StyledTypewriterPseudoText\n dangerouslySetInnerHTML={{ __html: textContent || '​' }}\n />\n )}\n </StyledTypewriter>\n );\n};\n\nTypewriter.displayName = 'Typewriter';\n\nexport default Typewriter;\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vBF,uBAAuB,EAAE;MAAEC,MAAM,EAAEjC,WAAW,IAAI;IAAU;EAAE,EAErE,CACc;AAE3B,CAAC;AAEDnB,UAAU,CAACsD,WAAW,GAAG,YAAY;AAAC,IAAAC,QAAA,GAEvBvD,UAAU;AAAAF,OAAA,CAAAhB,OAAA,GAAAyE,QAAA"}
|
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.103",
|
|
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": "5c33441135ab4b03adc14e0840321608b820fd19"
|
|
67
67
|
}
|