@@ -10,7 +10,6 @@ import React, {
10
10
} from 'react'
11
11
12
12
import mojs from 'mo-js'
13
- import wordConverter from 'number-to-words'
14
13
import { generateRandomNumber } from '../utils/generateRandomNumber'
15
14
import styles from './index.css'
16
15
import userStyles from './usage.css'
@@ -30,96 +29,93 @@ const useClapAnimation = ({
30
29
new mojs . Timeline ( )
31
30
)
32
31
33
- useLayoutEffect (
34
- ( ) => {
35
- if ( ! bounceEl || ! fadeEl || ! burstEl ) {
36
- return
37
- }
32
+ useLayoutEffect ( ( ) => {
33
+ if ( ! bounceEl || ! fadeEl || ! burstEl ) {
34
+ return
35
+ }
38
36
39
- const triangleBurst = new mojs . Burst ( {
40
- parent : burstEl ,
41
- radius : { 50 : 95 } ,
42
- count : 5 ,
43
- angle : 30 ,
44
- children : {
45
- shape : 'polygon' ,
46
- radius : { 6 : 0 } ,
47
- scale : 1 ,
48
- stroke : 'rgba(211,84,0 ,0.5)' ,
49
- strokeWidth : 2 ,
50
- angle : 210 ,
51
- delay : 30 ,
52
- speed : 0.2 ,
53
- easing : mojs . easing . bezier ( 0.1 , 1 , 0.3 , 1 ) ,
54
- duration : tlDuration
55
- }
56
- } )
57
-
58
- const circleBurst = new mojs . Burst ( {
59
- parent : burstEl ,
60
- radius : { 50 : 75 } ,
61
- angle : 25 ,
62
- duration : tlDuration ,
63
- children : {
64
- shape : 'circle' ,
65
- fill : 'rgba(149,165,166 ,0.5)' ,
66
- delay : 30 ,
67
- speed : 0.2 ,
68
- radius : { 3 : 0 } ,
69
- easing : mojs . easing . bezier ( 0.1 , 1 , 0.3 , 1 )
70
- }
71
- } )
72
-
73
- const countAnimation = new mojs . Html ( {
74
- el : bounceEl ,
75
- isShowStart : false ,
76
- isShowEnd : true ,
77
- y : { 0 : - 30 } ,
78
- opacity : { 0 : 1 } ,
37
+ const triangleBurst = new mojs . Burst ( {
38
+ parent : burstEl ,
39
+ radius : { 50 : 95 } ,
40
+ count : 5 ,
41
+ angle : 30 ,
42
+ children : {
43
+ shape : 'polygon' ,
44
+ radius : { 6 : 0 } ,
45
+ scale : 1 ,
46
+ stroke : 'rgba(211,84,0 ,0.5)' ,
47
+ strokeWidth : 2 ,
48
+ angle : 210 ,
49
+ delay : 30 ,
50
+ speed : 0.2 ,
51
+ easing : mojs . easing . bezier ( 0.1 , 1 , 0.3 , 1 ) ,
79
52
duration : tlDuration
80
- } ) . then ( {
81
- opacity : { 1 : 0 } ,
82
- y : - 80 ,
83
- delay : tlDuration / 2
84
- } )
85
-
86
- const countTotalAnimation = new mojs . Html ( {
87
- el : fadeEl ,
88
- isShowStart : false ,
89
- isShowEnd : true ,
90
- opacity : { 0 : 1 } ,
91
- delay : ( 3 * tlDuration ) / 2 ,
92
- duration : tlDuration ,
93
- y : { 0 : - 3 }
94
- } )
95
-
96
- const scaleButton = new mojs . Html ( {
97
- el : burstEl ,
98
- duration : tlDuration ,
99
- scale : { 1.3 : 1 } ,
100
- easing : mojs . easing . out
101
- } )
102
-
103
- if ( typeof burstEl === 'string' ) {
104
- clap . style . transform = 'scale(1, 1)'
105
- const el = document . getElementById ( id )
106
- el . style . transform = 'scale(1, 1)'
107
- } else {
108
- burstEl . style . transform = 'scale(1, 1)'
109
53
}
54
+ } )
110
55
111
- const updatedAnimationTimeline = animationTimeline . add ( [
112
- countAnimation ,
113
- countTotalAnimation ,
114
- scaleButton ,
115
- circleBurst ,
116
- triangleBurst
117
- ] )
118
-
119
- setAnimationTimeline ( updatedAnimationTimeline )
120
- } ,
121
- [ tlDuration , animationTimeline , bounceEl , fadeEl , burstEl ]
122
- )
56
+ const circleBurst = new mojs . Burst ( {
57
+ parent : burstEl ,
58
+ radius : { 50 : 75 } ,
59
+ angle : 25 ,
60
+ duration : tlDuration ,
61
+ children : {
62
+ shape : 'circle' ,
63
+ fill : 'rgba(149,165,166 ,0.5)' ,
64
+ delay : 30 ,
65
+ speed : 0.2 ,
66
+ radius : { 3 : 0 } ,
67
+ easing : mojs . easing . bezier ( 0.1 , 1 , 0.3 , 1 )
68
+ }
69
+ } )
70
+
71
+ const countAnimation = new mojs . Html ( {
72
+ el : bounceEl ,
73
+ isShowStart : false ,
74
+ isShowEnd : true ,
75
+ y : { 0 : - 30 } ,
76
+ opacity : { 0 : 1 } ,
77
+ duration : tlDuration
78
+ } ) . then ( {
79
+ opacity : { 1 : 0 } ,
80
+ y : - 80 ,
81
+ delay : tlDuration / 2
82
+ } )
83
+
84
+ const countTotalAnimation = new mojs . Html ( {
85
+ el : fadeEl ,
86
+ isShowStart : false ,
87
+ isShowEnd : true ,
88
+ opacity : { 0 : 1 } ,
89
+ delay : ( 3 * tlDuration ) / 2 ,
90
+ duration : tlDuration ,
91
+ y : { 0 : - 3 }
92
+ } )
93
+
94
+ const scaleButton = new mojs . Html ( {
95
+ el : burstEl ,
96
+ duration : tlDuration ,
97
+ scale : { 1.3 : 1 } ,
98
+ easing : mojs . easing . out
99
+ } )
100
+
101
+ if ( typeof burstEl === 'string' ) {
102
+ clap . style . transform = 'scale(1, 1)'
103
+ const el = document . getElementById ( id )
104
+ el . style . transform = 'scale(1, 1)'
105
+ } else {
106
+ burstEl . style . transform = 'scale(1, 1)'
107
+ }
108
+
109
+ const updatedAnimationTimeline = animationTimeline . add ( [
110
+ countAnimation ,
111
+ countTotalAnimation ,
112
+ scaleButton ,
113
+ circleBurst ,
114
+ triangleBurst
115
+ ] )
116
+
117
+ setAnimationTimeline ( updatedAnimationTimeline )
118
+ } , [ tlDuration , animationTimeline , bounceEl , fadeEl , burstEl ] )
123
119
124
120
return animationTimeline
125
121
}
@@ -174,24 +170,19 @@ const MediumClap = ({
174
170
175
171
const componentJustMounted = useRef ( true )
176
172
177
- useEffect (
178
- ( ) => {
179
- if ( ! componentJustMounted . current ) {
180
- onClap ( clapState )
181
- }
182
- componentJustMounted . current = false
183
- } ,
184
- [ count , onClap ]
185
- )
173
+ useEffect ( ( ) => {
174
+ if ( ! componentJustMounted . current ) {
175
+ onClap ( clapState )
176
+ }
177
+ componentJustMounted . current = false
178
+ } , [ count , onClap ] )
186
179
187
180
const memoizedValue = useMemo (
188
181
( ) => ( {
189
- count,
190
- countTotal,
191
- isClicked,
182
+ ...clapState ,
192
183
setRef
193
184
} ) ,
194
- [ count , countTotal , isClicked , setRef ]
185
+ [ clapState , setRef ]
195
186
)
196
187
197
188
const classNames = [ styles . clap , className ] . join ( ' ' ) . trim ( )
@@ -269,25 +260,18 @@ const CountTotal = ({ className = '', style: userStyles = {} }) => {
269
260
)
270
261
}
271
262
272
- const ClapInfo = ( { info } ) => {
273
- const { countTotal } = useContext ( MediumClapContext )
274
- return (
275
- < div className = { styles . info } >
276
- { info || wordConverter . toWords ( countTotal ) } claps!
277
- </ div >
278
- )
279
- }
280
-
281
263
MediumClap . Icon = ClapIcon
282
264
MediumClap . Count = ClapCount
283
265
MediumClap . Total = CountTotal
284
- MediumClap . Info = ClapInfo
285
266
286
267
/** ====================================
287
268
* 🔰USAGE
288
269
Below's how a potential user
289
270
may consume the component API
290
271
==================================== **/
272
+ const Info = ( { info } ) => {
273
+ return < div className = { styles . info } > { info } </ div >
274
+ }
291
275
292
276
const Usage = ( ) => {
293
277
const [ total , setTotal ] = useState ( 0 )
@@ -297,12 +281,16 @@ const Usage = () => {
297
281
}
298
282
299
283
return (
300
- < MediumClap onClap = { onClap } className = { userStyles . clap } >
301
- < MediumClap . Icon className = { userStyles . icon } />
302
- < MediumClap . Total className = { userStyles . total } />
303
- < MediumClap . Count className = { userStyles . count } />
304
- < MediumClap . Info info = { `Your article has been clapped ${ total } times` } />
305
- </ MediumClap >
284
+ < div style = { { width : '100%' } } >
285
+ < MediumClap onClap = { onClap } className = { userStyles . clap } >
286
+ < MediumClap . Icon className = { userStyles . icon } />
287
+ < MediumClap . Total className = { userStyles . total } />
288
+ < MediumClap . Count className = { userStyles . count } />
289
+ </ MediumClap >
290
+ { ! ! total && (
291
+ < Info info = { `Your article has been clapped ${ total } times` } />
292
+ ) }
293
+ </ div >
306
294
)
307
295
}
308
296
0 commit comments