-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtabs.ts
More file actions
192 lines (171 loc) · 4.58 KB
/
tabs.ts
File metadata and controls
192 lines (171 loc) · 4.58 KB
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
import { Element } from '@core/element'
import { html, css, nothing } from 'lit'
import { customElement, property } from 'lit/decorators.js'
const TABS_COMPONENT = 'wdio-devtools-tabs'
@customElement(TABS_COMPONENT)
export class DevtoolsTabs extends Element {
#activeTab: string | undefined
#tabList: string[] = []
#badgeCheckInterval?: number
@property({ type: String })
cacheId?: string
static styles = [
...Element.styles,
css`
:host {
width: 100%;
flex-grow: 1;
min-height: 0;
display: flex;
flex-direction: column;
color: var(--vscode-foreground);
background-color: var(--vscode-editor-background);
}
`
]
#getTabButton(tabId: string) {
const tabElement = this.tabs.find(
(el) => el.getAttribute('label') === tabId
)
const badge = (tabElement as any)?.badge
const showBadge = badge && badge > 0
return html`
<button
@click="${() => this.activateTab(tabId)}"
class="transition-colors px-4 py-2 hover:bg-toolbarHoverBackground ${this
.#activeTab === tabId
? 'bg-toolbarHoverBackground'
: ''} flex items-center gap-2"
>
<span>${tabId}</span>
${showBadge
? html`<span
class="inline-flex items-center justify-center min-w-[20px] px-1.5 py-0.5 text-xs font-semibold rounded-full"
style="background-color: #5a5a5a; color: #ffffff;"
>${badge}</span
>`
: nothing}
</button>
`
}
get tabs() {
if (!this.shadowRoot) {
return []
}
const slot = [...Array.from(this.shadowRoot.querySelectorAll('slot'))].find(
(s) => !s.hasAttribute('name')
)
if (!slot) {
return []
}
return slot.assignedElements({ flatten: true }) as Element[]
}
get updateComplete(): Promise<boolean> {
return super.updateComplete.then(() => {
const tab = this.querySelector(`[slot="${this.#activeTab}"]`)
if (tab) {
tab.setAttribute('active', '')
}
return true
})
}
activateTab(tabId: string) {
const activeTab = this.tabs.find((el) => el.getAttribute('label') === tabId)
if (!activeTab) {
return
}
this.#activeTab = tabId
this.tabs.forEach((el) => el.removeAttribute('active'))
activeTab?.setAttribute('active', '')
this.requestUpdate()
/**
* cache tab id in local storage
*/
if (this.cacheId) {
localStorage.setItem(this.cacheId, tabId)
}
}
connectedCallback() {
super.connectedCallback()
setTimeout(() => {
// wait till innerHTML is parsed
this.#tabList =
this.tabs
.map((el) => el.getAttribute('label') as string)
.filter(Boolean) || []
/**
* get tab id either from local storage or a tab element that
* has an "active" attribute
*/
this.#activeTab =
(this.cacheId && localStorage.getItem(this.cacheId)) ||
this.tabs
.find((el) => el.hasAttribute('active'))
?.getAttribute('label') ||
undefined
/**
* set active tab or first tab as active
*/
if (!this.#activeTab) {
this.#activeTab = this.#tabList[0]
this.tabs[0].setAttribute('active', '')
} else {
this.activateTab(this.#activeTab)
}
this.requestUpdate()
// Check for badge changes periodically
this.#badgeCheckInterval = window.setInterval(() => {
this.requestUpdate()
}, 250)
})
}
disconnectedCallback() {
super.disconnectedCallback()
if (this.#badgeCheckInterval) {
clearInterval(this.#badgeCheckInterval)
}
}
render() {
return html`
${this.#tabList.length
? html`
<nav class="flex w-full bg-sideBarBackground shadow-md z-10">
${this.#tabList.map((tab) => this.#getTabButton(tab))}
<slot name="actions"></slot>
</nav>
`
: nothing}
<slot></slot>
`
}
}
const TAB_COMPONENT = 'wdio-devtools-tab'
@customElement(TAB_COMPONENT)
export class DevtoolsTab extends Element {
@property({ type: Number })
badge?: number
static styles = [
...Element.styles,
css`
:host {
display: none;
flex-grow: 1;
min-height: 0;
overflow-y: auto;
scrollbar-width: none;
}
:host([active]) {
display: flex;
}
`
]
render() {
return html` <slot></slot> `
}
}
declare global {
interface HTMLElementTagNameMap {
[TABS_COMPONENT]: DevtoolsTabs
[TAB_COMPONENT]: DevtoolsTab
}
}