gradio-pr-bot commited on
Commit
349b2be
·
verified ·
1 Parent(s): 56647a8

Upload folder using huggingface_hub

Browse files
6.17.1/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.17.1/dropdown/Index.svelte ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script context="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
+ </script>
19
+
20
+ <Block
21
+ visible={gradio.shared.visible}
22
+ elem_id={gradio.shared.elem_id}
23
+ elem_classes={gradio.shared.elem_classes}
24
+ padding={gradio.shared.container}
25
+ allow_overflow={false}
26
+ scale={gradio.shared.scale}
27
+ min_width={gradio.shared.min_width}
28
+ >
29
+ <StatusTracker
30
+ autoscroll={gradio.shared.autoscroll}
31
+ i18n={gradio.i18n}
32
+ {...gradio.shared.loading_status}
33
+ on_clear_status={() => gradio.dispatch("clear_status", loading_status)}
34
+ />
35
+
36
+ {#if gradio.props.multiselect}
37
+ <Multiselect {gradio} />
38
+ {:else}
39
+ <Dropdown
40
+ label={gradio.shared.label}
41
+ info={gradio.props.info}
42
+ bind:value={gradio.props.value}
43
+ choices={gradio.props.choices}
44
+ interactive={gradio.shared.interactive}
45
+ show_label={gradio.shared.show_label}
46
+ container={gradio.shared.container}
47
+ allow_custom_value={gradio.props.allow_custom_value}
48
+ filterable={gradio.props.filterable}
49
+ buttons={gradio.props.buttons}
50
+ oncustom_button_click={(id) => {
51
+ gradio.dispatch("custom_button_click", { id });
52
+ }}
53
+ on_change={() => gradio.dispatch("change")}
54
+ on_input={() => gradio.dispatch("input")}
55
+ on_select={(data) => gradio.dispatch("select", data)}
56
+ on_focus={() => gradio.dispatch("focus")}
57
+ on_blur={() => gradio.dispatch("blur")}
58
+ on_key_up={(data) => gradio.dispatch("key_up", data)}
59
+ />
60
+ {/if}
61
+ </Block>
6.17.1/dropdown/package.json ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "@gradio/dropdown",
3
+ "version": "0.12.1",
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.17.1/dropdown/shared/Dropdown.svelte ADDED
@@ -0,0 +1,352 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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="listbox"
237
+ aria-controls={listbox_id}
238
+ aria-expanded={show_options}
239
+ aria-label={label}
240
+ class="border-none"
241
+ class:subdued={!choices_names.includes(input_text) &&
242
+ !allow_custom_value}
243
+ autocomplete="off"
244
+ {disabled}
245
+ bind:value={input_text}
246
+ bind:this={filter_input}
247
+ onkeydown={handle_key_down}
248
+ onkeyup={(e) => {
249
+ on_key_up?.({
250
+ key: e.key,
251
+ input_value: input_text
252
+ });
253
+ }}
254
+ onblur={handle_blur}
255
+ onfocus={handle_focus}
256
+ readonly={!filterable}
257
+ />
258
+ {#if !disabled}
259
+ <div class="icon-wrap">
260
+ <DropdownArrow />
261
+ </div>
262
+ {/if}
263
+ </div>
264
+ </div>
265
+ <DropdownOptions
266
+ {show_options}
267
+ {choices}
268
+ {filtered_indices}
269
+ {disabled}
270
+ {selected_indices}
271
+ {active_index}
272
+ {listbox_id}
273
+ onchange={handle_option_selected}
274
+ onload={() => (initialized = true)}
275
+ />
276
+ </div>
277
+ </div>
278
+
279
+ <style>
280
+ .icon-wrap {
281
+ position: absolute;
282
+ top: 50%;
283
+ transform: translateY(-50%);
284
+ right: var(--size-5);
285
+ color: var(--body-text-color);
286
+ width: var(--size-5);
287
+ pointer-events: none;
288
+ }
289
+ .container {
290
+ height: 100%;
291
+ }
292
+ .container .wrap {
293
+ box-shadow: var(--input-shadow);
294
+ border: var(--input-border-width) solid var(--border-color-primary);
295
+ }
296
+
297
+ .wrap {
298
+ position: relative;
299
+ border-radius: var(--input-radius);
300
+ background: var(--input-background-fill);
301
+ }
302
+
303
+ .wrap:focus-within {
304
+ box-shadow: var(--input-shadow-focus);
305
+ border-color: var(--input-border-color-focus);
306
+ background: var(--input-background-fill-focus);
307
+ }
308
+
309
+ .wrap-inner {
310
+ display: flex;
311
+ position: relative;
312
+ flex-wrap: wrap;
313
+ align-items: center;
314
+ gap: var(--checkbox-label-gap);
315
+ padding: var(--checkbox-label-padding);
316
+ height: 100%;
317
+ }
318
+ .secondary-wrap {
319
+ display: flex;
320
+ flex: 1 1 0%;
321
+ align-items: center;
322
+ border: none;
323
+ min-width: min-content;
324
+ height: 100%;
325
+ }
326
+
327
+ input {
328
+ margin: var(--spacing-sm);
329
+ outline: none;
330
+ border: none;
331
+ background: inherit;
332
+ width: var(--size-full);
333
+ color: var(--body-text-color);
334
+ font-size: var(--input-text-size);
335
+ height: 100%;
336
+ }
337
+
338
+ input:disabled {
339
+ -webkit-text-fill-color: var(--body-text-color);
340
+ -webkit-opacity: 1;
341
+ opacity: 1;
342
+ cursor: not-allowed;
343
+ }
344
+
345
+ .subdued {
346
+ color: var(--body-text-color-subdued);
347
+ }
348
+
349
+ input[readonly] {
350
+ cursor: pointer;
351
+ }
352
+ </style>
6.17.1/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 on:scroll={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.17.1/dropdown/shared/Multiselect.svelte ADDED
@@ -0,0 +1,396 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ let buttons = $derived(gradio.props.buttons);
18
+
19
+ let choices_names: string[] = $derived.by(() => {
20
+ return gradio.props.choices.map((c) => c[0]);
21
+ });
22
+ let choices_values: (string | number)[] = $derived.by(() => {
23
+ return gradio.props.choices.map((c) => c[1]);
24
+ });
25
+
26
+ let disabled = $derived(!gradio.shared.interactive);
27
+
28
+ let show_options = $state(false);
29
+
30
+ // All of these are indices with respect to the choices array
31
+ let [filtered_indices, active_index] = $derived.by(() => {
32
+ const filtered = handle_filter(gradio.props.choices, input_text);
33
+ return [
34
+ filtered,
35
+ filtered.length > 0 && !gradio.props.allow_custom_value
36
+ ? filtered[0]
37
+ : null
38
+ ];
39
+ });
40
+
41
+ function set_selected_indices(): Item[] {
42
+ if (gradio.props.value === undefined) {
43
+ return [];
44
+ } else if (Array.isArray(gradio.props.value)) {
45
+ return gradio.props.value
46
+ .map((v) => {
47
+ const index = choices_values.indexOf(v);
48
+ if (index !== -1) {
49
+ return index;
50
+ }
51
+ if (gradio.props.allow_custom_value) {
52
+ return v;
53
+ }
54
+ // Instead of returning null, skip this iteration
55
+ return undefined;
56
+ })
57
+ .filter((val): val is string | number => val !== undefined);
58
+ }
59
+ return [];
60
+ }
61
+
62
+ let selected_indices: (number | string)[] = $derived.by(set_selected_indices);
63
+
64
+ function handle_blur(): void {
65
+ if (!gradio.props.allow_custom_value) {
66
+ input_text = "";
67
+ }
68
+
69
+ if (gradio.props.allow_custom_value && input_text !== "") {
70
+ add_selected_choice(input_text);
71
+ input_text = "";
72
+ }
73
+ gradio.dispatch("blur");
74
+ show_options = false;
75
+ active_index = null;
76
+ }
77
+
78
+ function remove_selected_choice(option_index: number | string) {
79
+ selected_indices = selected_indices.filter((v) => v !== option_index);
80
+ gradio.props.value = selected_indices.map((index) =>
81
+ typeof index === "number" ? choices_values[index] : index
82
+ );
83
+ gradio.dispatch("input");
84
+ gradio.dispatch("select", {
85
+ index: typeof option_index === "number" ? option_index : -1,
86
+ value:
87
+ typeof option_index === "number"
88
+ ? choices_values[option_index]
89
+ : option_index,
90
+ selected: false
91
+ });
92
+ }
93
+
94
+ function add_selected_choice(option_index: number | string) {
95
+ if (
96
+ gradio.props.max_choices == null ||
97
+ selected_indices.length < gradio.props.max_choices
98
+ ) {
99
+ selected_indices.push(option_index);
100
+ gradio.dispatch("select", {
101
+ index: typeof option_index === "number" ? option_index : -1,
102
+ value:
103
+ typeof option_index === "number"
104
+ ? choices_values[option_index]
105
+ : option_index,
106
+ selected: true
107
+ });
108
+ }
109
+ if (selected_indices.length === gradio.props.max_choices) {
110
+ show_options = false;
111
+ active_index = null;
112
+ filter_input.blur();
113
+ }
114
+ gradio.props.value = selected_indices.map((index) =>
115
+ typeof index === "number" ? choices_values[index] : index
116
+ );
117
+ }
118
+
119
+ function handle_option_selected(index: any): void {
120
+ const option_index = parseInt(index);
121
+ add_or_remove_index(option_index);
122
+ }
123
+
124
+ function add_or_remove_index(option_index: number): void {
125
+ if (selected_indices.includes(option_index)) {
126
+ remove_selected_choice(option_index);
127
+ } else {
128
+ add_selected_choice(option_index);
129
+ }
130
+ input_text = "";
131
+ active_index = null;
132
+ gradio.dispatch("input");
133
+ }
134
+
135
+ function remove_all(e: any): void {
136
+ selected_indices = [];
137
+ input_text = "";
138
+ gradio.props.value = [];
139
+ e.preventDefault();
140
+ }
141
+
142
+ function handle_focus(e: FocusEvent): void {
143
+ filtered_indices = gradio.props.choices.map((_, i) => i);
144
+ if (
145
+ gradio.props.max_choices === null ||
146
+ selected_indices.length < gradio.props.max_choices
147
+ ) {
148
+ show_options = true;
149
+ }
150
+ gradio.dispatch("focus");
151
+ show_options = true;
152
+ }
153
+
154
+ function handle_key_down(e: KeyboardEvent): void {
155
+ [show_options, active_index] = handle_shared_keys(
156
+ e,
157
+ active_index,
158
+ filtered_indices
159
+ );
160
+ if (e.key === "Enter") {
161
+ if (active_index !== null) {
162
+ add_or_remove_index(active_index);
163
+ } else {
164
+ if (gradio.props.allow_custom_value) {
165
+ add_selected_choice(input_text);
166
+ input_text = "";
167
+ }
168
+ }
169
+ }
170
+ if (e.key === "Backspace" && input_text === "") {
171
+ const new_indices = selected_indices.slice(0, -1);
172
+ gradio.props.value = new_indices.map((index) =>
173
+ typeof index === "number" ? choices_values[index] : index
174
+ );
175
+ }
176
+ if (selected_indices.length === gradio.props.max_choices) {
177
+ show_options = false;
178
+ active_index = null;
179
+ }
180
+ }
181
+
182
+ let old_value = $state(gradio.props.value);
183
+
184
+ $effect(() => {
185
+ if (old_value !== gradio.props.value) {
186
+ old_value = gradio.props.value;
187
+ gradio.dispatch("change");
188
+ }
189
+ });
190
+
191
+ function oncustom_button_click(id: number) {
192
+ gradio.dispatch("custom_button_click", { id });
193
+ }
194
+ </script>
195
+
196
+ <div class:container={gradio.shared.container}>
197
+ {#if gradio.shared.show_label && buttons && buttons.length > 0}
198
+ <IconButtonWrapper
199
+ {buttons}
200
+ on_custom_button_click={oncustom_button_click}
201
+ />
202
+ {/if}
203
+ <BlockTitle show_label={gradio.shared.show_label} info={gradio.props.info}
204
+ >{label}</BlockTitle
205
+ >
206
+
207
+ <div class="wrap">
208
+ <div class="wrap-inner" class:show_options>
209
+ {#each selected_indices as s}
210
+ <div class="token">
211
+ <span>
212
+ {#if typeof s === "number"}
213
+ {choices_names[s]}
214
+ {:else}
215
+ {s}
216
+ {/if}
217
+ </span>
218
+ {#if !disabled}
219
+ <div
220
+ class="token-remove"
221
+ on:click|preventDefault={() => remove_selected_choice(s)}
222
+ on:keydown={(event) => {
223
+ if (event.key === "Enter") {
224
+ remove_selected_choice(s);
225
+ }
226
+ }}
227
+ role="button"
228
+ tabindex="0"
229
+ title={gradio.i18n("common.remove") + " " + s}
230
+ >
231
+ <Remove />
232
+ </div>
233
+ {/if}
234
+ </div>
235
+ {/each}
236
+ <div class="secondary-wrap">
237
+ <input
238
+ class="border-none"
239
+ class:subdued={(!choices_names.includes(input_text) &&
240
+ !gradio.props.allow_custom_value) ||
241
+ selected_indices.length === gradio.props.max_choices}
242
+ {disabled}
243
+ autocomplete="off"
244
+ bind:value={input_text}
245
+ bind:this={filter_input}
246
+ on:keydown={handle_key_down}
247
+ on:keyup={(e) => {
248
+ gradio.dispatch("key_up", {
249
+ key: e.key,
250
+ input_value: input_text
251
+ });
252
+ }}
253
+ on:blur={handle_blur}
254
+ on:focus={handle_focus}
255
+ readonly={!gradio.props.filterable}
256
+ />
257
+
258
+ {#if !disabled}
259
+ {#if selected_indices.length > 0}
260
+ <div
261
+ role="button"
262
+ tabindex="0"
263
+ class="token-remove remove-all"
264
+ title={gradio.i18n("common.clear")}
265
+ on:click={remove_all}
266
+ on:keydown={(event) => {
267
+ if (event.key === "Enter") {
268
+ remove_all(event);
269
+ }
270
+ }}
271
+ >
272
+ <Remove />
273
+ </div>
274
+ {/if}
275
+ <span class="icon-wrap"> <DropdownArrow /></span>
276
+ {/if}
277
+ </div>
278
+ </div>
279
+ <DropdownOptions
280
+ {show_options}
281
+ choices={gradio.props.choices}
282
+ {filtered_indices}
283
+ {disabled}
284
+ {selected_indices}
285
+ {active_index}
286
+ remember_scroll={true}
287
+ onchange={handle_option_selected}
288
+ />
289
+ </div>
290
+ </div>
291
+
292
+ <style>
293
+ .icon-wrap {
294
+ color: var(--body-text-color);
295
+ margin-right: var(--size-2);
296
+ width: var(--size-5);
297
+ }
298
+ .container .wrap {
299
+ box-shadow: var(--input-shadow);
300
+ border: var(--input-border-width) solid var(--border-color-primary);
301
+ }
302
+
303
+ .wrap {
304
+ position: relative;
305
+ border-radius: var(--input-radius);
306
+ background: var(--input-background-fill);
307
+ }
308
+
309
+ .wrap:focus-within {
310
+ box-shadow: var(--input-shadow-focus);
311
+ border-color: var(--input-border-color-focus);
312
+ }
313
+
314
+ .wrap-inner {
315
+ display: flex;
316
+ position: relative;
317
+ flex-wrap: wrap;
318
+ align-items: center;
319
+ gap: var(--checkbox-label-gap);
320
+ padding: var(--checkbox-label-padding);
321
+ }
322
+
323
+ .token {
324
+ display: flex;
325
+ align-items: center;
326
+ transition: var(--button-transition);
327
+ cursor: pointer;
328
+ box-shadow: var(--checkbox-label-shadow);
329
+ border: var(--checkbox-label-border-width) solid
330
+ var(--checkbox-label-border-color);
331
+ border-radius: var(--button-small-radius);
332
+ background: var(--checkbox-label-background-fill);
333
+ padding: var(--checkbox-label-padding);
334
+ color: var(--checkbox-label-text-color);
335
+ font-weight: var(--checkbox-label-text-weight);
336
+ font-size: var(--checkbox-label-text-size);
337
+ line-height: var(--line-md);
338
+ word-break: break-word;
339
+ }
340
+
341
+ .token > * + * {
342
+ margin-left: var(--size-2);
343
+ }
344
+
345
+ .token-remove {
346
+ fill: var(--body-text-color);
347
+ display: flex;
348
+ justify-content: center;
349
+ align-items: center;
350
+ cursor: pointer;
351
+ border: var(--checkbox-border-width) solid var(--border-color-primary);
352
+ border-radius: var(--radius-full);
353
+ background: var(--background-fill-primary);
354
+ padding: var(--size-0-5);
355
+ width: 16px;
356
+ height: 16px;
357
+ flex-shrink: 0;
358
+ }
359
+
360
+ .secondary-wrap {
361
+ display: flex;
362
+ flex: 1 1 0%;
363
+ align-items: center;
364
+ border: none;
365
+ min-width: min-content;
366
+ }
367
+
368
+ input {
369
+ margin: var(--spacing-sm);
370
+ outline: none;
371
+ border: none;
372
+ background: inherit;
373
+ width: var(--size-full);
374
+ color: var(--body-text-color);
375
+ font-size: var(--input-text-size);
376
+ }
377
+
378
+ input:disabled {
379
+ -webkit-text-fill-color: var(--body-text-color);
380
+ -webkit-opacity: 1;
381
+ opacity: 1;
382
+ cursor: not-allowed;
383
+ }
384
+
385
+ .remove-all {
386
+ margin-left: var(--size-1);
387
+ width: 20px;
388
+ height: 20px;
389
+ }
390
+ .subdued {
391
+ color: var(--body-text-color-subdued);
392
+ }
393
+ input[readonly] {
394
+ cursor: pointer;
395
+ }
396
+ </style>
6.17.1/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.17.1/dropdown/types.ts ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import type { KeyUpData, SelectData, CustomButton } from "@gradio/utils";
2
+
3
+ export type Item = string | number;
4
+
5
+ export interface DropdownProps {
6
+ multiselect: boolean;
7
+ max_choices: number | null;
8
+ choices: [string, Item][];
9
+ allow_custom_value: boolean;
10
+ value: Item | Item[];
11
+ info: string;
12
+ filterable: boolean;
13
+ buttons: (string | CustomButton)[] | null;
14
+ }
15
+
16
+ export interface DropdownEvents {
17
+ change: never;
18
+ input: never;
19
+ select: SelectData;
20
+ focus: never;
21
+ blur: never;
22
+ key_up: KeyUpData;
23
+ custom_button_click: { id: number };
24
+ }