Skip to content

Commit

Permalink
Merge pull request #88 from github/add-tab-index-to-event
Browse files Browse the repository at this point in the history
Add tab index to event object
  • Loading branch information
owenniblock authored Mar 12, 2024
2 parents 6e90dbc + becda14 commit ff00f5b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ If none of the tabs have `aria-selected=true`, then the first tab will be select

### Events

- `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.
- `tab-container-changed` (bubbles): fired on `<tab-container>` after a new tab is selected and visibility is updated. `event.tab` is the tab that is now active (and will be focused right after this event) and `event.panel` is the newly visible tab panel.
- `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, `event.tabIndex` is the 0-based index of the `tab` and `tab.panel` is the panel that will be shown if the event isn't cancelled.
- `tab-container-changed` (bubbles): fired on `<tab-container>` after a new tab is selected and visibility is updated. `event.tab` is the tab that is now active (and will be focused right after this event), `event.tabIndex` is the 0-based index of the `tab` and `event.panel` is the newly visible tab panel.

### Parts

Expand Down
22 changes: 22 additions & 0 deletions custom-elements.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@
"name": "detail",
"readonly": true
},
{
"kind": "field",
"name": "tabIndex",
"readonly": true
},
{
"kind": "field",
"name": "panel",
Expand Down Expand Up @@ -317,6 +322,23 @@
"name": "detail",
"readonly": true
},
{
"kind": "field",
"name": "#tabIndex",
"privacy": "private",
"type": {
"text": "number | null"
},
"default": "null"
},
{
"kind": "field",
"name": "tabIndex",
"type": {
"text": "number | null"
},
"readonly": true
},
{
"kind": "field",
"name": "#panel",
Expand Down
13 changes: 12 additions & 1 deletion src/tab-container-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ const HTMLElement = globalThis.HTMLElement || (null as unknown as (typeof window
const manualSlotsSupported = 'assign' in (globalThis.HTMLSlotElement?.prototype || {})

export class TabContainerChangeEvent extends Event {
constructor(type: string, {tab, panel, ...init}: EventInit & {tab?: Element; panel?: Element}) {
constructor(
type: string,
{tabIndex, tab, panel, ...init}: EventInit & {tabIndex?: number; tab?: Element; panel?: Element},
) {
super(type, init)
this.#tab = tab || null
this.#tabIndex = tabIndex || null
this.#panel = panel || null
}

Expand All @@ -14,6 +18,11 @@ export class TabContainerChangeEvent extends Event {
return {relatedTarget: this.#panel}
}

#tabIndex: number | null = null
get tabIndex(): number | null {
return this.#tabIndex
}

#panel: Element | null = null
get panel(): Element | null {
return this.#panel
Expand Down Expand Up @@ -341,6 +350,7 @@ export class TabContainerElement extends HTMLElement {
if (this.#setupComplete) {
const cancelled = !this.dispatchEvent(
new TabContainerChangeEvent('tab-container-change', {
tabIndex: index,
bubbles: true,
cancelable: true,
tab: selectedTab,
Expand Down Expand Up @@ -376,6 +386,7 @@ export class TabContainerElement extends HTMLElement {
selectedTab.focus()
this.dispatchEvent(
new TabContainerChangeEvent('tab-container-changed', {
tabIndex: index,
bubbles: true,
tab: selectedTab,
panel: selectedPanel,
Expand Down
7 changes: 6 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ describe('tab-container', function () {
expect(document.body).to.be.accessible()
})

it('click works and `tab-container-changed` event is dispatched', function () {
it('click works and `tab-container-changed` event is dispatched with correct index', function () {
tabs[1].click()
assert.deepStrictEqual(tabs.map(isSelected), [false, true, false], 'Second tab is selected')
assert.deepStrictEqual(panels.map(isHidden), [true, false, true], 'Second panel is visible')
Expand All @@ -201,6 +201,11 @@ describe('tab-container', function () {
[tabs[1], tabs[1]],
'change events point to second tab',
)
assert.deepStrictEqual(
events.map(e => e.tabIndex),
[1, 1],
'change events point to second tabIndex',
)
assert.deepStrictEqual(
events.map(e => e.panel),
[panels[1], panels[1]],
Expand Down

0 comments on commit ff00f5b

Please sign in to comment.