Skip to content

Commit 21ff802

Browse files
committed
docs(ai-chat-log): modified scrollable exmaple
1 parent e8c1874 commit 21ff802

File tree

2 files changed

+43
-31
lines changed

2 files changed

+43
-31
lines changed

packages/paste-core/components/ai-chat-log/src/AIChatMessageBody.tsx

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,51 @@ export interface AIChatMessageBodyProps extends HTMLPasteProps<"div"> {
4444
* @memberof AIChatMessageBodyProps
4545
*/
4646
animated?: boolean;
47+
/**
48+
* A callback when the animation is started
49+
*
50+
* @default false
51+
* @type {() => void}
52+
* @memberof AIChatMessageBodyProps
53+
*/
54+
onAnimationStart?: () => void;
55+
/**
56+
* A callback when the animation is complete
57+
*
58+
* @default false
59+
* @type {() => void}
60+
* @memberof AIChatMessageBodyProps
61+
*/
62+
onAnimationEnd?: () => void;
4763
}
4864

4965
export const AIChatMessageBody = React.forwardRef<HTMLDivElement, AIChatMessageBodyProps>(
50-
({ children, size = "default", element = "AI_CHAT_MESSAGE_BODY", animated = false, ...props }, ref) => {
66+
(
67+
{
68+
children,
69+
size = "default",
70+
element = "AI_CHAT_MESSAGE_BODY",
71+
animated = false,
72+
onAnimationEnd,
73+
onAnimationStart,
74+
...props
75+
},
76+
ref,
77+
) => {
5178
const { id } = React.useContext(AIMessageContext);
79+
const [showAnimation] = React.useState(animated && children !== undefined);
5280
const animationSpeed = size === "fullScreen" ? 8 : 10;
53-
const childrenToRender = animated ? useAnimatedText(children, animationSpeed) : children;
81+
const { animatedChildren, isAnimating } = useAnimatedText(children, animationSpeed, showAnimation);
82+
83+
React.useEffect(() => {
84+
if (onAnimationStart && animated && isAnimating) {
85+
onAnimationStart();
86+
}
87+
88+
if (animated && !isAnimating && onAnimationEnd) {
89+
onAnimationEnd();
90+
}
91+
}, [isAnimating, showAnimation]);
5492

5593
return (
5694
<Box
@@ -66,7 +104,7 @@ export const AIChatMessageBody = React.forwardRef<HTMLDivElement, AIChatMessageB
66104
whiteSpace="pre-wrap"
67105
id={id}
68106
>
69-
{childrenToRender}
107+
{animatedChildren}
70108
</Box>
71109
);
72110
},

packages/paste-website/src/pages/components/ai-chat-log/index.mdx

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ import {
6060
messageGenerationError,
6161
sendingMessageError,
6262
systemError,
63-
animatedBotWithFeedback,
6463
animatedBotScrollable
6564
} from "../../../component-examples/AIChatLogExamples";
6665
import ComponentPageLayout from "../../../layouts/ComponentPageLayout";
@@ -296,34 +295,9 @@ The SkeletonLoader lengths vary on each render to give a more natural pending me
296295
</LivePreview>
297296

298297
#### Animating
299-
The `AIChatMessageBody` component has an optional `animated` prop that can be used to apply a typewriter animation to the text. This should only be applied to the most recent message received from the AI.
298+
The `AIChatMessageBody` component has an optional `animated` prop that can be used to apply a typewriter animation to the text. This should only be applied to the messages received from the AI.
300299

301-
<LivePreview
302-
scope={{
303-
AIChatLog,
304-
AIChatMessage,
305-
AIChatMessageAuthor,
306-
AIChatMessageBody,
307-
AIChatMessageActionCard,
308-
AIChatMessageActionGroup,
309-
Button,
310-
ThumbsUpIcon,
311-
ThumbsDownIcon,
312-
CopyIcon,
313-
RefreshIcon,
314-
Button,
315-
ResetIcon,
316-
Anchor,
317-
InlineCode
318-
}}
319-
language="jsx"
320-
noInline
321-
>
322-
{animatedBotWithFeedback}
323-
</LivePreview>
324-
325-
##### Scrolling
326-
The `AIChatMessageBody` component has an optional `animated` prop that can be used to apply a typewriter animation to the text. This should only be applied to the most recent message received from the AI.
300+
It also accepts `onAnimationStart` and `onAnimationEnd` props to trigger actions when the animation starts and ends allowing additional logic such as scrolling to be implemented.
327301

328302
<LivePreview
329303
scope={{

0 commit comments

Comments
 (0)