Skip to content

Commit 04cd36e

Browse files
authored
Merge pull request #75 from github/refactor-setup-code-to-call-into-selecttab
Refactor setup code to call into selecttab
2 parents b11dd53 + a83a574 commit 04cd36e

1 file changed

Lines changed: 29 additions & 29 deletions

File tree

src/tab-container-element.ts

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -76,21 +76,17 @@ export class TabContainerElement extends HTMLElement {
7676
)
7777
}
7878

79+
#setup = false
7980
connectedCallback(): void {
8081
this.addEventListener('keydown', this)
8182
this.addEventListener('click', this)
82-
for (const tab of this.#tabs) {
83-
if (!tab.hasAttribute('aria-selected')) {
84-
tab.setAttribute('aria-selected', 'false')
85-
}
86-
if (!tab.hasAttribute('tabindex')) {
87-
if (tab.getAttribute('aria-selected') === 'true') {
88-
tab.setAttribute('tabindex', '0')
89-
} else {
90-
tab.setAttribute('tabindex', '-1')
91-
}
92-
}
93-
}
83+
this.selectTab(
84+
Math.max(
85+
this.#tabs.findIndex(el => el.matches('[aria-selected=true]')),
86+
0,
87+
),
88+
)
89+
this.#setup = true
9490
}
9591

9692
handleEvent(event: Event) {
@@ -150,15 +146,17 @@ export class TabContainerElement extends HTMLElement {
150146
const selectedTab = tabs[index]
151147
const selectedPanel = panels[index]
152148

153-
const cancelled = !this.dispatchEvent(
154-
new TabContainerChangeEvent('tab-container-change', {
155-
bubbles: true,
156-
cancelable: true,
157-
tab: selectedTab,
158-
panel: selectedPanel,
159-
}),
160-
)
161-
if (cancelled) return
149+
if (this.#setup) {
150+
const cancelled = !this.dispatchEvent(
151+
new TabContainerChangeEvent('tab-container-change', {
152+
bubbles: true,
153+
cancelable: true,
154+
tab: selectedTab,
155+
panel: selectedPanel,
156+
}),
157+
)
158+
if (cancelled) return
159+
}
162160

163161
for (const tab of tabs) {
164162
tab.setAttribute('aria-selected', 'false')
@@ -173,15 +171,17 @@ export class TabContainerElement extends HTMLElement {
173171

174172
selectedTab.setAttribute('aria-selected', 'true')
175173
selectedTab.setAttribute('tabindex', '0')
176-
selectedTab.focus()
177174
selectedPanel.hidden = false
178175

179-
this.dispatchEvent(
180-
new TabContainerChangeEvent('tab-container-changed', {
181-
bubbles: true,
182-
tab: selectedTab,
183-
panel: selectedPanel,
184-
}),
185-
)
176+
if (this.#setup) {
177+
selectedTab.focus()
178+
this.dispatchEvent(
179+
new TabContainerChangeEvent('tab-container-changed', {
180+
bubbles: true,
181+
tab: selectedTab,
182+
panel: selectedPanel,
183+
}),
184+
)
185+
}
186186
}
187187
}

0 commit comments

Comments
 (0)