@chayns-components/typewriter 5.0.53-alpha.1 → 5.0.55
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.
- package/lib/cjs/components/typewriter/AnimatedTypewriterText.js +3 -3
- package/lib/cjs/components/typewriter/AnimatedTypewriterText.js.map +1 -1
- package/lib/cjs/components/typewriter/Typewriter.js +98 -32
- package/lib/cjs/components/typewriter/Typewriter.js.map +1 -1
- package/lib/esm/components/typewriter/AnimatedTypewriterText.js +3 -3
- package/lib/esm/components/typewriter/AnimatedTypewriterText.js.map +1 -1
- package/lib/esm/components/typewriter/Typewriter.js +98 -32
- package/lib/esm/components/typewriter/Typewriter.js.map +1 -1
- package/lib/types/components/typewriter/Typewriter.d.ts +34 -43
- package/package.json +3 -3
- package/lib/cjs/components/typewriter/useTypewriterAnimation.js +0 -237
- package/lib/cjs/components/typewriter/useTypewriterAnimation.js.map +0 -1
- package/lib/esm/components/typewriter/useTypewriterAnimation.js +0 -231
- package/lib/esm/components/typewriter/useTypewriterAnimation.js.map +0 -1
- package/lib/types/components/typewriter/useTypewriterAnimation.d.ts +0 -30
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Typewriter.js","names":["ColorSchemeProvider","useColorScheme","ChaynsProvider","useFunctions","useValues","React","useCallback","useEffect","useLayoutEffect","useMemo","useRef","useState","createPortal","renderToString","CursorType","TypewriterDelay","TypewriterSpeed","AnimatedTypewriterText","StyledTypewriter","StyledTypewriterPseudoText","StyledTypewriterText","calculateAutoSpeed","getCharactersCount","getSubTextFromHTML","shuffleArray","updateChunkStreamingSpeedEMA","useTypewriterAnimation","useIsomorphicLayoutEffect","window","Typewriter","children","cursorType","Default","nextTextDelay","Medium","onFinish","onResetAnimationEnd","animationSteps","onResetAnimationStart","onTypingAnimationEnd","onTypingAnimationStart","pseudoChildren","resetDelay","shouldForceCursorAnimation","shouldHideCursor","shouldRemainSingleLine","shouldSortChildrenRandomly","shouldUseAnimationHeight","shouldUseResetAnimation","shouldWaitForContent","speed","resetSpeed","startDelay","None","textStyle","shouldCalcAutoSpeed","autoSpeedBaseFactor","currentChildrenIndex","setCurrentChildrenIndex","hasRenderedChildrenOnce","setHasRenderedChildrenOnce","autoSpeed","autoSteps","functions","values","colorScheme","sortedChildren","Array","isArray","areMultipleChildrenGiven","childrenCount","length","textContent","currentChildren","isValidElement","createElement","data","isModule","color","designSettings","colorMode","style","display","className","charactersCount","chunkIntervalExponentialMovingAverage","lastLength","ema","current","currentLength","state","undefined","calculatedAutoSpeed","steps","handleSetNextChildrenIndex","prevIndex","nextIndex","handleClick","isResetAnimationActive","shownCharCount","shouldPreventBlinkingCursor","autoSpeedRef","autoStepsRef","onAdvanceText","textContentLength","isAnimatingText","shownText","pseudoTextHTML","pseudoText","$cursorType","onClick","$isAnimatingText","$shouldHideCursor","$shouldPreventBlinkAnimation","$shouldRemainSingleLine","dangerouslySetInnerHTML","__html","position","visibility","document","body","displayName"],"sources":["../../../../src/components/typewriter/Typewriter.tsx"],"sourcesContent":["import { ColorSchemeProvider, useColorScheme } from '@chayns-components/core';\nimport { ChaynsProvider, useFunctions, useValues } from 'chayns-api';\nimport React, {\n FC,\n ReactElement,\n useCallback,\n useEffect,\n useLayoutEffect,\n useMemo,\n useRef,\n useState,\n} from 'react';\nimport { createPortal } from 'react-dom';\nimport { renderToString } from 'react-dom/server';\nimport { CSSPropertiesWithVars } from 'styled-components/dist/types';\nimport { CursorType } from '../../types/cursor';\nimport { TypewriterDelay, TypewriterSpeed } from '../../types/speed';\nimport AnimatedTypewriterText from './AnimatedTypewriterText';\nimport {\n StyledTypewriter,\n StyledTypewriterPseudoText,\n StyledTypewriterText,\n} from './Typewriter.styles';\nimport {\n calculateAutoSpeed,\n ChunkStreamingSpeedState,\n getCharactersCount,\n getSubTextFromHTML,\n shuffleArray,\n updateChunkStreamingSpeedEMA,\n} from './utils';\nimport useTypewriterAnimation from './useTypewriterAnimation';\n\nconst useIsomorphicLayoutEffect = typeof window !== 'undefined' ? useLayoutEffect : useEffect;\n\nexport type TypewriterProps = {\n /**\n * The number of characters that will be animated per animation step.\n *\n * The step is advanced by the requestAnimationFrame-driven scheduler. When\n * `shouldCalcAutoSpeed` is enabled, the component may override this value dynamically.\n */\n animationSteps?: number;\n /**\n * The content to type.\n *\n * Strings are animated directly. React elements and arrays are rendered to HTML first so the\n * typewriter can preserve nested markup and inline components.\n */\n children: ReactElement | ReactElement[] | string | string[];\n /**\n * The type of the cursor. Use the `CursorType` enum for this prop.\n */\n cursorType?: CursorType;\n /**\n * The delay in milliseconds before the next text is shown.\n *\n * This prop is only used when `children` is an array.\n */\n nextTextDelay?: TypewriterDelay;\n /**\n * Function that is executed when the entire typewriter animation has finished.\n *\n * This callback is not used while more texts are still queued.\n */\n onFinish?: VoidFunction;\n /**\n * Function that is executed when the reset animation has finished.\n *\n * This callback only runs when `shouldUseResetAnimation` is enabled.\n */\n onResetAnimationEnd?: VoidFunction;\n /**\n * Function that is executed when the reset animation has started.\n *\n * This callback only runs when `shouldUseResetAnimation` is enabled.\n */\n onResetAnimationStart?: VoidFunction;\n /**\n * Function that is executed when the typing animation has finished.\n *\n * If multiple texts are given, this function will be executed for each text.\n */\n onTypingAnimationEnd?: VoidFunction;\n /**\n * Function that is executed when the typing animation has started.\n *\n * If multiple texts are given, this function will be executed for each text.\n */\n onTypingAnimationStart?: VoidFunction;\n /**\n * Invisible content that defines the reserved layout size while the animation is running.\n *\n * When omitted, the visible `children` are used as the layout template.\n */\n pseudoChildren?: ReactElement | string;\n /**\n * Waiting time in milliseconds before the typewriter resets the text.\n *\n * This prop is only used when `children` is an array.\n */\n resetDelay?: TypewriterDelay;\n /**\n * The reset speed of the animation. Use the `TypewriterSpeed` enum for this prop.\n */\n resetSpeed?: TypewriterSpeed | number;\n /**\n * Keeps the cursor animated even if the current text is not actively changing.\n */\n shouldForceCursorAnimation?: boolean;\n /**\n * Hides the cursor entirely.\n */\n shouldHideCursor?: boolean;\n /**\n * Keeps the content on a single line and applies ellipsis overflow handling.\n */\n shouldRemainSingleLine?: boolean;\n /**\n * Randomizes the order of `children` when multiple texts are provided.\n */\n shouldSortChildrenRandomly?: boolean;\n /**\n * Makes the hidden sizing text follow the currently revealed chunk instead of the full content.\n */\n shouldUseAnimationHeight?: boolean;\n /**\n * Derives the typing cadence from the observed streaming rate.\n */\n shouldCalcAutoSpeed?: boolean;\n /**\n * Base factor used to initialize the auto-speed EMA.\n */\n autoSpeedBaseFactor?: number;\n /**\n * Animates the reset between multiple texts with a backspace-like effect.\n */\n shouldUseResetAnimation?: boolean;\n /**\n * Keeps the typewriter active while waiting for new content to arrive.\n */\n shouldWaitForContent?: boolean;\n /**\n * The speed of the animation. Use the `TypewriterSpeed` enum for this prop.\n */\n speed?: TypewriterSpeed | number;\n /**\n * The delay in milliseconds before the typewriter starts typing.\n */\n startDelay?: TypewriterDelay;\n /**\n * Inline styles for the visible text element.\n */\n textStyle?: CSSPropertiesWithVars;\n};\n\nconst Typewriter: FC<TypewriterProps> = ({\n children,\n cursorType = CursorType.Default,\n nextTextDelay = TypewriterDelay.Medium,\n onFinish,\n onResetAnimationEnd,\n animationSteps = 1,\n onResetAnimationStart,\n onTypingAnimationEnd,\n onTypingAnimationStart,\n pseudoChildren,\n resetDelay = TypewriterDelay.Medium,\n shouldForceCursorAnimation = false,\n shouldHideCursor = false,\n shouldRemainSingleLine = false,\n shouldSortChildrenRandomly = false,\n shouldUseAnimationHeight = false,\n shouldUseResetAnimation = false,\n shouldWaitForContent,\n speed = TypewriterSpeed.Medium,\n resetSpeed = speed,\n startDelay = TypewriterDelay.None,\n textStyle,\n shouldCalcAutoSpeed = false,\n autoSpeedBaseFactor = 2000,\n}) => {\n const [currentChildrenIndex, setCurrentChildrenIndex] = useState(0);\n const [hasRenderedChildrenOnce, setHasRenderedChildrenOnce] = useState(false);\n const autoSpeed = useRef<number>();\n const autoSteps = useRef<number>(animationSteps);\n\n const functions = useFunctions();\n const values = useValues();\n\n const colorScheme = useColorScheme();\n\n useIsomorphicLayoutEffect(() => {\n if (children) {\n setHasRenderedChildrenOnce(false);\n }\n }, [children]);\n\n useEffect(() => {\n if (!hasRenderedChildrenOnce) {\n setHasRenderedChildrenOnce(true);\n }\n }, [hasRenderedChildrenOnce]);\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(\n <ChaynsProvider data={values} functions={functions} isModule>\n <ColorSchemeProvider\n color={colorScheme?.designSettings?.color}\n colorMode={colorScheme?.designSettings?.colorMode}\n style={{ display: 'inline' }}\n >\n <span className=\"notranslate\">{currentChildren}</span>\n </ColorSchemeProvider>\n </ChaynsProvider>,\n )\n : (currentChildren as string);\n }\n\n return '';\n }\n\n return React.isValidElement(sortedChildren)\n ? renderToString(\n <ChaynsProvider data={values} functions={functions} isModule>\n <ColorSchemeProvider\n color={colorScheme?.designSettings?.color}\n colorMode={colorScheme?.designSettings?.colorMode}\n style={{ display: 'inline' }}\n >\n <span className=\"notranslate\">{sortedChildren}</span>\n </ColorSchemeProvider>\n </ChaynsProvider>,\n )\n : (sortedChildren as string);\n }, [\n areMultipleChildrenGiven,\n colorScheme?.designSettings?.color,\n colorScheme?.designSettings?.colorMode,\n currentChildrenIndex,\n functions,\n sortedChildren,\n values,\n ]);\n\n const charactersCount = useMemo(() => getCharactersCount(textContent), [textContent]);\n const chunkIntervalExponentialMovingAverage = useRef<ChunkStreamingSpeedState>({\n lastLength: charactersCount,\n ema: charactersCount / (autoSpeedBaseFactor / 1000),\n });\n\n useEffect(() => {\n if (shouldUseResetAnimation) {\n chunkIntervalExponentialMovingAverage.current = {\n ema: charactersCount / (autoSpeedBaseFactor / 1000),\n lastLength: charactersCount,\n };\n }\n chunkIntervalExponentialMovingAverage.current = updateChunkStreamingSpeedEMA({\n currentLength: charactersCount,\n state: chunkIntervalExponentialMovingAverage.current,\n });\n }, [autoSpeedBaseFactor, charactersCount, shouldUseResetAnimation]);\n\n useEffect(() => {\n if (!shouldCalcAutoSpeed) {\n autoSpeed.current = undefined;\n autoSteps.current = animationSteps;\n return;\n }\n const { speed: calculatedAutoSpeed, steps } = calculateAutoSpeed(\n chunkIntervalExponentialMovingAverage.current.ema,\n );\n\n autoSpeed.current = calculatedAutoSpeed;\n autoSteps.current = steps;\n }, [animationSteps, charactersCount, shouldCalcAutoSpeed]);\n\n const handleSetNextChildrenIndex = useCallback(() => {\n setCurrentChildrenIndex((prevIndex) => {\n const nextIndex = prevIndex + 1;\n\n return nextIndex > childrenCount - 1 ? 0 : nextIndex;\n });\n }, [childrenCount]);\n\n const { handleClick, isResetAnimationActive, shownCharCount, shouldPreventBlinkingCursor } =\n useTypewriterAnimation({\n areMultipleChildrenGiven,\n autoSpeedRef: autoSpeed,\n autoStepsRef: autoSteps,\n charactersCount,\n cursorType,\n nextTextDelay,\n onAdvanceText: handleSetNextChildrenIndex,\n onResetAnimationEnd,\n onResetAnimationStart,\n onTypingAnimationEnd,\n onTypingAnimationStart,\n resetDelay,\n resetSpeed,\n shouldUseResetAnimation,\n shouldWaitForContent,\n speed,\n startDelay,\n textContentLength: textContent.length,\n });\n\n const isAnimatingText =\n shownCharCount < textContent.length ||\n shouldForceCursorAnimation ||\n areMultipleChildrenGiven ||\n textContent.length === 0;\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 const pseudoTextHTML = useMemo(() => {\n if (pseudoChildren) {\n const pseudoText = React.isValidElement(pseudoChildren)\n ? renderToString(\n <ChaynsProvider data={values} functions={functions} isModule>\n <ColorSchemeProvider\n color={colorScheme?.designSettings?.color}\n colorMode={colorScheme?.designSettings?.colorMode}\n style={{ display: 'inline' }}\n >\n {pseudoChildren}\n </ColorSchemeProvider>\n </ChaynsProvider>,\n )\n : (pseudoChildren as string);\n\n if (shouldUseAnimationHeight) {\n return getSubTextFromHTML(pseudoText, shownCharCount);\n }\n\n return pseudoText;\n }\n\n if (shouldUseAnimationHeight && textContent) {\n return getSubTextFromHTML(textContent, shownCharCount);\n }\n\n return textContent || '​';\n }, [\n colorScheme?.designSettings?.color,\n colorScheme?.designSettings?.colorMode,\n functions,\n pseudoChildren,\n shouldUseAnimationHeight,\n shownCharCount,\n textContent,\n values,\n ]);\n\n return useMemo(\n () => (\n <StyledTypewriter\n $cursorType={cursorType}\n onClick={isAnimatingText ? handleClick : undefined}\n $isAnimatingText={isAnimatingText}\n $shouldHideCursor={shouldHideCursor}\n $shouldPreventBlinkAnimation={shouldPreventBlinkingCursor}\n >\n {isAnimatingText ? (\n <AnimatedTypewriterText\n shouldHideCursor={shouldHideCursor}\n shouldRemainSingleLine={shouldRemainSingleLine}\n shownText={shownText}\n textStyle={textStyle}\n />\n ) : (\n <StyledTypewriterText\n className=\"notranslate\"\n $shouldRemainSingleLine={shouldRemainSingleLine}\n dangerouslySetInnerHTML={\n typeof sortedChildren === 'string' ? { __html: shownText } : undefined\n }\n style={textStyle}\n >\n {typeof sortedChildren !== 'string' ? sortedChildren : undefined}\n </StyledTypewriterText>\n )}\n {isAnimatingText && (\n <StyledTypewriterPseudoText\n $isAnimatingText={isAnimatingText}\n $shouldHideCursor={shouldHideCursor}\n dangerouslySetInnerHTML={{ __html: pseudoTextHTML }}\n />\n )}\n {/*\n The following is needed because some components like the CodeHighlighter will not render correct\n if the element is not rendered on a client before...\n */}\n {!hasRenderedChildrenOnce &&\n createPortal(\n <div style={{ position: 'absolute', visibility: 'hidden' }}>\n {children}\n </div>,\n document.body,\n )}\n </StyledTypewriter>\n ),\n [\n children,\n cursorType,\n handleClick,\n hasRenderedChildrenOnce,\n isAnimatingText,\n pseudoTextHTML,\n shouldHideCursor,\n shouldPreventBlinkingCursor,\n shouldRemainSingleLine,\n shownText,\n sortedChildren,\n textStyle,\n ],\n );\n};\n\nTypewriter.displayName = 'Typewriter';\n\nexport default Typewriter;\n"],"mappings":"AAAA,SAASA,mBAAmB,EAAEC,cAAc,QAAQ,yBAAyB;AAC7E,SAASC,cAAc,EAAEC,YAAY,EAAEC,SAAS,QAAQ,YAAY;AACpE,OAAOC,KAAK,IAGRC,WAAW,EACXC,SAAS,EACTC,eAAe,EACfC,OAAO,EACPC,MAAM,EACNC,QAAQ,QACL,OAAO;AACd,SAASC,YAAY,QAAQ,WAAW;AACxC,SAASC,cAAc,QAAQ,kBAAkB;AAEjD,SAASC,UAAU,QAAQ,oBAAoB;AAC/C,SAASC,eAAe,EAAEC,eAAe,QAAQ,mBAAmB;AACpE,OAAOC,sBAAsB,MAAM,0BAA0B;AAC7D,SACIC,gBAAgB,EAChBC,0BAA0B,EAC1BC,oBAAoB,QACjB,qBAAqB;AAC5B,SACIC,kBAAkB,EAElBC,kBAAkB,EAClBC,kBAAkB,EAClBC,YAAY,EACZC,4BAA4B,QACzB,SAAS;AAChB,OAAOC,sBAAsB,MAAM,0BAA0B;AAE7D,MAAMC,yBAAyB,GAAG,OAAOC,MAAM,KAAK,WAAW,GAAGpB,eAAe,GAAGD,SAAS;AA2H7F,MAAMsB,UAA+B,GAAGA,CAAC;EACrCC,QAAQ;EACRC,UAAU,GAAGjB,UAAU,CAACkB,OAAO;EAC/BC,aAAa,GAAGlB,eAAe,CAACmB,MAAM;EACtCC,QAAQ;EACRC,mBAAmB;EACnBC,cAAc,GAAG,CAAC;EAClBC,qBAAqB;EACrBC,oBAAoB;EACpBC,sBAAsB;EACtBC,cAAc;EACdC,UAAU,GAAG3B,eAAe,CAACmB,MAAM;EACnCS,0BAA0B,GAAG,KAA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file":"Typewriter.js","names":["ColorSchemeProvider","useColorScheme","ChaynsProvider","useFunctions","useValues","React","useCallback","useEffect","useLayoutEffect","useMemo","useRef","useState","createPortal","renderToString","CursorType","TypewriterDelay","TypewriterSpeed","AnimatedTypewriterText","StyledTypewriter","StyledTypewriterPseudoText","StyledTypewriterText","calculateAutoSpeed","getCharactersCount","getSubTextFromHTML","shuffleArray","updateChunkStreamingSpeedEMA","useIsomorphicLayoutEffect","window","Typewriter","children","cursorType","Default","nextTextDelay","Medium","onFinish","onResetAnimationEnd","animationSteps","onResetAnimationStart","onTypingAnimationEnd","onTypingAnimationStart","pseudoChildren","resetDelay","shouldForceCursorAnimation","shouldHideCursor","shouldRemainSingleLine","shouldSortChildrenRandomly","shouldUseAnimationHeight","shouldUseResetAnimation","shouldWaitForContent","speed","resetSpeed","startDelay","None","textStyle","shouldCalcAutoSpeed","autoSpeedBaseFactor","currentChildrenIndex","setCurrentChildrenIndex","hasRenderedChildrenOnce","setHasRenderedChildrenOnce","shouldPreventBlinkingCursor","setShouldPreventBlinkingCursor","isResetAnimationActive","setIsResetAnimationActive","shouldStopAnimation","setShouldStopAnimation","autoSpeed","autoSteps","functions","values","colorScheme","sortedChildren","Array","isArray","areMultipleChildrenGiven","childrenCount","length","textContent","currentChildren","isValidElement","createElement","data","isModule","color","designSettings","colorMode","style","display","className","charactersCount","chunkIntervalExponentialMovingAverage","lastLength","ema","shownCharCount","setShownCharCount","currentPosition","current","currentLength","state","undefined","calculatedAutoSpeed","steps","isAnimatingText","handleClick","event","stopPropagation","preventDefault","handleSetNextChildrenIndex","newIndex","interval","setInterval","prevState","nextState","clearInterval","setTimeout","startTypingAnimation","Thin","runTypingInterval","Math","min","shownText","pseudoTextHTML","pseudoText","$cursorType","onClick","$isAnimatingText","$shouldHideCursor","$shouldPreventBlinkAnimation","$shouldRemainSingleLine","dangerouslySetInnerHTML","__html","position","visibility","document","body","displayName"],"sources":["../../../../src/components/typewriter/Typewriter.tsx"],"sourcesContent":["import { ColorSchemeProvider, useColorScheme } from '@chayns-components/core';\nimport { ChaynsProvider, useFunctions, useValues } from 'chayns-api';\nimport React, {\n FC,\n ReactElement,\n useCallback,\n useEffect,\n useLayoutEffect,\n useMemo,\n useRef,\n useState,\n} from 'react';\nimport { createPortal } from 'react-dom';\nimport { renderToString } from 'react-dom/server';\nimport { CSSPropertiesWithVars } from 'styled-components/dist/types';\nimport { CursorType } from '../../types/cursor';\nimport { TypewriterDelay, TypewriterSpeed } from '../../types/speed';\nimport AnimatedTypewriterText from './AnimatedTypewriterText';\nimport {\n StyledTypewriter,\n StyledTypewriterPseudoText,\n StyledTypewriterText,\n} from './Typewriter.styles';\nimport {\n calculateAutoSpeed,\n ChunkStreamingSpeedState,\n getCharactersCount,\n getSubTextFromHTML,\n shuffleArray,\n updateChunkStreamingSpeedEMA,\n} from './utils';\n\nconst useIsomorphicLayoutEffect = typeof window !== 'undefined' ? useLayoutEffect : useEffect;\n\nexport type TypewriterProps = {\n /**\n * The number of characters that will be animated per animation cycle.\n */\n animationSteps?: number;\n /**\n * The text to type\n */\n children: ReactElement | ReactElement[] | string | string[];\n /**\n * The type of the cursor. Use the CursorType enum for this prop.\n */\n cursorType?: CursorType;\n /**\n * The delay in milliseconds before the next text is shown.\n * This prop is only used if multiple texts are given.\n */\n nextTextDelay?: TypewriterDelay;\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 * Function that is executed when the reset animation has finished. This function will not be\n * executed if `shouldUseResetAnimation` is not set to `true`.\n */\n onResetAnimationEnd?: VoidFunction;\n /**\n * Function that is executed when the reset animation has started. This function will not be\n * executed if `shouldUseResetAnimation` is not set to `true`.\n */\n onResetAnimationStart?: VoidFunction;\n /**\n * Function that is executed when the typing animation has finished. If multiple texts are given,\n * this function will be executed for each text.\n */\n onTypingAnimationEnd?: VoidFunction;\n /**\n * Function that is executed when the typing animation has started. If multiple texts are given,\n * this function will be executed for each text.\n */\n onTypingAnimationStart?: VoidFunction;\n /**\n * Pseudo-element to be rendered invisible during animation to define the size of the element\n * for the typewriter effect. By default, the \"children\" is used for this purpose.\n */\n pseudoChildren?: ReactElement | string;\n /**\n * Waiting time in milliseconds before the typewriter resets the text.\n * This prop is only used if multiple texts are given.\n */\n resetDelay?: TypewriterDelay;\n /**\n * The reset speed of the animation. Use the TypewriterSpeed enum for this prop.\n */\n resetSpeed?: TypewriterSpeed | number;\n /**\n * Specifies whether the cursor should be forced to animate even if no text is currently animated.\n */\n shouldForceCursorAnimation?: boolean;\n /**\n * Specifies whether the cursor should be hidden\n */\n shouldHideCursor?: boolean;\n /**\n * Whether the content should remain a single line.\n */\n shouldRemainSingleLine?: boolean;\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 animation should use its full height or the height of the current\n * chunk.\n */\n shouldUseAnimationHeight?: boolean;\n /**\n * Whether the animation speed should be calculated with the chunk interval.\n */\n shouldCalcAutoSpeed?: boolean;\n /**\n * Sets how long the animation should last when `shouldCalcAutoSpeed` is enabled in milliseconds.\n * When chunks are streamed, this value will only be used for the initial speed and then change to the speed characters are added at\n */\n autoSpeedBaseFactor?: number;\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 * Whether the typewriter should wait for new content\n */\n shouldWaitForContent?: boolean;\n /**\n * The speed of the animation. Use the TypewriterSpeed enum for this prop.\n */\n speed?: TypewriterSpeed | number;\n /**\n * The delay in milliseconds before the typewriter starts typing.\n */\n startDelay?: TypewriterDelay;\n /**\n * The style of the typewriter text element\n */\n textStyle?: CSSPropertiesWithVars;\n};\n\nconst Typewriter: FC<TypewriterProps> = ({\n children,\n cursorType = CursorType.Default,\n nextTextDelay = TypewriterDelay.Medium,\n onFinish,\n onResetAnimationEnd,\n animationSteps = 1,\n onResetAnimationStart,\n onTypingAnimationEnd,\n onTypingAnimationStart,\n pseudoChildren,\n resetDelay = TypewriterDelay.Medium,\n shouldForceCursorAnimation = false,\n shouldHideCursor = false,\n shouldRemainSingleLine = false,\n shouldSortChildrenRandomly = false,\n shouldUseAnimationHeight = false,\n shouldUseResetAnimation = false,\n shouldWaitForContent,\n speed = TypewriterSpeed.Medium,\n resetSpeed = speed,\n startDelay = TypewriterDelay.None,\n textStyle,\n shouldCalcAutoSpeed = false,\n autoSpeedBaseFactor = 2000,\n}) => {\n const [currentChildrenIndex, setCurrentChildrenIndex] = useState(0);\n const [hasRenderedChildrenOnce, setHasRenderedChildrenOnce] = useState(false);\n const [shouldPreventBlinkingCursor, setShouldPreventBlinkingCursor] = useState(false);\n const [isResetAnimationActive, setIsResetAnimationActive] = useState(false);\n const [shouldStopAnimation, setShouldStopAnimation] = useState(false);\n const autoSpeed = useRef<number>();\n const autoSteps = useRef<number>(animationSteps);\n\n const functions = useFunctions();\n const values = useValues();\n\n const colorScheme = useColorScheme();\n\n useIsomorphicLayoutEffect(() => {\n if (children) {\n setHasRenderedChildrenOnce(false);\n }\n }, [children]);\n\n useEffect(() => {\n if (!hasRenderedChildrenOnce) {\n setHasRenderedChildrenOnce(true);\n }\n }, [hasRenderedChildrenOnce]);\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(\n <ChaynsProvider data={values} functions={functions} isModule>\n <ColorSchemeProvider\n color={colorScheme?.designSettings?.color}\n colorMode={colorScheme?.designSettings?.colorMode}\n style={{ display: 'inline' }}\n >\n <span className=\"notranslate\">{currentChildren}</span>\n </ColorSchemeProvider>\n </ChaynsProvider>,\n )\n : (currentChildren as string);\n }\n\n return '';\n }\n\n return React.isValidElement(sortedChildren)\n ? renderToString(\n <ChaynsProvider data={values} functions={functions} isModule>\n <ColorSchemeProvider\n color={colorScheme?.designSettings?.color}\n colorMode={colorScheme?.designSettings?.colorMode}\n style={{ display: 'inline' }}\n >\n <span className=\"notranslate\">{sortedChildren}</span>\n </ColorSchemeProvider>\n </ChaynsProvider>,\n )\n : (sortedChildren as string);\n }, [\n areMultipleChildrenGiven,\n colorScheme?.designSettings?.color,\n colorScheme?.designSettings?.colorMode,\n currentChildrenIndex,\n functions,\n sortedChildren,\n values,\n ]);\n\n const charactersCount = useMemo(() => getCharactersCount(textContent), [textContent]);\n const chunkIntervalExponentialMovingAverage = useRef<ChunkStreamingSpeedState>({\n lastLength: charactersCount,\n ema: charactersCount / (autoSpeedBaseFactor / 1000),\n });\n\n const [shownCharCount, setShownCharCount] = useState(\n charactersCount > 0 ? 0 : textContent.length,\n );\n\n const currentPosition = useRef(0);\n\n useEffect(() => {\n if (shouldUseResetAnimation) {\n chunkIntervalExponentialMovingAverage.current = {\n ema: charactersCount / (autoSpeedBaseFactor / 1000),\n lastLength: charactersCount,\n };\n }\n chunkIntervalExponentialMovingAverage.current = updateChunkStreamingSpeedEMA({\n currentLength: charactersCount,\n state: chunkIntervalExponentialMovingAverage.current,\n });\n }, [autoSpeedBaseFactor, charactersCount, shouldUseResetAnimation]);\n\n useEffect(() => {\n if (!shouldCalcAutoSpeed) {\n autoSpeed.current = undefined;\n autoSteps.current = animationSteps;\n return;\n }\n const { speed: calculatedAutoSpeed, steps } = calculateAutoSpeed(\n chunkIntervalExponentialMovingAverage.current.ema,\n );\n\n autoSpeed.current = calculatedAutoSpeed;\n autoSteps.current = steps;\n }, [animationSteps, charactersCount, shouldCalcAutoSpeed]);\n\n const isAnimatingText =\n shownCharCount < textContent.length ||\n shouldForceCursorAnimation ||\n areMultipleChildrenGiven ||\n textContent.length === 0;\n\n const handleClick = useCallback((event: React.MouseEvent) => {\n event.stopPropagation();\n event.preventDefault();\n\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 currentPosition.current = textContent.length;\n } else if (isResetAnimationActive) {\n if (typeof onResetAnimationStart === 'function') {\n onResetAnimationStart();\n }\n\n interval = window.setInterval(() => {\n setShownCharCount((prevState) => {\n const nextState = prevState - autoSteps.current;\n currentPosition.current = nextState;\n\n if (nextState === 0) {\n window.clearInterval(interval);\n\n if (typeof onResetAnimationEnd === 'function') {\n onResetAnimationEnd();\n }\n\n if (areMultipleChildrenGiven) {\n setTimeout(() => {\n setIsResetAnimationActive(false);\n handleSetNextChildrenIndex();\n }, nextTextDelay);\n }\n }\n\n return nextState;\n });\n }, resetSpeed);\n } else {\n const startTypingAnimation = () => {\n if (cursorType === CursorType.Thin) {\n setShouldPreventBlinkingCursor(true);\n }\n\n if (typeof onTypingAnimationStart === 'function') {\n onTypingAnimationStart();\n }\n\n const runTypingInterval = () => {\n setShownCharCount((prevState) => {\n let nextState = Math.min(prevState + autoSteps.current, charactersCount);\n\n if (nextState >= charactersCount && !shouldWaitForContent) {\n window.clearInterval(interval);\n\n if (cursorType === CursorType.Thin) {\n setShouldPreventBlinkingCursor(false);\n }\n\n if (typeof onTypingAnimationEnd === 'function') {\n onTypingAnimationEnd();\n }\n\n /**\n * At this point, the next value for \"shownCharCount\" is deliberately set to\n * the length of the textContent 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, nextTextDelay);\n }\n }, resetDelay);\n }\n }\n\n currentPosition.current = nextState;\n\n return nextState;\n });\n };\n interval = window.setInterval(runTypingInterval, autoSpeed.current ?? speed);\n };\n\n if (startDelay) {\n setTimeout(startTypingAnimation, startDelay);\n } else {\n startTypingAnimation();\n }\n }\n\n return () => {\n window.clearInterval(interval);\n };\n }, [\n areMultipleChildrenGiven,\n autoSteps,\n charactersCount,\n cursorType,\n handleSetNextChildrenIndex,\n isResetAnimationActive,\n nextTextDelay,\n onResetAnimationEnd,\n onResetAnimationStart,\n onTypingAnimationEnd,\n onTypingAnimationStart,\n resetDelay,\n resetSpeed,\n shouldStopAnimation,\n shouldUseResetAnimation,\n shouldWaitForContent,\n speed,\n startDelay,\n textContent.length,\n ]);\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 const pseudoTextHTML = useMemo(() => {\n if (pseudoChildren) {\n const pseudoText = React.isValidElement(pseudoChildren)\n ? renderToString(\n <ChaynsProvider data={values} functions={functions} isModule>\n <ColorSchemeProvider\n color={colorScheme?.designSettings?.color}\n colorMode={colorScheme?.designSettings?.colorMode}\n style={{ display: 'inline' }}\n >\n {pseudoChildren}\n </ColorSchemeProvider>\n </ChaynsProvider>,\n )\n : (pseudoChildren as string);\n\n if (shouldUseAnimationHeight) {\n return getSubTextFromHTML(pseudoText, shownCharCount);\n }\n\n return pseudoText;\n }\n\n if (shouldUseAnimationHeight && textContent) {\n return getSubTextFromHTML(textContent, shownCharCount);\n }\n\n return textContent || '​';\n }, [\n colorScheme?.designSettings?.color,\n colorScheme?.designSettings?.colorMode,\n functions,\n pseudoChildren,\n shouldUseAnimationHeight,\n shownCharCount,\n textContent,\n values,\n ]);\n\n return useMemo(\n () => (\n <StyledTypewriter\n $cursorType={cursorType}\n onClick={isAnimatingText ? handleClick : undefined}\n $isAnimatingText={isAnimatingText}\n $shouldHideCursor={shouldHideCursor}\n $shouldPreventBlinkAnimation={shouldPreventBlinkingCursor}\n >\n {isAnimatingText ? (\n <AnimatedTypewriterText\n shouldHideCursor={shouldHideCursor}\n shouldRemainSingleLine={shouldRemainSingleLine}\n shownText={shownText}\n textStyle={textStyle}\n />\n ) : (\n <StyledTypewriterText\n className=\"notranslate\"\n $shouldRemainSingleLine={shouldRemainSingleLine}\n dangerouslySetInnerHTML={\n typeof sortedChildren === 'string' ? { __html: shownText } : undefined\n }\n style={textStyle}\n >\n {typeof sortedChildren !== 'string' ? sortedChildren : undefined}\n </StyledTypewriterText>\n )}\n {isAnimatingText && (\n <StyledTypewriterPseudoText\n $isAnimatingText={isAnimatingText}\n $shouldHideCursor={shouldHideCursor}\n dangerouslySetInnerHTML={{ __html: pseudoTextHTML }}\n />\n )}\n {/*\n The following is needed because some components like the CodeHighlighter will not render correct\n if the element is not rendered on a client before...\n */}\n {!hasRenderedChildrenOnce &&\n createPortal(\n <div style={{ position: 'absolute', visibility: 'hidden' }}>\n {children}\n </div>,\n document.body,\n )}\n </StyledTypewriter>\n ),\n [\n children,\n cursorType,\n handleClick,\n hasRenderedChildrenOnce,\n isAnimatingText,\n pseudoTextHTML,\n shouldHideCursor,\n shouldPreventBlinkingCursor,\n shouldRemainSingleLine,\n shownText,\n sortedChildren,\n textStyle,\n ],\n );\n};\n\nTypewriter.displayName = 'Typewriter';\n\nexport default Typewriter;\n"],"mappings":"AAAA,SAASA,mBAAmB,EAAEC,cAAc,QAAQ,yBAAyB;AAC7E,SAASC,cAAc,EAAEC,YAAY,EAAEC,SAAS,QAAQ,YAAY;AACpE,OAAOC,KAAK,IAGRC,WAAW,EACXC,SAAS,EACTC,eAAe,EACfC,OAAO,EACPC,MAAM,EACNC,QAAQ,QACL,OAAO;AACd,SAASC,YAAY,QAAQ,WAAW;AACx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CursorType } from '../../types/cursor';
|
|
|
4
4
|
import { TypewriterDelay, TypewriterSpeed } from '../../types/speed';
|
|
5
5
|
export type TypewriterProps = {
|
|
6
6
|
/**
|
|
7
|
-
* The number of characters that will be animated per animation
|
|
8
|
-
*
|
|
9
|
-
* The step is advanced by the requestAnimationFrame-driven scheduler. When
|
|
10
|
-
* `shouldCalcAutoSpeed` is enabled, the component may override this value dynamically.
|
|
7
|
+
* The number of characters that will be animated per animation cycle.
|
|
11
8
|
*/
|
|
12
9
|
animationSteps?: number;
|
|
13
10
|
/**
|
|
14
|
-
* The
|
|
15
|
-
*
|
|
16
|
-
* Strings are animated directly. React elements and arrays are rendered to HTML first so the
|
|
17
|
-
* typewriter can preserve nested markup and inline components.
|
|
11
|
+
* The text to type
|
|
18
12
|
*/
|
|
19
13
|
children: ReactElement | ReactElement[] | string | string[];
|
|
20
14
|
/**
|
|
21
|
-
* The type of the cursor. Use the
|
|
15
|
+
* The type of the cursor. Use the CursorType enum for this prop.
|
|
22
16
|
*/
|
|
23
17
|
cursorType?: CursorType;
|
|
24
18
|
/**
|
|
25
19
|
* The delay in milliseconds before the next text is shown.
|
|
26
|
-
*
|
|
27
|
-
* This prop is only used when `children` is an array.
|
|
20
|
+
* This prop is only used if multiple texts are given.
|
|
28
21
|
*/
|
|
29
22
|
nextTextDelay?: TypewriterDelay;
|
|
30
23
|
/**
|
|
31
|
-
* Function that is executed when the
|
|
32
|
-
*
|
|
33
|
-
* This callback is not used while more texts are still queued.
|
|
24
|
+
* Function that is executed when the typewriter animation has finished. This function will not
|
|
25
|
+
* be executed if multiple texts are used.
|
|
34
26
|
*/
|
|
35
27
|
onFinish?: VoidFunction;
|
|
36
28
|
/**
|
|
37
|
-
* Function that is executed when the reset animation has finished.
|
|
38
|
-
*
|
|
39
|
-
* This callback only runs when `shouldUseResetAnimation` is enabled.
|
|
29
|
+
* Function that is executed when the reset animation has finished. This function will not be
|
|
30
|
+
* executed if `shouldUseResetAnimation` is not set to `true`.
|
|
40
31
|
*/
|
|
41
32
|
onResetAnimationEnd?: VoidFunction;
|
|
42
33
|
/**
|
|
43
|
-
* Function that is executed when the reset animation has started.
|
|
44
|
-
*
|
|
45
|
-
* This callback only runs when `shouldUseResetAnimation` is enabled.
|
|
34
|
+
* Function that is executed when the reset animation has started. This function will not be
|
|
35
|
+
* executed if `shouldUseResetAnimation` is not set to `true`.
|
|
46
36
|
*/
|
|
47
37
|
onResetAnimationStart?: VoidFunction;
|
|
48
38
|
/**
|
|
49
|
-
* Function that is executed when the typing animation has finished.
|
|
50
|
-
*
|
|
51
|
-
* If multiple texts are given, this function will be executed for each text.
|
|
39
|
+
* Function that is executed when the typing animation has finished. If multiple texts are given,
|
|
40
|
+
* this function will be executed for each text.
|
|
52
41
|
*/
|
|
53
42
|
onTypingAnimationEnd?: VoidFunction;
|
|
54
43
|
/**
|
|
55
|
-
* Function that is executed when the typing animation has started.
|
|
56
|
-
*
|
|
57
|
-
* If multiple texts are given, this function will be executed for each text.
|
|
44
|
+
* Function that is executed when the typing animation has started. If multiple texts are given,
|
|
45
|
+
* this function will be executed for each text.
|
|
58
46
|
*/
|
|
59
47
|
onTypingAnimationStart?: VoidFunction;
|
|
60
48
|
/**
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
-
* When omitted, the visible `children` are used as the layout template.
|
|
49
|
+
* Pseudo-element to be rendered invisible during animation to define the size of the element
|
|
50
|
+
* for the typewriter effect. By default, the "children" is used for this purpose.
|
|
64
51
|
*/
|
|
65
52
|
pseudoChildren?: ReactElement | string;
|
|
66
53
|
/**
|
|
67
54
|
* Waiting time in milliseconds before the typewriter resets the text.
|
|
68
|
-
*
|
|
69
|
-
* This prop is only used when `children` is an array.
|
|
55
|
+
* This prop is only used if multiple texts are given.
|
|
70
56
|
*/
|
|
71
57
|
resetDelay?: TypewriterDelay;
|
|
72
58
|
/**
|
|
73
|
-
* The reset speed of the animation. Use the
|
|
59
|
+
* The reset speed of the animation. Use the TypewriterSpeed enum for this prop.
|
|
74
60
|
*/
|
|
75
61
|
resetSpeed?: TypewriterSpeed | number;
|
|
76
62
|
/**
|
|
77
|
-
*
|
|
63
|
+
* Specifies whether the cursor should be forced to animate even if no text is currently animated.
|
|
78
64
|
*/
|
|
79
65
|
shouldForceCursorAnimation?: boolean;
|
|
80
66
|
/**
|
|
81
|
-
*
|
|
67
|
+
* Specifies whether the cursor should be hidden
|
|
82
68
|
*/
|
|
83
69
|
shouldHideCursor?: boolean;
|
|
84
70
|
/**
|
|
85
|
-
*
|
|
71
|
+
* Whether the content should remain a single line.
|
|
86
72
|
*/
|
|
87
73
|
shouldRemainSingleLine?: boolean;
|
|
88
74
|
/**
|
|
89
|
-
*
|
|
75
|
+
* Specifies whether the children should be sorted randomly if there are multiple texts.
|
|
76
|
+
* This makes the typewriter start with a different text each time and also changes them
|
|
77
|
+
* in a random order.
|
|
90
78
|
*/
|
|
91
79
|
shouldSortChildrenRandomly?: boolean;
|
|
92
80
|
/**
|
|
93
|
-
*
|
|
81
|
+
* Specifies whether the animation should use its full height or the height of the current
|
|
82
|
+
* chunk.
|
|
94
83
|
*/
|
|
95
84
|
shouldUseAnimationHeight?: boolean;
|
|
96
85
|
/**
|
|
97
|
-
*
|
|
86
|
+
* Whether the animation speed should be calculated with the chunk interval.
|
|
98
87
|
*/
|
|
99
88
|
shouldCalcAutoSpeed?: boolean;
|
|
100
89
|
/**
|
|
101
|
-
*
|
|
90
|
+
* Sets how long the animation should last when `shouldCalcAutoSpeed` is enabled in milliseconds.
|
|
91
|
+
* When chunks are streamed, this value will only be used for the initial speed and then change to the speed characters are added at
|
|
102
92
|
*/
|
|
103
93
|
autoSpeedBaseFactor?: number;
|
|
104
94
|
/**
|
|
105
|
-
*
|
|
95
|
+
* Specifies whether the reset of the text should be animated with a backspace animation for
|
|
96
|
+
* multiple texts.
|
|
106
97
|
*/
|
|
107
98
|
shouldUseResetAnimation?: boolean;
|
|
108
99
|
/**
|
|
109
|
-
*
|
|
100
|
+
* Whether the typewriter should wait for new content
|
|
110
101
|
*/
|
|
111
102
|
shouldWaitForContent?: boolean;
|
|
112
103
|
/**
|
|
113
|
-
* The speed of the animation. Use the
|
|
104
|
+
* The speed of the animation. Use the TypewriterSpeed enum for this prop.
|
|
114
105
|
*/
|
|
115
106
|
speed?: TypewriterSpeed | number;
|
|
116
107
|
/**
|
|
@@ -118,7 +109,7 @@ export type TypewriterProps = {
|
|
|
118
109
|
*/
|
|
119
110
|
startDelay?: TypewriterDelay;
|
|
120
111
|
/**
|
|
121
|
-
*
|
|
112
|
+
* The style of the typewriter text element
|
|
122
113
|
*/
|
|
123
114
|
textStyle?: CSSPropertiesWithVars;
|
|
124
115
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chayns-components/typewriter",
|
|
3
|
-
"version": "5.0.
|
|
3
|
+
"version": "5.0.55",
|
|
4
4
|
"description": "A set of beautiful React components for developing your own applications with chayns.",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"browserslist": [
|
|
@@ -70,7 +70,7 @@
|
|
|
70
70
|
"typescript": "^5.9.3"
|
|
71
71
|
},
|
|
72
72
|
"dependencies": {
|
|
73
|
-
"@chayns-components/core": "^5.0.
|
|
73
|
+
"@chayns-components/core": "^5.0.55",
|
|
74
74
|
"react-compiler-runtime": "^1.0.0"
|
|
75
75
|
},
|
|
76
76
|
"peerDependencies": {
|
|
@@ -83,5 +83,5 @@
|
|
|
83
83
|
"publishConfig": {
|
|
84
84
|
"access": "public"
|
|
85
85
|
},
|
|
86
|
-
"gitHead": "
|
|
86
|
+
"gitHead": "05d5f7508703e3fb5757bd36d984d6e1224d5e2b"
|
|
87
87
|
}
|
|
@@ -1,237 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.default = void 0;
|
|
7
|
-
var _react = require("react");
|
|
8
|
-
var _cursor = require("../../types/cursor");
|
|
9
|
-
const useTypewriterAnimation = ({
|
|
10
|
-
areMultipleChildrenGiven,
|
|
11
|
-
charactersCount,
|
|
12
|
-
cursorType,
|
|
13
|
-
onAdvanceText,
|
|
14
|
-
nextTextDelay,
|
|
15
|
-
onResetAnimationEnd,
|
|
16
|
-
onResetAnimationStart,
|
|
17
|
-
onTypingAnimationEnd,
|
|
18
|
-
onTypingAnimationStart,
|
|
19
|
-
resetDelay,
|
|
20
|
-
resetSpeed,
|
|
21
|
-
shouldUseResetAnimation,
|
|
22
|
-
shouldWaitForContent,
|
|
23
|
-
speed,
|
|
24
|
-
startDelay,
|
|
25
|
-
textContentLength,
|
|
26
|
-
autoSpeedRef,
|
|
27
|
-
autoStepsRef
|
|
28
|
-
}) => {
|
|
29
|
-
const [shouldPreventBlinkingCursor, setShouldPreventBlinkingCursor] = (0, _react.useState)(false);
|
|
30
|
-
const [isResetAnimationActive, setIsResetAnimationActive] = (0, _react.useState)(false);
|
|
31
|
-
const [shouldStopAnimation, setShouldStopAnimation] = (0, _react.useState)(false);
|
|
32
|
-
const [shownCharCount, setShownCharCount] = (0, _react.useState)(charactersCount > 0 ? 0 : textContentLength);
|
|
33
|
-
const handleClick = (0, _react.useCallback)(event => {
|
|
34
|
-
event.stopPropagation();
|
|
35
|
-
event.preventDefault();
|
|
36
|
-
setShouldStopAnimation(true);
|
|
37
|
-
}, []);
|
|
38
|
-
(0, _react.useEffect)(() => {
|
|
39
|
-
let animationFrameId;
|
|
40
|
-
let phaseStartTime = performance.now();
|
|
41
|
-
let lastFrameTime = phaseStartTime;
|
|
42
|
-
let currentShownCharCount = shownCharCount;
|
|
43
|
-
let typingAccumulator = 0;
|
|
44
|
-
let resetAccumulator = 0;
|
|
45
|
-
let hasStartedTyping = false;
|
|
46
|
-
let hasStartedReset = false;
|
|
47
|
-
let hasEndedTyping = false;
|
|
48
|
-
let hasEndedReset = false;
|
|
49
|
-
let phase = isResetAnimationActive ? 'resetting' : 'typing';
|
|
50
|
-
if (startDelay) {
|
|
51
|
-
phase = 'typingDelay';
|
|
52
|
-
}
|
|
53
|
-
const cancelFrame = () => {
|
|
54
|
-
if (typeof animationFrameId === 'number') {
|
|
55
|
-
window.cancelAnimationFrame(animationFrameId);
|
|
56
|
-
animationFrameId = undefined;
|
|
57
|
-
}
|
|
58
|
-
};
|
|
59
|
-
const updateShownCharCount = nextValue => {
|
|
60
|
-
currentShownCharCount = nextValue;
|
|
61
|
-
setShownCharCount(nextValue);
|
|
62
|
-
};
|
|
63
|
-
const startTyping = () => {
|
|
64
|
-
if (hasStartedTyping) {
|
|
65
|
-
return;
|
|
66
|
-
}
|
|
67
|
-
hasStartedTyping = true;
|
|
68
|
-
if (cursorType === _cursor.CursorType.Thin) {
|
|
69
|
-
setShouldPreventBlinkingCursor(true);
|
|
70
|
-
}
|
|
71
|
-
if (typeof onTypingAnimationStart === 'function') {
|
|
72
|
-
onTypingAnimationStart();
|
|
73
|
-
}
|
|
74
|
-
};
|
|
75
|
-
const finishTyping = () => {
|
|
76
|
-
if (hasEndedTyping) {
|
|
77
|
-
return;
|
|
78
|
-
}
|
|
79
|
-
hasEndedTyping = true;
|
|
80
|
-
if (cursorType === _cursor.CursorType.Thin) {
|
|
81
|
-
setShouldPreventBlinkingCursor(false);
|
|
82
|
-
}
|
|
83
|
-
if (typeof onTypingAnimationEnd === 'function') {
|
|
84
|
-
onTypingAnimationEnd();
|
|
85
|
-
}
|
|
86
|
-
};
|
|
87
|
-
const startReset = () => {
|
|
88
|
-
if (hasStartedReset) {
|
|
89
|
-
return;
|
|
90
|
-
}
|
|
91
|
-
hasStartedReset = true;
|
|
92
|
-
if (typeof onResetAnimationStart === 'function') {
|
|
93
|
-
onResetAnimationStart();
|
|
94
|
-
}
|
|
95
|
-
};
|
|
96
|
-
const finishReset = () => {
|
|
97
|
-
if (hasEndedReset) {
|
|
98
|
-
return;
|
|
99
|
-
}
|
|
100
|
-
hasEndedReset = true;
|
|
101
|
-
if (typeof onResetAnimationEnd === 'function') {
|
|
102
|
-
onResetAnimationEnd();
|
|
103
|
-
}
|
|
104
|
-
};
|
|
105
|
-
if (shouldStopAnimation || charactersCount === 0) {
|
|
106
|
-
updateShownCharCount(textContentLength);
|
|
107
|
-
return undefined;
|
|
108
|
-
}
|
|
109
|
-
if (phase === 'typing') {
|
|
110
|
-
startTyping();
|
|
111
|
-
} else if (phase === 'resetting') {
|
|
112
|
-
startReset();
|
|
113
|
-
}
|
|
114
|
-
const tick = now => {
|
|
115
|
-
if (shouldStopAnimation || charactersCount === 0) {
|
|
116
|
-
updateShownCharCount(textContentLength);
|
|
117
|
-
cancelFrame();
|
|
118
|
-
return;
|
|
119
|
-
}
|
|
120
|
-
const delta = now - lastFrameTime;
|
|
121
|
-
lastFrameTime = now;
|
|
122
|
-
switch (phase) {
|
|
123
|
-
case 'typingDelay':
|
|
124
|
-
{
|
|
125
|
-
if (now - phaseStartTime < startDelay) {
|
|
126
|
-
break;
|
|
127
|
-
}
|
|
128
|
-
phase = 'typing';
|
|
129
|
-
phaseStartTime = now;
|
|
130
|
-
typingAccumulator = 0;
|
|
131
|
-
startTyping();
|
|
132
|
-
break;
|
|
133
|
-
}
|
|
134
|
-
case 'typing':
|
|
135
|
-
{
|
|
136
|
-
startTyping();
|
|
137
|
-
typingAccumulator += delta;
|
|
138
|
-
const typingInterval = autoSpeedRef.current ?? speed;
|
|
139
|
-
while (typingAccumulator >= typingInterval) {
|
|
140
|
-
typingAccumulator -= typingInterval;
|
|
141
|
-
const nextValue = Math.min(currentShownCharCount + autoStepsRef.current, charactersCount);
|
|
142
|
-
if (nextValue >= charactersCount) {
|
|
143
|
-
updateShownCharCount(textContentLength);
|
|
144
|
-
if (shouldWaitForContent) {
|
|
145
|
-
typingAccumulator = 0;
|
|
146
|
-
break;
|
|
147
|
-
}
|
|
148
|
-
finishTyping();
|
|
149
|
-
if (areMultipleChildrenGiven) {
|
|
150
|
-
phase = 'resetDelay';
|
|
151
|
-
phaseStartTime = now;
|
|
152
|
-
typingAccumulator = 0;
|
|
153
|
-
} else {
|
|
154
|
-
cancelFrame();
|
|
155
|
-
return;
|
|
156
|
-
}
|
|
157
|
-
break;
|
|
158
|
-
}
|
|
159
|
-
updateShownCharCount(nextValue);
|
|
160
|
-
}
|
|
161
|
-
break;
|
|
162
|
-
}
|
|
163
|
-
case 'resetDelay':
|
|
164
|
-
{
|
|
165
|
-
if (now - phaseStartTime < resetDelay) {
|
|
166
|
-
break;
|
|
167
|
-
}
|
|
168
|
-
if (areMultipleChildrenGiven) {
|
|
169
|
-
if (shouldUseResetAnimation) {
|
|
170
|
-
setIsResetAnimationActive(true);
|
|
171
|
-
cancelFrame();
|
|
172
|
-
return;
|
|
173
|
-
}
|
|
174
|
-
updateShownCharCount(0);
|
|
175
|
-
phase = 'nextTextDelay';
|
|
176
|
-
phaseStartTime = now;
|
|
177
|
-
} else {
|
|
178
|
-
cancelFrame();
|
|
179
|
-
return;
|
|
180
|
-
}
|
|
181
|
-
break;
|
|
182
|
-
}
|
|
183
|
-
case 'resetting':
|
|
184
|
-
{
|
|
185
|
-
startReset();
|
|
186
|
-
resetAccumulator += delta;
|
|
187
|
-
const resetInterval = resetSpeed;
|
|
188
|
-
while (resetAccumulator >= resetInterval) {
|
|
189
|
-
resetAccumulator -= resetInterval;
|
|
190
|
-
const nextValue = Math.max(currentShownCharCount - autoStepsRef.current, 0);
|
|
191
|
-
updateShownCharCount(nextValue);
|
|
192
|
-
if (nextValue === 0) {
|
|
193
|
-
finishReset();
|
|
194
|
-
if (areMultipleChildrenGiven) {
|
|
195
|
-
phase = 'nextTextDelay';
|
|
196
|
-
phaseStartTime = now;
|
|
197
|
-
resetAccumulator = 0;
|
|
198
|
-
} else {
|
|
199
|
-
cancelFrame();
|
|
200
|
-
return;
|
|
201
|
-
}
|
|
202
|
-
break;
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
break;
|
|
206
|
-
}
|
|
207
|
-
case 'nextTextDelay':
|
|
208
|
-
{
|
|
209
|
-
if (now - phaseStartTime < nextTextDelay) {
|
|
210
|
-
break;
|
|
211
|
-
}
|
|
212
|
-
if (shouldUseResetAnimation) {
|
|
213
|
-
setIsResetAnimationActive(false);
|
|
214
|
-
}
|
|
215
|
-
onAdvanceText === null || onAdvanceText === void 0 || onAdvanceText();
|
|
216
|
-
cancelFrame();
|
|
217
|
-
return;
|
|
218
|
-
}
|
|
219
|
-
default:
|
|
220
|
-
break;
|
|
221
|
-
}
|
|
222
|
-
animationFrameId = window.requestAnimationFrame(tick);
|
|
223
|
-
};
|
|
224
|
-
animationFrameId = window.requestAnimationFrame(tick);
|
|
225
|
-
return () => {
|
|
226
|
-
cancelFrame();
|
|
227
|
-
};
|
|
228
|
-
}, [areMultipleChildrenGiven, charactersCount, cursorType, isResetAnimationActive, nextTextDelay, onResetAnimationEnd, onResetAnimationStart, onTypingAnimationEnd, onTypingAnimationStart, resetDelay, resetSpeed, shouldStopAnimation, shouldUseResetAnimation, shouldWaitForContent, speed, startDelay, textContentLength, autoSpeedRef, autoStepsRef, onAdvanceText]);
|
|
229
|
-
return {
|
|
230
|
-
handleClick,
|
|
231
|
-
isResetAnimationActive,
|
|
232
|
-
shownCharCount,
|
|
233
|
-
shouldPreventBlinkingCursor
|
|
234
|
-
};
|
|
235
|
-
};
|
|
236
|
-
var _default = exports.default = useTypewriterAnimation;
|
|
237
|
-
//# sourceMappingURL=useTypewriterAnimation.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useTypewriterAnimation.js","names":["_react","require","_cursor","useTypewriterAnimation","areMultipleChildrenGiven","charactersCount","cursorType","onAdvanceText","nextTextDelay","onResetAnimationEnd","onResetAnimationStart","onTypingAnimationEnd","onTypingAnimationStart","resetDelay","resetSpeed","shouldUseResetAnimation","shouldWaitForContent","speed","startDelay","textContentLength","autoSpeedRef","autoStepsRef","shouldPreventBlinkingCursor","setShouldPreventBlinkingCursor","useState","isResetAnimationActive","setIsResetAnimationActive","shouldStopAnimation","setShouldStopAnimation","shownCharCount","setShownCharCount","handleClick","useCallback","event","stopPropagation","preventDefault","useEffect","animationFrameId","phaseStartTime","performance","now","lastFrameTime","currentShownCharCount","typingAccumulator","resetAccumulator","hasStartedTyping","hasStartedReset","hasEndedTyping","hasEndedReset","phase","cancelFrame","window","cancelAnimationFrame","undefined","updateShownCharCount","nextValue","startTyping","CursorType","Thin","finishTyping","startReset","finishReset","tick","delta","typingInterval","current","Math","min","resetInterval","max","requestAnimationFrame","_default","exports","default"],"sources":["../../../../src/components/typewriter/useTypewriterAnimation.ts"],"sourcesContent":["import type { MouseEvent, MutableRefObject } from 'react';\nimport { useCallback, useEffect, useState } from 'react';\nimport { CursorType } from '../../types/cursor';\n\nexport type UseTypewriterAnimationOptions = {\n areMultipleChildrenGiven: boolean;\n charactersCount: number;\n cursorType: CursorType;\n onAdvanceText?: VoidFunction;\n nextTextDelay: number;\n onResetAnimationEnd?: VoidFunction;\n onResetAnimationStart?: VoidFunction;\n onTypingAnimationEnd?: VoidFunction;\n onTypingAnimationStart?: VoidFunction;\n resetDelay: number;\n resetSpeed: number;\n shouldUseResetAnimation: boolean;\n shouldWaitForContent?: boolean;\n speed: number;\n startDelay: number;\n textContentLength: number;\n autoSpeedRef: MutableRefObject<number | undefined>;\n autoStepsRef: MutableRefObject<number>;\n};\n\nexport type UseTypewriterAnimationResult = {\n handleClick: (event: MouseEvent) => void;\n isResetAnimationActive: boolean;\n shownCharCount: number;\n shouldPreventBlinkingCursor: boolean;\n};\n\nconst useTypewriterAnimation = ({\n areMultipleChildrenGiven,\n charactersCount,\n cursorType,\n onAdvanceText,\n nextTextDelay,\n onResetAnimationEnd,\n onResetAnimationStart,\n onTypingAnimationEnd,\n onTypingAnimationStart,\n resetDelay,\n resetSpeed,\n shouldUseResetAnimation,\n shouldWaitForContent,\n speed,\n startDelay,\n textContentLength,\n autoSpeedRef,\n autoStepsRef,\n}: UseTypewriterAnimationOptions): UseTypewriterAnimationResult => {\n const [shouldPreventBlinkingCursor, setShouldPreventBlinkingCursor] = useState(false);\n const [isResetAnimationActive, setIsResetAnimationActive] = useState(false);\n const [shouldStopAnimation, setShouldStopAnimation] = useState(false);\n const [shownCharCount, setShownCharCount] = useState(\n charactersCount > 0 ? 0 : textContentLength,\n );\n\n const handleClick = useCallback((event: MouseEvent) => {\n event.stopPropagation();\n event.preventDefault();\n\n setShouldStopAnimation(true);\n }, []);\n\n useEffect(() => {\n let animationFrameId: number | undefined;\n let phaseStartTime = performance.now();\n let lastFrameTime = phaseStartTime;\n let currentShownCharCount = shownCharCount;\n let typingAccumulator = 0;\n let resetAccumulator = 0;\n let hasStartedTyping = false;\n let hasStartedReset = false;\n let hasEndedTyping = false;\n let hasEndedReset = false;\n\n type AnimationPhase =\n | 'typingDelay'\n | 'typing'\n | 'resetDelay'\n | 'resetting'\n | 'nextTextDelay';\n let phase: AnimationPhase = isResetAnimationActive ? 'resetting' : 'typing';\n\n if (startDelay) {\n phase = 'typingDelay';\n }\n\n const cancelFrame = () => {\n if (typeof animationFrameId === 'number') {\n window.cancelAnimationFrame(animationFrameId);\n animationFrameId = undefined;\n }\n };\n\n const updateShownCharCount = (nextValue: number): void => {\n currentShownCharCount = nextValue;\n setShownCharCount(nextValue);\n };\n\n const startTyping = (): void => {\n if (hasStartedTyping) {\n return;\n }\n\n hasStartedTyping = true;\n\n if (cursorType === CursorType.Thin) {\n setShouldPreventBlinkingCursor(true);\n }\n\n if (typeof onTypingAnimationStart === 'function') {\n onTypingAnimationStart();\n }\n };\n\n const finishTyping = (): void => {\n if (hasEndedTyping) {\n return;\n }\n\n hasEndedTyping = true;\n\n if (cursorType === CursorType.Thin) {\n setShouldPreventBlinkingCursor(false);\n }\n\n if (typeof onTypingAnimationEnd === 'function') {\n onTypingAnimationEnd();\n }\n };\n\n const startReset = (): void => {\n if (hasStartedReset) {\n return;\n }\n\n hasStartedReset = true;\n\n if (typeof onResetAnimationStart === 'function') {\n onResetAnimationStart();\n }\n };\n\n const finishReset = (): void => {\n if (hasEndedReset) {\n return;\n }\n\n hasEndedReset = true;\n\n if (typeof onResetAnimationEnd === 'function') {\n onResetAnimationEnd();\n }\n };\n\n if (shouldStopAnimation || charactersCount === 0) {\n updateShownCharCount(textContentLength);\n return undefined;\n }\n\n if (phase === 'typing') {\n startTyping();\n } else if (phase === 'resetting') {\n startReset();\n }\n\n const tick = (now: number): void => {\n if (shouldStopAnimation || charactersCount === 0) {\n updateShownCharCount(textContentLength);\n cancelFrame();\n return;\n }\n\n const delta = now - lastFrameTime;\n lastFrameTime = now;\n\n switch (phase) {\n case 'typingDelay': {\n if (now - phaseStartTime < startDelay) {\n break;\n }\n\n phase = 'typing';\n phaseStartTime = now;\n typingAccumulator = 0;\n startTyping();\n break;\n }\n case 'typing': {\n startTyping();\n typingAccumulator += delta;\n\n const typingInterval = autoSpeedRef.current ?? speed;\n\n while (typingAccumulator >= typingInterval) {\n typingAccumulator -= typingInterval;\n\n const nextValue = Math.min(\n currentShownCharCount + autoStepsRef.current,\n charactersCount,\n );\n\n if (nextValue >= charactersCount) {\n updateShownCharCount(textContentLength);\n\n if (shouldWaitForContent) {\n typingAccumulator = 0;\n break;\n }\n\n finishTyping();\n\n if (areMultipleChildrenGiven) {\n phase = 'resetDelay';\n phaseStartTime = now;\n typingAccumulator = 0;\n } else {\n cancelFrame();\n return;\n }\n\n break;\n }\n\n updateShownCharCount(nextValue);\n }\n\n break;\n }\n case 'resetDelay': {\n if (now - phaseStartTime < resetDelay) {\n break;\n }\n\n if (areMultipleChildrenGiven) {\n if (shouldUseResetAnimation) {\n setIsResetAnimationActive(true);\n cancelFrame();\n return;\n }\n\n updateShownCharCount(0);\n phase = 'nextTextDelay';\n phaseStartTime = now;\n } else {\n cancelFrame();\n return;\n }\n\n break;\n }\n case 'resetting': {\n startReset();\n resetAccumulator += delta;\n\n const resetInterval = resetSpeed;\n\n while (resetAccumulator >= resetInterval) {\n resetAccumulator -= resetInterval;\n\n const nextValue = Math.max(currentShownCharCount - autoStepsRef.current, 0);\n\n updateShownCharCount(nextValue);\n\n if (nextValue === 0) {\n finishReset();\n\n if (areMultipleChildrenGiven) {\n phase = 'nextTextDelay';\n phaseStartTime = now;\n resetAccumulator = 0;\n } else {\n cancelFrame();\n return;\n }\n\n break;\n }\n }\n\n break;\n }\n case 'nextTextDelay': {\n if (now - phaseStartTime < nextTextDelay) {\n break;\n }\n\n if (shouldUseResetAnimation) {\n setIsResetAnimationActive(false);\n }\n\n onAdvanceText?.();\n cancelFrame();\n return;\n }\n default:\n break;\n }\n\n animationFrameId = window.requestAnimationFrame(tick);\n };\n\n animationFrameId = window.requestAnimationFrame(tick);\n\n return () => {\n cancelFrame();\n };\n }, [\n areMultipleChildrenGiven,\n charactersCount,\n cursorType,\n isResetAnimationActive,\n nextTextDelay,\n onResetAnimationEnd,\n onResetAnimationStart,\n onTypingAnimationEnd,\n onTypingAnimationStart,\n resetDelay,\n resetSpeed,\n shouldStopAnimation,\n shouldUseResetAnimation,\n shouldWaitForContent,\n speed,\n startDelay,\n textContentLength,\n autoSpeedRef,\n autoStepsRef,\n onAdvanceText,\n ]);\n\n return {\n handleClick,\n isResetAnimationActive,\n shownCharCount,\n shouldPreventBlinkingCursor,\n };\n};\n\nexport default useTypewriterAnimation;\n"],"mappings":";;;;;;AACA,IAAAA,MAAA,GAAAC,OAAA;AACA,IAAAC,OAAA,GAAAD,OAAA;AA8BA,MAAME,sBAAsB,GAAGA,CAAC;EAC5BC,wBAAwB;EACxBC,eAAe;EACfC,UAAU;EACVC,aAAa;EACbC,aAAa;EACbC,mBAAmB;EACnBC,qBAAqB;EACrBC,oBAAoB;EACpBC,sBAAsB;EACtBC,UAAU;EACVC,UAAU;EACVC,uBAAuB;EACvBC,oBAAoB;EACpBC,KAAK;EACLC,UAAU;EACVC,iBAAiB;EACjBC,YAAY;EACZC;AAC2B,CAAC,KAAmC;EAC/D,MAAM,CAACC,2BAA2B,EAAEC,8BAA8B,CAAC,GAAG,IAAAC,eAAQ,EAAC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