@gradio/tabs 0.1.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @gradio/tabs
2
2
 
3
+ ## 0.2.1
4
+
5
+ ### Fixes
6
+
7
+ - [#7107](https://github.com/gradio-app/gradio/pull/7107) [`80f8fbf`](https://github.com/gradio-app/gradio/commit/80f8fbf0e8900627b9c2575bbd7c68fad8108544) - Add logic to handle non-interactive or hidden tabs. Thanks [@hannahblair](https://github.com/hannahblair)!
8
+
9
+ ## 0.2.0
10
+
11
+ ### Features
12
+
13
+ - [#7018](https://github.com/gradio-app/gradio/pull/7018) [`ec28b4e`](https://github.com/gradio-app/gradio/commit/ec28b4e7c47a9233d9e3a725cc9fe8f9044dfa94) - Add `visible` and `interactive` params to `gr.Tab()`. Thanks [@hannahblair](https://github.com/hannahblair)!
14
+
3
15
  ## 0.1.0
4
16
 
5
17
  ### Features
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gradio/tabs",
3
- "version": "0.1.0",
3
+ "version": "0.2.1",
4
4
  "description": "Gradio UI packages",
5
5
  "type": "module",
6
6
  "author": "",
@@ -13,6 +13,6 @@
13
13
  "./package.json": "./package.json"
14
14
  },
15
15
  "dependencies": {
16
- "@gradio/utils": "^0.2.0"
16
+ "@gradio/utils": "^0.2.1"
17
17
  }
18
18
  }
@@ -3,7 +3,7 @@
3
3
  </script>
4
4
 
5
5
  <script lang="ts">
6
- import { setContext, createEventDispatcher, tick } from "svelte";
6
+ import { setContext, createEventDispatcher } from "svelte";
7
7
  import { writable } from "svelte/store";
8
8
  import type { SelectData } from "@gradio/utils";
9
9
 
@@ -11,6 +11,8 @@
11
11
  name: string;
12
12
  id: object;
13
13
  elem_id: string | undefined;
14
+ visible: boolean;
15
+ interactive: boolean;
14
16
  }
15
17
 
16
18
  export let visible = true;
@@ -29,8 +31,28 @@
29
31
 
30
32
  setContext(TABS, {
31
33
  register_tab: (tab: Tab) => {
32
- tabs.push({ name: tab.name, id: tab.id, elem_id: tab.elem_id });
33
- selected_tab.update((current) => current ?? tab.id);
34
+ let existingTab = tabs.find((t) => t.id === tab.id);
35
+ if (existingTab) {
36
+ // update existing tab with newer values
37
+ let i = tabs.findIndex((t) => t.id === tab.id);
38
+ tabs[i] = { ...tabs[i], ...tab };
39
+ } else {
40
+ tabs.push({
41
+ name: tab.name,
42
+ id: tab.id,
43
+ elem_id: tab.elem_id,
44
+ visible: tab.visible,
45
+ interactive: tab.interactive
46
+ });
47
+ }
48
+ selected_tab.update((current) => {
49
+ if (current === false && tab.visible && tab.interactive) {
50
+ return tab.id;
51
+ }
52
+
53
+ let nextTab = tabs.find((t) => t.visible && t.interactive);
54
+ return nextTab ? nextTab.id : current;
55
+ });
34
56
  tabs = tabs;
35
57
  return tabs.length - 1;
36
58
  },
@@ -46,32 +68,54 @@
46
68
  });
47
69
 
48
70
  function change_tab(id: object | string | number): void {
49
- selected = id;
50
- $selected_tab = id;
51
- $selected_tab_index = tabs.findIndex((t) => t.id === id);
52
- dispatch("change");
71
+ const tab_to_activate = tabs.find((t) => t.id === id);
72
+ if (
73
+ tab_to_activate &&
74
+ tab_to_activate.interactive &&
75
+ tab_to_activate.visible
76
+ ) {
77
+ selected = id;
78
+ $selected_tab = id;
79
+ $selected_tab_index = tabs.findIndex((t) => t.id === id);
80
+ dispatch("change");
81
+ } else {
82
+ console.warn("Attempted to select a non-interactive or hidden tab.");
83
+ }
53
84
  }
54
85
 
55
- $: selected !== null && change_tab(selected);
86
+ $: tabs, selected !== null && change_tab(selected);
56
87
  </script>
57
88
 
58
89
  <div class="tabs {elem_classes.join(' ')}" class:hide={!visible} id={elem_id}>
59
- <div class="tab-nav scroll-hide">
90
+ <div class="tab-nav scroll-hide" role="tablist">
60
91
  {#each tabs as t, i (t.id)}
61
- {#if t.id === $selected_tab}
62
- <button class="selected" id={t.elem_id ? t.elem_id + "-button" : null}>
63
- {t.name}
64
- </button>
65
- {:else}
66
- <button
67
- id={t.elem_id ? t.elem_id + "-button" : null}
68
- on:click={() => {
69
- change_tab(t.id);
70
- dispatch("select", { value: t.name, index: i });
71
- }}
72
- >
73
- {t.name}
74
- </button>
92
+ {#if t.visible}
93
+ {#if t.id === $selected_tab}
94
+ <button
95
+ role="tab"
96
+ class="selected"
97
+ aria-selected={true}
98
+ aria-controls={t.elem_id}
99
+ id={t.elem_id ? t.elem_id + "-button" : null}
100
+ >
101
+ {t.name}
102
+ </button>
103
+ {:else}
104
+ <button
105
+ role="tab"
106
+ aria-selected={false}
107
+ aria-controls={t.elem_id}
108
+ disabled={!t.interactive}
109
+ aria-disabled={!t.interactive}
110
+ id={t.elem_id ? t.elem_id + "-button" : null}
111
+ on:click={() => {
112
+ change_tab(t.id);
113
+ dispatch("select", { value: t.name, index: i });
114
+ }}
115
+ >
116
+ {t.name}
117
+ </button>
118
+ {/if}
75
119
  {/if}
76
120
  {/each}
77
121
  </div>
@@ -107,6 +151,12 @@
107
151
  font-size: var(--section-header-text-size);
108
152
  }
109
153
 
154
+ button:disabled {
155
+ color: var(--body-text-color-subdued);
156
+ opacity: 0.5;
157
+ cursor: not-allowed;
158
+ }
159
+
110
160
  button:hover {
111
161
  color: var(--body-text-color);
112
162
  }