-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ai-chat-log): wip typewriter animations
- Loading branch information
1 parent
77ec1d1
commit 2c100d8
Showing
5 changed files
with
226 additions
and
0 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
packages/paste-core/components/ai-chat-log/src/AIChatLogContext.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import * as React from "react"; | ||
|
||
|
||
export interface AIChatLogContextProps { | ||
isAnimating: boolean; | ||
setIsAnimating: (animating: boolean) => void; | ||
} | ||
export const AIChatLogContext = React.createContext<AIChatLogContextProps>({} as any); |
72 changes: 72 additions & 0 deletions
72
packages/paste-core/components/ai-chat-log/src/AIChatMessageBodyTypeWriter.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { Box, safelySpreadBoxProps } from "@twilio-paste/box"; | ||
import type { BoxElementProps } from "@twilio-paste/box"; | ||
import type { ThemeShape } from "@twilio-paste/theme"; | ||
import type { HTMLPasteProps } from "@twilio-paste/types"; | ||
import * as React from "react"; | ||
import { useAnimatedText } from "./utils"; | ||
|
||
const Variants = { | ||
default: { | ||
fontSize: "fontSize30" as ThemeShape["fontSizes"], | ||
lineHeight: "lineHeight30" as ThemeShape["lineHeights"], | ||
}, | ||
fullScreen: { | ||
fontSize: "fontSize40" as ThemeShape["fontSizes"], | ||
lineHeight: "lineHeight40" as ThemeShape["lineHeights"], | ||
}, | ||
}; | ||
|
||
export interface AIChatMessageBodyTypeWriterProps extends HTMLPasteProps<"div"> { | ||
children?: React.ReactNode; | ||
/** | ||
* Overrides the default element name to apply unique styles with the Customization Provider | ||
* | ||
* @default "AI_CHAT_MESSAGE_BODY_TYPE_WRITER" | ||
* @type {BoxProps["element"]} | ||
* @memberof AIChatMessageBodyTypeWriterProps | ||
*/ | ||
element?: BoxElementProps["element"]; | ||
/** | ||
* Override the font size for full screen experiences. | ||
* | ||
* @default "default" | ||
* @type {"default" | "fullScreen"} | ||
* @memberof AIChatMessageBodyTypeWriterProps | ||
*/ | ||
variant?: "default" | "fullScreen"; | ||
/** | ||
* Whether the text should be animated with type writer effect | ||
* | ||
* @default true | ||
* @type {boolean} | ||
* @memberof AIChatMessageBodyTypeWriterProps | ||
*/ | ||
animated?: boolean; | ||
} | ||
|
||
export const AIChatMessageBodyTypeWriter = React.forwardRef<HTMLDivElement, AIChatMessageBodyTypeWriterProps>( | ||
({ children, variant = "default", element = "AI_CHAT_MESSAGE_BODY_TYPE_WRITER", animated = true, onAnimationEnd, onAnimationStart, ...props }, ref) => { | ||
const animatedChildren = useAnimatedText(children); | ||
|
||
return ( | ||
<Box | ||
{...safelySpreadBoxProps(props)} | ||
{...Variants[variant]} | ||
display="inline-block" | ||
color="colorText" | ||
wordWrap="break-word" | ||
maxWidth="100%" | ||
minWidth={0} | ||
element={element} | ||
ref={ref} | ||
whiteSpace="pre-wrap" | ||
> | ||
{animated | ||
? animatedChildren | ||
: children} | ||
</Box> | ||
); | ||
}, | ||
); | ||
|
||
AIChatMessageBodyTypeWriter.displayName = "AIChatMessageBodyTypeWriter"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import React, { useState, useEffect } from "react"; | ||
import { AIChatLogContext } from "./AIChatLogContext"; | ||
|
||
// Hook to animate text content of React elements | ||
export const useAnimatedText = (children: React.ReactNode, speed: number = 10): React.ReactNode => { | ||
const {setIsAnimating, isAnimating} = React.useContext(AIChatLogContext); | ||
const [animatedChildren, setAnimatedChildren] = useState<React.ReactNode>(); | ||
const [textIndex, setTextIndex] = useState(0); | ||
|
||
// Effect to increment textIndex at a specified speed | ||
useEffect(() => { | ||
const interval = setInterval(() => { | ||
setTextIndex((prevIndex) => prevIndex + 1); | ||
}, speed); | ||
|
||
return () => clearInterval(interval); | ||
}, [speed]); | ||
|
||
// Function to calculate the total length of text within nested elements | ||
const calculateTotalTextLength = (children: React.ReactNode): number => { | ||
let length = 0; | ||
React.Children.forEach(children, (child) => { | ||
if (typeof child === "string") { | ||
length += child.length; | ||
} else if (React.isValidElement(child)) { | ||
length += calculateTotalTextLength(child.props.children); | ||
} | ||
}); | ||
return length; | ||
}; | ||
|
||
// Function to recursively clone children and apply text animation | ||
const cloneChildren = (children: React.ReactNode, currentIndex: number): React.ReactNode => { | ||
let currentTextIndex = currentIndex; | ||
return React.Children.map(children, (child) => { | ||
if (typeof child === "string") { | ||
// Only include text nodes if their animation has started | ||
if (currentTextIndex > 0) { | ||
const visibleText = child.slice(0, currentTextIndex); | ||
currentTextIndex -= child.length; | ||
return <span>{visibleText}</span>; | ||
} | ||
return null; | ||
} | ||
|
||
if (React.isValidElement(child)) { | ||
const totalChildTextLength = calculateTotalTextLength(child.props.children); | ||
// Only include elements if their text animation has started | ||
if (currentTextIndex > 0) { | ||
const clonedChild = React.cloneElement(child, {}, cloneChildren(child.props.children, currentTextIndex)); | ||
currentTextIndex -= totalChildTextLength; | ||
return clonedChild; | ||
} | ||
return null; | ||
} | ||
|
||
return child; | ||
}); | ||
}; | ||
|
||
// Effect to update animated children based on the current text index | ||
useEffect(() => { | ||
const totaLength = calculateTotalTextLength(children); | ||
if (textIndex <= totaLength) { | ||
setAnimatedChildren(cloneChildren(children, textIndex)); | ||
if(!isAnimating){ | ||
setIsAnimating && setIsAnimating(true); | ||
} | ||
} else if(isAnimating){ | ||
setIsAnimating && setIsAnimating(false); | ||
} | ||
}, [children, textIndex]); | ||
|
||
return animatedChildren | ||
}; | ||
|
||
export default useAnimatedText; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters