@chayns-components/typewriter 5.0.0-beta.103 → 5.0.0-beta.104

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.
@@ -14,6 +14,11 @@ export type TypewriterProps = {
14
14
  * The text to type
15
15
  */
16
16
  children: ReactElement | ReactElement[] | string | string[];
17
+ /**
18
+ * Function that is executed when the typewriter animation has finished. This function will not
19
+ * be executed if multiple texts are used.
20
+ */
21
+ onFinish?: VoidFunction;
17
22
  /**
18
23
  * Waiting time before the typewriter resets the content if multiple texts are given
19
24
  */
@@ -25,7 +30,8 @@ export type TypewriterProps = {
25
30
  */
26
31
  shouldSortChildrenRandomly?: boolean;
27
32
  /**
28
- * Specifies whether the reset of the text should be animated with a backspace animation for multiple texts.
33
+ * Specifies whether the reset of the text should be animated with a backspace animation for
34
+ * multiple texts.
29
35
  */
30
36
  shouldUseResetAnimation?: boolean;
31
37
  /**
@@ -10,23 +10,25 @@ var _Typewriter = require("./Typewriter.styles");
10
10
  var _utils = require("./utils");
11
11
  function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
12
12
  function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
13
- let TypewriterResetDelay;
14
- exports.TypewriterResetDelay = TypewriterResetDelay;
15
- (function (TypewriterResetDelay) {
13
+ // noinspection JSUnusedGlobalSymbols
14
+ let TypewriterResetDelay = /*#__PURE__*/function (TypewriterResetDelay) {
16
15
  TypewriterResetDelay[TypewriterResetDelay["Slow"] = 4000] = "Slow";
17
16
  TypewriterResetDelay[TypewriterResetDelay["Medium"] = 2000] = "Medium";
18
17
  TypewriterResetDelay[TypewriterResetDelay["Fast"] = 1000] = "Fast";
19
- })(TypewriterResetDelay || (exports.TypewriterResetDelay = TypewriterResetDelay = {}));
20
- let TypewriterSpeed;
21
- exports.TypewriterSpeed = TypewriterSpeed;
22
- (function (TypewriterSpeed) {
18
+ return TypewriterResetDelay;
19
+ }({}); // noinspection JSUnusedGlobalSymbols
20
+ exports.TypewriterResetDelay = TypewriterResetDelay;
21
+ let TypewriterSpeed = /*#__PURE__*/function (TypewriterSpeed) {
23
22
  TypewriterSpeed[TypewriterSpeed["Slow"] = 40] = "Slow";
24
23
  TypewriterSpeed[TypewriterSpeed["Medium"] = 30] = "Medium";
25
24
  TypewriterSpeed[TypewriterSpeed["Fast"] = 20] = "Fast";
26
- })(TypewriterSpeed || (exports.TypewriterSpeed = TypewriterSpeed = {}));
25
+ return TypewriterSpeed;
26
+ }({});
27
+ exports.TypewriterSpeed = TypewriterSpeed;
27
28
  const Typewriter = _ref => {
28
29
  let {
29
30
  children,
31
+ onFinish,
30
32
  resetDelay = TypewriterResetDelay.Medium,
31
33
  shouldSortChildrenRandomly = false,
32
34
  shouldUseResetAnimation = false,
@@ -119,6 +121,11 @@ const Typewriter = _ref => {
119
121
  setShownCharCount(0);
120
122
  }
121
123
  }, [charactersCount]);
124
+ (0, _react.useEffect)(() => {
125
+ if (!isAnimatingText && typeof onFinish === 'function') {
126
+ onFinish();
127
+ }
128
+ }, [isAnimatingText, onFinish]);
122
129
  const shownText = (0, _react.useMemo)(() => (0, _utils.getSubTextFromHTML)(textContent, shownCharCount), [shownCharCount, textContent]);
123
130
  return /*#__PURE__*/_react.default.createElement(_Typewriter.StyledTypewriter, {
124
131
  onClick: handleClick
@@ -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 =\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 || '&#8203;' }}\n />\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,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,K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,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;IACvBF,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"}
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","onFinish","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\n// noinspection JSUnusedGlobalSymbols\nexport enum TypewriterResetDelay {\n Slow = 4000,\n Medium = 2000,\n Fast = 1000,\n}\n\n// noinspection JSUnusedGlobalSymbols\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 * Function that is executed when the typewriter animation has finished. This function will not\n * be executed if multiple texts are used.\n */\n onFinish?: VoidFunction;\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\n * 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 onFinish,\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 useEffect(() => {\n if (!isAnimatingText && typeof onFinish === 'function') {\n onFinish();\n }\n }, [isAnimatingText, onFinish]);\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 || '&#8203;' }}\n />\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,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,GAA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AK,CAAC;MAChCE,iBAAiB,CAAC,CAAC,CAAC;IACxB;EACJ,CAAC,EAAE,CAACL,eAAe,CAAC,CAAC;EAErB,IAAAa,gBAAS,EAAC,MAAM;IACZ,IAAI,CAACL,eAAe,IAAI,OAAO9B,QAAQ,KAAK,UAAU,EAAE;MACpDA,QAAQ,EAAE;IACd;EACJ,CAAC,EAAE,CAAC8B,eAAe,EAAE9B,QAAQ,CAAC,CAAC;EAE/B,MAAM2C,SAAS,GAAG,IAAAjC,cAAO,EACrB,MAAM,IAAAkC,yBAAkB,EAAC3B,WAAW,EAAES,cAAc,CAAC,EACrD,CAACA,cAAc,EAAET,WAAW,CAAC,CAChC;EAED,oBACInD,MAAA,CAAAa,OAAA,CAAAkE,aAAA,CAAC3E,WAAA,CAAA4E,gBAAgB;IAACC,OAAO,EAAEhB;EAAY,GAClCD,eAAe,gBACZhE,MAAA,CAAAa,OAAA,CAAAkE,aAAA,CAAC3E,WAAA,CAAA8E,oBAAoB;IACjBC,uBAAuB,EAAE;MAAEC,MAAM,EAAEP;IAAU,CAAE;IAC/Cb,eAAe;EAAA,EACjB,gBAEFhE,MAAA,CAAAa,OAAA,CAAAkE,aAAA,CAAC3E,WAAA,CAAA8E,oBAAoB,QAAEvC,cAAc,CACxC,EACAqB,eAAe,iBACZhE,MAAA,CAAAa,OAAA,CAAAkE,aAAA,CAAC3E,WAAA,CAAAiF,0BAA0B;IACvBF,uBAAuB,EAAE;MAAEC,MAAM,EAAEjC,WAAW,IAAI;IAAU;EAAE,EAErE,CACc;AAE3B,CAAC;AAEDpB,UAAU,CAACuD,WAAW,GAAG,YAAY;AAAC,IAAAC,QAAA,GAEvBxD,UAAU;AAAAF,OAAA,CAAAhB,OAAA,GAAA0E,QAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chayns-components/typewriter",
3
- "version": "5.0.0-beta.103",
3
+ "version": "5.0.0-beta.104",
4
4
  "description": "A set of beautiful React components for developing your own applications with chayns.",
5
5
  "keywords": [
6
6
  "chayns",
@@ -34,22 +34,22 @@
34
34
  },
35
35
  "devDependencies": {
36
36
  "@babel/cli": "^7.21.0",
37
- "@babel/core": "^7.21.3",
38
- "@babel/preset-env": "^7.20.2",
37
+ "@babel/core": "^7.21.4",
38
+ "@babel/preset-env": "^7.21.4",
39
39
  "@babel/preset-react": "^7.18.6",
40
- "@babel/preset-typescript": "^7.21.0",
41
- "@types/react": "^17.0.53",
40
+ "@babel/preset-typescript": "^7.21.4",
41
+ "@types/react": "^17.0.58",
42
42
  "@types/react-dom": "^17.0.19",
43
43
  "@types/styled-components": "^5.1.26",
44
44
  "@types/uuid": "^9.0.1",
45
45
  "babel-loader": "^8.3.0",
46
- "lerna": "^6.5.1",
46
+ "lerna": "^6.6.1",
47
47
  "react": "^17.0.2",
48
48
  "react-dom": "^17.0.2",
49
49
  "typescript": "^4.9.5"
50
50
  },
51
51
  "dependencies": {
52
- "@chayns-components/core": "^5.0.0-beta.98",
52
+ "@chayns-components/core": "^5.0.0-beta.104",
53
53
  "@chayns/colors": "^2.0.0",
54
54
  "clsx": "^1.2.1",
55
55
  "framer-motion": "^6.5.1",
@@ -63,5 +63,5 @@
63
63
  "publishConfig": {
64
64
  "access": "public"
65
65
  },
66
- "gitHead": "5c33441135ab4b03adc14e0840321608b820fd19"
66
+ "gitHead": "14f15be7baf4fd754b013d36f1b88b2f9a8c4f77"
67
67
  }