|
3 | 3 | import { SECONDARY_ERROR_MESSAGES, TRY_AGAIN_LATER } from "./error.js"; |
4 | 4 | import { getCardColors } from "./color.js"; |
5 | 5 | import { encodeHTML } from "./html.js"; |
| 6 | +import { clampValue } from "./ops.js"; |
6 | 7 |
|
7 | 8 | /** |
8 | 9 | * Auto layout utility, allows us to layout things vertically or horizontally with |
@@ -45,6 +46,46 @@ const createLanguageNode = (langName, langColor) => { |
45 | 46 | `; |
46 | 47 | }; |
47 | 48 |
|
| 49 | +/** |
| 50 | + * Create a node to indicate progress in percentage along a horizontal line. |
| 51 | + * |
| 52 | + * @param {Object} params Object that contains the createProgressNode parameters. |
| 53 | + * @param {number} params.x X-axis position. |
| 54 | + * @param {number} params.y Y-axis position. |
| 55 | + * @param {number} params.width Width of progress bar. |
| 56 | + * @param {string} params.color Progress color. |
| 57 | + * @param {number} params.progress Progress value. |
| 58 | + * @param {string} params.progressBarBackgroundColor Progress bar bg color. |
| 59 | + * @param {number} params.delay Delay before animation starts. |
| 60 | + * @returns {string} Progress node. |
| 61 | + */ |
| 62 | +const createProgressNode = ({ |
| 63 | + x, |
| 64 | + y, |
| 65 | + width, |
| 66 | + color, |
| 67 | + progress, |
| 68 | + progressBarBackgroundColor, |
| 69 | + delay, |
| 70 | +}) => { |
| 71 | + const progressPercentage = clampValue(progress, 2, 100); |
| 72 | + |
| 73 | + return ` |
| 74 | + <svg width="${width}" x="${x}" y="${y}"> |
| 75 | + <rect rx="5" ry="5" x="0" y="0" width="${width}" height="8" fill="${progressBarBackgroundColor}"></rect> |
| 76 | + <svg data-testid="lang-progress" width="${progressPercentage}%"> |
| 77 | + <rect |
| 78 | + height="8" |
| 79 | + fill="${color}" |
| 80 | + rx="5" ry="5" x="0" y="0" |
| 81 | + class="lang-progress" |
| 82 | + style="animation-delay: ${delay}ms;" |
| 83 | + /> |
| 84 | + </svg> |
| 85 | + </svg> |
| 86 | + `; |
| 87 | +}; |
| 88 | + |
48 | 89 | /** |
49 | 90 | * Creates an icon with label to display repository/gist stats like forks, stars, etc. |
50 | 91 | * |
@@ -191,6 +232,7 @@ export { |
191 | 232 | ERROR_CARD_LENGTH, |
192 | 233 | renderError, |
193 | 234 | createLanguageNode, |
| 235 | + createProgressNode, |
194 | 236 | iconWithLabel, |
195 | 237 | flexLayout, |
196 | 238 | measureText, |
|
0 commit comments