Skip to content

Commit 726a7ff

Browse files
author
yashksaini-coder
committed
feat(app): Add react-slot-counter animation to LandingCommunity Stats
Signed-off-by: yashksaini-coder <[email protected]>
1 parent cd3611b commit 726a7ff

File tree

3 files changed

+41
-37
lines changed

3 files changed

+41
-37
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"react": "^18.0.0",
4545
"react-dom": "^18.0.0",
4646
"react-icons": "^5.5.0",
47+
"react-slot-counter": "^3.3.1",
4748
"rehype-katex": "^7.0.1",
4849
"remark-math": "^6.0.0",
4950
"styled-components": "^6.1.18",

src/components/Community/LandingCommunity.css

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,21 @@
8989
position: relative;
9090
}
9191

92+
/* SlotCounter styling */
93+
.landing-community .slot-counter-number {
94+
font-size: inherit;
95+
font-weight: inherit;
96+
color: inherit;
97+
text-shadow: inherit;
98+
}
99+
100+
.landing-community .slot-counter-separator {
101+
font-size: inherit;
102+
font-weight: inherit;
103+
color: inherit;
104+
text-shadow: inherit;
105+
}
106+
92107
.landing-community .landing-community__content .landing-community__stats .landing-community__stat-item .landing-community__loading {
93108
display: flex;
94109
align-items: center;

src/components/Community/index.tsx

Lines changed: 25 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React, { type FC, useEffect, useState, useMemo } from "react";
2+
import SlotCounter from 'react-slot-counter';
23
import "./LandingCommunity.css";
34
import { useCommunityStatsContext } from "@site/src/lib/statsProvider";
45

@@ -12,74 +13,52 @@ export const LandingCommunity: FC<Props> = ({ className }) => {
1213
githubContributorsCountText,
1314
githubForksCountText,
1415
githubReposCountText,
16+
githubStarCount,
17+
githubContributorsCount,
18+
githubForksCount,
19+
githubReposCount,
1520
loading,
1621
error
1722
} = useCommunityStatsContext();
1823

19-
const [state, setState] = useState({
20-
stat0: 0,
21-
stat1: 0,
22-
stat2: 0,
23-
stat3: 0,
24-
});
25-
24+
2625
const generateList = useMemo(() => [
2726
{
28-
stat: githubStarCountText,
27+
stat: githubStarCount,
28+
statText: githubStarCountText,
2929
description: "Stars across all our GitHub repositories, showcasing the support and appreciation from the community.",
3030
href: "https://github.com/recodehive/Support",
3131
label: "GitHub Stars"
3232
},
3333
{
34-
stat: githubReposCountText,
34+
stat: githubReposCount,
35+
statText: githubReposCountText,
3536
description: "Live public projects on RecodHive, demonstrating the power of open-source collaboration.",
3637
href: "https://github.com/orgs/recodehive/repositories?q=visibility%3Apublic+archived%3Afalse",
3738
label: "Public Repositories"
3839
},
3940
{
40-
stat: githubContributorsCountText,
41+
stat: githubContributorsCount,
42+
statText: githubContributorsCountText,
4143
description: "Amazing contributors who have made our repositories better and helped build our community.",
4244
href: "https://github.com/orgs/recodehive/people",
4345
label: "Contributors"
4446
},
4547
{
46-
stat: githubForksCountText,
48+
stat: githubForksCount,
49+
statText: githubForksCountText,
4750
description: "Forks of our repositories, showing how our community extends and builds upon our work.",
4851
href: "https://github.com/orgs/recodehive/discussions",
4952
label: "Community Forks"
5053
},
51-
], [githubStarCountText, githubReposCountText, githubContributorsCountText, githubForksCountText]);
52-
53-
const handleDynamicChange = (target: number, index: number) => {
54-
let count = 0;
55-
const increment = target / 100;
56-
const interval = setInterval(() => {
57-
count += increment;
58-
setState(prev => ({ ...prev, [`stat${index}`]: Math.round(count) }));
59-
if (count >= target) {
60-
setState(prev => ({ ...prev, [`stat${index}`]: target }));
61-
clearInterval(interval);
62-
}
63-
}, 20);
64-
};
54+
], [githubStarCount, githubStarCountText, githubReposCount, githubReposCountText, githubContributorsCount, githubContributorsCountText, githubForksCount, githubForksCountText]);
6555

6656
const handleCardClick = (href: string) => {
6757
if (href) {
6858
window.open(href, '_blank', 'noopener,noreferrer');
6959
}
7060
};
7161

72-
useEffect(() => {
73-
if (!loading) {
74-
generateList.forEach((item, index) => {
75-
const numericStat = typeof item.stat === 'string' ?
76-
parseInt(item.stat.replace(/[^\d]/g, '')) || 0 :
77-
Number(item.stat);
78-
handleDynamicChange(numericStat, index);
79-
});
80-
}
81-
}, [generateList, loading]);
82-
8362
return (
8463
<div className={`landing-community ${className || ""}`}>
8564
<div className="landing-community__header">
@@ -118,7 +97,16 @@ export const LandingCommunity: FC<Props> = ({ className }) => {
11897
</div>
11998
) : (
12099
<span>
121-
{item.stat}
100+
<SlotCounter
101+
value={item.stat}
102+
duration={2}
103+
animateOnVisible={{
104+
triggerOnce: true,
105+
rootMargin: '0px 0px -100px 0px'
106+
}}
107+
numberSlotClassName="slot-counter-number"
108+
separatorClassName="slot-counter-separator"
109+
/>
122110
{item.href && <span className="external-link-icon"></span>}
123111
</span>
124112
)}

0 commit comments

Comments
 (0)