Skip to content

Commit f45f4c2

Browse files
committed
style: enhance GitHub star counter with modern design and better formatting
1 parent 6596178 commit f45f4c2

File tree

1 file changed

+56
-31
lines changed

1 file changed

+56
-31
lines changed

src/components/GitHubStarCounter.tsx

+56-31
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useState, useEffect } from 'react';
22
import { GithubOutlined, StarFilled } from '@ant-design/icons';
3-
import { Tooltip, Badge } from 'antd';
3+
import { Tooltip, Spin } from 'antd';
44

55
interface GitHubStarCounterProps {
66
owner: string;
@@ -87,47 +87,69 @@ const GitHubStarCounter: React.FC<GitHubStarCounterProps> = ({
8787
fetchStarCount();
8888
}, [owner, repo, cacheTime]);
8989

90-
// 容器样式
90+
// 格式化星星数量,大于1000显示为1k+的形式
91+
const formatStarCount = (count: number): string => {
92+
if (count >= 1000) {
93+
const formattedCount = (count / 1000).toFixed(1);
94+
// 如果小数点后是0,则不显示小数点
95+
return formattedCount.endsWith('.0')
96+
? `${formattedCount.slice(0, -2)}k`
97+
: `${formattedCount}k`;
98+
}
99+
return count.toString();
100+
};
101+
102+
// 美化后的容器样式
91103
const containerStyle = {
92104
display: 'inline-flex',
93105
alignItems: 'center',
94106
justifyContent: 'center',
95107
fontSize: '13px',
96-
color: '#333',
108+
color: '#24292e',
97109
cursor: 'pointer',
98-
transition: 'transform 0.2s ease, color 0.2s ease',
99-
height: '100%',
100-
borderRadius: '4px',
110+
transition: 'all 0.3s cubic-bezier(0.4, 0, 0.2, 1)',
111+
height: '28px',
112+
borderRadius: '15px',
101113
textDecoration: 'none',
102-
padding: '0 8px',
103-
fontWeight: 600,
114+
padding: '0 12px',
115+
fontWeight: 500,
116+
border: '1px solid rgba(36, 41, 46, 0.1)',
117+
background: 'linear-gradient(to bottom, #fafbfc, #f6f8fa)',
118+
boxShadow: '0 1px 2px rgba(0, 0, 0, 0.05)',
119+
};
120+
121+
// 悬停样式
122+
const hoverStyle = {
123+
background: 'linear-gradient(to bottom, #f6f8fa, #e1e4e8)',
124+
boxShadow: '0 1px 3px rgba(0, 0, 0, 0.1)',
125+
transform: 'translateY(-1px)',
126+
borderColor: 'rgba(36, 41, 46, 0.2)',
104127
};
105128

106129
// GitHub图标样式
107130
const githubIconStyle = {
108-
fontSize: '22px',
131+
fontSize: '16px',
109132
color: '#24292e',
110-
marginRight: '2px',
111-
verticalAlign: 'middle'
133+
marginRight: '4px',
112134
};
113135

114136
// Star图标样式
115137
const starIconStyle = {
116138
fontSize: '14px',
117-
color: '#ff9800',
118-
marginRight: '3px',
139+
color: '#f1c40f',
140+
marginRight: '4px',
141+
filter: 'drop-shadow(0px 1px 1px rgba(0, 0, 0, 0.1))',
142+
};
143+
144+
// 数字样式
145+
const countStyle = {
146+
fontWeight: 600,
147+
color: '#24292e',
119148
};
120149

121-
// 徽章样式
122-
const badgeStyle = {
123-
backgroundColor: '#24292e',
124-
color: 'white',
125-
fontSize: '12px',
126-
padding: '1px 6px',
127-
borderRadius: '10px',
128-
fontWeight: 'bold',
129-
marginLeft: '4px',
130-
display: 'inline-block',
150+
// 加载动画样式
151+
const spinnerStyle = {
152+
margin: '0 5px',
131153
};
132154

133155
return (
@@ -138,19 +160,22 @@ const GitHubStarCounter: React.FC<GitHubStarCounterProps> = ({
138160
rel="noopener noreferrer"
139161
style={containerStyle}
140162
onMouseOver={(e) => {
141-
e.currentTarget.style.color = '#1890ff';
142-
e.currentTarget.style.transform = 'scale(1.05)';
163+
Object.assign(e.currentTarget.style, hoverStyle);
143164
}}
144165
onMouseOut={(e) => {
145-
e.currentTarget.style.color = '#333';
146-
e.currentTarget.style.transform = 'scale(1)';
166+
// 重置为原始样式
167+
Object.assign(e.currentTarget.style, containerStyle);
147168
}}
148169
>
149170
<GithubOutlined style={githubIconStyle} />
150-
<div style={{ display: 'flex', alignItems: 'center', marginLeft: '5px' }}>
151-
<StarFilled style={starIconStyle} />
152-
<span>{loading ? '...' : starCount}</span>
153-
</div>
171+
<StarFilled style={starIconStyle} />
172+
{loading ? (
173+
<Spin size="small" style={spinnerStyle} />
174+
) : (
175+
<span style={countStyle}>
176+
{starCount !== null ? formatStarCount(starCount) : '0'}
177+
</span>
178+
)}
154179
</a>
155180
</Tooltip>
156181
);

0 commit comments

Comments
 (0)