Skip to content

Commit ff00f5b

Browse files
authored
Merge pull request #88 from github/add-tab-index-to-event
Add tab index to event object
2 parents 6e90dbc + becda14 commit ff00f5b

File tree

4 files changed

+42
-4
lines changed

4 files changed

+42
-4
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ If none of the tabs have `aria-selected=true`, then the first tab will be select
4444

4545
### Events
4646

47-
- `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.
48-
- `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.
47+
- `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.
48+
- `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.
4949

5050
### Parts
5151

custom-elements.json

+22
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@
117117
"name": "detail",
118118
"readonly": true
119119
},
120+
{
121+
"kind": "field",
122+
"name": "tabIndex",
123+
"readonly": true
124+
},
120125
{
121126
"kind": "field",
122127
"name": "panel",
@@ -317,6 +322,23 @@
317322
"name": "detail",
318323
"readonly": true
319324
},
325+
{
326+
"kind": "field",
327+
"name": "#tabIndex",
328+
"privacy": "private",
329+
"type": {
330+
"text": "number | null"
331+
},
332+
"default": "null"
333+
},
334+
{
335+
"kind": "field",
336+
"name": "tabIndex",
337+
"type": {
338+
"text": "number | null"
339+
},
340+
"readonly": true
341+
},
320342
{
321343
"kind": "field",
322344
"name": "#panel",

src/tab-container-element.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@ const HTMLElement = globalThis.HTMLElement || (null as unknown as (typeof window
22
const manualSlotsSupported = 'assign' in (globalThis.HTMLSlotElement?.prototype || {})
33

44
export class TabContainerChangeEvent extends Event {
5-
constructor(type: string, {tab, panel, ...init}: EventInit & {tab?: Element; panel?: Element}) {
5+
constructor(
6+
type: string,
7+
{tabIndex, tab, panel, ...init}: EventInit & {tabIndex?: number; tab?: Element; panel?: Element},
8+
) {
69
super(type, init)
710
this.#tab = tab || null
11+
this.#tabIndex = tabIndex || null
812
this.#panel = panel || null
913
}
1014

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

21+
#tabIndex: number | null = null
22+
get tabIndex(): number | null {
23+
return this.#tabIndex
24+
}
25+
1726
#panel: Element | null = null
1827
get panel(): Element | null {
1928
return this.#panel
@@ -341,6 +350,7 @@ export class TabContainerElement extends HTMLElement {
341350
if (this.#setupComplete) {
342351
const cancelled = !this.dispatchEvent(
343352
new TabContainerChangeEvent('tab-container-change', {
353+
tabIndex: index,
344354
bubbles: true,
345355
cancelable: true,
346356
tab: selectedTab,
@@ -376,6 +386,7 @@ export class TabContainerElement extends HTMLElement {
376386
selectedTab.focus()
377387
this.dispatchEvent(
378388
new TabContainerChangeEvent('tab-container-changed', {
389+
tabIndex: index,
379390
bubbles: true,
380391
tab: selectedTab,
381392
panel: selectedPanel,

test/test.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ describe('tab-container', function () {
186186
expect(document.body).to.be.accessible()
187187
})
188188

189-
it('click works and `tab-container-changed` event is dispatched', function () {
189+
it('click works and `tab-container-changed` event is dispatched with correct index', function () {
190190
tabs[1].click()
191191
assert.deepStrictEqual(tabs.map(isSelected), [false, true, false], 'Second tab is selected')
192192
assert.deepStrictEqual(panels.map(isHidden), [true, false, true], 'Second panel is visible')
@@ -201,6 +201,11 @@ describe('tab-container', function () {
201201
[tabs[1], tabs[1]],
202202
'change events point to second tab',
203203
)
204+
assert.deepStrictEqual(
205+
events.map(e => e.tabIndex),
206+
[1, 1],
207+
'change events point to second tabIndex',
208+
)
204209
assert.deepStrictEqual(
205210
events.map(e => e.panel),
206211
[panels[1], panels[1]],

0 commit comments

Comments
 (0)