// @ts-check /** * Calculates progress along the boundary of the circle i.e it's circumference. * * @param {number} value The rank value to calculate progress for. * @returns {number} Progress value. */ const calculateCircleProgress = (value) => { const radius = 40; const c = Math.PI * (radius * 2); if (value < 0) value = 0; if (value > 100) value = 100; return ((100 - value) / 100) * c; }; /** * Retrieves the animation to display progress along the circumference of circle * from the beginning to the given value in a clockwise direction. * * @param {{progress: number}} progress The progress value to animate to. * @returns {string} Progress animation css. */ const getProgressAnimation = ({ progress }) => { return ` @keyframes rankAnimation { from { stroke-dashoffset: ${calculateCircleProgress(0)}; } to { stroke-dashoffset: ${calculateCircleProgress(progress)}; } } `; }; /** * Retrieves css animations for a card. * * @returns {string} Animation css. */ const getAnimations = () => { return ` /* Animations */ @keyframes scaleInAnimation { from { transform: translate(-5px, 5px) scale(0); } to { transform: translate(-5px, 5px) scale(1); } } @keyframes fadeInAnimation { from { opacity: 0; } to { opacity: 1; } } `; }; /** * Retrieves CSS styles for a card. * * @param {Object[]} colors The colors to use for the card. * @param {string} colors.titleColor The title color. * @param {string} colors.textColor The text color. * @param {string} colors.iconColor The icon color. * @param {boolean} colors.show_icons Whether to show icons. * @param {number} colors.progress The progress value to animate to. * @returns {string} Card CSS styles. */ const getStyles = ({ titleColor, textColor, iconColor, ringColor, show_icons, progress, }) => { return ` .stat { font: 600 14px 'Segoe UI', Ubuntu, "Helvetica Neue", Sans-Serif; fill: ${textColor}; } @supports(-moz-appearance: auto) { /* Selector detects Firefox */ .stat { font-size:12px; } } .stagger { opacity: 0; animation: fadeInAnimation 0.3s ease-in-out forwards; } .rank-text { font: 800 24px 'Segoe UI', Ubuntu, Sans-Serif; fill: ${textColor}; animation: scaleInAnimation 0.3s ease-in-out forwards; } .not_bold { font-weight: 400 } .bold { font-weight: 700 } .icon { fill: ${iconColor}; display: ${!!show_icons ? "block" : "none"}; } .rank-circle-rim { stroke: ${ringColor}; fill: none; stroke-width: 6; opacity: 0.2; } .rank-circle { stroke: ${ringColor}; stroke-dasharray: 250; fill: none; stroke-width: 6; stroke-linecap: round; opacity: 0.8; transform-origin: -10px 8px; transform: rotate(-90deg); animation: rankAnimation 1s forwards ease-in-out; } ${process.env.NODE_ENV === "test" ? "" : getProgressAnimation({ progress })} `; }; export { getStyles, getAnimations };