@gravity-ui/aikit 1.3.3 → 1.3.5
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/dist/components/organisms/MessageList/MessageList.js +2 -2
- package/dist/components/organisms/PromptInput/PromptInputWithSuggestions.js +2 -3
- package/dist/components/pages/ChatContainer/__stories__/ChatContainer.stories.d.ts +6 -0
- package/dist/components/pages/ChatContainer/__stories__/ChatContainer.stories.js +90 -0
- package/dist/hooks/useSmartScroll.d.ts +0 -1
- package/dist/hooks/useSmartScroll.js +6 -5
- package/package.json +1 -1
|
@@ -13,7 +13,7 @@ export function MessageList({ messages, messageRendererRegistry, transformOption
|
|
|
13
13
|
const isStreaming = status === 'streaming';
|
|
14
14
|
const isSubmitted = status === 'submitted';
|
|
15
15
|
const showLoader = status && loaderStatuses.includes(status);
|
|
16
|
-
const { containerRef
|
|
16
|
+
const { containerRef } = useSmartScroll({
|
|
17
17
|
isStreaming: isStreaming || isSubmitted,
|
|
18
18
|
messagesCount: messages.length,
|
|
19
19
|
status,
|
|
@@ -38,5 +38,5 @@ export function MessageList({ messages, messageRendererRegistry, transformOption
|
|
|
38
38
|
}
|
|
39
39
|
return null;
|
|
40
40
|
};
|
|
41
|
-
return (_jsxs("div", { ref: containerRef, className: b(null, className), "data-qa": qa, children: [hasPreviousMessages && (_jsx(IntersectionContainer, { onIntersect: onLoadPreviousMessages, className: b('load-trigger'), children: _jsx(Loader, { view: "loading" }) })), _jsx("div", { className: b('messages'), "data-qa": qa, children: messages.map(renderMessage) }), showLoader && _jsx(Loader, { className: b('loader') }), status === 'error' && (_jsx(ErrorAlert, { className: b('error-alert'), onRetry: onRetry, errorMessage: errorMessage }))
|
|
41
|
+
return (_jsxs("div", { ref: containerRef, className: b(null, className), "data-qa": qa, children: [hasPreviousMessages && (_jsx(IntersectionContainer, { onIntersect: onLoadPreviousMessages, className: b('load-trigger'), children: _jsx(Loader, { view: "loading" }) })), _jsx("div", { className: b('messages'), "data-qa": qa, children: messages.map(renderMessage) }), showLoader && _jsx(Loader, { className: b('loader') }), status === 'error' && (_jsx(ErrorAlert, { className: b('error-alert'), onRetry: onRetry, errorMessage: errorMessage }))] }));
|
|
42
42
|
}
|
|
@@ -10,7 +10,6 @@ const b = block('prompt-input');
|
|
|
10
10
|
*/
|
|
11
11
|
export function PromptInputWithSuggestions(props) {
|
|
12
12
|
const { children, suggestionsProps = {}, className, qa } = props;
|
|
13
|
-
const { showSuggestions = false, suggestions, suggestTitle, onSuggestionClick, suggestionsLayout, suggestionsTextAlign = 'center', } = suggestionsProps;
|
|
14
|
-
|
|
15
|
-
return (_jsxs("div", { className: b('suggestions-wrapper', className), "data-qa": qa, children: [hasSuggestions && (_jsx("div", { className: b('suggestions'), children: _jsx(Suggestions, { items: suggestions, onClick: onSuggestionClick || (() => { }), title: suggestTitle, layout: suggestionsLayout, textAlign: suggestionsTextAlign }) })), children] }));
|
|
13
|
+
const { showSuggestions = false, suggestions = [], suggestTitle, onSuggestionClick, suggestionsLayout, suggestionsTextAlign = 'center', } = suggestionsProps;
|
|
14
|
+
return (_jsxs("div", { className: b('suggestions-wrapper', className), "data-qa": qa, children: [showSuggestions && (_jsx("div", { className: b('suggestions'), children: _jsx(Suggestions, { items: suggestions, onClick: onSuggestionClick || (() => { }), title: suggestTitle, layout: suggestionsLayout, textAlign: suggestionsTextAlign }) })), children] }));
|
|
16
15
|
}
|
|
@@ -72,6 +72,12 @@ export declare const FullStreamingExample: Story;
|
|
|
72
72
|
* and preview when the chat is empty (no messages)
|
|
73
73
|
*/
|
|
74
74
|
export declare const HiddenTitleOnEmpty: Story;
|
|
75
|
+
/**
|
|
76
|
+
* Embedded in page with tall content and streaming.
|
|
77
|
+
* Chat is placed in a sidebar; main content has large height and is scrollable.
|
|
78
|
+
* When long text streams in the chat, only the chat panel scrolls — the main page does not jump.
|
|
79
|
+
*/
|
|
80
|
+
export declare const EmbeddedInPageWithStreaming: Story;
|
|
75
81
|
/**
|
|
76
82
|
* With Additional Actions
|
|
77
83
|
* Demonstrates passing additional actions to Header and custom actions to BaseMessage
|
|
@@ -949,6 +949,96 @@ const customAssistantActions = [
|
|
|
949
949
|
const addCustomActionsToMessages = (messages) => {
|
|
950
950
|
return messages.map((msg) => (Object.assign(Object.assign({}, msg), { actions: msg.role === 'user' ? customUserActions : customAssistantActions })));
|
|
951
951
|
};
|
|
952
|
+
/**
|
|
953
|
+
* Embedded in page with tall content and streaming.
|
|
954
|
+
* Chat is placed in a sidebar; main content has large height and is scrollable.
|
|
955
|
+
* When long text streams in the chat, only the chat panel scrolls — the main page does not jump.
|
|
956
|
+
*/
|
|
957
|
+
export const EmbeddedInPageWithStreaming = {
|
|
958
|
+
args: {
|
|
959
|
+
messages: [],
|
|
960
|
+
showActionsOnHover: true,
|
|
961
|
+
welcomeConfig: {
|
|
962
|
+
title: 'Sidebar Chat',
|
|
963
|
+
description: 'Send a message to see streaming. Only the chat area scrolls.',
|
|
964
|
+
suggestionTitle: 'Try:',
|
|
965
|
+
suggestions: [{ id: '1', title: 'Stream a long response' }],
|
|
966
|
+
},
|
|
967
|
+
},
|
|
968
|
+
render: (args) => {
|
|
969
|
+
const [messages, setMessages] = useState([]);
|
|
970
|
+
const [status, setStatus] = useState('ready');
|
|
971
|
+
const handleSendMessage = async (data) => {
|
|
972
|
+
const userMessageId = Date.now().toString();
|
|
973
|
+
const userMessage = {
|
|
974
|
+
id: userMessageId,
|
|
975
|
+
role: 'user',
|
|
976
|
+
content: data.content,
|
|
977
|
+
actions: createMessageActions(userMessageId, 'user'),
|
|
978
|
+
};
|
|
979
|
+
setMessages((prev) => [...prev, userMessage]);
|
|
980
|
+
setStatus('streaming');
|
|
981
|
+
const assistantMessageId = (Date.now() + 1).toString();
|
|
982
|
+
const fullResponse = [
|
|
983
|
+
'This is a simulated long streaming response. When the chat is embedded in an application,',
|
|
984
|
+
'the scroll logic must only scroll the chat container, not the main page.',
|
|
985
|
+
'Using containerRef.scrollTo({ top: scrollHeight, behavior }) ensures that only the message list',
|
|
986
|
+
'container scrolls to the bottom, so the main content stays in place and there is no jerking.',
|
|
987
|
+
'',
|
|
988
|
+
'**Before the fix**, scrollIntoView on the end element could scroll all scrollable ancestors',
|
|
989
|
+
'(including the document), which caused the whole page to jump during streaming.',
|
|
990
|
+
'',
|
|
991
|
+
'**After the fix**, we explicitly scroll only the container element that holds the messages.',
|
|
992
|
+
'The main content on the left can stay scrolled at any position while the chat streams.',
|
|
993
|
+
'',
|
|
994
|
+
'You can scroll the left column up or down and then send a message — only the chat panel',
|
|
995
|
+
'will scroll to show the new content.',
|
|
996
|
+
].join('\n\n');
|
|
997
|
+
setMessages((prev) => [
|
|
998
|
+
...prev,
|
|
999
|
+
{
|
|
1000
|
+
id: assistantMessageId,
|
|
1001
|
+
role: 'assistant',
|
|
1002
|
+
content: '',
|
|
1003
|
+
actions: createMessageActions(assistantMessageId, 'assistant'),
|
|
1004
|
+
},
|
|
1005
|
+
]);
|
|
1006
|
+
const words = fullResponse.split(' ');
|
|
1007
|
+
for (let i = 0; i < words.length; i++) {
|
|
1008
|
+
await new Promise((resolve) => setTimeout(resolve, 80));
|
|
1009
|
+
const currentText = words.slice(0, i + 1).join(' ');
|
|
1010
|
+
setMessages((prev) => prev.map((msg) => msg.id === assistantMessageId ? Object.assign(Object.assign({}, msg), { content: currentText }) : msg));
|
|
1011
|
+
}
|
|
1012
|
+
setStatus('ready');
|
|
1013
|
+
};
|
|
1014
|
+
const handleCancel = async () => {
|
|
1015
|
+
setStatus('ready');
|
|
1016
|
+
};
|
|
1017
|
+
const tallContent = Array.from({ length: 30 }, (_, i) => (_jsxs("p", { children: ["Main application content paragraph ", i + 1, ". This area has large height so the page is scrollable. When the chat on the right streams a long response, only the chat container should scroll \u2014 this column must not move."] }, i)));
|
|
1018
|
+
return (_jsxs("div", { style: {
|
|
1019
|
+
display: 'flex',
|
|
1020
|
+
minHeight: '100vh',
|
|
1021
|
+
}, children: [_jsxs("div", { style: {
|
|
1022
|
+
flex: 1,
|
|
1023
|
+
minWidth: 0,
|
|
1024
|
+
padding: 24,
|
|
1025
|
+
borderRight: '1px solid var(--g-color-line-generic, #e5e5e5)',
|
|
1026
|
+
}, children: [_jsx("h2", { style: { marginTop: 0 }, children: "Main content (tall, scrollable)" }), tallContent] }), _jsx("div", { style: {
|
|
1027
|
+
width: 420,
|
|
1028
|
+
flexShrink: 0,
|
|
1029
|
+
position: 'sticky',
|
|
1030
|
+
top: 0,
|
|
1031
|
+
alignSelf: 'flex-start',
|
|
1032
|
+
height: '100vh',
|
|
1033
|
+
display: 'flex',
|
|
1034
|
+
flexDirection: 'column',
|
|
1035
|
+
minHeight: 0,
|
|
1036
|
+
}, children: _jsx(ChatContainer, Object.assign({}, args, { messages: messages, onSendMessage: handleSendMessage, onCancel: handleCancel, status: status })) })] }));
|
|
1037
|
+
},
|
|
1038
|
+
decorators: [
|
|
1039
|
+
(Story) => (_jsx("div", { style: { minHeight: '100vh', overflow: 'visible' }, children: _jsx(Story, {}) })),
|
|
1040
|
+
],
|
|
1041
|
+
};
|
|
952
1042
|
/**
|
|
953
1043
|
* With Additional Actions
|
|
954
1044
|
* Demonstrates passing additional actions to Header and custom actions to BaseMessage
|
|
@@ -2,7 +2,6 @@ import { type RefObject } from 'react';
|
|
|
2
2
|
import type { ChatStatus } from '../types';
|
|
3
3
|
export interface UseSmartScrollReturn<T extends HTMLElement> {
|
|
4
4
|
containerRef: RefObject<T>;
|
|
5
|
-
endRef: RefObject<T>;
|
|
6
5
|
scrollToBottom: (behavior?: ScrollBehavior) => void;
|
|
7
6
|
}
|
|
8
7
|
export declare function useSmartScroll<T extends HTMLElement>({ isStreaming, messagesCount, status, }: {
|
|
@@ -2,13 +2,15 @@ import { useCallback, useEffect, useRef } from 'react';
|
|
|
2
2
|
const SCROLL_THRESHOLD = 10;
|
|
3
3
|
export function useSmartScroll({ isStreaming = false, messagesCount, status, }) {
|
|
4
4
|
const containerRef = useRef(null);
|
|
5
|
-
const endRef = useRef(null);
|
|
6
5
|
const userScrolledUpRef = useRef(false);
|
|
7
6
|
const scrollToBottom = useCallback((behavior = 'instant') => {
|
|
8
7
|
if (!userScrolledUpRef.current) {
|
|
9
|
-
const
|
|
10
|
-
if (
|
|
11
|
-
|
|
8
|
+
const container = containerRef.current;
|
|
9
|
+
if (container) {
|
|
10
|
+
container.scrollTo({
|
|
11
|
+
top: container.scrollHeight,
|
|
12
|
+
behavior,
|
|
13
|
+
});
|
|
12
14
|
}
|
|
13
15
|
}
|
|
14
16
|
}, []);
|
|
@@ -66,7 +68,6 @@ export function useSmartScroll({ isStreaming = false, messagesCount, status, })
|
|
|
66
68
|
}, [messagesCount]);
|
|
67
69
|
return {
|
|
68
70
|
containerRef,
|
|
69
|
-
endRef,
|
|
70
71
|
scrollToBottom,
|
|
71
72
|
};
|
|
72
73
|
}
|