Skip to content

Commit f612384

Browse files
committed
fix: only hide tab panels and set aria-selected of closest tab-container.
1 parent f151b74 commit f612384

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export default class TabContainerElement extends HTMLElement {
77
if (!(target instanceof HTMLElement)) return
88
if (target.closest(this.tagName) !== this) return
99
if (target.getAttribute('role') !== 'tab' && !target.closest('[role="tablist"]')) return
10-
const tabs = Array.from(this.querySelectorAll('[role="tablist"] [role="tab"]'))
10+
const tabs = Array.from(this.querySelectorAll('[role="tablist"] [role="tab"]')).filter(tab => tab.closest(this.tagName) === this)
1111
const currentIndex = tabs.indexOf(tabs.find(tab => tab.matches('[aria-selected="true"]'))!)
1212

1313
if (event.code === 'ArrowRight') {
@@ -58,8 +58,8 @@ export default class TabContainerElement extends HTMLElement {
5858
}
5959

6060
function selectTab(tabContainer: TabContainerElement, index: number) {
61-
const tabs = tabContainer.querySelectorAll<HTMLElement>('[role="tablist"] [role="tab"]')
62-
const panels = tabContainer.querySelectorAll<HTMLElement>('[role="tabpanel"]')
61+
const tabs = Array.from(tabContainer.querySelectorAll<HTMLElement>('[role="tablist"] [role="tab"]')).filter(tab => tab.closest(tabContainer.tagName) === tabContainer)
62+
const panels = Array.from(tabContainer.querySelectorAll<HTMLElement>('[role="tabpanel"]')).filter(panel => panel.closest(tabContainer.tagName) === tabContainer)
6363

6464
const selectedTab = tabs[index]
6565
const selectedPanel = panels[index]

test/test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,10 @@ describe('tab-container', function () {
177177
assert.deepStrictEqual(nestedTabs.map(isSelected), [false, true], 'nested tabs did change state')
178178
assert.deepStrictEqual(panels.map(isHidden), [false, true, true], 'top panels changed state')
179179
assert.deepStrictEqual(nestedPanels.map(isHidden), [true, false], 'nested panels did not change state')
180+
181+
tabs[1].click()
182+
183+
assert.deepStrictEqual(nestedPanels.map(isHidden), [true, false], 'nested panels did change state')
180184
})
181185

182186
it('only switches closest tab-containers on arrow', () => {
@@ -198,6 +202,11 @@ describe('tab-container', function () {
198202
assert.deepStrictEqual(nestedTabs.map(isSelected), [false, true], 'nested tabs did change state')
199203
assert.deepStrictEqual(panels.map(isHidden), [false, true, true], 'top panels changed state')
200204
assert.deepStrictEqual(nestedPanels.map(isHidden), [true, false], 'nested panels did not change state')
205+
206+
tabs[1].dispatchEvent(new KeyboardEvent('keydown', {code: 'ArrowLeft', bubbles: true}))
207+
208+
assert.deepStrictEqual(nestedPanels.map(isHidden), [true, false], 'nested panels changed state when top panel changed')
209+
assert.deepStrictEqual(nestedTabs.map(isSelected), [false, true], 'nested tabs changed state when top panel changed')
201210
})
202211
})
203212
})

0 commit comments

Comments
 (0)