-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcount-up.templ
44 lines (40 loc) · 895 Bytes
/
count-up.templ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package components
import (
"strconv"
)
type CountUpComponentProps struct {
From int
To int
Class string
}
/**
* Count Up Component.
* Gradually counts up FROM to TO.
*
* @params CountUpComponentProps
* @author: github.com/tego101 <-> x.com/tegodotdev
* @license: MIT
*
* Usage:
* @components.CountUp(components.CountUpComponentProps{
* From: 0,
* To: 100,
* Class: "any custom css"
* })
*/
templ CountUp(props CountUpComponentProps) {
<div
id={ "counter-" + components.UniqueKey() }
x-data={ "{ count: " + strconv.Itoa(props.From) + ", target: " + strconv.Itoa(props.To) + "}" }
x-init="intervalId = setInterval(() => {
if (count < target) {
count++;
} else {
clearInterval(intervalId); // Clear the interval when target is reached
}
}, 50)"
class={ props.Class }
>
<h1 x-text="count"></h1>
</div>
}