Skip to content

Commit 9d7e467

Browse files
committed
Add chat input suggestions
1 parent 3d1108c commit 9d7e467

File tree

9 files changed

+183
-9
lines changed

9 files changed

+183
-9
lines changed

js/chat/chat.scss

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ shiny-chat-container {
1212
p:last-child {
1313
margin-bottom: 0;
1414
}
15+
16+
.chat-input-suggestion, [data-input-suggestion] {
17+
text-decoration: underline;
18+
text-decoration-style: dotted;
19+
text-decoration-color: var(--bs-link-color, #007bc2);
20+
cursor: pointer;
21+
}
1522
}
1623

1724
shiny-chat-messages {

js/chat/chat.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ class ChatContainer extends LightElement {
227227
"shiny-chat-remove-loading-message",
228228
this.#onRemoveLoadingMessage
229229
);
230+
this.addEventListener("click", this.#onInputSuggestionClick);
230231
}
231232

232233
disconnectedCallback(): void {
@@ -247,6 +248,7 @@ class ChatContainer extends LightElement {
247248
"shiny-chat-remove-loading-message",
248249
this.#onRemoveLoadingMessage
249250
);
251+
this.removeEventListener("click", this.#onInputSuggestionClick);
250252
}
251253

252254
// When user submits input, append it to the chat, and add a loading message
@@ -332,6 +334,24 @@ class ChatContainer extends LightElement {
332334
}
333335
}
334336

337+
#onInputSuggestionClick(e: Event): void {
338+
const target = e.target;
339+
if (!(target instanceof HTMLElement)) return;
340+
341+
const isSuggestion =
342+
target.classList.contains("chat-input-suggestion") ||
343+
target.dataset.inputSuggestion !== undefined;
344+
345+
if (!isSuggestion) return;
346+
347+
e.preventDefault();
348+
349+
const suggestion = target.dataset.inputSuggestion || target.textContent;
350+
if (suggestion) {
351+
this.input.setInputValue(suggestion);
352+
}
353+
}
354+
335355
#onRemoveLoadingMessage(): void {
336356
this.#removeLoadingMessage();
337357
this.#finalizeMessage();

shiny/www/py-shiny/chat/chat.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

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

0 commit comments

Comments
 (0)