Skip to content

Commit 0e0f186

Browse files
cpsievertgadenbuie
andauthored
fix(Chat): ui.Chat() should never scroll a parent element of the chat container element (#1996)
Co-authored-by: Garrick Aden-Buie <garrick@posit.co>
1 parent e779c84 commit 0e0f186

File tree

7 files changed

+101
-45
lines changed

7 files changed

+101
-45
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2121

2222
* Fix missing session when trying to display an error duing bookmarking. (#1984)
2323

24+
* Fixed an issue with `ui.Chat()` sometimes wanting to scroll a parent element. (#1996)
25+
2426

2527
## [1.4.0] - 2025-04-08
2628

js/chat/chat.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -557,11 +557,19 @@ class ChatContainer extends LightElement {
557557

558558
// ------- Register custom elements and shiny bindings ---------
559559

560-
customElements.define(CHAT_MESSAGE_TAG, ChatMessage);
561-
customElements.define(CHAT_USER_MESSAGE_TAG, ChatUserMessage);
562-
customElements.define(CHAT_MESSAGES_TAG, ChatMessages);
563-
customElements.define(CHAT_INPUT_TAG, ChatInput);
564-
customElements.define(CHAT_CONTAINER_TAG, ChatContainer);
560+
const chatCustomElements = [
561+
{ tag: CHAT_MESSAGE_TAG, component: ChatMessage },
562+
{ tag: CHAT_USER_MESSAGE_TAG, component: ChatUserMessage },
563+
{ tag: CHAT_MESSAGES_TAG, component: ChatMessages },
564+
{ tag: CHAT_INPUT_TAG, component: ChatInput },
565+
{ tag: CHAT_CONTAINER_TAG, component: ChatContainer }
566+
];
567+
568+
chatCustomElements.forEach(({ tag, component }) => {
569+
if (!customElements.get(tag)) {
570+
customElements.define(tag, component);
571+
}
572+
});
565573

566574
window.Shiny.addCustomMessageHandler(
567575
"shinyChatMessage",
@@ -590,3 +598,5 @@ window.Shiny.addCustomMessageHandler(
590598
el.dispatchEvent(evt);
591599
}
592600
);
601+
602+
export { CHAT_CONTAINER_TAG };

js/markdown-stream/markdown-stream.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import ClipboardJS from "clipboard";
66
import hljs from "highlight.js/lib/common";
77
import { Renderer, parse } from "marked";
88

9+
import { CHAT_CONTAINER_TAG } from "../chat/chat";
10+
911
import {
1012
LightElement,
1113
createElement,
@@ -282,6 +284,12 @@ class MarkdownElement extends LightElement {
282284
while (el) {
283285
if (el.scrollHeight > el.clientHeight) return el;
284286
el = el.parentElement;
287+
if (el?.tagName === CHAT_CONTAINER_TAG) {
288+
// This ensures that we do not accidentally scroll a parent element of the chat
289+
// container. If the chat container itself is scrollable, a scrollable element
290+
// would already have been identified.
291+
break;
292+
}
285293
}
286294
return null;
287295
}

shiny/www/py-shiny/chat/chat.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

shiny/www/py-shiny/chat/chat.js.map

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

shiny/www/py-shiny/markdown-stream/markdown-stream.js

Lines changed: 66 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

shiny/www/py-shiny/markdown-stream/markdown-stream.js.map

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)