Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure tabs are available before selecting #97

Merged
merged 2 commits into from
Jun 19, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions src/tab-container-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export class TabContainerElement extends HTMLElement {

static observedAttributes = ['vertical']

get #tabList() {
get #tabList(): HTMLElement {
const wrapper = this.querySelector('[slot=tablist-wrapper]')
if (wrapper?.closest(this.tagName) === this) {
return wrapper.querySelector('[role=tablist]') as HTMLElement
Expand Down Expand Up @@ -220,8 +220,20 @@ export class TabContainerElement extends HTMLElement {
this.addEventListener('keydown', this)
this.addEventListener('click', this)

this.selectTab(-1)
this.#setupComplete = true
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) {
Expand Down Expand Up @@ -356,7 +368,7 @@ export class TabContainerElement extends HTMLElement {
* Out of bounds index
*/
if (index > tabs.length - 1) {
throw new RangeError(`Index "${index}" out of bounds`)
return
}

const selectedTab = tabs[index]
Expand Down
Loading