| 1 |
import { register } from '@elementor/frontend-handlers'; |
| 2 |
import { Alpine } from '@elementor/alpinejs'; |
| 3 |
import { TAB_ELEMENT_TYPE, TAB_CONTENT_ELEMENT_TYPE, getTabId, getTabContentId, getIndex, getNextTab } from './utils'; |
| 4 |
|
| 5 |
const SELECTED_CLASS = 'e--selected'; |
| 6 |
|
| 7 |
register( { |
| 8 |
elementType: 'e-tabs', |
| 9 |
id: 'e-tabs-handler', |
| 10 |
callback: ( { element, settings } ) => { |
| 11 |
const tabsId = element.dataset.id; |
| 12 |
|
| 13 |
Alpine.data( `eTabs${ tabsId }`, () => ( { |
| 14 |
activeTab: settings[ 'default-active-tab' ], |
| 15 |
|
| 16 |
navigateTabs( { key, target: tab } ) { |
| 17 |
const nextTab = getNextTab( key, tab ); |
| 18 |
|
| 19 |
nextTab.focus(); |
| 20 |
}, |
| 21 |
tab: { |
| 22 |
':id'() { |
| 23 |
const index = getIndex( this.$el, TAB_ELEMENT_TYPE ); |
| 24 |
|
| 25 |
return getTabId( tabsId, index ); |
| 26 |
}, |
| 27 |
'@click'() { |
| 28 |
const id = this.$el.id; |
| 29 |
|
| 30 |
this.activeTab = id; |
| 31 |
}, |
| 32 |
'@keydown.arrow-right.prevent'( event ) { |
| 33 |
this.navigateTabs( event ); |
| 34 |
}, |
| 35 |
'@keydown.arrow-left.prevent'( event ) { |
| 36 |
this.navigateTabs( event ); |
| 37 |
}, |
| 38 |
':class'() { |
| 39 |
const id = this.$el.id; |
| 40 |
|
| 41 |
return { [ SELECTED_CLASS ]: this.activeTab === id }; |
| 42 |
}, |
| 43 |
':aria-selected'() { |
| 44 |
const id = this.$el.id; |
| 45 |
|
| 46 |
return this.activeTab === id ? 'true' : 'false'; |
| 47 |
}, |
| 48 |
':tabindex'() { |
| 49 |
const id = this.$el.id; |
| 50 |
|
| 51 |
return this.activeTab === id ? '0' : '-1'; |
| 52 |
}, |
| 53 |
':aria-controls'() { |
| 54 |
const index = getIndex( this.$el, TAB_ELEMENT_TYPE ); |
| 55 |
|
| 56 |
return getTabContentId( tabsId, index ); |
| 57 |
}, |
| 58 |
}, |
| 59 |
|
| 60 |
tabContent: { |
| 61 |
':aria-labelledby'() { |
| 62 |
const index = getIndex( this.$el, TAB_CONTENT_ELEMENT_TYPE ); |
| 63 |
|
| 64 |
return getTabId( tabsId, index ); |
| 65 |
}, |
| 66 |
'x-show'() { |
| 67 |
const index = getIndex( this.$el, TAB_CONTENT_ELEMENT_TYPE ); |
| 68 |
const tabId = getTabId( tabsId, index ); |
| 69 |
|
| 70 |
const isActive = this.activeTab === tabId; |
| 71 |
|
| 72 |
this.$nextTick( () => { |
| 73 |
this.$el.classList.toggle( SELECTED_CLASS, isActive ); |
| 74 |
} ); |
| 75 |
|
| 76 |
return isActive; |
| 77 |
}, |
| 78 |
':id'() { |
| 79 |
const index = getIndex( this.$el, TAB_CONTENT_ELEMENT_TYPE ); |
| 80 |
|
| 81 |
return getTabContentId( tabsId, index ); |
| 82 |
}, |
| 83 |
}, |
| 84 |
} ) ); |
| 85 |
}, |
| 86 |
} ); |
| 87 |
|
| 88 |
|