-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathtab-container-element.ts
417 lines (364 loc) · 13.8 KB
/
tab-container-element.ts
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
const HTMLElement = globalThis.HTMLElement || (null as unknown as (typeof window)['HTMLElement'])
// Function to see if manual slots are supported and if not, manual assign the slot attribute
const assignSlotWithFallback =
'assign' in (globalThis.HTMLSlotElement?.prototype || {})
? (slot: HTMLSlotElement, ...elements: Element[]) => {
slot.assign(...elements)
}
: (slot: HTMLSlotElement, ...elements: Element[]) => {
const host = (slot.getRootNode() as ShadowRoot).host
for (const element of host.querySelectorAll(`[slot="${slot.name}"]`)) {
element.removeAttribute('slot')
}
for (const element of elements) {
element.setAttribute('slot', slot.name)
}
}
export class TabContainerChangeEvent extends Event {
constructor(
type: string,
{tabIndex, tab, panel, ...init}: EventInit & {tabIndex?: number; tab?: Element; panel?: Element},
) {
super(type, init)
this.#tab = tab || null
this.#tabIndex = tabIndex || null
this.#panel = panel || null
}
get detail() {
// eslint-disable-next-line no-console
console.warn('TabContainerElement.detail is deprecated, please use .panel instead')
return {relatedTarget: this.#panel}
}
#tabIndex: number | null = null
get tabIndex(): number | null {
return this.#tabIndex
}
#panel: Element | null = null
get panel(): Element | null {
return this.#panel
}
#tab: Element | null = null
get tab(): Element | null {
return this.#tab
}
}
export class TabContainerElement extends HTMLElement {
static define(tag = 'tab-container', registry = customElements) {
registry.define(tag, this)
return this
}
get onChange() {
return this.onTabContainerChange
}
set onChange(listener: ((event: TabContainerChangeEvent) => void) | null) {
this.onTabContainerChange = listener
}
#onTabContainerChange: ((event: TabContainerChangeEvent) => void) | null = null
get onTabContainerChange() {
return this.#onTabContainerChange
}
set onTabContainerChange(listener: ((event: TabContainerChangeEvent) => void) | null) {
if (this.#onTabContainerChange) {
this.removeEventListener(
'tab-container-change',
this.#onTabContainerChange as unknown as EventListenerOrEventListenerObject,
)
}
this.#onTabContainerChange = typeof listener === 'object' || typeof listener === 'function' ? listener : null
if (typeof listener === 'function') {
this.addEventListener('tab-container-change', listener as unknown as EventListenerOrEventListenerObject)
}
}
#onTabContainerChanged: ((event: TabContainerChangeEvent) => void) | null = null
get onTabContainerChanged() {
return this.#onTabContainerChanged
}
set onTabContainerChanged(listener: ((event: TabContainerChangeEvent) => void) | null) {
if (this.#onTabContainerChanged) {
this.removeEventListener(
'tab-container-changed',
this.#onTabContainerChanged as unknown as EventListenerOrEventListenerObject,
)
}
this.#onTabContainerChanged = typeof listener === 'object' || typeof listener === 'function' ? listener : null
if (typeof listener === 'function') {
this.addEventListener('tab-container-changed', listener as unknown as EventListenerOrEventListenerObject)
}
}
get onChanged() {
return this.onTabContainerChanged
}
set onChanged(listener: ((event: TabContainerChangeEvent) => void) | null) {
this.onTabContainerChanged = listener
}
static observedAttributes = ['vertical']
get #tabList(): HTMLElement {
const wrapper = this.querySelector('[slot=tablist-wrapper]')
if (wrapper?.closest(this.tagName) === this) {
return wrapper.querySelector('[role=tablist]') as HTMLElement
}
const slot = this.#tabListSlot
if (this.#tabListTabWrapper.hasAttribute('role')) {
return this.#tabListTabWrapper
} else {
return slot.assignedNodes()[0] as HTMLElement
}
}
get #tabListWrapper() {
return this.shadowRoot!.querySelector<HTMLSlotElement>('slot[part="tablist-wrapper"]')!
}
get #tabListTabWrapper() {
return this.shadowRoot!.querySelector<HTMLElement>('div[part="tablist-tab-wrapper"]')!
}
get #beforeTabsSlot() {
return this.shadowRoot!.querySelector<HTMLSlotElement>('slot[part="before-tabs"]')!
}
get #afterTabsSlot() {
return this.shadowRoot!.querySelector<HTMLSlotElement>('slot[part="after-tabs"]')!
}
get #afterPanelsSlot() {
return this.shadowRoot!.querySelector<HTMLSlotElement>('slot[part="after-panels"]')!
}
get #tabListSlot() {
return this.shadowRoot!.querySelector<HTMLSlotElement>('slot[part="tablist"]')!
}
get #panelSlot() {
return this.shadowRoot!.querySelector<HTMLSlotElement>('slot[part="panel"]')!
}
get #tabs() {
if (this.#tabListTabWrapper.matches('[role=tablist]')) {
return this.#tabListSlot.assignedNodes() as HTMLElement[]
}
return Array.from(this.#tabList?.querySelectorAll<HTMLElement>('[role="tab"]') || []).filter(
tab => tab instanceof HTMLElement && tab.closest(this.tagName) === this,
)
}
get activeTab() {
return this.#tabs[this.selectedTabIndex]
}
get activePanel() {
return this.#panelSlot.assignedNodes()[0] as HTMLElement
}
get vertical(): boolean {
return this.#tabList?.getAttribute('aria-orientation') === 'vertical'
}
set vertical(isVertical: boolean) {
const tabList = this.#tabList
if (tabList && isVertical) {
tabList.setAttribute('aria-orientation', 'vertical')
} else {
tabList.setAttribute('aria-orientation', 'horizontal')
}
}
#setupComplete = false
#internals!: ElementInternals | null
connectedCallback(): void {
this.#internals ||= this.attachInternals ? this.attachInternals() : null
const shadowRoot = this.shadowRoot || this.attachShadow({mode: 'open', slotAssignment: 'manual'})
const tabListContainer = document.createElement('slot')
tabListContainer.style.display = 'flex'
tabListContainer.setAttribute('part', 'tablist-wrapper')
tabListContainer.setAttribute('name', 'tablist-wrapper')
const tabListTabWrapper = document.createElement('div')
tabListTabWrapper.setAttribute('part', 'tablist-tab-wrapper')
tabListTabWrapper.setAttribute('name', 'tablist-tab-wrapper')
const tabListSlot = document.createElement('slot')
tabListSlot.setAttribute('part', 'tablist')
tabListSlot.setAttribute('name', 'tablist')
tabListTabWrapper.append(tabListSlot)
const panelSlot = document.createElement('slot')
panelSlot.setAttribute('part', 'panel')
panelSlot.setAttribute('name', 'panel')
const beforeTabSlot = document.createElement('slot')
beforeTabSlot.setAttribute('part', 'before-tabs')
beforeTabSlot.setAttribute('name', 'before-tabs')
const afterTabSlot = document.createElement('slot')
afterTabSlot.setAttribute('part', 'after-tabs')
afterTabSlot.setAttribute('name', 'after-tabs')
tabListContainer.append(beforeTabSlot, tabListTabWrapper, afterTabSlot)
const afterSlot = document.createElement('slot')
afterSlot.setAttribute('part', 'after-panels')
afterSlot.setAttribute('name', 'after-panels')
shadowRoot.replaceChildren(tabListContainer, panelSlot, afterSlot)
if (this.#internals && 'role' in this.#internals) {
this.#internals.role = 'presentation'
} else {
this.setAttribute('role', 'presentation')
}
this.addEventListener('keydown', this)
this.addEventListener('click', this)
if (this.#tabs.length > 0) {
this.selectTab(-1)
this.#setupComplete = true
} else {
const mutationObserver = new MutationObserver(() => {
if (this.#tabs.length > 0) {
this.selectTab(-1)
this.#setupComplete = true
mutationObserver.disconnect()
}
})
mutationObserver.observe(this, {childList: true, subtree: true})
}
}
attributeChangedCallback(name: string) {
if (!this.isConnected || !this.shadowRoot) return
if (name === 'vertical') {
this.vertical = this.hasAttribute('vertical')
}
}
handleEvent(event: Event) {
if (event.type === 'click') return this.#handleClick(event as MouseEvent)
if (event.type === 'keydown') return this.#handleKeydown(event as KeyboardEvent)
}
#handleKeydown(event: KeyboardEvent) {
const tab = (event.target as HTMLElement)?.closest?.('[role="tab"]')
if (!tab) return
const tabs = this.#tabs
if (!tabs.includes(tab as HTMLElement)) return
const currentIndex = this.selectedTabIndex
const vertical = tab.closest('[role="tablist"]')?.getAttribute('aria-orientation') === 'vertical'
const prevTab = event.code === 'ArrowLeft' || (vertical && event.code === 'ArrowUp')
const nextTab = event.code === 'ArrowRight' || (vertical && event.code === 'ArrowDown')
if (nextTab) {
let index = currentIndex + 1
if (index >= tabs.length) index = 0
this.selectTab(index)
} else if (prevTab) {
let index = currentIndex - 1
if (index < 0) index = tabs.length - 1
this.selectTab(index)
} else if (event.code === 'Home') {
this.selectTab(0)
event.preventDefault()
} else if (event.code === 'End') {
this.selectTab(tabs.length - 1)
event.preventDefault()
}
}
#handleClick(event: MouseEvent) {
const tab = (event.target as HTMLElement)?.closest?.('[role=tab]')
if (!tab) return
const tabs = this.#tabs
const index = tabs.indexOf(tab as HTMLElement)
if (index >= 0) this.selectTab(index)
}
#reflectAttributeToShadow(name: string, node: Element) {
if (this.hasAttribute(name)) {
node.setAttribute(name, this.getAttribute(name)!)
this.removeAttribute(name)
}
}
get selectedTabIndex(): number {
return this.#tabs.findIndex(el => el.matches('[aria-selected=true]'))
}
set selectedTabIndex(i: number) {
this.selectTab(i)
}
get defaultTabIndex(): number {
return Number(this.getAttribute('default-tab') || -1)
}
set defaultTabIndex(index: number) {
this.setAttribute('default-tab', String(index))
}
selectTab(index: number): void {
if (!this.#setupComplete) {
const tabListSlot = this.#tabListSlot
const tabListWrapper = this.#tabListWrapper
const customTabList = this.querySelector('[role=tablist]')
const customTabListWrapper = this.querySelector('[slot=tablist-wrapper]')
if (customTabListWrapper && customTabListWrapper.closest(this.tagName) === this) {
assignSlotWithFallback(tabListWrapper, customTabListWrapper)
} else if (customTabList && customTabList.closest(this.tagName) === this) {
assignSlotWithFallback(tabListSlot, customTabList)
} else {
this.#tabListTabWrapper.role = 'tablist'
assignSlotWithFallback(tabListSlot, ...[...this.children].filter(e => e.matches('[role=tab]')))
}
const tabList = this.#tabList
this.#reflectAttributeToShadow('aria-description', tabList)
this.#reflectAttributeToShadow('aria-label', tabList)
if (this.vertical) {
this.#tabList.setAttribute('aria-orientation', 'vertical')
}
const bringsOwnWrapper = this.querySelector('[slot=tablist-wrapper]')?.closest(this.tagName) === this
if (!bringsOwnWrapper) {
const beforeSlotted: Element[] = []
const afterTabSlotted: Element[] = []
const afterSlotted: Element[] = []
let autoSlotted = beforeSlotted
for (const child of this.children) {
if (child.getAttribute('role') === 'tab' || child.getAttribute('role') === 'tablist') {
autoSlotted = afterTabSlotted
continue
}
if (child.getAttribute('role') === 'tabpanel') {
autoSlotted = afterSlotted
continue
}
if (child.getAttribute('slot') === 'before-tabs') {
beforeSlotted.push(child)
} else if (child.getAttribute('slot') === 'after-tabs') {
afterTabSlotted.push(child)
} else {
autoSlotted.push(child)
}
}
assignSlotWithFallback(this.#beforeTabsSlot, ...beforeSlotted)
assignSlotWithFallback(this.#afterTabsSlot, ...afterTabSlotted)
assignSlotWithFallback(this.#afterPanelsSlot, ...afterSlotted)
}
const defaultTab = this.defaultTabIndex
const defaultIndex = defaultTab >= 0 ? defaultTab : this.selectedTabIndex
index = index >= 0 ? index : Math.max(0, defaultIndex)
}
const tabs = this.#tabs
const panels = Array.from(this.querySelectorAll<HTMLElement>('[role="tabpanel"]')).filter(
panel => panel.closest(this.tagName) === this,
)
/**
* Out of bounds index
*/
if (index > tabs.length - 1) {
return
}
const selectedTab = tabs[index]
const selectedPanel = panels[index]
if (this.#setupComplete) {
const cancelled = !this.dispatchEvent(
new TabContainerChangeEvent('tab-container-change', {
tabIndex: index,
bubbles: true,
cancelable: true,
tab: selectedTab,
panel: selectedPanel,
}),
)
if (cancelled) return
}
for (const tab of tabs) {
tab.setAttribute('aria-selected', 'false')
tab.setAttribute('tabindex', '-1')
}
for (const panel of panels) {
if (!panel.hasAttribute('tabindex') && !panel.hasAttribute('data-tab-container-no-tabstop')) {
panel.setAttribute('tabindex', '0')
}
}
selectedTab.setAttribute('aria-selected', 'true')
selectedTab.setAttribute('tabindex', '0')
assignSlotWithFallback(this.#panelSlot, selectedPanel)
selectedPanel.hidden = false
if (this.#setupComplete) {
selectedTab.focus()
this.dispatchEvent(
new TabContainerChangeEvent('tab-container-changed', {
tabIndex: index,
bubbles: true,
tab: selectedTab,
panel: selectedPanel,
}),
)
}
}
}