Skip to content

Commit 2a0e8d2

Browse files
committed
adds the ability to have a custom tablist-tab-wrapper
1 parent cdbfb46 commit 2a0e8d2

File tree

3 files changed

+58
-7
lines changed

3 files changed

+58
-7
lines changed

examples/index.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ <h2>Panel with extra buttons</h2>
161161

162162
</main>
163163

164-
<script src="../dist/index.js" type="module"></script>
165-
<!-- <script src="https://unpkg.com/@github/tab-container-element@latest/dist/index.js" type="module"></script> -->
164+
<!-- <script src="../dist/index.js" type="module"></script> -->
165+
<script src="https://unpkg.com/@github/tab-container-element@latest/dist/index.js" type="module"></script>
166166
</body>
167167
</html>

src/tab-container-element.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -283,15 +283,13 @@ export class TabContainerElement extends HTMLElement {
283283
} else {
284284
customTabListWrapper.setAttribute('slot', 'tablist')
285285
}
286-
}
287-
else if (customTabList && customTabList.closest(this.tagName) === this) {
286+
} else if (customTabList && customTabList.closest(this.tagName) === this) {
288287
if (manualSlotsSupported) {
289288
tabListSlot.assign(customTabList)
290289
} else {
291290
customTabList.setAttribute('slot', 'tablist')
292291
}
293-
}
294-
else {
292+
} else {
295293
this.#tabListTabWrapper.role = 'tablist'
296294
if (manualSlotsSupported) {
297295
tabListSlot.assign(...[...this.children].filter(e => e.matches('[role=tab]')))
@@ -312,7 +310,11 @@ export class TabContainerElement extends HTMLElement {
312310
const afterSlotted: Element[] = []
313311
let autoSlotted = beforeSlotted
314312
for (const child of this.children) {
315-
if (child.getAttribute('role') === 'tab' || child.getAttribute('role') === 'tablist') {
313+
if (
314+
child.getAttribute('role') === 'tab' ||
315+
child.getAttribute('role') === 'tablist' ||
316+
child.getAttribute('slot') === 'tablist-tab-wrapper'
317+
) {
316318
autoSlotted = afterTabSlotted
317319
continue
318320
}

test/test.js

+49
Original file line numberDiff line numberDiff line change
@@ -669,4 +669,53 @@ describe('tab-container', function () {
669669
)
670670
})
671671
})
672+
describe('with custom tablist-tab-wrapper', function () {
673+
beforeEach(function () {
674+
document.body.innerHTML = `
675+
<tab-container>
676+
<div slot="tablist-tab-wrapper">
677+
<div role="tablist">
678+
<button type="button" role="tab">Tab one</button>
679+
<button type="button" role="tab" aria-selected="true">Tab two</button>
680+
<button type="button" role="tab">Tab three</button>
681+
</div>
682+
</div>
683+
<div role="tabpanel" hidden>
684+
Panel 1
685+
</div>
686+
<div role="tabpanel">
687+
Panel 2
688+
</div>
689+
<div role="tabpanel" hidden data-tab-container-no-tabstop>
690+
Panel 3
691+
</div>
692+
</tab-container>
693+
`
694+
tabs = Array.from(document.querySelectorAll('button'))
695+
panels = Array.from(document.querySelectorAll('[role="tabpanel"]'))
696+
})
697+
698+
afterEach(function () {
699+
// Check to make sure we still have accessible markup after the test finishes running.
700+
expect(document.body).to.be.accessible()
701+
702+
document.body.innerHTML = ''
703+
})
704+
705+
it('has accessible markup', function () {
706+
expect(document.body).to.be.accessible()
707+
})
708+
709+
it('the second tab is still selected', function () {
710+
assert.deepStrictEqual(tabs.map(isSelected), [false, true, false], 'Second tab is selected')
711+
assert.deepStrictEqual(panels.map(isHidden), [true, false, true], 'Second panel is visible')
712+
})
713+
714+
it('selects the clicked tab', function () {
715+
tabs[0].click()
716+
717+
assert.deepStrictEqual(tabs.map(isSelected), [true, false, false], 'First tab is selected')
718+
assert.deepStrictEqual(panels.map(isHidden), [false, true, true], 'First panel is visible')
719+
})
720+
})
672721
})

0 commit comments

Comments
 (0)