aboutsummaryrefslogtreecommitdiff
path: root/source/library/components/TabBar.svelte
blob: 9c34f20c666bb675a1631e7a6fde692c7d84b0cb (plain) (blame)
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
<script lang="ts">
  import { tick, type Snippet } from "svelte";
  import type { Tab } from "../state/session.svelte.ts";

  type Props = {
    activeId: string;
    extras?: Snippet<[{ tab: Tab }]>;
    onAdd: () => void;
    onClose: (id: string) => void;
    onRename: (id: string, title: string) => void;
    onSelect: (id: string) => void;
    tabs: Tab[];
  };

  let { activeId, extras, onAdd, onClose, onRename, onSelect, tabs }: Props = $props();

  let editingId = $state<string | null>(null);
  let draft = $state<string>("");
  let inputEl = $state<HTMLInputElement | null>(null);

  async function startEditing(tab: Tab) {
    editingId = tab.id;
    draft = tab.title;
    await tick();
    inputEl?.select();
  }

  function commit() {
    if (editingId === null) return;
    onRename(editingId, draft);
    editingId = null;
    draft = "";
  }

  function cancel() {
    editingId = null;
    draft = "";
  }

  function onKeydown(event: KeyboardEvent) {
    if (event.key === "Enter") {
      event.preventDefault();
      commit();
    } else if (event.key === "Escape") {
      event.preventDefault();
      cancel();
    }
  }

  function handleClose(event: MouseEvent, id: string) {
    event.stopPropagation();
    onClose(id);
  }
</script>

<style lang="scss">
  .tabbar {
    align-items: stretch;
    background: var(--graphiql-panel, #252526);
    border-bottom: 1px solid var(--graphiql-border, #333);
    display: flex;
    font-size: 0.8125rem;
    min-height: 2rem;
    overflow-x: auto;
  }

  .tab {
    align-items: center;
    background: transparent;
    border: none;
    border-right: 1px solid var(--graphiql-border, #333);
    color: var(--graphiql-muted, #858585);
    cursor: pointer;
    display: flex;
    gap: 0.5rem;
    padding: 0 0.75rem;

    &.active {
      background: var(--graphiql-bg, #1e1e1e);
      color: var(--graphiql-fg, #d4d4d4);
    }

    &:hover:not(.active) {
      color: var(--graphiql-fg, #d4d4d4);
    }
  }

  .title {
    white-space: nowrap;
  }

  .edit {
    background: var(--graphiql-bg, #1e1e1e);
    border: 1px solid var(--graphiql-accent, #0e639c);
    border-radius: 2px;
    color: var(--graphiql-fg, #d4d4d4);
    font-family: inherit;
    font-size: inherit;
    min-width: 6rem;
    outline: none;
    padding: 0.125rem 0.25rem;
  }

  .close {
    background: none;
    border: none;
    color: inherit;
    cursor: pointer;
    font-size: 1rem;
    line-height: 1;
    opacity: 0.6;
    padding: 0;

    &:hover {
      opacity: 1;
    }
  }

  .add {
    background: none;
    border: none;
    color: var(--graphiql-muted, #858585);
    cursor: pointer;
    font-size: 1rem;
    padding: 0 0.75rem;

    &:hover {
      color: var(--graphiql-fg, #d4d4d4);
    }
  }
</style>

<div class="tabbar">
  {#each tabs as tab (tab.id)}
    <button
      class="tab"
      class:active={tab.id === activeId}
      ondblclick={() => startEditing(tab)}
      onclick={() => onSelect(tab.id)}
    >
      {#if editingId === tab.id}
        <input
          bind:this={inputEl}
          bind:value={draft}
          class="edit"
          onblur={commit}
          onclick={(e) => e.stopPropagation()}
          onkeydown={onKeydown}
          type="text"
        />
      {:else}
        <span class="title">{tab.title}</span>
      {/if}
      {#if extras}{@render extras({ tab })}{/if}
      <button
        aria-label="Close tab"
        class="close"
        onclick={(e) => handleClose(e, tab.id)}
      >×</button>
    </button>
  {/each}
  <button aria-label="New tab" class="add" onclick={onAdd}>+</button>
</div>