Skip to content

Commit f8ec0f2

Browse files
authored
Merge pull request #3 from github/event
Update event, docs, and tests (breaking change)
2 parents bd4f38d + bc656f4 commit f8ec0f2

File tree

4 files changed

+72
-6
lines changed

4 files changed

+72
-6
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ import '@github/tab-container-element'
3333
</tab-container>
3434
```
3535

36+
### Events
37+
38+
- `tab-container-change` (bubbles): fired on `<tab-container>` after a new tab is selected and visibility is updated. `event.detail.relatedTarget` is the newly visible tab panel.
39+
3640
## Browser support
3741

3842
Browsers without native [custom element support][support] require a [polyfill][].

examples/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
</div>
2424
</tab-container>
2525

26-
<!-- script src="../dist/index.umd.js"></script -->
26+
<!-- <script src="../dist/index.umd.js"></script> -->
2727
<script src="https://unpkg.com/@github/tab-container-element@latest/dist/index.umd.js"></script>
2828
</body>
2929
</html>

index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,12 @@ class TabContainerElement extends HTMLElement {
6161
tab.focus()
6262
panel.hidden = false
6363

64-
panel.dispatchEvent(new CustomEvent('tabcontainer:tabchange'))
64+
this.dispatchEvent(
65+
new CustomEvent('tab-container-change', {
66+
bubbles: true,
67+
detail: {relatedTarget: panel}
68+
})
69+
)
6570
}
6671
}
6772

test/test.js

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,73 @@ describe('tab-container', function() {
1313

1414
describe('after tree insertion', function() {
1515
beforeEach(function() {
16-
document.body.innerHTML = '<tab-container>Tabs go here</tab-container>'
16+
document.body.innerHTML = `
17+
<tab-container>
18+
<div role="tablist">
19+
<button type="button" role="tab" aria-selected="true">Tab one</button>
20+
<button type="button" role="tab">Tab two</button>
21+
<button type="button" role="tab">Tab three</button>
22+
</div>
23+
<div role="tabpanel">
24+
Panel 1
25+
</div>
26+
<div role="tabpanel" hidden>
27+
Panel 2
28+
</div>
29+
<div role="tabpanel" hidden>
30+
Panel 3
31+
</div>
32+
</tab-container>
33+
`
1734
})
1835

1936
afterEach(function() {
2037
document.body.innerHTML = ''
2138
})
2239

23-
it('initiates', function() {
24-
const ce = document.querySelector('tab-container')
25-
assert.equal(ce.textContent, 'Tabs go here')
40+
it('click works and event is dispatched', function() {
41+
const tabContainer = document.querySelector('tab-container')
42+
const tabs = document.querySelectorAll('button')
43+
const panels = document.querySelectorAll('[role="tabpanel"]')
44+
let counter = 0
45+
tabContainer.addEventListener('tab-container-change', event => {
46+
counter++
47+
assert.equal(event.detail.relatedTarget, panels[1])
48+
})
49+
50+
tabs[1].click()
51+
assert(panels[0].hidden)
52+
assert(!panels[1].hidden)
53+
assert.equal(counter, 1)
54+
assert.equal(document.activeElement, tabs[1])
55+
})
56+
57+
it('keyboard shortcuts work and events are dispatched', function() {
58+
const tabContainer = document.querySelector('tab-container')
59+
const tabs = document.querySelectorAll('button')
60+
const panels = document.querySelectorAll('[role="tabpanel"]')
61+
let counter = 0
62+
tabContainer.addEventListener('tab-container-change', () => counter++)
63+
64+
tabs[0].dispatchEvent(new KeyboardEvent('keydown', {code: 'ArrowLeft', bubbles: true}))
65+
assert(panels[0].hidden)
66+
assert(!panels[2].hidden)
67+
assert.equal(document.activeElement, tabs[2])
68+
69+
tabs[0].dispatchEvent(new KeyboardEvent('keydown', {code: 'Home', bubbles: true}))
70+
assert(!panels[0].hidden)
71+
assert(panels[2].hidden)
72+
assert.equal(document.activeElement, tabs[0])
73+
assert.equal(counter, 2)
74+
})
75+
76+
it('selectTab(index) works', function() {
77+
const tabContainer = document.querySelector('tab-container')
78+
const panels = document.querySelectorAll('[role="tabpanel"]')
79+
80+
tabContainer.selectTab(2)
81+
assert(panels[0].hidden)
82+
assert(!panels[2].hidden)
2683
})
2784
})
2885
})

0 commit comments

Comments
 (0)