gradio-pr-bot commited on
Commit
3c460c7
·
verified ·
1 Parent(s): 51ed902

Upload folder using huggingface_hub

Browse files
6.5.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.5.0/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.5.0/dropdown/package.json ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "@gradio/dropdown",
3
+ "version": "0.11.3",
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.5.0/dropdown/shared/Dropdown.svelte ADDED
@@ -0,0 +1,311 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ let show_options = $derived.by(() => {
56
+ return is_browser && filter_input === document.activeElement;
57
+ });
58
+ let choices_names: string[] = $derived.by(() => {
59
+ return choices.map((c) => c[0]);
60
+ });
61
+ let choices_values: (string | number)[] = $derived.by(() => {
62
+ return choices.map((c) => c[1]);
63
+ });
64
+ let [input_text, selected_index] = $derived.by(() => {
65
+ if (
66
+ value === undefined ||
67
+ value === null ||
68
+ (Array.isArray(value) && value.length === 0)
69
+ ) {
70
+ return ["", null];
71
+ } else if (choices_values.includes(value as string | number)) {
72
+ return [
73
+ choices_names[choices_values.indexOf(value as string | number)],
74
+ choices_values.indexOf(value as string | number)
75
+ ];
76
+ } else if (allow_custom_value) {
77
+ return [value as string, null];
78
+ } else {
79
+ return ["", null];
80
+ }
81
+ });
82
+ // Use last_typed_value to track when the user has typed
83
+ // on_blur we only want to update value if the user has typed
84
+ let last_typed_value = input_text;
85
+ let initialized = $state(false);
86
+ let disabled = $derived(!interactive);
87
+
88
+ // All of these are indices with respect to the choices array
89
+ let filtered_indices = $state(choices.map((_, i) => i));
90
+ let active_index: number | null = $state(null);
91
+
92
+ function handle_option_selected(index: any): void {
93
+ selected_index = parseInt(index);
94
+ if (isNaN(selected_index)) {
95
+ // This is the case when the user clicks on the scrollbar
96
+ selected_index = null;
97
+ return;
98
+ }
99
+
100
+ let [_input_text, _value] = choices[selected_index];
101
+ input_text = _input_text;
102
+ last_typed_value = input_text;
103
+ value = _value;
104
+ on_select?.({
105
+ index: selected_index,
106
+ value: choices_values[selected_index],
107
+ selected: true
108
+ });
109
+ show_options = false;
110
+ active_index = null;
111
+ on_input?.();
112
+ filter_input.blur();
113
+ }
114
+
115
+ function handle_focus(e: FocusEvent): void {
116
+ filtered_indices = choices.map((_, i) => i);
117
+ show_options = true;
118
+ on_focus?.();
119
+ }
120
+
121
+ function handle_blur(): void {
122
+ if (!allow_custom_value) {
123
+ input_text =
124
+ choices_names[choices_values.indexOf(value as string | number)];
125
+ } else {
126
+ if (choices_names.includes(input_text)) {
127
+ selected_index = choices_names.indexOf(input_text);
128
+ value = choices_values[selected_index];
129
+ } else if (input_text !== last_typed_value) {
130
+ value = input_text;
131
+ selected_index = null;
132
+ }
133
+ }
134
+ show_options = false;
135
+ active_index = null;
136
+ filtered_indices = choices.map((_, i) => i);
137
+ on_blur?.();
138
+ on_input?.();
139
+ }
140
+
141
+ async function handle_key_down(e: KeyboardEvent): Promise<void> {
142
+ await tick();
143
+ filtered_indices = handle_filter(choices, input_text);
144
+ active_index = filtered_indices.length > 0 ? filtered_indices[0] : null;
145
+ [show_options, active_index] = handle_shared_keys(
146
+ e,
147
+ active_index,
148
+ filtered_indices
149
+ );
150
+ if (e.key === "Enter") {
151
+ last_typed_value = input_text;
152
+ if (active_index !== null) {
153
+ selected_index = active_index;
154
+ value = choices_values[active_index];
155
+ show_options = false;
156
+ filter_input.blur();
157
+ active_index = null;
158
+ } else if (choices_names.includes(input_text)) {
159
+ selected_index = choices_names.indexOf(input_text);
160
+ value = choices_values[selected_index];
161
+ show_options = false;
162
+ active_index = null;
163
+ filter_input.blur();
164
+ } else if (allow_custom_value) {
165
+ value = input_text;
166
+ selected_index = null;
167
+ show_options = false;
168
+ active_index = null;
169
+ filter_input.blur();
170
+ }
171
+ }
172
+ }
173
+
174
+ let old_value = $state(value);
175
+ $effect(() => {
176
+ if (old_value !== value) {
177
+ old_value = value;
178
+ on_change?.(value);
179
+ }
180
+ });
181
+ </script>
182
+
183
+ <div class:container>
184
+ {#if show_label && buttons && buttons.length > 0}
185
+ <IconButtonWrapper
186
+ {buttons}
187
+ on_custom_button_click={oncustom_button_click}
188
+ />
189
+ {/if}
190
+ <BlockTitle {show_label} {info}>{label}</BlockTitle>
191
+
192
+ <div class="wrap">
193
+ <div class="wrap-inner" class:show_options>
194
+ <div class="secondary-wrap">
195
+ <input
196
+ role="listbox"
197
+ aria-controls="dropdown-options"
198
+ aria-expanded={show_options}
199
+ aria-label={label}
200
+ class="border-none"
201
+ class:subdued={!choices_names.includes(input_text) &&
202
+ !allow_custom_value}
203
+ autocomplete="off"
204
+ {disabled}
205
+ bind:value={input_text}
206
+ bind:this={filter_input}
207
+ onkeydown={handle_key_down}
208
+ onkeyup={(e) => {
209
+ on_key_up?.({
210
+ key: e.key,
211
+ input_value: input_text
212
+ });
213
+ }}
214
+ onblur={handle_blur}
215
+ onfocus={handle_focus}
216
+ readonly={!filterable}
217
+ />
218
+ {#if !disabled}
219
+ <div class="icon-wrap">
220
+ <DropdownArrow />
221
+ </div>
222
+ {/if}
223
+ </div>
224
+ </div>
225
+ <DropdownOptions
226
+ {show_options}
227
+ {choices}
228
+ {filtered_indices}
229
+ {disabled}
230
+ selected_indices={selected_index === null ? [] : [selected_index]}
231
+ {active_index}
232
+ onchange={handle_option_selected}
233
+ onload={() => (initialized = true)}
234
+ />
235
+ </div>
236
+ </div>
237
+
238
+ <style>
239
+ .icon-wrap {
240
+ position: absolute;
241
+ top: 50%;
242
+ transform: translateY(-50%);
243
+ right: var(--size-5);
244
+ color: var(--body-text-color);
245
+ width: var(--size-5);
246
+ pointer-events: none;
247
+ }
248
+ .container {
249
+ height: 100%;
250
+ }
251
+ .container .wrap {
252
+ box-shadow: var(--input-shadow);
253
+ border: var(--input-border-width) solid var(--border-color-primary);
254
+ }
255
+
256
+ .wrap {
257
+ position: relative;
258
+ border-radius: var(--input-radius);
259
+ background: var(--input-background-fill);
260
+ }
261
+
262
+ .wrap:focus-within {
263
+ box-shadow: var(--input-shadow-focus);
264
+ border-color: var(--input-border-color-focus);
265
+ background: var(--input-background-fill-focus);
266
+ }
267
+
268
+ .wrap-inner {
269
+ display: flex;
270
+ position: relative;
271
+ flex-wrap: wrap;
272
+ align-items: center;
273
+ gap: var(--checkbox-label-gap);
274
+ padding: var(--checkbox-label-padding);
275
+ height: 100%;
276
+ }
277
+ .secondary-wrap {
278
+ display: flex;
279
+ flex: 1 1 0%;
280
+ align-items: center;
281
+ border: none;
282
+ min-width: min-content;
283
+ height: 100%;
284
+ }
285
+
286
+ input {
287
+ margin: var(--spacing-sm);
288
+ outline: none;
289
+ border: none;
290
+ background: inherit;
291
+ width: var(--size-full);
292
+ color: var(--body-text-color);
293
+ font-size: var(--input-text-size);
294
+ height: 100%;
295
+ }
296
+
297
+ input:disabled {
298
+ -webkit-text-fill-color: var(--body-text-color);
299
+ -webkit-opacity: 1;
300
+ opacity: 1;
301
+ cursor: not-allowed;
302
+ }
303
+
304
+ .subdued {
305
+ color: var(--body-text-color-subdued);
306
+ }
307
+
308
+ input[readonly] {
309
+ cursor: pointer;
310
+ }
311
+ </style>
6.5.0/dropdown/shared/DropdownOptions.svelte ADDED
@@ -0,0 +1,182 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ onchange,
14
+ onload
15
+ }: {
16
+ choices: [string, string | number][];
17
+ filtered_indices: number[];
18
+ show_options: boolean;
19
+ disabled?: boolean;
20
+ selected_indices?: (string | number)[];
21
+ active_index: number | null;
22
+ remember_scroll?: boolean;
23
+ offset_from_top?: number;
24
+ from_top?: boolean;
25
+ onchange?: (index: any) => void;
26
+ onload?: () => void;
27
+ } = $props();
28
+
29
+ let distance_from_top: number;
30
+ let distance_from_bottom: number;
31
+ let input_height: number;
32
+ let input_width = $state(0);
33
+ let refElement: HTMLDivElement;
34
+ let listElement: HTMLUListElement;
35
+ let top: string | null = $state(null);
36
+ let bottom: string | null = $state(null);
37
+ let max_height: number = $state(0);
38
+ let innerHeight = $state(0);
39
+ let list_scroll_y = 0;
40
+
41
+ function calculate_window_distance(): void {
42
+ const { top: ref_top, bottom: ref_bottom } =
43
+ refElement.getBoundingClientRect();
44
+ if (from_top) {
45
+ distance_from_top = offset_from_top;
46
+ } else {
47
+ distance_from_top = ref_top;
48
+ }
49
+ distance_from_bottom = innerHeight - ref_bottom;
50
+ }
51
+
52
+ let scroll_timeout: NodeJS.Timeout | null = null;
53
+ function scroll_listener(): void {
54
+ if (!show_options) return;
55
+ if (scroll_timeout !== null) {
56
+ clearTimeout(scroll_timeout);
57
+ }
58
+
59
+ scroll_timeout = setTimeout(() => {
60
+ calculate_window_distance();
61
+ scroll_timeout = null;
62
+ }, 10);
63
+ }
64
+
65
+ function restore_last_scroll(): void {
66
+ listElement?.scrollTo?.(0, list_scroll_y);
67
+ }
68
+
69
+ $effect(() => {
70
+ if (show_options && refElement) {
71
+ if (remember_scroll) {
72
+ restore_last_scroll();
73
+ } else {
74
+ if (listElement && selected_indices.length > 0) {
75
+ let elements = listElement.querySelectorAll("li");
76
+ for (const element of Array.from(elements)) {
77
+ if (
78
+ element.getAttribute("data-index") ===
79
+ selected_indices[0].toString()
80
+ ) {
81
+ listElement?.scrollTo?.(0, (element as HTMLLIElement).offsetTop);
82
+ break;
83
+ }
84
+ }
85
+ }
86
+ }
87
+ calculate_window_distance();
88
+ const rect = refElement.parentElement?.getBoundingClientRect();
89
+ input_height = rect?.height || 0;
90
+ input_width = rect?.width || 0;
91
+ onload?.();
92
+ }
93
+ if (distance_from_bottom > distance_from_top || from_top) {
94
+ top = `${distance_from_top}px`;
95
+ max_height = distance_from_bottom;
96
+ bottom = null;
97
+ } else {
98
+ bottom = `${distance_from_bottom + input_height}px`;
99
+ max_height = distance_from_top - input_height;
100
+ top = null;
101
+ }
102
+ });
103
+ </script>
104
+
105
+ <svelte:window on:scroll={scroll_listener} bind:innerHeight />
106
+
107
+ <div class="reference" bind:this={refElement} />
108
+ {#if show_options && !disabled}
109
+ <ul
110
+ class="options"
111
+ transition:fly={{ duration: 200, y: 5 }}
112
+ onmousedown={(e) => {
113
+ e.preventDefault();
114
+ onchange?.((e.target as HTMLElement).dataset.index);
115
+ }}
116
+ onscroll={(e) => (list_scroll_y = e.currentTarget.scrollTop)}
117
+ style:top
118
+ style:bottom
119
+ style:max-height={`calc(${max_height}px - var(--window-padding))`}
120
+ style:width={input_width + "px"}
121
+ bind:this={listElement}
122
+ role="listbox"
123
+ >
124
+ {#each filtered_indices as index}
125
+ <li
126
+ class="item"
127
+ class:selected={selected_indices.includes(index)}
128
+ class:active={index === active_index}
129
+ class:bg-gray-100={index === active_index}
130
+ class:dark:bg-gray-600={index === active_index}
131
+ style:width={input_width + "px"}
132
+ data-index={index}
133
+ aria-label={choices[index][0]}
134
+ data-testid="dropdown-option"
135
+ role="option"
136
+ aria-selected={selected_indices.includes(index)}
137
+ >
138
+ <span class:hide={!selected_indices.includes(index)} class="inner-item">
139
+
140
+ </span>
141
+ {choices[index][0]}
142
+ </li>
143
+ {/each}
144
+ </ul>
145
+ {/if}
146
+
147
+ <style>
148
+ .options {
149
+ --window-padding: var(--size-8);
150
+ position: fixed;
151
+ z-index: var(--layer-top);
152
+ margin-left: 0;
153
+ box-shadow: var(--shadow-drop-lg);
154
+ border-radius: var(--container-radius);
155
+ background: var(--background-fill-primary);
156
+ min-width: fit-content;
157
+ max-width: inherit;
158
+ overflow: auto;
159
+ color: var(--body-text-color);
160
+ list-style: none;
161
+ }
162
+
163
+ .item {
164
+ display: flex;
165
+ cursor: pointer;
166
+ padding: var(--size-2);
167
+ word-break: break-word;
168
+ }
169
+
170
+ .item:hover,
171
+ .active {
172
+ background: var(--background-fill-secondary);
173
+ }
174
+
175
+ .inner-item {
176
+ padding-right: var(--size-1);
177
+ }
178
+
179
+ .hide {
180
+ visibility: hidden;
181
+ }
182
+ </style>
6.5.0/dropdown/shared/Multiselect.svelte ADDED
@@ -0,0 +1,393 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ selected_indices = [...selected_indices.slice(0, -1)];
172
+ }
173
+ if (selected_indices.length === gradio.props.max_choices) {
174
+ show_options = false;
175
+ active_index = null;
176
+ }
177
+ }
178
+
179
+ let old_value = $state(gradio.props.value);
180
+
181
+ $effect(() => {
182
+ if (old_value !== gradio.props.value) {
183
+ old_value = gradio.props.value;
184
+ gradio.dispatch("change");
185
+ }
186
+ });
187
+
188
+ function oncustom_button_click(id: number) {
189
+ gradio.dispatch("custom_button_click", { id });
190
+ }
191
+ </script>
192
+
193
+ <div class:container={gradio.shared.container}>
194
+ {#if gradio.shared.show_label && buttons && buttons.length > 0}
195
+ <IconButtonWrapper
196
+ {buttons}
197
+ on_custom_button_click={oncustom_button_click}
198
+ />
199
+ {/if}
200
+ <BlockTitle show_label={gradio.shared.show_label} info={gradio.props.info}
201
+ >{label}</BlockTitle
202
+ >
203
+
204
+ <div class="wrap">
205
+ <div class="wrap-inner" class:show_options>
206
+ {#each selected_indices as s}
207
+ <div class="token">
208
+ <span>
209
+ {#if typeof s === "number"}
210
+ {choices_names[s]}
211
+ {:else}
212
+ {s}
213
+ {/if}
214
+ </span>
215
+ {#if !disabled}
216
+ <div
217
+ class="token-remove"
218
+ on:click|preventDefault={() => remove_selected_choice(s)}
219
+ on:keydown={(event) => {
220
+ if (event.key === "Enter") {
221
+ remove_selected_choice(s);
222
+ }
223
+ }}
224
+ role="button"
225
+ tabindex="0"
226
+ title={gradio.i18n("common.remove") + " " + s}
227
+ >
228
+ <Remove />
229
+ </div>
230
+ {/if}
231
+ </div>
232
+ {/each}
233
+ <div class="secondary-wrap">
234
+ <input
235
+ class="border-none"
236
+ class:subdued={(!choices_names.includes(input_text) &&
237
+ !gradio.props.allow_custom_value) ||
238
+ selected_indices.length === gradio.props.max_choices}
239
+ {disabled}
240
+ autocomplete="off"
241
+ bind:value={input_text}
242
+ bind:this={filter_input}
243
+ on:keydown={handle_key_down}
244
+ on:keyup={(e) => {
245
+ gradio.dispatch("key_up", {
246
+ key: e.key,
247
+ input_value: input_text
248
+ });
249
+ }}
250
+ on:blur={handle_blur}
251
+ on:focus={handle_focus}
252
+ readonly={!gradio.props.filterable}
253
+ />
254
+
255
+ {#if !disabled}
256
+ {#if selected_indices.length > 0}
257
+ <div
258
+ role="button"
259
+ tabindex="0"
260
+ class="token-remove remove-all"
261
+ title={gradio.i18n("common.clear")}
262
+ on:click={remove_all}
263
+ on:keydown={(event) => {
264
+ if (event.key === "Enter") {
265
+ remove_all(event);
266
+ }
267
+ }}
268
+ >
269
+ <Remove />
270
+ </div>
271
+ {/if}
272
+ <span class="icon-wrap"> <DropdownArrow /></span>
273
+ {/if}
274
+ </div>
275
+ </div>
276
+ <DropdownOptions
277
+ {show_options}
278
+ choices={gradio.props.choices}
279
+ {filtered_indices}
280
+ {disabled}
281
+ {selected_indices}
282
+ {active_index}
283
+ remember_scroll={true}
284
+ onchange={handle_option_selected}
285
+ />
286
+ </div>
287
+ </div>
288
+
289
+ <style>
290
+ .icon-wrap {
291
+ color: var(--body-text-color);
292
+ margin-right: var(--size-2);
293
+ width: var(--size-5);
294
+ }
295
+ .container .wrap {
296
+ box-shadow: var(--input-shadow);
297
+ border: var(--input-border-width) solid var(--border-color-primary);
298
+ }
299
+
300
+ .wrap {
301
+ position: relative;
302
+ border-radius: var(--input-radius);
303
+ background: var(--input-background-fill);
304
+ }
305
+
306
+ .wrap:focus-within {
307
+ box-shadow: var(--input-shadow-focus);
308
+ border-color: var(--input-border-color-focus);
309
+ }
310
+
311
+ .wrap-inner {
312
+ display: flex;
313
+ position: relative;
314
+ flex-wrap: wrap;
315
+ align-items: center;
316
+ gap: var(--checkbox-label-gap);
317
+ padding: var(--checkbox-label-padding);
318
+ }
319
+
320
+ .token {
321
+ display: flex;
322
+ align-items: center;
323
+ transition: var(--button-transition);
324
+ cursor: pointer;
325
+ box-shadow: var(--checkbox-label-shadow);
326
+ border: var(--checkbox-label-border-width) solid
327
+ var(--checkbox-label-border-color);
328
+ border-radius: var(--button-small-radius);
329
+ background: var(--checkbox-label-background-fill);
330
+ padding: var(--checkbox-label-padding);
331
+ color: var(--checkbox-label-text-color);
332
+ font-weight: var(--checkbox-label-text-weight);
333
+ font-size: var(--checkbox-label-text-size);
334
+ line-height: var(--line-md);
335
+ word-break: break-word;
336
+ }
337
+
338
+ .token > * + * {
339
+ margin-left: var(--size-2);
340
+ }
341
+
342
+ .token-remove {
343
+ fill: var(--body-text-color);
344
+ display: flex;
345
+ justify-content: center;
346
+ align-items: center;
347
+ cursor: pointer;
348
+ border: var(--checkbox-border-width) solid var(--border-color-primary);
349
+ border-radius: var(--radius-full);
350
+ background: var(--background-fill-primary);
351
+ padding: var(--size-0-5);
352
+ width: 16px;
353
+ height: 16px;
354
+ flex-shrink: 0;
355
+ }
356
+
357
+ .secondary-wrap {
358
+ display: flex;
359
+ flex: 1 1 0%;
360
+ align-items: center;
361
+ border: none;
362
+ min-width: min-content;
363
+ }
364
+
365
+ input {
366
+ margin: var(--spacing-sm);
367
+ outline: none;
368
+ border: none;
369
+ background: inherit;
370
+ width: var(--size-full);
371
+ color: var(--body-text-color);
372
+ font-size: var(--input-text-size);
373
+ }
374
+
375
+ input:disabled {
376
+ -webkit-text-fill-color: var(--body-text-color);
377
+ -webkit-opacity: 1;
378
+ opacity: 1;
379
+ cursor: not-allowed;
380
+ }
381
+
382
+ .remove-all {
383
+ margin-left: var(--size-1);
384
+ width: 20px;
385
+ height: 20px;
386
+ }
387
+ .subdued {
388
+ color: var(--body-text-color-subdued);
389
+ }
390
+ input[readonly] {
391
+ cursor: pointer;
392
+ }
393
+ </style>
6.5.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.5.0/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
+ }