UNPKG

@builder.io/mitosis

Version:

Write components once, run everywhere. Compiles to Vue, React, Solid, and Liquid. Import code from Figma and Builder.io

71 lines (70 loc) 2.66 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.renderStyles = exports.collectStyles = void 0; const json5_1 = __importDefault(require("json5")); const dash_case_1 = require("../../../helpers/dash-case"); function collectStyles(children, styleMap) { var _a; const nodes = [...children]; while (nodes.length) { const child = nodes.shift(); nodes.push(...child.children); const css = (_a = child.bindings.css) === null || _a === void 0 ? void 0 : _a.code; if (css && typeof css == 'string') { const value = { CLASS_NAME: 'c' + hashCode(css), ...json5_1.default.parse(css) }; styleMap.set(css, value); } } return styleMap; } exports.collectStyles = collectStyles; function hashCode(text) { var hash = 0, i, chr; if (text.length === 0) return hash; for (i = 0; i < text.length; i++) { chr = text.charCodeAt(i); hash = (hash << 5) - hash + chr; hash |= 0; // Convert to 32bit integer } return Number(Math.abs(hash)).toString(36); } function renderStyles(styles) { return function () { this.emit('`'); const mediaStyles = []; styles.forEach((styles) => { this.emit('.', styles.CLASS_NAME, /*'.🏷️�', WS,*/ '{'); for (const key in styles) { if (key !== 'CLASS_NAME' && Object.prototype.hasOwnProperty.call(styles, key)) { const value = styles[key]; if (value && typeof value == 'object') { mediaStyles.push(styles.CLASS_NAME, key, value); } else { this.emit((0, dash_case_1.dashCase)(key), ':', value, ';'); } } } this.emit('}'); }); while (mediaStyles.length) { const className = mediaStyles.shift(); const mediaKey = mediaStyles.shift(); const mediaObj = mediaStyles.shift(); this.emit(mediaKey, '{.', className, /*'.🏷️�',*/ '{'); for (const key in mediaObj) { if (Object.prototype.hasOwnProperty.call(mediaObj, key)) { const value = mediaObj[key]; this.emit((0, dash_case_1.dashCase)(key), ':', value, ';'); } } this.emit('}}'); } this.emit('`'); }; } exports.renderStyles = renderStyles;