Skip to content

Commit d8b5c9d

Browse files
authored
Merge pull request #1 from rickstaa/include_icons_in_min_width_calculation
fix: add icon width to stats-card min width calculation
2 parents dc78bad + 34d6423 commit d8b5c9d

File tree

1 file changed

+34
-18
lines changed

1 file changed

+34
-18
lines changed

src/cards/stats-card.js

+34-18
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ const createTextNode = ({
3535
<g class="stagger" style="animation-delay: ${staggerDelay}ms" transform="translate(25, 0)">
3636
${iconSvg}
3737
<text class="stat bold" ${labelOffset} y="12.5">${label}:</text>
38-
<text
39-
class="stat"
40-
x="${(showIcons ? 140 : 120) + shiftValuePos}"
41-
y="12.5"
38+
<text
39+
class="stat"
40+
x="${(showIcons ? 140 : 120) + shiftValuePos}"
41+
y="12.5"
4242
data-testid="${id}"
4343
>${kValue}</text>
4444
</g>
@@ -76,7 +76,6 @@ const renderStatsCard = (stats = {}, options = { hide: [] }) => {
7676
disable_animations = false,
7777
} = options;
7878

79-
8079
const lheight = parseInt(line_height, 10);
8180

8281
// returns theme based colors with proper overrides and defaults
@@ -186,20 +185,18 @@ const renderStatsCard = (stats = {}, options = { hide: [] }) => {
186185
};
187186

188187
/*
189-
When hide_rank=true, minimum card width is max of 270 and length of title + paddings.
190-
When hide_rank=false, minimum card_width is 340.
188+
When hide_rank=true, the minimum card width is 270 px + the title length and padding.
189+
When hide_rank=false, the minimum card_width is 340 px + the icon width (if show_icons=true).
191190
Numbers are picked by looking at existing dimensions on production.
192191
*/
192+
const iconWidth = show_icons ? 16 : 0;
193193
const minCardWidth = hide_rank
194-
? clampValue(
195-
50 /* padding */ + calculateTextWidth() * 2,
196-
270,
197-
Infinity)
198-
: 340
199-
const defaultCardWidth = hide_rank ? 270 : 495
200-
let width = isNaN(card_width) ? defaultCardWidth : card_width
194+
? clampValue(50 /* padding */ + calculateTextWidth() * 2, 270, Infinity)
195+
: 340 + iconWidth;
196+
const defaultCardWidth = hide_rank ? 270 : 495;
197+
let width = isNaN(card_width) ? defaultCardWidth : card_width;
201198
if (width < minCardWidth) {
202-
width = minCardWidth
199+
width = minCardWidth;
203200
}
204201

205202
const card = new Card({
@@ -223,11 +220,30 @@ const renderStatsCard = (stats = {}, options = { hide: [] }) => {
223220

224221
if (disable_animations) card.disableAnimations();
225222

223+
/**
224+
* Calculates the right rank circle translation values such that the rank circle
225+
* keeps respecting the padding.
226+
*
227+
* width > 450: The default left padding of 50 px will be used.
228+
* width < 450: The left and right padding will shrink equally.
229+
*
230+
* @returns {number} - Rank circle translation value.
231+
*/
232+
const calculateRankXTranslation = () => {
233+
if (width < 450) {
234+
return width - 95 + (45 * (450 - 340)) / 110;
235+
} else {
236+
return width - 95;
237+
}
238+
};
239+
226240
// Conditionally rendered elements
227241
const rankCircle = hide_rank
228242
? ""
229-
: `<g data-testid="rank-circle"
230-
transform="translate(${width - 50}, ${height / 2 - 50})">
243+
: `<g data-testid="rank-circle"
244+
transform="translate(${calculateRankXTranslation()}, ${
245+
height / 2 - 50
246+
})">
231247
<circle class="rank-circle-rim" cx="-10" cy="8" r="40" />
232248
<circle class="rank-circle" cx="-10" cy="8" r="40" />
233249
<g class="rank-text">
@@ -252,7 +268,7 @@ const renderStatsCard = (stats = {}, options = { hide: [] }) => {
252268
gap: lheight,
253269
direction: "column",
254270
}).join("")}
255-
</svg>
271+
</svg>
256272
`);
257273
};
258274

0 commit comments

Comments
 (0)