Skip to content

Commit dcd072c

Browse files
dawoodkhan82gradio-pr-botcursoragent
authored
Migrate Chatbot, Tabs, TabItem to Svelte 5 (#13509)
* migration * add changeset * fixes * Fix tabs reactive storm during mount that froze E2E tests. Use in-place tab updates and untrack sync effects so registering many TabItems no longer retriggers overflow and change_tab on every mount. Co-authored-by: Cursor <[email protected]> * fixes * add changeset * Address Copilot review comments * fixes * more fixes * fix scroll bar in multimodal textbox * add changeset * textbox height --------- Co-authored-by: gradio-pr-bot <[email protected]> Co-authored-by: Cursor <[email protected]>
1 parent 1b611f8 commit dcd072c

27 files changed

Lines changed: 1128 additions & 666 deletions

‎.agents/skills/frontend-unit-testing/SKILL.md‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,27 +131,40 @@ await event.clear(el);
131131

132132
**Always use `pnpm test:run`.** Never use `pnpm test` — it starts in watch mode and never exits.
133133

134+
**Always prefix test commands with `CI=1`** so Vitest runs Playwright in headless mode (`headless: !!process.env.CI` in `js/spa/vite.config.ts`). This prevents a visible Chromium window from opening on the developer's desktop during agent verification runs.
135+
134136
All commands are run from the repo root.
135137

136138
```bash
137-
# Run all unit tests
138-
pnpm test:run
139+
# Run all unit tests (headless)
140+
CI=1 pnpm test:run
139141

140142
# Run a specific test file (match by filename)
141-
pnpm test:run Textbox.test.ts
143+
CI=1 pnpm test:run Textbox.test.ts
142144

143145
# Run all tests within a folder (match by path segment)
144-
pnpm test:run dataframe
146+
CI=1 pnpm test:run dataframe
145147

146148
# Filter by test name with -t
147-
pnpm test:run -t elem_id
149+
CI=1 pnpm test:run -t elem_id
148150

149151
# Combine file/folder filter with test name filter
150-
pnpm test:run accordion -t elem_id
152+
CI=1 pnpm test:run accordion -t elem_id
153+
154+
# Single file via vitest directly
155+
CI=1 npx vitest run --config .config/vitest.config.ts js/<pkg>/<Test>.test.ts
156+
```
157+
158+
If tests fail with a missing `chrome-headless-shell` executable, install it once:
159+
160+
```bash
161+
pnpm exec playwright install chromium --only-shell
151162
```
152163

153164
After writing or modifying tests, always run them to verify they pass.
154165

166+
**Do NOT run browser tests automatically on branch checkout or `git pull`.** Only run tests when the user explicitly asks, or immediately after writing/modifying a test file. Branch operations are routine and should not trigger test runs.
167+
155168
## Test File Structure
156169

157170
```ts

‎.changeset/neat-turkeys-yell.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
"@gradio/chatbot": minor
3+
"@gradio/core": minor
4+
"@gradio/multimodaltextbox": minor
5+
"@gradio/tabitem": minor
6+
"@gradio/tabs": minor
7+
"gradio": minor
8+
---
9+
10+
feat:Migrate Chatbot, Tabs, TabItem to Svelte 5

‎js/chatbot/Chatbot.test.ts‎

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { test, describe, expect, afterEach, vi } from "vitest";
2-
import { cleanup, render, fireEvent } from "@self/tootils/render";
2+
import { cleanup, render, fireEvent, waitFor } from "@self/tootils/render";
33
import { run_shared_prop_tests } from "@self/tootils/shared-prop-tests";
44
import Chatbot from "./Index.svelte";
55
import {
@@ -408,6 +408,44 @@ describe("Props: allow_tags", () => {
408408
});
409409
});
410410

411+
describe("Props: autoscroll", () => {
412+
afterEach(() => cleanup());
413+
414+
test("does not force-scroll to the bottom when the user has scrolled up", async () => {
415+
const messages = Array.from({ length: 20 }, (_, index) =>
416+
text_msg(
417+
index % 2 === 0 ? "user" : "assistant",
418+
`message ${index}`,
419+
index
420+
)
421+
);
422+
const { getByRole, set_data } = await render(Chatbot, {
423+
...default_props,
424+
autoscroll: true,
425+
height: 180,
426+
value: messages
427+
});
428+
const log = getByRole("log") as HTMLDivElement;
429+
430+
await waitFor(() =>
431+
expect(log.scrollHeight).toBeGreaterThan(log.clientHeight)
432+
);
433+
await waitFor(() => expect(log.scrollTop).toBeGreaterThan(0));
434+
435+
log.scrollTop = 0;
436+
await fireEvent.scroll(log);
437+
438+
await set_data({
439+
value: [
440+
...messages,
441+
text_msg("assistant", "streamed token", messages.length)
442+
]
443+
});
444+
445+
expect(log.scrollTop).toBe(0);
446+
});
447+
});
448+
411449
describe("Props: options", () => {
412450
afterEach(() => cleanup());
413451

‎js/chatbot/Index.svelte‎

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import { Block, BlockLabel } from "@gradio/atoms";
88
import { Chat } from "@gradio/icons";
99
import { StatusTracker } from "@gradio/statustracker";
10-
import type { Message, ExampleMessage, NormalisedMessage } from "./types";
10+
import type { Message, NormalisedMessage } from "./types";
1111
import type { ChatbotProps, ChatbotEvents } from "./types";
1212
import { normalise_messages } from "./shared/utils";
1313
import { Gradio } from "@gradio/utils";
@@ -96,31 +96,31 @@
9696
}}
9797
like_user_message={gradio.props.like_user_message}
9898
show_progress={gradio.shared.loading_status?.show_progress || "full"}
99-
on:change={() => (
100-
(gradio.props.value = gradio.props.value),
101-
gradio.dispatch("change", gradio.props.value)
102-
)}
103-
on:select={(e) => gradio.dispatch("select", e.detail)}
104-
on:like={(e) => gradio.dispatch("like", e.detail)}
105-
on:share={(e) => gradio.dispatch("share", e.detail)}
106-
on:error={(e) => gradio.dispatch("error", e.detail)}
107-
on:example_select={(e) => gradio.dispatch("example_select", e.detail)}
108-
on:option_select={(e) => gradio.dispatch("option_select", e.detail)}
109-
on:retry={(e) => gradio.dispatch("retry", e.detail)}
110-
on:undo={(e) => gradio.dispatch("undo", e.detail)}
111-
on:clear={() => {
99+
onchange={() => {
100+
gradio.props.value = gradio.props.value;
101+
gradio.dispatch("change", gradio.props.value);
102+
}}
103+
onselect={(data) => gradio.dispatch("select", data)}
104+
onlike={(data) => gradio.dispatch("like", data)}
105+
onshare={(data) => gradio.dispatch("share", data)}
106+
onerror={(message) => gradio.dispatch("error", message)}
107+
onexampleselect={(data) => gradio.dispatch("example_select", data)}
108+
onoptionselect={(data) => gradio.dispatch("option_select", data)}
109+
onretry={(data) => gradio.dispatch("retry", data)}
110+
onundo={(data) => gradio.dispatch("undo", data)}
111+
onclear={() => {
112112
gradio.props.value = [];
113113
gradio.dispatch("clear");
114114
}}
115-
on:copy={(e) => gradio.dispatch("copy", e.detail)}
116-
on:edit={(e) => {
115+
oncopy={(data) => gradio.dispatch("copy", data)}
116+
onedit={(data) => {
117117
if (gradio.props.value === null || gradio.props.value.length === 0)
118118
return;
119119
//@ts-ignore
120-
gradio.props.value[e.detail.index].content = [
121-
{ text: e.detail.value, type: "text" }
120+
gradio.props.value[data.index].content = [
121+
{ text: data.value, type: "text" }
122122
];
123-
gradio.dispatch("edit", e.detail);
123+
gradio.dispatch("edit", data);
124124
}}
125125
avatar_images={gradio.props.avatar_images}
126126
sanitize_html={gradio.props.sanitize_html}

‎js/chatbot/shared/ButtonPanel.svelte‎

Lines changed: 53 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,58 @@
1313
import { all_text, is_all_text } from "./utils";
1414
import type { I18nFormatter } from "js/core/src/gradio_helper";
1515
import { uploadToHuggingFace } from "@gradio/utils";
16+
import type { CopyData } from "@gradio/utils";
1617
17-
export let i18n: I18nFormatter;
18-
export let likeable: boolean;
19-
export let feedback_options: string[];
20-
export let show_retry: boolean;
21-
export let show_undo: boolean;
22-
export let show_edit: boolean;
23-
export let in_edit_mode: boolean;
24-
export let show_copy_button: boolean;
25-
export let watermark: string | null = null;
26-
export let message: NormalisedMessage | NormalisedMessage[];
27-
export let position: "right" | "left";
28-
export let avatar: FileData | null;
29-
export let generating: boolean;
30-
export let current_feedback: string | null;
31-
export let file: FileData | null = null;
32-
export let show_download_button = false;
33-
export let show_share_button = false;
18+
let {
19+
i18n,
20+
likeable,
21+
feedback_options,
22+
show_retry,
23+
show_undo,
24+
show_edit,
25+
in_edit_mode,
26+
show_copy_button,
27+
watermark = null,
28+
message,
29+
position,
30+
avatar,
31+
generating,
32+
current_feedback,
33+
file = null,
34+
show_download_button = false,
35+
show_share_button = false,
36+
handle_action,
37+
layout,
38+
oncopy,
39+
onerror,
40+
onshare
41+
}: {
42+
i18n: I18nFormatter;
43+
likeable: boolean;
44+
feedback_options: string[];
45+
show_retry: boolean;
46+
show_undo: boolean;
47+
show_edit: boolean;
48+
in_edit_mode: boolean;
49+
show_copy_button: boolean;
50+
watermark?: string | null;
51+
message: NormalisedMessage | NormalisedMessage[];
52+
position: "right" | "left";
53+
avatar: FileData | null;
54+
generating: boolean;
55+
current_feedback: string | null;
56+
file?: FileData | null;
57+
show_download_button?: boolean;
58+
show_share_button?: boolean;
59+
handle_action: (selected: string | null) => void;
60+
layout: "bubble" | "panel";
61+
oncopy?: (data: CopyData) => void;
62+
onerror?: (message: string) => void;
63+
onshare?: (data: any) => void;
64+
} = $props();
3465
35-
export let handle_action: (selected: string | null) => void;
36-
export let layout: "bubble" | "panel";
37-
export let dispatch: any;
38-
39-
$: message_text = is_all_text(message) ? all_text(message) : "";
40-
$: show_copy = show_copy_button && message && is_all_text(message);
66+
let message_text = $derived(is_all_text(message) ? all_text(message) : "");
67+
let show_copy = $derived(show_copy_button && message && is_all_text(message));
4168
</script>
4269

4370
{#if show_copy || show_retry || show_undo || show_edit || likeable || (show_download_button && file?.url) || (show_share_button && file)}
@@ -61,12 +88,7 @@
6188
/>
6289
{:else}
6390
{#if show_copy}
64-
<Copy
65-
value={message_text}
66-
on:copy={(e) => dispatch("copy", e.detail)}
67-
{watermark}
68-
{i18n}
69-
/>
91+
<Copy value={message_text} {oncopy} {watermark} {i18n} />
7092
{/if}
7193
{#if show_download_button && file?.url}
7294
<DownloadLink
@@ -81,8 +103,8 @@
81103
{#if show_share_button && file}
82104
<ShareButton
83105
{i18n}
84-
on:error={(e) => dispatch("error", e.detail)}
85-
on:share={(e) => dispatch("share", e.detail)}
106+
on:error={(e) => onerror?.(e.detail)}
107+
on:share={(e) => onshare?.(e.detail)}
86108
formatter={async (value) => {
87109
if (!value) return "";
88110
let url = await uploadToHuggingFace(value.url, "url");

0 commit comments

Comments
 (0)