diff --git a/README.md b/README.md index 65509eb..ea77013 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,10 @@ import '@github/tab-container-element' ``` +### Events + +- `tab-container-change` (bubbles): fired on `` after a new tab is selected and visibility is updated. `event.detail.relatedTarget` is the newly visible tab panel. + ## Browser support Browsers without native [custom element support][support] require a [polyfill][]. diff --git a/examples/index.html b/examples/index.html index d80c34d..d1472e1 100644 --- a/examples/index.html +++ b/examples/index.html @@ -23,7 +23,7 @@ - + diff --git a/index.js b/index.js index 06deb6c..e6695c4 100644 --- a/index.js +++ b/index.js @@ -61,7 +61,12 @@ class TabContainerElement extends HTMLElement { tab.focus() panel.hidden = false - panel.dispatchEvent(new CustomEvent('tabcontainer:tabchange')) + this.dispatchEvent( + new CustomEvent('tab-container-change', { + bubbles: true, + detail: {relatedTarget: panel} + }) + ) } } diff --git a/test/test.js b/test/test.js index 27bbece..468535f 100644 --- a/test/test.js +++ b/test/test.js @@ -13,16 +13,73 @@ describe('tab-container', function() { describe('after tree insertion', function() { beforeEach(function() { - document.body.innerHTML = 'Tabs go here' + document.body.innerHTML = ` + +
+ + + +
+
+ Panel 1 +
+ + +
+ ` }) afterEach(function() { document.body.innerHTML = '' }) - it('initiates', function() { - const ce = document.querySelector('tab-container') - assert.equal(ce.textContent, 'Tabs go here') + it('click works and event is dispatched', function() { + const tabContainer = document.querySelector('tab-container') + const tabs = document.querySelectorAll('button') + const panels = document.querySelectorAll('[role="tabpanel"]') + let counter = 0 + tabContainer.addEventListener('tab-container-change', event => { + counter++ + assert.equal(event.detail.relatedTarget, panels[1]) + }) + + tabs[1].click() + assert(panels[0].hidden) + assert(!panels[1].hidden) + assert.equal(counter, 1) + assert.equal(document.activeElement, tabs[1]) + }) + + it('keyboard shortcuts work and events are dispatched', function() { + const tabContainer = document.querySelector('tab-container') + const tabs = document.querySelectorAll('button') + const panels = document.querySelectorAll('[role="tabpanel"]') + let counter = 0 + tabContainer.addEventListener('tab-container-change', () => counter++) + + tabs[0].dispatchEvent(new KeyboardEvent('keydown', {code: 'ArrowLeft', bubbles: true})) + assert(panels[0].hidden) + assert(!panels[2].hidden) + assert.equal(document.activeElement, tabs[2]) + + tabs[0].dispatchEvent(new KeyboardEvent('keydown', {code: 'Home', bubbles: true})) + assert(!panels[0].hidden) + assert(panels[2].hidden) + assert.equal(document.activeElement, tabs[0]) + assert.equal(counter, 2) + }) + + it('selectTab(index) works', function() { + const tabContainer = document.querySelector('tab-container') + const panels = document.querySelectorAll('[role="tabpanel"]') + + tabContainer.selectTab(2) + assert(panels[0].hidden) + assert(!panels[2].hidden) }) }) })