-
Notifications
You must be signed in to change notification settings - Fork 119
feat(ai-chat-log): add typewriter animations #4199
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 7 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
2c100d8
feat(ai-chat-log): wip typewriter animations
krisantrobus 94a9315
fix(ai-chat-log): remove nested span
krisantrobus 32cc702
feat(ai-chat-log): typewiter code
krisantrobus 59470a6
feat(docs): udpate docs and changesets
krisantrobus 7cf6a46
chore(ai-chat-log): linting
krisantrobus da4b8d0
docs(ai-chat-log): update definition
krisantrobus 19b5b88
docs(ai-chat-log): docs spelling
krisantrobus 9ca09bd
feat(ai-chat-log): typewiter speeds
krisantrobus a0f40c3
chore(docs): rearrange ai chat log sections
krisantrobus e8c1874
docs(ai-chat-log): added scollable example
krisantrobus 21ff802
docs(ai-chat-log): modified scrollable exmaple
krisantrobus 869544f
chore(ai-chat-log): typedocs
krisantrobus d62b979
feat(ai-chat-log): story for user cancel scroll to end
krisantrobus d6e8cac
Merge branch 'main' into feat-ai-typewriter-animation
kodiakhq[bot] 85a919b
Merge branch 'main' into feat-ai-typewriter-animation
krisantrobus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,6 @@ | ||
--- | ||
"@twilio-paste/ai-chat-log": minor | ||
"@twilio-paste/core": minor | ||
--- | ||
|
||
[AI Chat Log] added optional typewriter animation to AIChatMessageBody |
This file contains hidden or 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 hidden or 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,70 @@ | ||
import React, { useEffect, useState } from "react"; | ||
|
||
// Hook to animate text content of React elements | ||
export const useAnimatedText = (children: React.ReactNode, speed = 10): React.ReactNode => { | ||
krisantrobus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 = (nodes: React.ReactNode): number => { | ||
let length = 0; | ||
React.Children.forEach(nodes, (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 = (nodes: React.ReactNode, currentIndex: number): React.ReactNode => { | ||
let currentTextIndex = currentIndex; | ||
return React.Children.map(nodes, (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 visibleText; | ||
} | ||
return null; | ||
} else 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; | ||
} else if (currentTextIndex === 0 && totalChildTextLength === 0) { | ||
return child; | ||
} | ||
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)); | ||
} | ||
}, [children, textIndex]); | ||
|
||
return animatedChildren; | ||
}; | ||
|
||
export default useAnimatedText; |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.