@@ -14,16 +14,16 @@ class TabContainerElement extends HTMLElement {
1414 if ( event . code === 'ArrowRight' ) {
1515 let index = currentIndex + 1
1616 if ( index >= tabs . length ) index = 0
17- this . selectTab ( index )
17+ selectTab ( this , index )
1818 } else if ( event . code === 'ArrowLeft' ) {
1919 let index = currentIndex - 1
2020 if ( index < 0 ) index = tabs . length - 1
21- this . selectTab ( index )
21+ selectTab ( this , index )
2222 } else if ( event . code === 'Home' ) {
23- this . selectTab ( 0 )
23+ selectTab ( this , 0 )
2424 event . preventDefault ( )
2525 } else if ( event . code === 'End' ) {
26- this . selectTab ( tabs . length - 1 )
26+ selectTab ( this , tabs . length - 1 )
2727 event . preventDefault ( )
2828 }
2929 } )
@@ -36,38 +36,38 @@ class TabContainerElement extends HTMLElement {
3636 if ( ! tab || ! tab . closest ( '[role="tablist"]' ) ) return
3737
3838 const index = tabs . indexOf ( tab )
39- this . selectTab ( index )
39+ selectTab ( this , index )
4040 } )
4141 }
42+ }
4243
43- selectTab ( index : number ) {
44- const tabs = this . querySelectorAll ( '[role="tablist"] [role="tab"]' )
45- const panels = this . querySelectorAll ( '[role="tabpanel"]' )
44+ function selectTab ( tabContainer : TabContainerElement , index : number ) {
45+ const tabs = tabContainer . querySelectorAll ( '[role="tablist"] [role="tab"]' )
46+ const panels = tabContainer . querySelectorAll ( '[role="tabpanel"]' )
4647
47- for ( const tab of tabs ) {
48- tab . setAttribute ( 'aria-selected' , 'false' )
49- tab . setAttribute ( 'tabindex' , '-1' )
50- }
51- for ( const panel of panels ) {
52- panel . hidden = true
53- panel . setAttribute ( 'tabindex' , '0' )
54- }
48+ for ( const tab of tabs ) {
49+ tab . setAttribute ( 'aria-selected' , 'false' )
50+ tab . setAttribute ( 'tabindex' , '-1' )
51+ }
52+ for ( const panel of panels ) {
53+ panel . hidden = true
54+ panel . setAttribute ( 'tabindex' , '0' )
55+ }
5556
56- const tab = tabs [ index ]
57- const panel = panels [ index ]
57+ const tab = tabs [ index ]
58+ const panel = panels [ index ]
5859
59- tab . setAttribute ( 'aria-selected' , 'true' )
60- tab . removeAttribute ( 'tabindex' )
61- tab . focus ( )
62- panel . hidden = false
60+ tab . setAttribute ( 'aria-selected' , 'true' )
61+ tab . removeAttribute ( 'tabindex' )
62+ tab . focus ( )
63+ panel . hidden = false
6364
64- this . dispatchEvent (
65- new CustomEvent ( 'tab-container-change' , {
66- bubbles : true ,
67- detail : { relatedTarget : panel }
68- } )
69- )
70- }
65+ tabContainer . dispatchEvent (
66+ new CustomEvent ( 'tab-container-change' , {
67+ bubbles : true ,
68+ detail : { relatedTarget : panel }
69+ } )
70+ )
7171}
7272
7373if ( ! window . customElements . get ( 'tab-container' ) ) {
0 commit comments