Skip to content

Commit f96c7ee

Browse files
authored
feat: integration with WP Interactions
* fix: carousel dispatch event when changing slide * fix: tabs dispatch event when changing tab * fix: count up attach count object to each count up element similar to other blocks like carousel
1 parent 72edbd0 commit f96c7ee

File tree

3 files changed

+38
-8
lines changed

3 files changed

+38
-8
lines changed

src/block/carousel/frontend-carousel.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,14 @@ class _StackableCarousel {
320320
this.setDotActive( slide )
321321
this.currentSlide = slide
322322

323+
// Dispatch an event when the carousel change slide
324+
this.el.dispatchEvent( new CustomEvent( 'stackable-carousel-slide-change', {
325+
detail: {
326+
element: this.el,
327+
currentSlide: slide,
328+
},
329+
} ) )
330+
323331
try {
324332
this.liveregion.textContent = this.sliderEl.dataset.labelSlideOf.replace( /%+d/, slide ).replace( /%+d/, this.maxSlides() )
325333
} catch ( err ) {

src/block/count-up/frontend-count-up.js

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ import counterUp from 'counterup2'
99
import domReady from '@wordpress/dom-ready'
1010

1111
const ACTIVE = 'stk--count-up-active'
12-
class StackableCountUp {
12+
class _StackableCountUp {
13+
constructor( el ) {
14+
this.el = el
15+
}
16+
1317
callback = entries => {
1418
entries.forEach( entry => {
1519
const el = entry.target
@@ -25,15 +29,12 @@ class StackableCountUp {
2529
}
2630

2731
init = () => {
28-
const els = document.querySelectorAll( '.stk-block-count-up__text' )
29-
32+
this.el.classList.remove( ACTIVE )
3033
// If reduce motion is on, don't animate.
3134
const reduceMotion = window.matchMedia( '(prefers-reduced-motion: reduce)' ).matches
3235
// If IntersectionObserver is not supported, just show the blocks.
3336
if ( ! ( 'IntersectionObserver' in window ) || reduceMotion ) {
34-
els.forEach( el => {
35-
el.classList.add( ACTIVE )
36-
} )
37+
this.el.classList.add( ACTIVE )
3738
return
3839
}
3940

@@ -43,8 +44,20 @@ class StackableCountUp {
4344
// Don't use threshold 1 because if a small part of the text is hidden,
4445
// the IO won't trigger.
4546
this.io = new IntersectionObserver( this.callback, { threshold: 0.25 } ) // eslint-disable-line compat/compat
47+
this.io.observe( this.el )
48+
}
49+
}
50+
51+
class StackableCountUp {
52+
init = () => {
53+
const els = document.querySelectorAll( '.stk-block-count-up__text' )
4654
els.forEach( el => {
47-
this.io.observe( el )
55+
if ( ! el._StackableHasInitCountUp ) {
56+
const countUp = new _StackableCountUp( el )
57+
el.countUp = countUp
58+
countUp.init()
59+
el.StackableHasInitCountUp = true
60+
}
4861
} )
4962
}
5063
}

src/block/tabs/frontend-tabs.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,14 @@ class _StackableTabs {
127127
} )
128128

129129
this.activeTab = tabIndex
130+
131+
// Dispatch an event when the tab is changed
132+
this.parentEl.dispatchEvent( new CustomEvent( 'stackable-tabs-change', {
133+
detail: {
134+
element: this.parentEl,
135+
activeTab: tabIndex,
136+
},
137+
} ) )
130138
}
131139

132140
initWindowEventListeners = () => {
@@ -157,7 +165,8 @@ class StackableTabs {
157165
document.querySelectorAll( '.stk-block-tabs' )
158166
.forEach( el => {
159167
if ( ! el._StackableHasInitTabs ) {
160-
new _StackableTabs( el )
168+
const tabs = new _StackableTabs( el )
169+
el.tabs = tabs
161170
el._StackableHasInitTabs = true
162171
}
163172
} )

0 commit comments

Comments
 (0)