gradio-pr-bot commited on
Commit
5b31fa1
·
verified ·
1 Parent(s): 0836073

Upload folder using huggingface_hub

Browse files
6.26.0/dropdown/Example.svelte ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ let {
3
+ value,
4
+ type,
5
+ selected = false,
6
+ choices
7
+ }: {
8
+ value: string | string[] | null;
9
+ type: "gallery" | "table";
10
+ selected?: boolean;
11
+ choices: [string, string | number][];
12
+ } = $props();
13
+
14
+ let value_array = value ? (Array.isArray(value) ? value : [value]) : [];
15
+ let names = value_array
16
+ .map(
17
+ (val) =>
18
+ (
19
+ choices.find((pair) => pair[1] === val) as
20
+ | [string, string | number]
21
+ | undefined
22
+ )?.[0]
23
+ )
24
+ .filter((name) => name !== undefined);
25
+ let names_string = names.join(", ");
26
+ </script>
27
+
28
+ <div
29
+ class:table={type === "table"}
30
+ class:gallery={type === "gallery"}
31
+ class:selected
32
+ >
33
+ {names_string}
34
+ </div>
35
+
36
+ <style>
37
+ .gallery {
38
+ padding: var(--size-1) var(--size-2);
39
+ }
40
+ </style>
6.26.0/dropdown/Index.svelte ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script module lang="ts">
2
+ export { default as BaseDropdown } from "./shared/Dropdown.svelte";
3
+ export { default as BaseDropdownOptions } from "./shared/DropdownOptions.svelte";
4
+ export { default as BaseMultiselect } from "./shared/Multiselect.svelte";
5
+ export { default as BaseExample } from "./Example.svelte";
6
+ </script>
7
+
8
+ <script lang="ts">
9
+ import { Gradio } from "@gradio/utils";
10
+ import Multiselect from "./shared/Multiselect.svelte";
11
+ import Dropdown from "./shared/Dropdown.svelte";
12
+ import { Block, IconButtonWrapper } from "@gradio/atoms";
13
+ import { StatusTracker } from "@gradio/statustracker";
14
+ import type { DropdownProps, DropdownEvents } from "./types.ts";
15
+
16
+ let props = $props();
17
+ const gradio = new Gradio<DropdownEvents, DropdownProps>(props);
18
+
19
+ // Translate the display side only; values stay raw for event payloads.
20
+ let translated_choices = $derived(
21
+ gradio.props.choices.map(
22
+ ([display, value]) =>
23
+ [gradio.live_i18n(display), value] as [string, string | number]
24
+ )
25
+ );
26
+ </script>
27
+
28
+ <Block
29
+ visible={gradio.shared.visible}
30
+ elem_id={gradio.shared.elem_id}
31
+ elem_classes={gradio.shared.elem_classes}
32
+ padding={gradio.shared.container}
33
+ allow_overflow={false}
34
+ scale={gradio.shared.scale}
35
+ min_width={gradio.shared.min_width}
36
+ >
37
+ <StatusTracker
38
+ autoscroll={gradio.shared.autoscroll}
39
+ i18n={gradio.i18n}
40
+ {...gradio.shared.loading_status}
41
+ on_clear_status={() =>
42
+ gradio.dispatch("clear_status", gradio.shared.loading_status)}
43
+ />
44
+
45
+ {#if gradio.props.multiselect}
46
+ <Multiselect {gradio} />
47
+ {:else}
48
+ <Dropdown
49
+ label={gradio.shared.label}
50
+ info={gradio.props.info}
51
+ bind:value={gradio.props.value as string | number | null}
52
+ choices={translated_choices}
53
+ interactive={gradio.shared.interactive}
54
+ show_label={gradio.shared.show_label}
55
+ container={gradio.shared.container}
56
+ allow_custom_value={gradio.props.allow_custom_value}
57
+ filterable={gradio.props.filterable}
58
+ buttons={gradio.props.buttons}
59
+ oncustom_button_click={(id) => {
60
+ gradio.dispatch("custom_button_click", { id });
61
+ }}
62
+ on_change={() => gradio.dispatch("change")}
63
+ on_input={() => gradio.dispatch("input")}
64
+ on_select={(data) => gradio.dispatch("select", data)}
65
+ on_focus={() => gradio.dispatch("focus")}
66
+ on_blur={() => gradio.dispatch("blur")}
67
+ on_key_up={(data) => gradio.dispatch("key_up", data)}
68
+ />
69
+ {/if}
70
+ </Block>
6.26.0/dropdown/package.json ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "@gradio/dropdown",
3
+ "version": "0.14.2",
4
+ "description": "Gradio UI packages",
5
+ "type": "module",
6
+ "author": "",
7
+ "license": "ISC",
8
+ "private": false,
9
+ "main_changeset": true,
10
+ "exports": {
11
+ ".": {
12
+ "gradio": "./Index.svelte",
13
+ "svelte": "./dist/Index.svelte",
14
+ "types": "./dist/Index.svelte.d.ts"
15
+ },
16
+ "./example": {
17
+ "gradio": "./Example.svelte",
18
+ "svelte": "./dist/Example.svelte",
19
+ "types": "./dist/Example.svelte.d.ts"
20
+ },
21
+ "./package.json": "./package.json"
22
+ },
23
+ "dependencies": {
24
+ "@gradio/atoms": "workspace:^",
25
+ "@gradio/icons": "workspace:^",
26
+ "@gradio/statustracker": "workspace:^",
27
+ "@gradio/utils": "workspace:^"
28
+ },
29
+ "devDependencies": {
30
+ "@gradio/preview": "workspace:^"
31
+ },
32
+ "peerDependencies": {
33
+ "svelte": "^5.48.0"
34
+ },
35
+ "repository": {
36
+ "type": "git",
37
+ "url": "git+https://github.com/gradio-app/gradio.git",
38
+ "directory": "js/dropdown"
39
+ }
40
+ }
6.26.0/dropdown/shared/Dropdown.svelte ADDED
@@ -0,0 +1,356 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import DropdownOptions from "./DropdownOptions.svelte";
3
+ import { BlockTitle, IconButtonWrapper } from "@gradio/atoms";
4
+ import { DropdownArrow } from "@gradio/icons";
5
+ import { handle_filter, handle_shared_keys } from "./utils";
6
+ import {
7
+ type SelectData,
8
+ type KeyUpData,
9
+ type CustomButton as CustomButtonType
10
+ } from "@gradio/utils";
11
+ import { tick } from "svelte";
12
+
13
+ const is_browser = typeof window !== "undefined";
14
+
15
+ let {
16
+ label = "Dropdown",
17
+ info = undefined,
18
+ value = $bindable<string | number | null>(),
19
+ choices = [],
20
+ interactive = true,
21
+ show_label = true,
22
+ container = true,
23
+ allow_custom_value = false,
24
+ filterable = true,
25
+ buttons = null,
26
+ oncustom_button_click = null,
27
+ on_change,
28
+ on_input,
29
+ on_select,
30
+ on_focus,
31
+ on_blur,
32
+ on_key_up
33
+ }: {
34
+ label?: string;
35
+ info?: string;
36
+ value?: string | number | null;
37
+ choices?: [string, string | number][];
38
+ interactive?: boolean;
39
+ show_label?: boolean;
40
+ container?: boolean;
41
+ allow_custom_value?: boolean;
42
+ filterable?: boolean;
43
+ buttons?: (string | CustomButtonType)[] | null;
44
+ oncustom_button_click?: ((id: number) => void) | null;
45
+ on_change?: (value: string | number | null) => void;
46
+ on_input?: () => void;
47
+ on_select?: (data: SelectData) => void;
48
+ on_focus?: () => void;
49
+ on_blur?: () => void;
50
+ on_key_up?: (data: KeyUpData) => void;
51
+ } = $props();
52
+
53
+ let filter_input: HTMLElement;
54
+
55
+ const uid = $props.id();
56
+ const listbox_id = `${uid}-options`;
57
+
58
+ let show_options = $derived.by(() => {
59
+ return is_browser && filter_input === document.activeElement;
60
+ });
61
+ let choices_names = $derived(choices.map((c) => c[0]));
62
+ let choices_values = $derived(choices.map((c) => c[1]));
63
+ let input_text = $state("");
64
+ let selected_index: number | null = $state(null);
65
+ // Use last_typed_value to track when the user has typed
66
+ // on_blur we only want to update value if the user has typed
67
+ let last_typed_value = input_text;
68
+ let focused = $state(false);
69
+
70
+ $effect(() => {
71
+ const current_value = value;
72
+ const values = choices_values;
73
+ const names = choices_names;
74
+ if (focused) {
75
+ return;
76
+ }
77
+ if (
78
+ current_value === undefined ||
79
+ current_value === null ||
80
+ (Array.isArray(current_value) && current_value.length === 0)
81
+ ) {
82
+ input_text = "";
83
+ selected_index = null;
84
+ } else if (values.includes(current_value as string | number)) {
85
+ input_text = names[values.indexOf(current_value as string | number)];
86
+ selected_index = values.indexOf(current_value as string | number);
87
+ } else if (allow_custom_value) {
88
+ input_text = current_value as string;
89
+ selected_index = null;
90
+ } else {
91
+ input_text = "";
92
+ selected_index = null;
93
+ }
94
+ last_typed_value = input_text;
95
+ });
96
+ let initialized = $state(false);
97
+ let disabled = $derived(!interactive);
98
+
99
+ // All of these are indices with respect to the choices array
100
+ let filtered_indices = $state(choices.map((_, i) => i));
101
+ let active_index: number | null = $state(null);
102
+ let selected_indices = $derived(
103
+ selected_index === null ? [] : [selected_index]
104
+ );
105
+
106
+ $effect(() => {
107
+ choices;
108
+ filtered_indices = choices.map((_, i) => i);
109
+ });
110
+
111
+ function handle_option_selected(index: any): void {
112
+ selected_index = parseInt(index);
113
+ if (isNaN(selected_index)) {
114
+ // This is the case when the user clicks on the scrollbar
115
+ selected_index = null;
116
+ return;
117
+ }
118
+
119
+ let [_input_text, _value] = choices[selected_index];
120
+ input_text = _input_text;
121
+ last_typed_value = input_text;
122
+ value = _value;
123
+ on_select?.({
124
+ index: selected_index,
125
+ value: choices_values[selected_index],
126
+ selected: true
127
+ });
128
+ show_options = false;
129
+ active_index = null;
130
+ on_input?.();
131
+ filter_input.blur();
132
+ }
133
+
134
+ function handle_focus(e: FocusEvent): void {
135
+ focused = true;
136
+ filtered_indices = choices.map((_, i) => i);
137
+ active_index = selected_index;
138
+ show_options = true;
139
+ on_focus?.();
140
+ }
141
+
142
+ function handle_blur(): void {
143
+ if (!allow_custom_value) {
144
+ input_text =
145
+ choices_names[choices_values.indexOf(value as string | number)];
146
+ } else if (input_text !== last_typed_value) {
147
+ if (choices_names.includes(input_text)) {
148
+ selected_index = choices_names.indexOf(input_text);
149
+ value = choices_values[selected_index];
150
+ } else {
151
+ value = input_text;
152
+ selected_index = null;
153
+ }
154
+ }
155
+ show_options = false;
156
+ active_index = null;
157
+ filtered_indices = choices.map((_, i) => i);
158
+ focused = false;
159
+ on_blur?.();
160
+ on_input?.();
161
+ }
162
+
163
+ async function handle_key_down(e: KeyboardEvent): Promise<void> {
164
+ await tick();
165
+ const is_navigation_key =
166
+ e.key === "ArrowUp" ||
167
+ e.key === "ArrowDown" ||
168
+ e.key === "Enter" ||
169
+ e.key === "Escape";
170
+ const is_filtering = input_text !== last_typed_value;
171
+ if (!is_navigation_key) {
172
+ filtered_indices = handle_filter(choices, input_text);
173
+ active_index = filtered_indices.length > 0 ? filtered_indices[0] : null;
174
+ } else {
175
+ // Recompute the visible options so navigation always spans the full
176
+ // current list. Filter by the typed text when the user is filtering,
177
+ // otherwise (a value is just displayed/selected) show every option.
178
+ filtered_indices = is_filtering
179
+ ? handle_filter(choices, input_text)
180
+ : choices.map((_, i) => i);
181
+ if (active_index !== null && !filtered_indices.includes(active_index)) {
182
+ active_index = filtered_indices.length > 0 ? filtered_indices[0] : null;
183
+ }
184
+ }
185
+ [show_options, active_index] = handle_shared_keys(
186
+ e,
187
+ active_index,
188
+ filtered_indices
189
+ );
190
+ if (e.key === "Enter") {
191
+ last_typed_value = input_text;
192
+ if (active_index !== null) {
193
+ selected_index = active_index;
194
+ value = choices_values[active_index];
195
+ show_options = false;
196
+ filter_input.blur();
197
+ active_index = null;
198
+ } else if (choices_names.includes(input_text)) {
199
+ selected_index = choices_names.indexOf(input_text);
200
+ value = choices_values[selected_index];
201
+ show_options = false;
202
+ active_index = null;
203
+ filter_input.blur();
204
+ } else if (allow_custom_value) {
205
+ value = input_text;
206
+ selected_index = null;
207
+ show_options = false;
208
+ active_index = null;
209
+ filter_input.blur();
210
+ }
211
+ }
212
+ }
213
+
214
+ let old_value = $state(value);
215
+ $effect(() => {
216
+ if (old_value !== value) {
217
+ old_value = value;
218
+ on_change?.(value);
219
+ }
220
+ });
221
+ </script>
222
+
223
+ <div class:container>
224
+ {#if show_label && buttons && buttons.length > 0}
225
+ <IconButtonWrapper
226
+ {buttons}
227
+ on_custom_button_click={oncustom_button_click}
228
+ />
229
+ {/if}
230
+ <BlockTitle {show_label} {info}>{label}</BlockTitle>
231
+
232
+ <div class="wrap">
233
+ <div class="wrap-inner" class:show_options>
234
+ <div class="secondary-wrap">
235
+ <input
236
+ role="combobox"
237
+ aria-controls={listbox_id}
238
+ aria-expanded={show_options}
239
+ aria-activedescendant={show_options && active_index !== null
240
+ ? `${listbox_id}-option-${active_index}`
241
+ : undefined}
242
+ aria-autocomplete={filterable ? "list" : "none"}
243
+ aria-label={label}
244
+ class="border-none"
245
+ class:subdued={!choices_names.includes(input_text) &&
246
+ !allow_custom_value}
247
+ autocomplete="off"
248
+ {disabled}
249
+ bind:value={input_text}
250
+ bind:this={filter_input}
251
+ onkeydown={handle_key_down}
252
+ onkeyup={(e) => {
253
+ on_key_up?.({
254
+ key: e.key,
255
+ input_value: input_text
256
+ });
257
+ }}
258
+ onblur={handle_blur}
259
+ onfocus={handle_focus}
260
+ readonly={!filterable}
261
+ />
262
+ {#if !disabled}
263
+ <div class="icon-wrap">
264
+ <DropdownArrow />
265
+ </div>
266
+ {/if}
267
+ </div>
268
+ </div>
269
+ <DropdownOptions
270
+ {show_options}
271
+ {choices}
272
+ {filtered_indices}
273
+ {disabled}
274
+ {selected_indices}
275
+ {active_index}
276
+ {listbox_id}
277
+ onchange={handle_option_selected}
278
+ onload={() => (initialized = true)}
279
+ />
280
+ </div>
281
+ </div>
282
+
283
+ <style>
284
+ .icon-wrap {
285
+ position: absolute;
286
+ top: 50%;
287
+ transform: translateY(-50%);
288
+ right: var(--size-5);
289
+ color: var(--body-text-color);
290
+ width: var(--size-5);
291
+ pointer-events: none;
292
+ }
293
+ .container {
294
+ height: 100%;
295
+ }
296
+ .container .wrap {
297
+ box-shadow: var(--input-shadow);
298
+ border: var(--input-border-width) solid var(--border-color-primary);
299
+ }
300
+
301
+ .wrap {
302
+ position: relative;
303
+ border-radius: var(--input-radius);
304
+ background: var(--input-background-fill);
305
+ }
306
+
307
+ .wrap:focus-within {
308
+ box-shadow: var(--input-shadow-focus);
309
+ border-color: var(--input-border-color-focus);
310
+ background: var(--input-background-fill-focus);
311
+ }
312
+
313
+ .wrap-inner {
314
+ display: flex;
315
+ position: relative;
316
+ flex-wrap: wrap;
317
+ align-items: center;
318
+ gap: var(--checkbox-label-gap);
319
+ padding: var(--checkbox-label-padding);
320
+ height: 100%;
321
+ }
322
+ .secondary-wrap {
323
+ display: flex;
324
+ flex: 1 1 0%;
325
+ align-items: center;
326
+ border: none;
327
+ min-width: min-content;
328
+ height: 100%;
329
+ }
330
+
331
+ input {
332
+ margin: var(--spacing-sm);
333
+ outline: none;
334
+ border: none;
335
+ background: inherit;
336
+ width: var(--size-full);
337
+ color: var(--body-text-color);
338
+ font-size: var(--input-text-size);
339
+ height: 100%;
340
+ }
341
+
342
+ input:disabled {
343
+ -webkit-text-fill-color: var(--body-text-color);
344
+ -webkit-opacity: 1;
345
+ opacity: 1;
346
+ cursor: not-allowed;
347
+ }
348
+
349
+ .subdued {
350
+ color: var(--body-text-color-subdued);
351
+ }
352
+
353
+ input[readonly] {
354
+ cursor: pointer;
355
+ }
356
+ </style>
6.26.0/dropdown/shared/DropdownOptions.svelte ADDED
@@ -0,0 +1,186 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import { fly } from "svelte/transition";
3
+ let {
4
+ choices,
5
+ filtered_indices,
6
+ show_options = false,
7
+ disabled = false,
8
+ selected_indices = [],
9
+ active_index = null,
10
+ remember_scroll = false,
11
+ offset_from_top = 0,
12
+ from_top = false,
13
+ listbox_id = undefined,
14
+ onchange,
15
+ onload
16
+ }: {
17
+ choices: [string, string | number][];
18
+ filtered_indices: number[];
19
+ show_options: boolean;
20
+ disabled?: boolean;
21
+ selected_indices?: (string | number)[];
22
+ active_index: number | null;
23
+ remember_scroll?: boolean;
24
+ offset_from_top?: number;
25
+ from_top?: boolean;
26
+ listbox_id?: string;
27
+ onchange?: (index: any) => void;
28
+ onload?: () => void;
29
+ } = $props();
30
+
31
+ let distance_from_top = $state(0);
32
+ let distance_from_bottom = $state(0);
33
+ let input_height = $state(0);
34
+ let input_width = $state(0);
35
+ let refElement: HTMLDivElement;
36
+ let listElement: HTMLUListElement;
37
+ let top: string | null = $state(null);
38
+ let bottom: string | null = $state(null);
39
+ let max_height: number = $state(0);
40
+ let innerHeight = $state(0);
41
+ let list_scroll_y = 0;
42
+
43
+ function calculate_window_distance(): void {
44
+ const { top: ref_top, bottom: ref_bottom } =
45
+ refElement.getBoundingClientRect();
46
+ if (from_top) {
47
+ distance_from_top = offset_from_top;
48
+ } else {
49
+ distance_from_top = ref_top;
50
+ }
51
+ distance_from_bottom = innerHeight - ref_bottom;
52
+ }
53
+
54
+ let scroll_timeout: NodeJS.Timeout | null = null;
55
+ function scroll_listener(): void {
56
+ if (!show_options) return;
57
+ if (scroll_timeout !== null) {
58
+ clearTimeout(scroll_timeout);
59
+ }
60
+
61
+ scroll_timeout = setTimeout(() => {
62
+ calculate_window_distance();
63
+ scroll_timeout = null;
64
+ }, 10);
65
+ }
66
+
67
+ function restore_last_scroll(): void {
68
+ listElement?.scrollTo?.(0, list_scroll_y);
69
+ }
70
+
71
+ $effect(() => {
72
+ if (show_options && refElement) {
73
+ if (remember_scroll) {
74
+ restore_last_scroll();
75
+ } else {
76
+ if (listElement && selected_indices.length > 0) {
77
+ let elements = listElement.querySelectorAll("li");
78
+ for (const element of Array.from(elements)) {
79
+ if (
80
+ element.getAttribute("data-index") ===
81
+ selected_indices[0].toString()
82
+ ) {
83
+ listElement?.scrollTo?.(0, (element as HTMLLIElement).offsetTop);
84
+ break;
85
+ }
86
+ }
87
+ }
88
+ }
89
+ calculate_window_distance();
90
+ const rect = refElement.parentElement?.getBoundingClientRect();
91
+ input_height = rect?.height || 0;
92
+ input_width = rect?.width || 0;
93
+ onload?.();
94
+ }
95
+ if (distance_from_bottom > distance_from_top || from_top) {
96
+ top = `${distance_from_top}px`;
97
+ max_height = distance_from_bottom;
98
+ bottom = null;
99
+ } else {
100
+ bottom = `${distance_from_bottom + input_height}px`;
101
+ max_height = distance_from_top - input_height;
102
+ top = null;
103
+ }
104
+ });
105
+ </script>
106
+
107
+ <svelte:window onscroll={scroll_listener} bind:innerHeight />
108
+
109
+ <div class="reference" bind:this={refElement} />
110
+ {#if show_options && !disabled}
111
+ <ul
112
+ class="options"
113
+ transition:fly={{ duration: 200, y: 5 }}
114
+ onmousedown={(e) => {
115
+ e.preventDefault();
116
+ onchange?.((e.target as HTMLElement).dataset.index);
117
+ }}
118
+ onscroll={(e) => (list_scroll_y = e.currentTarget.scrollTop)}
119
+ style:top
120
+ style:bottom
121
+ style:max-height={`calc(${max_height}px - var(--window-padding))`}
122
+ style:width={input_width + "px"}
123
+ bind:this={listElement}
124
+ id={listbox_id}
125
+ role="listbox"
126
+ >
127
+ {#each filtered_indices as index}
128
+ <li
129
+ class="item"
130
+ class:selected={selected_indices.includes(index)}
131
+ class:active={index === active_index}
132
+ class:bg-gray-100={index === active_index}
133
+ class:dark:bg-gray-600={index === active_index}
134
+ style:width={input_width + "px"}
135
+ data-index={index}
136
+ id={listbox_id ? `${listbox_id}-option-${index}` : undefined}
137
+ aria-label={choices[index][0]}
138
+ data-testid="dropdown-option"
139
+ role="option"
140
+ aria-selected={selected_indices.includes(index)}
141
+ >
142
+ <span class:hide={!selected_indices.includes(index)} class="inner-item">
143
+ ✓
144
+ </span>
145
+ {choices[index][0]}
146
+ </li>
147
+ {/each}
148
+ </ul>
149
+ {/if}
150
+
151
+ <style>
152
+ .options {
153
+ --window-padding: var(--size-8);
154
+ position: fixed;
155
+ z-index: var(--layer-top);
156
+ margin-left: 0;
157
+ box-shadow: var(--shadow-drop-lg);
158
+ border-radius: var(--container-radius);
159
+ background: var(--background-fill-primary);
160
+ min-width: fit-content;
161
+ max-width: inherit;
162
+ overflow: auto;
163
+ color: var(--body-text-color);
164
+ list-style: none;
165
+ }
166
+
167
+ .item {
168
+ display: flex;
169
+ cursor: pointer;
170
+ padding: var(--size-2);
171
+ word-break: break-word;
172
+ }
173
+
174
+ .item:hover,
175
+ .active {
176
+ background: var(--background-fill-secondary);
177
+ }
178
+
179
+ .inner-item {
180
+ padding-right: var(--size-1);
181
+ }
182
+
183
+ .hide {
184
+ visibility: hidden;
185
+ }
186
+ </style>
6.26.0/dropdown/shared/Multiselect.svelte ADDED
@@ -0,0 +1,418 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import { _ } from "svelte-i18n";
3
+ import { BlockTitle, IconButtonWrapper } from "@gradio/atoms";
4
+ import { Remove, DropdownArrow } from "@gradio/icons";
5
+ import type { Gradio } from "@gradio/utils";
6
+ import DropdownOptions from "./DropdownOptions.svelte";
7
+ import { handle_filter, handle_shared_keys } from "./utils";
8
+ import type { DropdownEvents, DropdownProps, Item } from "../types.ts";
9
+
10
+ const props = $props();
11
+
12
+ const gradio: Gradio<DropdownEvents, DropdownProps> = props.gradio;
13
+
14
+ let filter_input: HTMLElement;
15
+ let input_text = $state("");
16
+ let label = $derived(gradio.shared.label || "Multiselect");
17
+
18
+ const uid = $props.id();
19
+ const listbox_id = `${uid}-options`;
20
+ let buttons = $derived(gradio.props.buttons);
21
+
22
+ // Translate the display side only; values stay raw for event payloads.
23
+ let translated_choices: [string, string | number][] = $derived.by(() => {
24
+ return gradio.props.choices.map(
25
+ ([display, value]) =>
26
+ [gradio.live_i18n(display), value] as [string, string | number]
27
+ );
28
+ });
29
+ let choices_names: string[] = $derived.by(() => {
30
+ return translated_choices.map((c) => c[0]);
31
+ });
32
+ let choices_values: (string | number)[] = $derived.by(() => {
33
+ return gradio.props.choices.map((c) => c[1]);
34
+ });
35
+
36
+ let disabled = $derived(!gradio.shared.interactive);
37
+
38
+ let show_options = $state(false);
39
+
40
+ // All of these are indices with respect to the choices array
41
+ let [filtered_indices, active_index] = $derived.by(() => {
42
+ const filtered = handle_filter(translated_choices, input_text);
43
+ return [
44
+ filtered,
45
+ filtered.length > 0 && !gradio.props.allow_custom_value
46
+ ? filtered[0]
47
+ : null
48
+ ];
49
+ });
50
+
51
+ function set_selected_indices(): Item[] {
52
+ if (gradio.props.value === undefined) {
53
+ return [];
54
+ } else if (Array.isArray(gradio.props.value)) {
55
+ return gradio.props.value
56
+ .map((v) => {
57
+ const index = choices_values.indexOf(v);
58
+ if (index !== -1) {
59
+ return index;
60
+ }
61
+ if (gradio.props.allow_custom_value) {
62
+ return v;
63
+ }
64
+ // Instead of returning null, skip this iteration
65
+ return undefined;
66
+ })
67
+ .filter((val): val is string | number => val !== undefined);
68
+ }
69
+ return [];
70
+ }
71
+
72
+ let selected_indices: (number | string)[] = $derived.by(set_selected_indices);
73
+
74
+ function handle_blur(): void {
75
+ if (!gradio.props.allow_custom_value) {
76
+ input_text = "";
77
+ }
78
+
79
+ if (gradio.props.allow_custom_value && input_text !== "") {
80
+ add_selected_choice(input_text);
81
+ input_text = "";
82
+ }
83
+ gradio.dispatch("blur");
84
+ show_options = false;
85
+ active_index = null;
86
+ }
87
+
88
+ function remove_selected_choice(option_index: number | string) {
89
+ selected_indices = selected_indices.filter((v) => v !== option_index);
90
+ gradio.props.value = selected_indices.map((index) =>
91
+ typeof index === "number" ? choices_values[index] : index
92
+ );
93
+ gradio.dispatch("input");
94
+ gradio.dispatch("select", {
95
+ index: typeof option_index === "number" ? option_index : -1,
96
+ value:
97
+ typeof option_index === "number"
98
+ ? choices_values[option_index]
99
+ : option_index,
100
+ selected: false
101
+ });
102
+ }
103
+
104
+ function add_selected_choice(option_index: number | string) {
105
+ if (
106
+ gradio.props.max_choices == null ||
107
+ selected_indices.length < gradio.props.max_choices
108
+ ) {
109
+ selected_indices.push(option_index);
110
+ gradio.dispatch("select", {
111
+ index: typeof option_index === "number" ? option_index : -1,
112
+ value:
113
+ typeof option_index === "number"
114
+ ? choices_values[option_index]
115
+ : option_index,
116
+ selected: true
117
+ });
118
+ }
119
+ if (selected_indices.length === gradio.props.max_choices) {
120
+ show_options = false;
121
+ active_index = null;
122
+ filter_input.blur();
123
+ }
124
+ gradio.props.value = selected_indices.map((index) =>
125
+ typeof index === "number" ? choices_values[index] : index
126
+ );
127
+ }
128
+
129
+ function handle_option_selected(index: any): void {
130
+ const option_index = parseInt(index);
131
+ add_or_remove_index(option_index);
132
+ }
133
+
134
+ function add_or_remove_index(option_index: number): void {
135
+ if (selected_indices.includes(option_index)) {
136
+ remove_selected_choice(option_index);
137
+ } else {
138
+ add_selected_choice(option_index);
139
+ }
140
+ input_text = "";
141
+ active_index = null;
142
+ gradio.dispatch("input");
143
+ }
144
+
145
+ function remove_all(e: any): void {
146
+ selected_indices = [];
147
+ input_text = "";
148
+ gradio.props.value = [];
149
+ e.preventDefault();
150
+ }
151
+
152
+ function handle_focus(e: FocusEvent): void {
153
+ filtered_indices = gradio.props.choices.map((_, i) => i);
154
+ if (
155
+ gradio.props.max_choices === null ||
156
+ selected_indices.length < gradio.props.max_choices
157
+ ) {
158
+ show_options = true;
159
+ }
160
+ gradio.dispatch("focus");
161
+ show_options = true;
162
+ }
163
+
164
+ function handle_key_down(e: KeyboardEvent): void {
165
+ [show_options, active_index] = handle_shared_keys(
166
+ e,
167
+ active_index,
168
+ filtered_indices
169
+ );
170
+ if (e.key === "Enter") {
171
+ if (active_index !== null) {
172
+ add_or_remove_index(active_index);
173
+ } else {
174
+ if (gradio.props.allow_custom_value) {
175
+ add_selected_choice(input_text);
176
+ input_text = "";
177
+ }
178
+ }
179
+ }
180
+ if (e.key === "Backspace" && input_text === "") {
181
+ const new_indices = selected_indices.slice(0, -1);
182
+ gradio.props.value = new_indices.map((index) =>
183
+ typeof index === "number" ? choices_values[index] : index
184
+ );
185
+ }
186
+ if (selected_indices.length === gradio.props.max_choices) {
187
+ show_options = false;
188
+ active_index = null;
189
+ }
190
+ }
191
+
192
+ let old_value = $state(gradio.props.value);
193
+
194
+ $effect(() => {
195
+ if (old_value !== gradio.props.value) {
196
+ old_value = gradio.props.value;
197
+ gradio.dispatch("change");
198
+ }
199
+ });
200
+
201
+ function oncustom_button_click(id: number) {
202
+ gradio.dispatch("custom_button_click", { id });
203
+ }
204
+ </script>
205
+
206
+ <div class:container={gradio.shared.container}>
207
+ {#if gradio.shared.show_label && buttons && buttons.length > 0}
208
+ <IconButtonWrapper
209
+ {buttons}
210
+ on_custom_button_click={oncustom_button_click}
211
+ />
212
+ {/if}
213
+ <BlockTitle show_label={gradio.shared.show_label} info={gradio.props.info}
214
+ >{label}</BlockTitle
215
+ >
216
+
217
+ <div class="wrap">
218
+ <div class="wrap-inner" class:show_options>
219
+ {#each selected_indices as s}
220
+ <div class="token">
221
+ <span>
222
+ {#if typeof s === "number"}
223
+ {choices_names[s]}
224
+ {:else}
225
+ {s}
226
+ {/if}
227
+ </span>
228
+ {#if !disabled}
229
+ <div
230
+ class="token-remove"
231
+ onclick={(event) => {
232
+ event.preventDefault();
233
+ remove_selected_choice(s);
234
+ }}
235
+ onkeydown={(event) => {
236
+ if (event.key === "Enter") {
237
+ remove_selected_choice(s);
238
+ }
239
+ }}
240
+ role="button"
241
+ tabindex="0"
242
+ title={gradio.i18n("common.remove") + " " + s}
243
+ >
244
+ <Remove />
245
+ </div>
246
+ {/if}
247
+ </div>
248
+ {/each}
249
+ <div class="secondary-wrap">
250
+ <input
251
+ role="combobox"
252
+ aria-controls={listbox_id}
253
+ aria-expanded={show_options}
254
+ aria-activedescendant={show_options && active_index !== null
255
+ ? `${listbox_id}-option-${active_index}`
256
+ : undefined}
257
+ aria-autocomplete={gradio.props.filterable ? "list" : "none"}
258
+ aria-label={label}
259
+ class="border-none"
260
+ class:subdued={(!choices_names.includes(input_text) &&
261
+ !gradio.props.allow_custom_value) ||
262
+ selected_indices.length === gradio.props.max_choices}
263
+ {disabled}
264
+ autocomplete="off"
265
+ bind:value={input_text}
266
+ bind:this={filter_input}
267
+ onkeydown={handle_key_down}
268
+ onkeyup={(e) => {
269
+ gradio.dispatch("key_up", {
270
+ key: e.key,
271
+ input_value: input_text
272
+ });
273
+ }}
274
+ onblur={handle_blur}
275
+ onfocus={handle_focus}
276
+ readonly={!gradio.props.filterable}
277
+ />
278
+
279
+ {#if !disabled}
280
+ {#if selected_indices.length > 0}
281
+ <div
282
+ role="button"
283
+ tabindex="0"
284
+ class="token-remove remove-all"
285
+ title={gradio.i18n("common.clear")}
286
+ onclick={remove_all}
287
+ onkeydown={(event) => {
288
+ if (event.key === "Enter") {
289
+ remove_all(event);
290
+ }
291
+ }}
292
+ >
293
+ <Remove />
294
+ </div>
295
+ {/if}
296
+ <span class="icon-wrap"> <DropdownArrow /></span>
297
+ {/if}
298
+ </div>
299
+ </div>
300
+ <DropdownOptions
301
+ {show_options}
302
+ choices={translated_choices}
303
+ {filtered_indices}
304
+ {disabled}
305
+ {selected_indices}
306
+ {active_index}
307
+ {listbox_id}
308
+ remember_scroll={true}
309
+ onchange={handle_option_selected}
310
+ />
311
+ </div>
312
+ </div>
313
+
314
+ <style>
315
+ .icon-wrap {
316
+ color: var(--body-text-color);
317
+ margin-right: var(--size-2);
318
+ width: var(--size-5);
319
+ }
320
+ .container .wrap {
321
+ box-shadow: var(--input-shadow);
322
+ border: var(--input-border-width) solid var(--border-color-primary);
323
+ }
324
+
325
+ .wrap {
326
+ position: relative;
327
+ border-radius: var(--input-radius);
328
+ background: var(--input-background-fill);
329
+ }
330
+
331
+ .wrap:focus-within {
332
+ box-shadow: var(--input-shadow-focus);
333
+ border-color: var(--input-border-color-focus);
334
+ }
335
+
336
+ .wrap-inner {
337
+ display: flex;
338
+ position: relative;
339
+ flex-wrap: wrap;
340
+ align-items: center;
341
+ gap: var(--checkbox-label-gap);
342
+ padding: var(--checkbox-label-padding);
343
+ }
344
+
345
+ .token {
346
+ display: flex;
347
+ align-items: center;
348
+ transition: var(--button-transition);
349
+ cursor: pointer;
350
+ box-shadow: var(--checkbox-label-shadow);
351
+ border: var(--checkbox-label-border-width) solid
352
+ var(--checkbox-label-border-color);
353
+ border-radius: var(--button-small-radius);
354
+ background: var(--checkbox-label-background-fill);
355
+ padding: var(--checkbox-label-padding);
356
+ color: var(--checkbox-label-text-color);
357
+ font-weight: var(--checkbox-label-text-weight);
358
+ font-size: var(--checkbox-label-text-size);
359
+ line-height: var(--line-md);
360
+ word-break: break-word;
361
+ }
362
+
363
+ .token > * + * {
364
+ margin-left: var(--size-2);
365
+ }
366
+
367
+ .token-remove {
368
+ fill: var(--body-text-color);
369
+ display: flex;
370
+ justify-content: center;
371
+ align-items: center;
372
+ cursor: pointer;
373
+ border: var(--checkbox-border-width) solid var(--border-color-primary);
374
+ border-radius: var(--radius-full);
375
+ background: var(--background-fill-primary);
376
+ padding: var(--size-0-5);
377
+ width: 16px;
378
+ height: 16px;
379
+ flex-shrink: 0;
380
+ }
381
+
382
+ .secondary-wrap {
383
+ display: flex;
384
+ flex: 1 1 0%;
385
+ align-items: center;
386
+ border: none;
387
+ min-width: min-content;
388
+ }
389
+
390
+ input {
391
+ margin: var(--spacing-sm);
392
+ outline: none;
393
+ border: none;
394
+ background: inherit;
395
+ width: var(--size-full);
396
+ color: var(--body-text-color);
397
+ font-size: var(--input-text-size);
398
+ }
399
+
400
+ input:disabled {
401
+ -webkit-text-fill-color: var(--body-text-color);
402
+ -webkit-opacity: 1;
403
+ opacity: 1;
404
+ cursor: not-allowed;
405
+ }
406
+
407
+ .remove-all {
408
+ margin-left: var(--size-1);
409
+ width: 20px;
410
+ height: 20px;
411
+ }
412
+ .subdued {
413
+ color: var(--body-text-color-subdued);
414
+ }
415
+ input[readonly] {
416
+ cursor: pointer;
417
+ }
418
+ </style>
6.26.0/dropdown/shared/utils.ts ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ function positive_mod(n: number, m: number): number {
2
+ return ((n % m) + m) % m;
3
+ }
4
+
5
+ export function handle_filter(
6
+ choices: [string, string | number][],
7
+ input_text: string
8
+ ): number[] {
9
+ return choices.reduce((filtered_indices, o, index) => {
10
+ if (
11
+ input_text ? o[0].toLowerCase().includes(input_text.toLowerCase()) : true
12
+ ) {
13
+ filtered_indices.push(index);
14
+ }
15
+ return filtered_indices;
16
+ }, [] as number[]);
17
+ }
18
+
19
+ export function handle_change(
20
+ dispatch: any,
21
+ value: string | number | (string | number)[] | undefined,
22
+ value_is_output: boolean
23
+ ): void {
24
+ dispatch("change", value);
25
+ if (!value_is_output) {
26
+ dispatch("input");
27
+ }
28
+ }
29
+
30
+ export function handle_shared_keys(
31
+ e: KeyboardEvent,
32
+ active_index: number | null,
33
+ filtered_indices: number[]
34
+ ): [boolean, number | null] {
35
+ if (e.key === "Escape") {
36
+ return [false, active_index];
37
+ }
38
+ if (e.key === "ArrowDown" || e.key === "ArrowUp") {
39
+ if (filtered_indices.length > 0) {
40
+ if (active_index === null) {
41
+ active_index =
42
+ e.key === "ArrowDown"
43
+ ? filtered_indices[0]
44
+ : filtered_indices[filtered_indices.length - 1];
45
+ } else {
46
+ const index_in_filtered = filtered_indices.indexOf(active_index);
47
+ const increment = e.key === "ArrowUp" ? -1 : 1;
48
+ active_index =
49
+ filtered_indices[
50
+ positive_mod(index_in_filtered + increment, filtered_indices.length)
51
+ ];
52
+ }
53
+ }
54
+ }
55
+ return [true, active_index];
56
+ }
6.26.0/dropdown/types.ts ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import type { ILoadingStatus as LoadingStatus } from "@gradio/statustracker";
2
+ import type { KeyUpData, SelectData, CustomButton } from "@gradio/utils";
3
+
4
+ export type Item = string | number;
5
+
6
+ export interface DropdownProps {
7
+ multiselect: boolean;
8
+ max_choices: number | null;
9
+ choices: [string, Item][];
10
+ allow_custom_value: boolean;
11
+ value: Item | Item[];
12
+ info: string;
13
+ filterable: boolean;
14
+ buttons: (string | CustomButton)[] | null;
15
+ }
16
+
17
+ export interface DropdownEvents {
18
+ change: never;
19
+ input: never;
20
+ select: SelectData;
21
+ focus: never;
22
+ blur: never;
23
+ key_up: KeyUpData;
24
+ clear_status: LoadingStatus;
25
+ custom_button_click: { id: number };
26
+ }