Skip to content

Commit 98f5d77

Browse files
authored
Merge pull request #97 from github/ensure_tabs_available
Ensure tabs are available before selecting
2 parents b294c24 + a947f7d commit 98f5d77

File tree

4 files changed

+23
-8
lines changed

4 files changed

+23
-8
lines changed

.tool-versions

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
nodejs 22.3.0

custom-elements.json

+3
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,9 @@
437437
"kind": "field",
438438
"name": "#tabList",
439439
"privacy": "private",
440+
"type": {
441+
"text": "HTMLElement"
442+
},
440443
"readonly": true
441444
},
442445
{

src/tab-container-element.ts

+16-3
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ export class TabContainerElement extends HTMLElement {
109109

110110
static observedAttributes = ['vertical']
111111

112-
get #tabList() {
112+
get #tabList(): HTMLElement {
113113
const wrapper = this.querySelector('[slot=tablist-wrapper]')
114114
if (wrapper?.closest(this.tagName) === this) {
115115
return wrapper.querySelector('[role=tablist]') as HTMLElement
@@ -221,7 +221,18 @@ export class TabContainerElement extends HTMLElement {
221221
this.addEventListener('click', this)
222222

223223
this.selectTab(-1)
224-
this.#setupComplete = true
224+
225+
if (!this.#setupComplete) {
226+
const mutationObserver = new MutationObserver(() => {
227+
this.selectTab(-1)
228+
229+
if (this.#setupComplete) {
230+
mutationObserver.disconnect()
231+
}
232+
})
233+
234+
mutationObserver.observe(this, {childList: true, subtree: true})
235+
}
225236
}
226237

227238
attributeChangedCallback(name: string) {
@@ -356,7 +367,7 @@ export class TabContainerElement extends HTMLElement {
356367
* Out of bounds index
357368
*/
358369
if (index > tabs.length - 1) {
359-
throw new RangeError(`Index "${index}" out of bounds`)
370+
return
360371
}
361372

362373
const selectedTab = tabs[index]
@@ -401,5 +412,7 @@ export class TabContainerElement extends HTMLElement {
401412
}),
402413
)
403414
}
415+
416+
this.#setupComplete = true
404417
}
405418
}

test/test.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -339,11 +339,9 @@ describe('tab-container', function () {
339339
})
340340

341341
it('result in noop, when selectTab receives out of bounds index', function () {
342-
assert.throws(() => tabContainer.selectTab(3), 'Index "3" out of bounds')
343-
344-
tabContainer.selectTab(2)
345-
assert.deepStrictEqual(tabs.map(isSelected), [false, false, true], 'Third tab is selected')
346-
assert.deepStrictEqual(panels.map(isHidden), [true, true, false], 'Third panel is visible')
342+
tabContainer.selectTab(3)
343+
assert.deepStrictEqual(tabs.map(isSelected), [true, false, false], 'First tab is selected')
344+
assert.deepStrictEqual(panels.map(isHidden), [false, true, true], 'First panel is visible')
347345
})
348346
})
349347

0 commit comments

Comments
 (0)