forked from anuraghazra/github-readme-stats
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: added reusable Card class to reduce code & test duplication (…
…anuraghazra#260) * refactor: added reusable Card class to reduce code & test duplication * fix: top-langs card width & documented card_width option
- Loading branch information
1 parent
34b5dcb
commit 3b0f1b1
Showing
10 changed files
with
433 additions
and
229 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
const { FlexLayout } = require("./utils"); | ||
const { getAnimations } = require("./getStyles"); | ||
|
||
class Card { | ||
constructor({ | ||
width = 100, | ||
height = 100, | ||
colors = {}, | ||
title = "", | ||
titlePrefixIcon, | ||
}) { | ||
this.width = width; | ||
this.height = height; | ||
|
||
this.hideBorder = false; | ||
this.hideTitle = false; | ||
|
||
// returns theme based colors with proper overrides and defaults | ||
this.colors = colors; | ||
this.title = title; | ||
this.css = ""; | ||
|
||
this.paddingX = 25; | ||
this.paddingY = 35; | ||
this.titlePrefixIcon = titlePrefixIcon; | ||
this.animations = true; | ||
} | ||
|
||
disableAnimations() { | ||
this.animations = false; | ||
} | ||
|
||
setCSS(value) { | ||
this.css = value; | ||
} | ||
|
||
setHideBorder(value) { | ||
this.hideBorder = value; | ||
} | ||
|
||
setHideTitle(value) { | ||
this.hideTitle = value; | ||
if (value) { | ||
this.height -= 30; | ||
} | ||
} | ||
|
||
setTitle(text) { | ||
this.title = text; | ||
} | ||
|
||
renderTitle() { | ||
const titleText = ` | ||
<text | ||
x="0" | ||
y="0" | ||
class="header" | ||
data-testid="header" | ||
>${this.title}</text> | ||
`; | ||
|
||
const prefixIcon = ` | ||
<svg | ||
class="icon" | ||
x="0" | ||
y="-13" | ||
viewBox="0 0 16 16" | ||
version="1.1" | ||
width="16" | ||
height="16" | ||
> | ||
${this.titlePrefixIcon} | ||
</svg> | ||
`; | ||
return ` | ||
<g | ||
data-testid="card-title" | ||
transform="translate(${this.paddingX}, ${this.paddingY})" | ||
> | ||
${FlexLayout({ | ||
items: [this.titlePrefixIcon && prefixIcon, titleText], | ||
gap: 25, | ||
}).join("")} | ||
</g> | ||
`; | ||
} | ||
|
||
render(body) { | ||
return ` | ||
<svg | ||
width="${this.width}" | ||
height="${this.height}" | ||
viewBox="0 0 ${this.width} ${this.height}" | ||
fill="none" | ||
xmlns="http://www.w3.org/2000/svg" | ||
> | ||
<style> | ||
.header { | ||
font: 600 18px 'Segoe UI', Ubuntu, Sans-Serif; | ||
fill: ${this.colors.titleColor}; | ||
animation: fadeInAnimation 0.8s ease-in-out forwards; | ||
} | ||
${this.css} | ||
${ | ||
process.env.NODE_ENV === "test" || !this.animations | ||
? "" | ||
: getAnimations() | ||
} | ||
</style> | ||
<rect | ||
data-testid="card-bg" | ||
x="0.5" | ||
y="0.5" | ||
rx="4.5" | ||
height="99%" | ||
stroke="#E4E2E2" | ||
width="${this.width - 1}" | ||
fill="${this.colors.bgColor}" | ||
stroke-opacity="${this.hideBorder ? 0 : 1}" | ||
/> | ||
${this.hideTitle ? "" : this.renderTitle()} | ||
<g | ||
data-testid="main-card-body" | ||
transform="translate(0, ${ | ||
this.hideTitle ? this.paddingX : this.paddingY + 20 | ||
})" | ||
> | ||
${body} | ||
</g> | ||
</svg> | ||
`; | ||
} | ||
} | ||
|
||
module.exports = Card; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.