Skip to content

Commit df9d585

Browse files
committed
add default-tab attribute
1 parent e329880 commit df9d585

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ import '@github/tab-container-element'
3131
</tab-container>
3232
```
3333

34+
If none of the tabs have `aria-selected=true`, then the first tab will be selected automatically. You can also add the `default-tab=N` attribute to avoid having to set `aria-selected=true` on the desired tab.
35+
3436
### Events
3537

3638
- `tab-container-change` (bubbles, cancelable): fired on `<tab-container>` before a new tab is selected and visibility is updated. `event.tab` is the tab that will be focused and `tab.panel` is the panel that will be shown if the event isn't cancelled.

src/tab-container-element.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,8 @@ export class TabContainerElement extends HTMLElement {
253253
this.#beforeTabsSlot.assign(...beforeSlotted)
254254
this.#afterTabsSlot.assign(...afterTabSlotted)
255255
this.#afterPanelsSlot.assign(...afterSlotted)
256-
const defaultIndex = this.#tabs.findIndex(el => el.matches('[aria-selected=true]'))
256+
const defaultTab = Number(this.getAttribute('default-tab') || -1)
257+
const defaultIndex = defaultTab >= 0 ? defaultTab : this.#tabs.findIndex(el => el.matches('[aria-selected=true]'))
257258
index = index >= 0 ? index : Math.max(0, defaultIndex)
258259
}
259260

test/test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,38 @@ describe('tab-container', function () {
7777
})
7878
})
7979

80+
describe('after tree insertion with default-tab', function () {
81+
beforeEach(function () {
82+
document.body.innerHTML = `
83+
<tab-container default-tab=1>
84+
<button type="button" role="tab">Tab one</button>
85+
<button type="button" role="tab">Tab two</button>
86+
<button type="button" role="tab">Tab three</button>
87+
<div role="tabpanel" hidden>
88+
Panel 1
89+
</div>
90+
<div role="tabpanel">
91+
Panel 2
92+
</div>
93+
<div role="tabpanel" hidden data-tab-container-no-tabstop>
94+
Panel 3
95+
</div>
96+
</tab-container>
97+
`
98+
tabs = Array.from(document.querySelectorAll('button'))
99+
panels = Array.from(document.querySelectorAll('[role="tabpanel"]'))
100+
})
101+
102+
afterEach(function () {
103+
document.body.innerHTML = ''
104+
})
105+
106+
it('the second tab is still selected', function () {
107+
assert.deepStrictEqual(tabs.map(isSelected), [false, true, false], 'Second tab is selected')
108+
assert.deepStrictEqual(panels.map(isHidden), [true, false, true], 'Second panel is visible')
109+
})
110+
})
111+
80112
describe('after tree insertion', function () {
81113
beforeEach(function () {
82114
document.body.innerHTML = `

0 commit comments

Comments
 (0)