@chayns-components/typewriter 5.0.0-beta.98 → 5.0.0-beta.99
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.
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { FC, ReactElement } from 'react';
|
|
2
2
|
export declare enum TypewriterResetDelay {
|
|
3
|
-
Slow =
|
|
4
|
-
Medium =
|
|
5
|
-
Fast =
|
|
3
|
+
Slow = 4000,
|
|
4
|
+
Medium = 2000,
|
|
5
|
+
Fast = 1000
|
|
6
6
|
}
|
|
7
7
|
export declare enum TypewriterSpeed {
|
|
8
8
|
Slow = 40,
|
|
@@ -18,6 +18,10 @@ export type TypewriterProps = {
|
|
|
18
18
|
* Waiting time before the typewriter resets the content if multiple texts are given
|
|
19
19
|
*/
|
|
20
20
|
resetDelay?: TypewriterResetDelay;
|
|
21
|
+
/**
|
|
22
|
+
* Specifies whether the reset of the text should be animated with a backspace animation for multiple texts.
|
|
23
|
+
*/
|
|
24
|
+
shouldUseResetAnimation?: boolean;
|
|
21
25
|
/**
|
|
22
26
|
* The speed of the animation. Use the TypewriterSpeed enum for this prop.
|
|
23
27
|
*/
|
|
@@ -13,9 +13,9 @@ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj &&
|
|
|
13
13
|
let TypewriterResetDelay;
|
|
14
14
|
exports.TypewriterResetDelay = TypewriterResetDelay;
|
|
15
15
|
(function (TypewriterResetDelay) {
|
|
16
|
-
TypewriterResetDelay[TypewriterResetDelay["Slow"] =
|
|
17
|
-
TypewriterResetDelay[TypewriterResetDelay["Medium"] =
|
|
18
|
-
TypewriterResetDelay[TypewriterResetDelay["Fast"] =
|
|
16
|
+
TypewriterResetDelay[TypewriterResetDelay["Slow"] = 4000] = "Slow";
|
|
17
|
+
TypewriterResetDelay[TypewriterResetDelay["Medium"] = 2000] = "Medium";
|
|
18
|
+
TypewriterResetDelay[TypewriterResetDelay["Fast"] = 1000] = "Fast";
|
|
19
19
|
})(TypewriterResetDelay || (exports.TypewriterResetDelay = TypewriterResetDelay = {}));
|
|
20
20
|
let TypewriterSpeed;
|
|
21
21
|
exports.TypewriterSpeed = TypewriterSpeed;
|
|
@@ -28,6 +28,7 @@ const Typewriter = _ref => {
|
|
|
28
28
|
let {
|
|
29
29
|
children,
|
|
30
30
|
resetDelay = TypewriterResetDelay.Medium,
|
|
31
|
+
shouldUseResetAnimation = false,
|
|
31
32
|
speed = TypewriterSpeed.Medium
|
|
32
33
|
} = _ref;
|
|
33
34
|
const [currentChildrenIndex, setCurrentChildrenIndex] = (0, _react.useState)(0);
|
|
@@ -51,6 +52,13 @@ const Typewriter = _ref => {
|
|
|
51
52
|
const handleClick = (0, _react.useCallback)(() => {
|
|
52
53
|
setShouldStopAnimation(true);
|
|
53
54
|
}, []);
|
|
55
|
+
const handleSetNextChildrenIndex = (0, _react.useCallback)(() => setCurrentChildrenIndex(() => {
|
|
56
|
+
let newIndex = currentChildrenIndex + 1;
|
|
57
|
+
if (newIndex > childrenCount - 1) {
|
|
58
|
+
newIndex = 0;
|
|
59
|
+
}
|
|
60
|
+
return newIndex;
|
|
61
|
+
}), [childrenCount, currentChildrenIndex]);
|
|
54
62
|
(0, _react.useEffect)(() => {
|
|
55
63
|
let interval;
|
|
56
64
|
if (shouldStopAnimation || charactersCount === 0) {
|
|
@@ -64,13 +72,7 @@ const Typewriter = _ref => {
|
|
|
64
72
|
if (areMultipleChildrenGiven) {
|
|
65
73
|
setTimeout(() => {
|
|
66
74
|
setIsResetAnimationActive(false);
|
|
67
|
-
|
|
68
|
-
let newIndex = currentIndex + 1;
|
|
69
|
-
if (newIndex > childrenCount - 1) {
|
|
70
|
-
newIndex = 0;
|
|
71
|
-
}
|
|
72
|
-
return newIndex;
|
|
73
|
-
});
|
|
75
|
+
handleSetNextChildrenIndex();
|
|
74
76
|
}, resetDelay);
|
|
75
77
|
}
|
|
76
78
|
}
|
|
@@ -91,7 +93,14 @@ const Typewriter = _ref => {
|
|
|
91
93
|
*/
|
|
92
94
|
nextState = textContent.length;
|
|
93
95
|
if (areMultipleChildrenGiven) {
|
|
94
|
-
setTimeout(
|
|
96
|
+
setTimeout(() => {
|
|
97
|
+
if (shouldUseResetAnimation) {
|
|
98
|
+
setIsResetAnimationActive(true);
|
|
99
|
+
} else {
|
|
100
|
+
setShownCharCount(0);
|
|
101
|
+
setTimeout(handleSetNextChildrenIndex, resetDelay / 2);
|
|
102
|
+
}
|
|
103
|
+
}, resetDelay);
|
|
95
104
|
}
|
|
96
105
|
}
|
|
97
106
|
return nextState;
|
|
@@ -101,7 +110,7 @@ const Typewriter = _ref => {
|
|
|
101
110
|
return () => {
|
|
102
111
|
window.clearInterval(interval);
|
|
103
112
|
};
|
|
104
|
-
}, [shouldStopAnimation, speed, textContent.length, charactersCount, isResetAnimationActive, areMultipleChildrenGiven, resetDelay, childrenCount]);
|
|
113
|
+
}, [shouldStopAnimation, speed, textContent.length, charactersCount, isResetAnimationActive, areMultipleChildrenGiven, resetDelay, childrenCount, handleSetNextChildrenIndex, shouldUseResetAnimation]);
|
|
105
114
|
(0, _react.useEffect)(() => {
|
|
106
115
|
if (charactersCount) {
|
|
107
116
|
setIsResetAnimationActive(false);
|
|
@@ -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","speed","currentChildrenIndex","setCurrentChildrenIndex","useState","areMultipleChildrenGiven","Array","isArray","childrenCount","length","textContent","useMemo","currentChildren","React","isValidElement","renderToString","charactersCount","getCharactersCount","isResetAnimationActive","setIsResetAnimationActive","shownCharCount","setShownCharCount","shouldStopAnimation","setShouldStopAnimation","isAnimatingText","handleClick","useCallback","useEffect","interval","window","setInterval","prevState","nextState","clearInterval","setTimeout","currentIndex","newIndex","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 } from './utils';\n\nexport enum TypewriterResetDelay {\n Slow = 3000,\n Medium = 1500,\n Fast = 750,\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 * 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 speed = TypewriterSpeed.Medium,\n}) => {\n const [currentChildrenIndex, setCurrentChildrenIndex] = useState(0);\n\n const areMultipleChildrenGiven = Array.isArray(children);\n const childrenCount = areMultipleChildrenGiven ? children.length : 1;\n\n const textContent = useMemo(() => {\n if (areMultipleChildrenGiven) {\n const currentChildren = children[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(children) ? renderToString(children) : children;\n }, [areMultipleChildrenGiven, children, currentChildrenIndex]);\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 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 setCurrentChildrenIndex((currentIndex) => {\n let newIndex = currentIndex + 1;\n\n if (newIndex > childrenCount - 1) {\n newIndex = 0;\n }\n\n return newIndex;\n });\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(setIsResetAnimationActive, resetDelay, true);\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 ]);\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>{children}</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;AAAiE,SAAAI,yBAAAC,WAAA,eAAAC,OAAA,kCAAAC,iBAAA,OAAAD,OAAA,QAAAE,gBAAA,OAAAF,OAAA,YAAAF,wBAAA,YAAAA,CAAAC,WAAA,WAAAA,WAAA,GAAAG,gBAAA,GAAAD,iBAAA,KAAAF,WAAA;AAAA,SAAAN,wBAAAU,GAAA,EAAAJ,WAAA,SAAAA,WAAA,IAAAI,GAAA,IAAAA,GAAA,CAAAC,UAAA,WAAAD,GAAA,QAAAA,GAAA,oBAAAA,GAAA,wBAAAA,GAAA,4BAAAE,OAAA,EAAAF,GAAA,UAAAG,KAAA,GAAAR,wBAAA,CAAAC,WAAA,OAAAO,KAAA,IAAAA,KAAA,CAAAC,GAAA,CAAAJ,GAAA,YAAAG,KAAA,CAAAE,GAAA,CAAAL,GAAA,SAAAM,MAAA,WAAAC,qBAAA,GAAAC,MAAA,CAAAC,cAAA,IAAAD,MAAA,CAAAE,wBAAA,WAAAC,GAAA,IAAAX,GAAA,QAAAW,GAAA,kBAAAH,MAAA,CAAAI,SAAA,CAAAC,cAAA,CAAAC,IAAA,CAAAd,GAAA,EAAAW,GAAA,SAAAI,IAAA,GAAAR,qBAAA,GAAAC,MAAA,CAAAE,wBAAA,CAAAV,GAAA,EAAAW,GAAA,cAAAI,IAAA,KAAAA,IAAA,CAAAV,GAAA,IAAAU,IAAA,CAAAC,GAAA,KAAAR,MAAA,CAAAC,cAAA,CAAAH,MAAA,EA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AAa,CAChB,CAAC;EAEF,IAAAmB,gBAAS,EAAC,MAAM;IACZ,IAAIX,eAAe,EAAE;MACjBG,yBAAyB,CAAC,KAAK,CAAC;MAChCE,iBAAiB,CAAC,CAAC,CAAC;IACxB;EACJ,CAAC,EAAE,CAACL,eAAe,CAAC,CAAC;EAErB,MAAMqB,SAAS,GAAG,IAAA1B,cAAO,EACrB,MAAM,IAAA2B,yBAAkB,EAAC5B,WAAW,EAAEU,cAAc,CAAC,EACrD,CAACA,cAAc,EAAEV,WAAW,CAAC,CAChC;EAED,oBACI7C,MAAA,CAAAa,OAAA,CAAA6D,aAAA,CAACtE,WAAA,CAAAuE,gBAAgB;IAACC,OAAO,EAAEhB;EAAY,GAClCD,eAAe,gBACZ3D,MAAA,CAAAa,OAAA,CAAA6D,aAAA,CAACtE,WAAA,CAAAyE,oBAAoB;IACjBC,uBAAuB,EAAE;MAAEC,MAAM,EAAEP;IAAU,CAAE;IAC/Cb,eAAe;EAAA,EACjB,gBAEF3D,MAAA,CAAAa,OAAA,CAAA6D,aAAA,CAACtE,WAAA,CAAAyE,oBAAoB,QAAE5C,QAAQ,CAClC,EACA0B,eAAe,iBACZ3D,MAAA,CAAAa,OAAA,CAAA6D,aAAA,CAACtE,WAAA,CAAA4E,0BAA0B;IAACF,uBAAuB,EAAE;MAAEC,MAAM,EAAElC;IAAY;EAAE,EAChF,CACc;AAE3B,CAAC;AAEDd,UAAU,CAACkD,WAAW,GAAG,YAAY;AAAC,IAAAC,QAAA,GAEvBnD,UAAU;AAAAF,OAAA,CAAAhB,OAAA,GAAAqE,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","shouldUseResetAnimation","speed","currentChildrenIndex","setCurrentChildrenIndex","useState","areMultipleChildrenGiven","Array","isArray","childrenCount","length","textContent","useMemo","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 } 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 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 shouldUseResetAnimation = false,\n speed = TypewriterSpeed.Medium,\n}) => {\n const [currentChildrenIndex, setCurrentChildrenIndex] = useState(0);\n\n const areMultipleChildrenGiven = Array.isArray(children);\n const childrenCount = areMultipleChildrenGiven ? children.length : 1;\n\n const textContent = useMemo(() => {\n if (areMultipleChildrenGiven) {\n const currentChildren = children[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(children) ? renderToString(children) : children;\n }, [areMultipleChildrenGiven, children, currentChildrenIndex]);\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>{children}</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;AAAiE,SAAAI,yBAAAC,WAAA,eAAAC,OAAA,kCAAAC,iBAAA,OAAAD,OAAA,QAAAE,gBAAA,OAAAF,OAAA,YAAAF,wBAAA,YAAAA,CAAAC,WAAA,WAAAA,WAAA,GAAAG,gBAAA,GAAAD,iBAAA,KAAAF,WAAA;AAAA,SAAAN,wBAAAU,GAAA,EAAAJ,WAAA,SAAAA,WAAA,IAAAI,GAAA,IAAAA,GAAA,CAAAC,UAAA,WAAAD,GAAA,QAAAA,GAAA,oBAAAA,GAAA,wBAAAA,GAAA,4BAAAE,OAAA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AChC;EAED,oBACI9C,MAAA,CAAAa,OAAA,CAAA8D,aAAA,CAACvE,WAAA,CAAAwE,gBAAgB;IAACC,OAAO,EAAEhB;EAAY,GAClCD,eAAe,gBACZ5D,MAAA,CAAAa,OAAA,CAAA8D,aAAA,CAACvE,WAAA,CAAA0E,oBAAoB;IACjBC,uBAAuB,EAAE;MAAEC,MAAM,EAAEP;IAAU,CAAE;IAC/Cb,eAAe;EAAA,EACjB,gBAEF5D,MAAA,CAAAa,OAAA,CAAA8D,aAAA,CAACvE,WAAA,CAAA0E,oBAAoB,QAAE7C,QAAQ,CAClC,EACA2B,eAAe,iBACZ5D,MAAA,CAAAa,OAAA,CAAA8D,aAAA,CAACvE,WAAA,CAAA6E,0BAA0B;IAACF,uBAAuB,EAAE;MAAEC,MAAM,EAAElC;IAAY;EAAE,EAChF,CACc;AAE3B,CAAC;AAEDf,UAAU,CAACmD,WAAW,GAAG,YAAY;AAAC,IAAAC,QAAA,GAEvBpD,UAAU;AAAAF,OAAA,CAAAhB,OAAA,GAAAsE,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.99",
|
|
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": "792291e8747f1f6989cf9bb1ee8bfbc4c9028e5a"
|
|
67
67
|
}
|