import { getAnimations } from "../getStyles.js"; import { encodeHTML, flexLayout } from "./utils.js"; class Card { /** * @param {object} args * @param {number?=} args.width * @param {number?=} args.height * @param {number?=} args.border_radius * @param {string?=} args.customTitle * @param {string?=} args.defaultTitle * @param {string?=} args.titlePrefixIcon * @param {ReturnType?=} args.colors */ constructor({ width = 100, height = 100, border_radius = 4.5, colors = {}, customTitle, defaultTitle = "", titlePrefixIcon, }) { this.width = width; this.height = height; this.hideBorder = false; this.hideTitle = false; this.border_radius = border_radius; // returns theme based colors with proper overrides and defaults this.colors = colors; this.title = customTitle !== undefined ? encodeHTML(customTitle) : encodeHTML(defaultTitle); this.css = ""; this.paddingX = 25; this.paddingY = 35; this.titlePrefixIcon = titlePrefixIcon; this.animations = true; this.a11yTitle = ""; this.a11yDesc = ""; } disableAnimations() { this.animations = false; } /** * @param {{title: string, desc: string}} prop */ setAccessibilityLabel({ title, desc }) { this.a11yTitle = title; this.a11yDesc = desc; } /** * @param {string} value */ setCSS(value) { this.css = value; } /** * @param {boolean} value */ setHideBorder(value) { this.hideBorder = value; } /** * @param {boolean} value */ setHideTitle(value) { this.hideTitle = value; if (value) { this.height -= 30; } } /** * @param {string} text */ setTitle(text) { this.title = text; } renderTitle() { const titleText = ` ${this.title} `; const prefixIcon = ` ${this.titlePrefixIcon} `; return ` ${flexLayout({ items: [this.titlePrefixIcon && prefixIcon, titleText], gap: 25, }).join("")} `; } renderGradient() { if (typeof this.colors.bgColor !== "object") return ""; const gradients = this.colors.bgColor.slice(1); return typeof this.colors.bgColor === "object" ? ` ${gradients.map((grad, index) => { let offset = (index * 100) / (gradients.length - 1); return ``; })} ` : ""; } /** * @param {string} body */ render(body) { return ` ${this.a11yTitle} ${this.a11yDesc} ${this.renderGradient()} ${this.hideTitle ? "" : this.renderTitle()} ${body} `; } } export { Card }; export default Card;