Skip to content

Commit ac749b7

Browse files
authored
refactor: resolve vscode type errors in wakatime card render and remove redundant css (anuraghazra#3232)
* refactor: resolve vscode type errors in wakatime card render and remove redundant css * dev
1 parent 00394cf commit ac749b7

File tree

6 files changed

+170
-216
lines changed

6 files changed

+170
-216
lines changed

src/cards/stats-card.js

+112-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
kFormatter,
1111
measureText,
1212
} from "../common/utils.js";
13-
import { getStyles } from "../getStyles.js";
1413
import { statCardLocales } from "../translations.js";
1514

1615
const CARD_MIN_WIDTH = 287;
@@ -76,6 +75,118 @@ const createTextNode = ({
7675
`;
7776
};
7877

78+
/**
79+
* Calculates progress along the boundary of the circle i.e it's circumference.
80+
*
81+
* @param {number} value The rank value to calculate progress for.
82+
* @returns {number} Progress value.
83+
*/
84+
const calculateCircleProgress = (value) => {
85+
const radius = 40;
86+
const c = Math.PI * (radius * 2);
87+
88+
if (value < 0) {
89+
value = 0;
90+
}
91+
if (value > 100) {
92+
value = 100;
93+
}
94+
95+
return ((100 - value) / 100) * c;
96+
};
97+
98+
/**
99+
* Retrieves the animation to display progress along the circumference of circle
100+
* from the beginning to the given value in a clockwise direction.
101+
*
102+
* @param {{progress: number}} progress The progress value to animate to.
103+
* @returns {string} Progress animation css.
104+
*/
105+
const getProgressAnimation = ({ progress }) => {
106+
return `
107+
@keyframes rankAnimation {
108+
from {
109+
stroke-dashoffset: ${calculateCircleProgress(0)};
110+
}
111+
to {
112+
stroke-dashoffset: ${calculateCircleProgress(progress)};
113+
}
114+
}
115+
`;
116+
};
117+
118+
/**
119+
* Retrieves CSS styles for a card.
120+
*
121+
* @param {Object} colors The colors to use for the card.
122+
* @param {string} colors.titleColor The title color.
123+
* @param {string} colors.textColor The text color.
124+
* @param {string} colors.iconColor The icon color.
125+
* @param {string} colors.ringColor The ring color.
126+
* @param {boolean} colors.show_icons Whether to show icons.
127+
* @param {number} colors.progress The progress value to animate to.
128+
* @returns {string} Card CSS styles.
129+
*/
130+
const getStyles = ({
131+
// eslint-disable-next-line no-unused-vars
132+
titleColor,
133+
textColor,
134+
iconColor,
135+
ringColor,
136+
show_icons,
137+
progress,
138+
}) => {
139+
return `
140+
.stat {
141+
font: 600 14px 'Segoe UI', Ubuntu, "Helvetica Neue", Sans-Serif; fill: ${textColor};
142+
}
143+
@supports(-moz-appearance: auto) {
144+
/* Selector detects Firefox */
145+
.stat { font-size:12px; }
146+
}
147+
.stagger {
148+
opacity: 0;
149+
animation: fadeInAnimation 0.3s ease-in-out forwards;
150+
}
151+
.rank-text {
152+
font: 800 24px 'Segoe UI', Ubuntu, Sans-Serif; fill: ${textColor};
153+
animation: scaleInAnimation 0.3s ease-in-out forwards;
154+
}
155+
.rank-percentile-header {
156+
font-size: 14px;
157+
}
158+
.rank-percentile-text {
159+
font-size: 16px;
160+
}
161+
162+
.not_bold { font-weight: 400 }
163+
.bold { font-weight: 700 }
164+
.icon {
165+
fill: ${iconColor};
166+
display: ${!!show_icons ? "block" : "none"};
167+
}
168+
169+
.rank-circle-rim {
170+
stroke: ${ringColor};
171+
fill: none;
172+
stroke-width: 6;
173+
opacity: 0.2;
174+
}
175+
.rank-circle {
176+
stroke: ${ringColor};
177+
stroke-dasharray: 250;
178+
fill: none;
179+
stroke-width: 6;
180+
stroke-linecap: round;
181+
opacity: 0.8;
182+
transform-origin: -10px 8px;
183+
transform: rotate(-90deg);
184+
animation: rankAnimation 1s forwards ease-in-out;
185+
}
186+
${process.env.NODE_ENV === "test" ? "" : getProgressAnimation({ progress })}
187+
`;
188+
};
189+
79190
/**
80191
* @typedef {import('../fetchers/types').StatsData} StatsData
81192
* @typedef {import('./types').StatCardOptions} StatCardOptions

src/cards/wakatime-card.js

+30-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
getCardColors,
99
lowercaseTrim,
1010
} from "../common/utils.js";
11-
import { getStyles } from "../getStyles.js";
1211
import { wakatimeCardLocales } from "../translations.js";
1312

1413
/** Import language colors.
@@ -158,6 +157,36 @@ const recalculatePercentages = (languages) => {
158157
});
159158
};
160159

160+
/**
161+
* Retrieves CSS styles for a card.
162+
*
163+
* @param {Object} colors The colors to use for the card.
164+
* @param {string} colors.titleColor The title color.
165+
* @param {string} colors.textColor The text color.
166+
* @returns {string} Card CSS styles.
167+
*/
168+
const getStyles = ({
169+
// eslint-disable-next-line no-unused-vars
170+
titleColor,
171+
textColor,
172+
}) => {
173+
return `
174+
.stat {
175+
font: 600 14px 'Segoe UI', Ubuntu, "Helvetica Neue", Sans-Serif; fill: ${textColor};
176+
}
177+
@supports(-moz-appearance: auto) {
178+
/* Selector detects Firefox */
179+
.stat { font-size:12px; }
180+
}
181+
.stagger {
182+
opacity: 0;
183+
animation: fadeInAnimation 0.3s ease-in-out forwards;
184+
}
185+
.not_bold { font-weight: 400 }
186+
.bold { font-weight: 700 }
187+
`;
188+
};
189+
161190
/**
162191
* @typedef {import('../fetchers/types').WakaTimeData} WakaTimeData
163192
* @typedef {import('./types').WakaTimeOptions} WakaTimeOptions
@@ -235,7 +264,6 @@ const renderWakatimeCard = (stats = {}, options = { hide: [] }) => {
235264
const cssStyles = getStyles({
236265
titleColor,
237266
textColor,
238-
iconColor,
239267
});
240268

241269
let finalLayout = "";

src/common/Card.js

+28-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { getAnimations } from "../getStyles.js";
21
import { encodeHTML, flexLayout } from "./utils.js";
32

43
class Card {
@@ -173,6 +172,33 @@ class Card {
173172
: "";
174173
}
175174

175+
/**
176+
* Retrieves css animations for a card.
177+
*
178+
* @returns {string} Animation css.
179+
*/
180+
getAnimations = () => {
181+
return `
182+
/* Animations */
183+
@keyframes scaleInAnimation {
184+
from {
185+
transform: translate(-5px, 5px) scale(0);
186+
}
187+
to {
188+
transform: translate(-5px, 5px) scale(1);
189+
}
190+
}
191+
@keyframes fadeInAnimation {
192+
from {
193+
opacity: 0;
194+
}
195+
to {
196+
opacity: 1;
197+
}
198+
}
199+
`;
200+
};
201+
176202
/**
177203
* @param {string} body The inner body of the card.
178204
* @returns {string} The rendered card.
@@ -202,7 +228,7 @@ class Card {
202228
}
203229
${this.css}
204230
205-
${process.env.NODE_ENV === "test" ? "" : getAnimations()}
231+
${process.env.NODE_ENV === "test" ? "" : this.getAnimations()}
206232
${
207233
this.animations === false
208234
? `* { animation-duration: 0s !important; animation-delay: 0s !important; }`

src/getStyles.js

-142
This file was deleted.

src/index.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
export * from "./common/index.js";
22
export * from "./cards/index.js";
3-
export { getStyles, getAnimations } from "./getStyles.js";

0 commit comments

Comments
 (0)