File size: 4,782 Bytes
6434c78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<script lang="ts">
	import { Copy, Check } from "@gradio/icons";
	import { FullscreenButton } from "@gradio/atoms";
	import { onDestroy } from "svelte";
	import { createEventDispatcher } from "svelte";
	import { IconButton } from "@gradio/atoms";

	export let show_fullscreen_button = false;
	export let show_copy_button = false;
	export let show_search: "none" | "search" | "filter" = "none";
	export let fullscreen = false;
	export let on_copy: () => Promise<void>;
	export let on_commit_filter: () => void;

	const dispatch = createEventDispatcher<{
		search: string | null;
	}>();

	let copied = false;
	let timer: ReturnType<typeof setTimeout>;
	export let current_search_query: string | null = null;
	let input_value = "";

	function handle_search_input(e: Event): void {
		const target = e.target as HTMLTextAreaElement;
		input_value = target.value;
		const new_query = input_value || null;
		if (current_search_query !== new_query) {
			current_search_query = new_query;
			dispatch("search", current_search_query);
		}
	}

	function copy_feedback(): void {
		copied = true;
		if (timer) clearTimeout(timer);
		timer = setTimeout(() => {
			copied = false;
		}, 2000);
	}

	async function handle_copy(): Promise<void> {
		await on_copy();
		copy_feedback();
	}

	onDestroy(() => {
		if (timer) clearTimeout(timer);
	});
</script>

<div class="toolbar" role="toolbar" aria-label="Table actions">
	<div class="toolbar-buttons">
		{#if show_search !== "none"}
			<div class="search-container">
				<input
					type="text"
					value={current_search_query || ""}
					on:input={handle_search_input}
					placeholder={show_search === "filter" ? "Filter..." : "Search..."}
					class="search-input"
					class:filter-mode={show_search === "filter"}
					title={`Enter text to ${show_search} the table`}
				/>
				{#if current_search_query && show_search === "filter"}
					<button
						class="toolbar-button check-button"
						on:click={on_commit_filter}
						aria-label="Apply filter and update dataframe values"
						title="Apply filter and update dataframe values"
					>
						<Check />
					</button>
				{/if}
			</div>
		{/if}
		{#if show_copy_button}
			<IconButton
				Icon={copied ? Check : Copy}
				label={copied ? "Copied to clipboard" : "Copy table data"}
				on:click={handle_copy}
			/>
		{/if}
		{#if show_fullscreen_button}
			<FullscreenButton {fullscreen} on:fullscreen />
		{/if}
	</div>
</div>

<style>
	.toolbar {
		display: flex;
		align-items: center;
		gap: var(--size-2);
		flex: 0 0 auto;
		font-family: var(--font-sans);
	}

	.toolbar-buttons {
		display: flex;
		gap: var(--size-1);
		flex-wrap: nowrap;
		align-items: center;
	}

	.toolbar-button {
		display: flex;
		align-items: center;
		justify-content: center;
		width: var(--size-6);
		height: var(--size-6);
		padding: var(--size-1);
		border: none;
		border-radius: var(--radius-sm);
		background: transparent;
		color: var(--body-text-color-subdued);
		cursor: pointer;
		transition: all 0.2s;
	}

	.toolbar-button:hover {
		background: var(--background-fill-secondary);
		color: var(--body-text-color);
	}

	.toolbar-button :global(svg) {
		width: var(--size-4);
		height: var(--size-4);
	}

	.search-container {
		position: relative;
	}

	.search-input {
		width: var(--size-full);
		height: var(--size-6);
		padding: var(--size-2);
		padding-right: var(--size-8);
		border: 1px solid var(--border-color-primary);
		border-radius: var(--table-radius);
		font-size: var(--text-sm);
		color: var(--body-text-color);
		background: var(--background-fill-secondary);
		transition: all 0.2s ease;
	}

	.search-input:hover {
		border-color: var(--border-color-secondary);
		background: var(--background-fill-primary);
	}

	.search-input:focus {
		outline: none;
		border-color: var(--color-accent);
		background: var(--background-fill-primary);
		box-shadow: 0 0 0 1px var(--color-accent);
	}

	.check-button {
		position: absolute;
		right: var(--size-1);
		top: 50%;
		transform: translateY(-50%);
		background: var(--color-accent);
		color: white;
		border: none;
		width: var(--size-4);
		height: var(--size-4);
		border-radius: var(--radius-sm);
		display: flex;
		align-items: center;
		justify-content: center;
		padding: var(--size-1);
	}

	.check-button :global(svg) {
		width: var(--size-3);
		height: var(--size-3);
	}

	.check-button:hover {
		background: var(--color-accent-soft);
	}

	.toolbar-buttons :global(.icon-button) {
		background: transparent !important;
		height: var(--size-6);
		width: var(--size-6);
	}

	.toolbar-buttons :global(.icon-button:hover) {
		background: var(--background-fill-secondary) !important;
		color: var(--body-text-color) !important;
		border: 1px solid var(--border-color-primary);
		border-radius: var(--radius-sm) !important;
	}
</style>