Skip to content

Commit 6d4fbfe

Browse files
ricosmallanuraghazraMicaelJarniacJounQin
authored
perf: align icons and text vertical (#33)
* improve: improved rating algorithm wip * Fixed typos, punctuation [...] Corrected many instances of "Github" to "GitHub", fixed the punctuation on some sections, fixed the ~lack of~ uppercase chars in others, tweaked the grammar a bit, and added the Vercel guide to a spoiler-ish section so it's only visible if you click to expand. <3 * fix: github rate limiter with multiple PATs * perf: vertically align text left * refactor: refactored retryer logic & handled invalid tokens * chore: remove redundant codes `axios` is Promise based, there is no need to wrap it into a Promise constructor again * fix: query param booleans * design: fixed rank alignment * chore: rebase from master * fix: fixed repo card breaking in absence of primaryLanguage * fix: fixed stars count #39 & fixed progressbar percentage * perf: replace emoji icons with GitHub SVG icons * chore: added funding link * refactor: refacted icons to another file * test: added test for icons Co-authored-by: anuraghazra <[email protected]> Co-authored-by: Micael Jarniac <[email protected]> Co-authored-by: JounQin <[email protected]>
1 parent 3312951 commit 6d4fbfe

File tree

5 files changed

+78
-22
lines changed

5 files changed

+78
-22
lines changed

src/getStyles.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,6 @@ const getStyles = ({
6464
}
6565
6666
.bold { font-weight: 700 }
67-
.star-icon {
68-
font: 600 18px 'Segoe UI', Ubuntu, Sans-Serif;
69-
}
7067
.icon {
7168
fill: ${iconColor};
7269
display: ${!!show_icons ? "block" : "none"};

src/icons.js

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/renderRepoCard.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const { kFormatter, encodeHTML, fallbackColor } = require("../src/utils");
2+
const icons = require("./icons");
23

34
const renderRepoCard = (repo, options = {}) => {
45
const { name, description, primaryLanguage, stargazers, forkCount } = repo;
@@ -32,7 +33,7 @@ const renderRepoCard = (repo, options = {}) => {
3233
</style>
3334
<rect data-testid="card-border" x="0.5" y="0.5" width="399" height="99%" rx="4.5" fill="${bgColor}" stroke="#E4E2E2"/>
3435
<svg class="icon" x="25" y="25" viewBox="0 0 16 16" version="1.1" width="16" height="16">
35-
<path fill-rule="evenodd" d="M2 2.5A2.5 2.5 0 014.5 0h8.75a.75.75 0 01.75.75v12.5a.75.75 0 01-.75.75h-2.5a.75.75 0 110-1.5h1.75v-2h-8a1 1 0 00-.714 1.7.75.75 0 01-1.072 1.05A2.495 2.495 0 012 11.5v-9zm10.5-1V9h-8c-.356 0-.694.074-1 .208V2.5a1 1 0 011-1h8zM5 12.25v3.25a.25.25 0 00.4.2l1.45-1.087a.25.25 0 01.3 0L8.6 15.7a.25.25 0 00.4-.2v-3.25a.25.25 0 00-.25-.25h-3.5a.25.25 0 00-.25.25z"></path>
36+
${icons.contribs}
3637
</svg>
3738
3839
<text x="50" y="38" class="header">${name}</text>
@@ -45,14 +46,14 @@ const renderRepoCard = (repo, options = {}) => {
4546
4647
<g transform="translate(${155 - shiftText}, 100)">
4748
<svg class="icon" y="-12" viewBox="0 0 16 16" version="1.1" width="16" height="16">
48-
<path fill-rule="evenodd" d="M8 .25a.75.75 0 01.673.418l1.882 3.815 4.21.612a.75.75 0 01.416 1.279l-3.046 2.97.719 4.192a.75.75 0 01-1.088.791L8 12.347l-3.766 1.98a.75.75 0 01-1.088-.79l.72-4.194L.818 6.374a.75.75 0 01.416-1.28l4.21-.611L7.327.668A.75.75 0 018 .25zm0 2.445L6.615 5.5a.75.75 0 01-.564.41l-3.097.45 2.24 2.184a.75.75 0 01.216.664l-.528 3.084 2.769-1.456a.75.75 0 01.698 0l2.77 1.456-.53-3.084a.75.75 0 01.216-.664l2.24-2.183-3.096-.45a.75.75 0 01-.564-.41L8 2.694v.001z"></path>
49+
${icons.star}
4950
</svg>
5051
<text data-testid="stargazers" class="gray" x="25">${totalStars}</text>
5152
</g>
5253
5354
<g transform="translate(${220 - shiftText}, 100)">
5455
<svg class="icon" y="-12" viewBox="0 0 16 16" version="1.1" width="16" height="16">
55-
<path fill-rule="evenodd" d="M5 3.25a.75.75 0 11-1.5 0 .75.75 0 011.5 0zm0 2.122a2.25 2.25 0 10-1.5 0v.878A2.25 2.25 0 005.75 8.5h1.5v2.128a2.251 2.251 0 101.5 0V8.5h1.5a2.25 2.25 0 002.25-2.25v-.878a2.25 2.25 0 10-1.5 0v.878a.75.75 0 01-.75.75h-4.5A.75.75 0 015 6.25v-.878zm3.75 7.378a.75.75 0 11-1.5 0 .75.75 0 011.5 0zm3-8.75a.75.75 0 100-1.5.75.75 0 000 1.5z"></path>
56+
${icons.fork}
5657
</svg>
5758
<text data-testid="forkcount" class="gray" x="25">${totalForks}</text>
5859
</g>

src/renderStatsCard.js

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,36 @@
11
const { kFormatter, fallbackColor } = require("../src/utils");
22
const getStyles = require("./getStyles");
3+
const icons = require("./icons");
34

4-
const createTextNode = ({ icon, label, value, id, index, lineHeight }) => {
5-
const classname = icon === "★" && "star-icon";
5+
const createTextNode = ({
6+
icon,
7+
label,
8+
value,
9+
id,
10+
index,
11+
lineHeight,
12+
showIcons,
13+
}) => {
614
const kValue = kFormatter(value);
715
const staggerDelay = (index + 3) * 150;
816
// manually calculating lineHeight based on index instead of using <tspan dy="" />
917
// to fix firefox layout bug
1018
const lheight = lineHeight * (index + 1);
19+
const translateY = lheight - lineHeight / 2;
20+
const labelOffset = showIcons ? `x="25"` : "";
21+
const iconSvg = showIcons
22+
? `
23+
<svg data-testid="icon" class="icon" viewBox="0 0 16 16" version="1.1" width="16" height="16">
24+
${icon}
25+
</svg>
26+
`
27+
: "";
1128
return `
12-
<text class="stagger" style="animation-delay: ${staggerDelay}ms" x="25" y="${lheight}">
13-
<tspan dx="0" data-testid="icon" class="icon ${classname}">${icon}</tspan>
14-
<tspan dx="0" class="stat bold">
15-
${label}:
16-
</tspan>
17-
<tspan x="160" data-testid="${id}" class="stat">${kValue}</tspan>
18-
</text>
29+
<g class="stagger" style="animation-delay: ${staggerDelay}ms" transform="translate(25, ${translateY})">
30+
${iconSvg}
31+
<text class="stat bold" ${labelOffset} y="12.5">${label}:</text>
32+
<text class="stat" x="135" y="12.5" data-testid="${id}">${kValue}</text>
33+
</g>
1934
`;
2035
};
2136

@@ -50,31 +65,31 @@ const renderStatsCard = (stats = {}, options = { hide: [] }) => {
5065

5166
const STATS = {
5267
stars: {
53-
icon: "★",
68+
icon: icons.star,
5469
label: "Total Stars",
5570
value: totalStars,
5671
id: "stars",
5772
},
5873
commits: {
59-
icon: "🕗",
74+
icon: icons.commits,
6075
label: "Total Commits",
6176
value: totalCommits,
6277
id: "commits",
6378
},
6479
prs: {
65-
icon: "🔀",
80+
icon: icons.prs,
6681
label: "Total PRs",
6782
value: totalPRs,
6883
id: "prs",
6984
},
7085
issues: {
71-
icon: "ⓘ",
86+
icon: icons.issues,
7287
label: "Total Issues",
7388
value: totalIssues,
7489
id: "issues",
7590
},
7691
contribs: {
77-
icon: "📕",
92+
icon: icons.contribs,
7893
label: "Contributed to",
7994
value: contributedTo,
8095
id: "contribs",
@@ -85,7 +100,12 @@ const renderStatsCard = (stats = {}, options = { hide: [] }) => {
85100
.filter((key) => !hide.includes(key))
86101
.map((key, index) =>
87102
// create the text nodes, and pass index so that we can calculate the line spacing
88-
createTextNode({ ...STATS[key], index, lineHeight: lheight })
103+
createTextNode({
104+
...STATS[key],
105+
index,
106+
lineHeight: lheight,
107+
showIcons: show_icons,
108+
})
89109
);
90110

91111
// Calculate the card height depending on how many items there are

tests/renderStatsCard.test.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ require("@testing-library/jest-dom");
22
const cssToObject = require("css-to-object");
33
const renderStatsCard = require("../src/renderStatsCard");
44

5-
const { getByTestId, queryByTestId } = require("@testing-library/dom");
5+
const {
6+
getByTestId,
7+
queryByTestId,
8+
queryAllByTestId,
9+
} = require("@testing-library/dom");
610

711
describe("Test renderStatsCard", () => {
812
const stats = {
@@ -107,4 +111,27 @@ describe("Test renderStatsCard", () => {
107111
"#252525"
108112
);
109113
});
114+
115+
it("should render icons correctly", () => {
116+
document.body.innerHTML = renderStatsCard(stats, {
117+
show_icons: "true",
118+
});
119+
120+
expect(queryAllByTestId(document.body, "icon")[0]).toBeDefined();
121+
expect(queryByTestId(document.body, "stars")).toBeDefined();
122+
expect(
123+
queryByTestId(document.body, "stars").previousElementSibling // the label
124+
).toHaveAttribute("x", "25");
125+
});
126+
127+
it("should not have icons if show_icons is false", () => {
128+
document.body.innerHTML = renderStatsCard(stats, { show_icons: false });
129+
130+
console.log(queryAllByTestId(document.body, "icon"));
131+
expect(queryAllByTestId(document.body, "icon")[0]).not.toBeDefined();
132+
expect(queryByTestId(document.body, "stars")).toBeDefined();
133+
expect(
134+
queryByTestId(document.body, "stars").previousElementSibling // the label
135+
).not.toHaveAttribute("x");
136+
});
110137
});

0 commit comments

Comments
 (0)