@@ -9,7 +9,11 @@ import counterUp from 'counterup2'
99import domReady from '@wordpress/dom-ready'
1010
1111const 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