Skip to content

Commit 26b47ba

Browse files
authored
Merge pull request #9 from github/add-tab-container-will-change-event
Add tab container will change event
2 parents eb31430 + 098501f commit 26b47ba

3 files changed

Lines changed: 45 additions & 14 deletions

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ import '@github/tab-container-element'
3535

3636
### Events
3737

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.
38+
- `tab-container-change` (bubbles, cancelable): fired on `<tab-container>` before a new tab is selected and visibility is updated. `event.detail.relatedTarget` is the tab panel that will be selected if the event isn't cancelled.
39+
- `tab-container-changed` (bubbles): fired on `<tab-container>` after a new tab is selected and visibility is updated. `event.detail.relatedTarget` is the newly visible tab panel.
3940

4041
## Browser support
4142

index.js

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,18 @@ function selectTab(tabContainer: TabContainerElement, index: number) {
4545
const tabs = tabContainer.querySelectorAll('[role="tablist"] [role="tab"]')
4646
const panels = tabContainer.querySelectorAll('[role="tabpanel"]')
4747

48+
const selectedTab = tabs[index]
49+
const selectedPanel = panels[index]
50+
51+
const cancelled = !tabContainer.dispatchEvent(
52+
new CustomEvent('tab-container-change', {
53+
bubbles: true,
54+
cancelable: true,
55+
detail: {relatedTarget: selectedPanel}
56+
})
57+
)
58+
if (cancelled) return
59+
4860
for (const tab of tabs) {
4961
tab.setAttribute('aria-selected', 'false')
5062
tab.setAttribute('tabindex', '-1')
@@ -54,18 +66,15 @@ function selectTab(tabContainer: TabContainerElement, index: number) {
5466
panel.setAttribute('tabindex', '0')
5567
}
5668

57-
const tab = tabs[index]
58-
const panel = panels[index]
59-
60-
tab.setAttribute('aria-selected', 'true')
61-
tab.removeAttribute('tabindex')
62-
tab.focus()
63-
panel.hidden = false
69+
selectedTab.setAttribute('aria-selected', 'true')
70+
selectedTab.removeAttribute('tabindex')
71+
selectedTab.focus()
72+
selectedPanel.hidden = false
6473

6574
tabContainer.dispatchEvent(
66-
new CustomEvent('tab-container-change', {
75+
new CustomEvent('tab-container-changed', {
6776
bubbles: true,
68-
detail: {relatedTarget: panel}
77+
detail: {relatedTarget: selectedPanel}
6978
})
7079
)
7180
}

test/test.js

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ describe('tab-container', function() {
3737
document.body.innerHTML = ''
3838
})
3939

40-
it('click works and event is dispatched', function() {
40+
it('click works and `tab-container-changed` event is dispatched', function() {
4141
const tabContainer = document.querySelector('tab-container')
4242
const tabs = document.querySelectorAll('button')
4343
const panels = document.querySelectorAll('[role="tabpanel"]')
4444
let counter = 0
45-
tabContainer.addEventListener('tab-container-change', event => {
45+
tabContainer.addEventListener('tab-container-changed', event => {
4646
counter++
4747
assert.equal(event.detail.relatedTarget, panels[1])
4848
})
@@ -54,12 +54,12 @@ describe('tab-container', function() {
5454
assert.equal(document.activeElement, tabs[1])
5555
})
5656

57-
it('keyboard shortcuts work and events are dispatched', function() {
57+
it('keyboard shortcuts work and `tab-container-changed` events are dispatched', function() {
5858
const tabContainer = document.querySelector('tab-container')
5959
const tabs = document.querySelectorAll('button')
6060
const panels = document.querySelectorAll('[role="tabpanel"]')
6161
let counter = 0
62-
tabContainer.addEventListener('tab-container-change', () => counter++)
62+
tabContainer.addEventListener('tab-container-changed', () => counter++)
6363

6464
tabs[0].dispatchEvent(new KeyboardEvent('keydown', {code: 'ArrowLeft', bubbles: true}))
6565
assert(panels[0].hidden)
@@ -72,5 +72,26 @@ describe('tab-container', function() {
7272
assert.equal(document.activeElement, tabs[0])
7373
assert.equal(counter, 2)
7474
})
75+
76+
it('click works and a cancellable `tab-container-change` event is dispatched', function() {
77+
const tabContainer = document.querySelector('tab-container')
78+
const tabs = document.querySelectorAll('button')
79+
const panels = document.querySelectorAll('[role="tabpanel"]')
80+
let counter = 0
81+
tabContainer.addEventListener('tab-container-change', event => {
82+
counter++
83+
assert.equal(event.detail.relatedTarget, panels[1])
84+
event.preventDefault()
85+
})
86+
87+
tabs[1].click()
88+
89+
// Since we prevented the event, nothing should have changed.
90+
assert(!panels[0].hidden)
91+
assert(panels[1].hidden)
92+
93+
// The event listener should have been called.
94+
assert.equal(counter, 1)
95+
})
7596
})
7697
})

0 commit comments

Comments
 (0)