Skip to content

Commit 5671271

Browse files
committed
fix: count up attach count object to each count up element similar to other blocks like carousel
1 parent fdf25d1 commit 5671271

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

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
}

0 commit comments

Comments
 (0)