Skip to content

The Open Standard for Generative UI

fix scroll behaviour (#131)

abhithesys8a9b89c77032
diff --git a/js/packages/react-ui/package.json b/js/packages/react-ui/package.json
index 77868aa43..69e60fc59 100644
--- a/js/packages/react-ui/package.json
+++ b/js/packages/react-ui/package.json
@@ -2,7 +2,7 @@
"type": "module",
"name": "@crayonai/react-ui",
"license": "MIT",
- "version": "0.7.10",
+ "version": "0.7.11",
"description": "Component library for Generative UI SDK",
"main": "dist/index.js",
"types": "dist/index.d.ts",
diff --git a/js/packages/react-ui/src/components/CopilotShell/Thread.tsx b/js/packages/react-ui/src/components/CopilotShell/Thread.tsx
index d57d1f7ec..f3cf94ef5 100644
--- a/js/packages/react-ui/src/components/CopilotShell/Thread.tsx
+++ b/js/packages/react-ui/src/components/CopilotShell/Thread.tsx
@@ -9,7 +9,7 @@ import clsx from "clsx";
import { ArrowRight, Square } from "lucide-react";
import React, { memo, useLayoutEffect, useRef } from "react";
import { useComposerState } from "../../hooks/useComposerState";
-import { useScrollToBottom } from "../../hooks/useScrollToBottom";
+import { ScrollVariant, useScrollToBottom } from "../../hooks/useScrollToBottom";
import { IconButton } from "../IconButton";
import { MessageLoading as MessageLoadingComponent } from "../MessageLoading";
@@ -26,22 +26,22 @@ export const ThreadContainer = ({
export const ScrollArea = ({
children,
className,
- scrollVariant = "always",
- userMessageSelector,
+ scrollVariant = "user-message-anchor",
+ userMessageSelector = ".crayon-copilot-shell-thread-message-user",
}: {
children?: React.ReactNode;
className?: string;
/**
* Scroll to bottom once the last message is added
*/
- scrollVariant?: "always" | "once" | "user-message-anchor";
+ scrollVariant?: ScrollVariant;
/**
* Selector for the user message
*/
userMessageSelector?: string;
}) => {
const ref = useRef<HTMLDivElement>(null);
- const { messages, isRunning } = useThreadState();
+ const { messages, isRunning, isLoadingMessages } = useThreadState();
useScrollToBottom({
ref,
@@ -49,10 +49,21 @@ export const ScrollArea = ({
scrollVariant,
userMessageSelector,
isRunning,
+ isLoadingMessages,
});
return (
- <div ref={ref} className={clsx("crayon-copilot-shell-thread-scroll-area", className)}>
+ <div
+ ref={ref}
+ className={clsx(
+ "crayon-copilot-shell-thread-scroll-area",
+ {
+ "crayon-copilot-shell-thread-scroll-area--user-message-anchor":
+ scrollVariant === "user-message-anchor",
+ },
+ className,
+ )}
+ >
{children}
</div>
);
@@ -177,7 +188,7 @@ export const Messages = ({
</MessageProvider>
);
})}
- {isRunning && loader}
+ {isRunning && <div>{loader}</div>}
</div>
);
};
diff --git a/js/packages/react-ui/src/components/CopilotShell/thread.scss b/js/packages/react-ui/src/components/CopilotShell/thread.scss
index 41581ea87..db6fdcc50 100644
--- a/js/packages/react-ui/src/components/CopilotShell/thread.scss
+++ b/js/packages/react-ui/src/components/CopilotShell/thread.scss
@@ -12,6 +12,11 @@
flex: 1;
overflow: auto;
padding: cssUtils.$spacing-l;
+ &--user-message-anchor {
+ & .crayon-copilot-shell-thread-messages > *:last-child {
+ min-height: calc(-200px + 100dvh);
+ }
+ }
}
.crayon-copilot-shell-thread-messages {
diff --git a/js/packages/react-ui/src/components/CrayonChat/ComposedCopilot.tsx b/js/packages/react-ui/src/components/CrayonChat/ComposedCopilot.tsx
index 10e14d086..8d702d127 100644
--- a/js/packages/react-ui/src/components/CrayonChat/ComposedCopilot.tsx
+++ b/js/packages/react-ui/src/components/CrayonChat/ComposedCopilot.tsx
@@ -1,3 +1,4 @@
+import { ScrollVariant } from "../../hooks/useScrollToBottom";
import {
Composer,
Container,
@@ -7,23 +8,24 @@ import {
ScrollArea,
ThreadContainer,
} from "../CopilotShell";
-
interface ComposedCopilotProps {
logoUrl?: string;
agentName?: string;
messageLoadingComponent?: () => React.ReactNode;
+ scrollVariant: ScrollVariant;
}
export const ComposedCopilot = ({
logoUrl = "https://crayonai.org/img/logo.png",
agentName = "My Agent",
messageLoadingComponent: MessageLoadingComponent = MessageLoading,
+ scrollVariant,
}: ComposedCopilotProps) => {
return (
<Container logoUrl={logoUrl} agentName={agentName}>
<ThreadContainer>
<Header />
- <ScrollArea>
+ <ScrollArea scrollVariant={scrollVariant}>
<Messages loader={<MessageLoadingComponent />} />
</ScrollArea>
<Composer />
diff --git a/js/packages/react-ui/src/components/CrayonChat/ComposedStandalone.tsx b/js/packages/react-ui/src/components/CrayonChat/ComposedStandalone.tsx
index 7ef05fc69..4d8b697dd 100644
--- a/js/packages/react-ui/src/components/CrayonChat/ComposedStandalone.tsx
+++ b/js/packages/react-ui/src/components/CrayonChat/ComposedStandalone.tsx
@@ -1,3 +1,4 @@
+import { ScrollVariant } from "../../hooks/useScrollToBottom";
import {
Composer,
Container,
@@ -13,17 +14,18 @@ import {
ThreadContainer,
ThreadList,
} from "../Shell";
-
interface ComposedStandaloneProps {
logoUrl?: string;
agentName?: string;
messageLoadingComponent?: () => React.ReactNode;
+ scrollVariant: ScrollVariant;
}
export const ComposedStandalone = ({
logoUrl = "https://crayonai.org/img/logo.png",
agentName = "My Agent",
messageLoadingComponent: MessageLoadingComponent = MessageLoading,
+ scrollVariant,
}: ComposedStandaloneProps) => {
return (
<Container logoUrl={logoUrl} agentName={agentName}>
@@ -37,7 +39,7 @@ export const ComposedStandalone = ({
</SidebarContainer>
<ThreadContainer>
<MobileHeader />
- <ScrollArea>
+ <ScrollArea scrollVariant={scrollVariant}>
<Messages loader={<MessageLoadingComponent />} />
</ScrollArea>
<Composer />
diff --git a/js/packages/react-ui/src/components/CrayonChat/CrayonChat.tsx b/js/packages/react-ui/src/components/CrayonChat/CrayonChat.tsx
index e8398effc..76785affb 100644
--- a/js/packages/react-ui/src/components/CrayonChat/CrayonChat.tsx
+++ b/js/packages/react-ui/src/components/CrayonChat/CrayonChat.tsx
@@ -13,6 +13,7 @@ import {
} from "@crayonai/react-core";
import { useEffect, useRef } from "react";
import invariant from "tiny-invariant";
+import { ScrollVariant } from "../../hooks/useScrollToBottom";
import { ThemeProps, ThemeProvider } from "../ThemeProvider";
import { ComposedCopilot } from "./ComposedCopilot";
import { ComposedStandalone } from "./ComposedStandalone";
@@ -39,6 +40,7 @@ type CrayonChatProps = {
logoUrl?: string;
agentName?: string;
type?: "copilot" | "standalone";
+ scrollVariant?: ScrollVariant;
messageLoadingComponent?: () => React.ReactNode;
};
@@ -56,6 +58,7 @@ export const CrayonChat = ({
messageLoadingComponent,
type = "standalone",
theme,
+ scrollVariant = "user-message-anchor",
}: CrayonChatProps) => {
invariant(processMessage || userThreadManager, "processMessage or threadManager is required");
@@ -137,12 +140,14 @@ export const CrayonChat = ({
logoUrl={logoUrl}
agentName={agentName}
messageLoadingComponent={messageLoadingComponent}
+ scrollVariant={scrollVariant}
/>
) : (
<ComposedStandalone
logoUrl={logoUrl}
agentName={agentName}
messageLoadingComponent={messageLoadingComponent}
+ scrollVariant={scrollVariant}
/>
)}
</ChatProvider>
diff --git a/js/packages/react-ui/src/components/Shell/Thread.tsx b/js/packages/react-ui/src/components/Shell/Thread.tsx
index 39aa60602..1a7c9a034 100644
--- a/js/packages/react-ui/src/components/Shell/Thread.tsx
+++ b/js/packages/react-ui/src/components/Shell/Thread.tsx
@@ -9,7 +9,7 @@ import clsx from "clsx";
import { ArrowRight, Square } from "lucide-react";
import React, { memo, useLayoutEffect, useRef } from "react";
import { useComposerState } from "../../hooks/useComposerState";
-import { useScrollToBottom } from "../../hooks/useScrollToBottom";
+import { ScrollVariant, useScrollToBottom } from "../../hooks/useScrollToBottom";
import { IconButton } from "../IconButton";
import { MessageLoading as MessageLoadingComponent } from "../MessageLoading";
import { useShellStore } from "./store";
@@ -27,7 +27,7 @@ export const ThreadContainer = ({
export const ScrollArea = ({
children,
className,
- scrollVariant = "always",
+ scrollVariant = "user-message-anchor",
userMessageSelector,
}: {
children?: React.ReactNode;
@@ -35,14 +35,14 @@ export const ScrollArea = ({
/**
* Scroll to bottom once the last message is added
*/
- scrollVariant?: "always" | "once" | "user-message-anchor";
+ scrollVariant?: ScrollVariant;
/**
* Selector for the user message
*/
userMessageSelector?: string;
}) => {
const ref = useRef<HTMLDivElement>(null);
- const { messages, isRunning } = useThreadState();
+ const { messages, isRunning, isLoadingMessages } = useThreadState();
useScrollToBottom({
ref,
@@ -50,10 +50,21 @@ export const ScrollArea = ({
scrollVariant,
userMessageSelector,
isRunning,
+ isLoadingMessages,
});
return (
- <div ref={ref} className={clsx("crayon-shell-thread-scroll-area", className)}>
+ <div
+ ref={ref}
+ className={clsx(
+ "crayon-shell-thread-scroll-area",
+ {
+ "crayon-shell-thread-scroll-area--user-message-anchor":
+ scrollVariant === "user-message-anchor",
+ },
+ className,
+ )}
+ >
{children}
</div>
);
@@ -180,7 +191,7 @@ export const Messages = ({
</MessageProvider>
);
})}
- {isRunning && loader}
+ {isRunning && <div>{loader}</div>}
</div>
);
};
diff --git a/js/packages/react-ui/src/components/Shell/thread.scss b/js/packages/react-ui/src/components/Shell/thread.scss
index bf3b2ef96..9de532f23 100644
--- a/js/packages/react-ui/src/components/Shell/thread.scss
+++ b/js/packages/react-ui/src/components/Shell/thread.scss
@@ -18,6 +18,12 @@ $center-align-spacing: calc(32px + cssUtils.$spacing-s);
.crayon-shell-container--mobile & {
padding: cssUtils.$spacing-l;
}
+
+ &--user-message-anchor {
+ & .crayon-shell-thread-messages > *:last-child {
+ min-height: calc(-200px + 100dvh);
+ }
+ }
}
.crayon-shell-thread-messages {
diff --git a/js/packages/react-ui/src/hooks/useScrollToBottom.ts b/js/packages/react-ui/src/hooks/useScrollToBottom.ts
index 4456dd083..04a34a26f 100644
--- a/js/packages/react-ui/src/hooks/useScrollToBottom.ts
+++ b/js/packages/react-ui/src/hooks/useScrollToBottom.ts
@@ -1,21 +1,76 @@
import { RefObject, useCallback, useEffect, useRef } from "react";
+export type ScrollVariant = "always" | "once" | "user-message-anchor";
export const useScrollToBottom = <T extends HTMLElement | null, L extends { id: string }>({
ref,
lastMessage,
scrollVariant,
userMessageSelector = ".crayon-shell-thread-message-user",
isRunning,
+ isLoadingMessages,
}: {
ref: RefObject<T>;
// scroll to bottom when this value changes
lastMessage: L;
- scrollVariant: "always" | "once" | "user-message-anchor";
+ scrollVariant: ScrollVariant;
userMessageSelector?: string;
+ // if true, scroll to bottom logic will work
isRunning?: boolean;
+ isLoadingMessages?: boolean;
}) => {
const previousLastMessage = useRef<L | null>(null);
const lastUserMessage = useRef<HTMLElement | null>(null);
+ const hasUserScrolledWhenIsRunning = useRef(false);
+ const wasScrolledToBottomAfterLoading = useRef(false);
+
+ useEffect(() => {
+ const element = ref.current;
+ if (!isRunning || !element) {
+ hasUserScrolledWhenIsRunning.current = false;
+ return;
+ }
+ let isUserScrolling = false;
+
+ const userScrollingDisabler = () => {
+ isUserScrolling = false;
+ };
+
+ const userScrollingEnabler = () => {
+ isUserScrolling = true;
+ };
+
+ const scrollListener = () => {
+ if (!isUserScrolling) {
+ return;
+ }
+ hasUserScrolledWhenIsRunning.current = true;
+ removeListeners();
+ };
+
+ const addListeners = () => {
+ element.addEventListener("scroll", scrollListener);
+ element.addEventListener("click", userScrollingDisabler);
+ element.addEventListener("touchmove", userScrollingEnabler);
+ element.addEventListener("wheel", userScrollingEnabler);
+ element.addEventListener("keydown", userScrollingEnabler);
+ element.addEventListener("mousedown", userScrollingEnabler);
+ };
+
+ const removeListeners = () => {
+ element.removeEventListener("scroll", scrollListener);
+ element.removeEventListener("click", userScrollingDisabler);
+ element.removeEventListener("touchmove", userScrollingEnabler);
+ element.removeEventListener("wheel", userScrollingEnabler);
+ element.removeEventListener("keydown", userScrollingEnabler);
+ element.removeEventListener("mousedown", userScrollingEnabler);
+ };
+
+ addListeners();
+
+ return () => {
+ removeListeners();
+ };
+ }, [isRunning]);
const scrollToBottom = useCallback(() => {
const element = ref.current;
@@ -25,10 +80,15 @@ export const useScrollToBottom = <T extends HTMLElement | null, L extends { id:
if (scrollVariant === "always") {
if (previousLastMessage.current !== lastMessage) {
- element.scrollTo({
- top: element.scrollHeight,
- behavior: "smooth",
- });
+ const scrollBy = element.scrollHeight - element.scrollTop - element.clientHeight;
+ if (scrollBy > 90) {
+ element.scrollTo({
+ top: element.scrollHeight,
+ behavior: "smooth",
+ });
+ } else {
+ element.scrollTop = element.scrollHeight;
+ }
}
} else if (scrollVariant === "once") {
if (previousLastMessage.current?.id !== lastMessage.id) {
@@ -42,10 +102,10 @@ export const useScrollToBottom = <T extends HTMLElement | null, L extends { id:
| HTMLElement
| undefined;
const lastUserMessageNextSibling = lastUserMessageDiv?.nextElementSibling;
-
if (
lastUserMessageDiv &&
- !lastUserMessageNextSibling?.nextElementSibling &&
+ (!lastUserMessageNextSibling?.nextElementSibling ||
+ !wasScrolledToBottomAfterLoading.current) &&
previousLastMessage.current?.id !== lastMessage.id
) {
// scroll to last user message till there is only 1 more message
@@ -63,12 +123,16 @@ export const useScrollToBottom = <T extends HTMLElement | null, L extends { id:
}, [ref, lastMessage, scrollVariant, userMessageSelector]);
useEffect(() => {
- scrollToBottom();
- }, [scrollToBottom]);
-
- useCallback(() => {
- if (isRunning) {
+ if (
+ (isRunning && !hasUserScrolledWhenIsRunning.current) || // scroll to bottom if user hasn't scrolled when isRunning is true
+ (!wasScrolledToBottomAfterLoading.current && !isLoadingMessages) // scroll to bottom once when messages are loaded
+ ) {
scrollToBottom();
+ wasScrolledToBottomAfterLoading.current = true;
}
- }, [isRunning]);
+
+ if (isLoadingMessages) {
+ wasScrolledToBottomAfterLoading.current = false;
+ }
+ }, [scrollToBottom, isRunning, isLoadingMessages]);
};