Skip to content

Commit c5e8610

Browse files
add card min height
1 parent 21c6db7 commit c5e8610

12 files changed

+56
-26
lines changed

src/cards/gist-card.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
flexLayout,
1111
iconWithLabel,
1212
createLanguageNode,
13+
getAppropriateHeight,
1314
} from "../common/utils.js";
1415
import Card from "../common/Card.js";
1516
import { icons } from "../common/icons.js";
@@ -78,9 +79,9 @@ const renderGistCard = (gistData, options = {}) => {
7879
.join("");
7980

8081
const lineHeight = descriptionLines > 3 ? 12 : 10;
81-
const height = card_height
82-
? card_height
83-
: (descriptionLines > 1 ? 120 : 110) + descriptionLines * lineHeight;
82+
const minHeight =
83+
(descriptionLines > 1 ? 120 : 110) + descriptionLines * lineHeight;
84+
const height = getAppropriateHeight(card_height, minHeight);
8485

8586
const totalStars = kFormatter(starsCount);
8687
const totalForks = kFormatter(forksCount);

src/cards/repo-card.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
wrapTextMultiline,
1313
iconWithLabel,
1414
createLanguageNode,
15+
getAppropriateHeight,
1516
} from "../common/utils.js";
1617
import { repoCardLocales } from "../translations.js";
1718

@@ -88,9 +89,9 @@ const renderRepoCard = (repo, options = {}) => {
8889
.map((line) => `<tspan dy="1.2em" x="25">${encodeHTML(line)}</tspan>`)
8990
.join("");
9091

91-
const height = card_height
92-
? card_height
93-
: (descriptionLines > 1 ? 120 : 110) + descriptionLines * lineHeight;
92+
const minHeight =
93+
(descriptionLines > 1 ? 120 : 110) + descriptionLines * lineHeight;
94+
const height = getAppropriateHeight(card_height, minHeight);
9495

9596
const i18n = new I18n({
9697
locale,

src/cards/stats-card.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
CustomError,
77
clampValue,
88
flexLayout,
9+
getAppropriateHeight,
910
getCardColors,
1011
kFormatter,
1112
measureText,
@@ -392,12 +393,11 @@ const renderStatsCard = (stats, options = {}) => {
392393

393394
// Calculate the card height depending on how many items there are
394395
// but if rank circle is visible clamp the minimum height to `150`
395-
let height = card_height
396-
? card_height
397-
: Math.max(
398-
45 + (statItems.length + 1) * lheight,
399-
hide_rank ? 0 : statItems.length ? 150 : 180,
400-
);
396+
const minHeight = Math.max(
397+
45 + (statItems.length + 1) * lheight,
398+
hide_rank ? 0 : statItems.length ? 150 : 180,
399+
);
400+
const height = getAppropriateHeight(card_height, minHeight);
401401

402402
// the lower the user's percentile the better
403403
const progress = 100 - rank.percentile;

src/cards/top-languages-card.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
chunkArray,
77
clampValue,
88
flexLayout,
9+
getAppropriateHeight,
910
getCardColors,
1011
lowercaseTrim,
1112
measureText,
@@ -806,7 +807,7 @@ const renderTopLanguages = (topLangs, options = {}) => {
806807
customTitle: custom_title,
807808
defaultTitle: i18n.t("langcard.title"),
808809
width,
809-
height: card_height ? card_height : height,
810+
height: getAppropriateHeight(card_height, height),
810811
border_radius,
811812
colors,
812813
});

src/cards/wakatime-card.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { I18n } from "../common/I18n.js";
55
import {
66
clampValue,
77
flexLayout,
8+
getAppropriateHeight,
89
getCardColors,
910
lowercaseTrim,
1011
} from "../common/utils.js";
@@ -260,9 +261,11 @@ const renderWakatimeCard = (stats = {}, options = { hide: [] }) => {
260261

261262
// Calculate the card height depending on how many items there are
262263
// but if rank circle is visible clamp the minimum height to `150`
263-
let height = card_height
264-
? card_height
265-
: Math.max(45 + (filteredLanguages.length + 1) * lheight, 150);
264+
const minHeight = Math.max(
265+
45 + (filteredLanguages.length + 1) * lheight,
266+
150,
267+
);
268+
let height = getAppropriateHeight(card_height, minHeight);
266269

267270
const cssStyles = getStyles({
268271
titleColor,

src/common/utils.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,16 @@ const parseEmojis = (str) => {
552552
});
553553
};
554554

555+
/**
556+
* Get the appropriate height
557+
*
558+
* @param {number | undefined} height height wanted
559+
* @param {number} minHeight minimum height.
560+
* @returns {number} appropriate height.
561+
*/
562+
const getAppropriateHeight = (height, minHeight) =>
563+
height && height > minHeight ? height : minHeight;
564+
555565
/**
556566
* Get diff in minutes between two dates.
557567
*
@@ -592,4 +602,5 @@ export {
592602
chunkArray,
593603
parseEmojis,
594604
dateDiff,
605+
getAppropriateHeight,
595606
};

tests/renderGistCard.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,13 +223,13 @@ describe("test renderGistCard", () => {
223223
...data,
224224
},
225225
{
226-
card_height: 50,
226+
card_height: 280,
227227
},
228228
);
229229

230230
expect(
231231
document.body.getElementsByTagName("svg")[0].getAttribute("height"),
232-
).toBe("50");
232+
).toBe("280");
233233
});
234234

235235
it("should render without rounding", () => {

tests/renderRepoCard.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,13 +326,13 @@ describe("Test renderRepoCard", () => {
326326
isTemplate: true,
327327
},
328328
{
329-
card_height: 75,
329+
card_height: 340,
330330
},
331331
);
332332

333333
expect(
334334
document.body.getElementsByTagName("svg")[0].getAttribute("height"),
335-
).toBe("75");
335+
).toBe("340");
336336
});
337337

338338
it("should render without rounding", () => {

tests/renderStatsCard.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,12 +374,12 @@ describe("Test renderStatsCard", () => {
374374

375375
it("should render custom height correctly", () => {
376376
document.body.innerHTML = renderStatsCard(stats, {
377-
card_height: 50,
377+
card_height: 823,
378378
});
379379

380380
expect(
381381
document.body.getElementsByTagName("svg")[0].getAttribute("height"),
382-
).toBe("50");
382+
).toBe("823");
383383
});
384384

385385
it("should render translations", () => {

tests/renderTopLanguagesCard.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -853,12 +853,12 @@ describe("Test renderTopLanguages", () => {
853853
document.body.innerHTML = renderTopLanguages(
854854
{},
855855
{
856-
card_height: 56,
856+
card_height: 324,
857857
},
858858
);
859859

860860
expect(
861861
document.body.getElementsByTagName("svg")[0].getAttribute("height"),
862-
).toBe("56");
862+
).toBe("324");
863863
});
864864
});

0 commit comments

Comments
 (0)