UNPKG

@knapsack/app

Version:

Build Design Systems with Knapsack

69 lines 2.59 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.hasSlotOptions = hasSlotOptions; exports.createSlotOptionsHtmlTags = createSlotOptionsHtmlTags; exports.isPathCodeSrcPath = isPathCodeSrcPath; const file_utils_1 = require("@knapsack/file-utils"); const utils_1 = require("@knapsack/utils"); function hasSlotOptions(slotOptions) { if (!slotOptions || (0, utils_1.isEmpty)(slotOptions)) return false; if (!(0, utils_1.isEmpty)(slotOptions.cssClasses)) return true; if (!(0, utils_1.isEmpty)(slotOptions.attributes)) return true; if (!(0, utils_1.isEmpty)(slotOptions.styles)) return true; return false; } function createSlotOptionsHtmlTags({ slotOptionsComputed, classAttributeName, stylesValueType, }) { if (!hasSlotOptions(slotOptionsComputed)) { return { openTag: '', closeTag: '', }; } const { attributes: originalAttributes = {}, cssClasses = [], styles = {}, wrappingElementName = 'div', } = slotOptionsComputed; // attributes are immutable, so we need to create a new object const attributes = { ...originalAttributes }; if (!(0, utils_1.isEmpty)(cssClasses)) { attributes[classAttributeName] = cssClasses.join(' '); } if (!(0, utils_1.isEmpty)(styles)) { const cleanedStyles = Object.fromEntries(Object.entries(styles).filter(([, value]) => !(0, utils_1.isEmpty)(value))); attributes.style = stylesValueType === 'string' ? Object.entries(cleanedStyles) .map(([key, value]) => `${(0, utils_1.kebabCase)(key)}: ${value}`) .join('; ') : JSON.stringify(cleanedStyles); } const attrs = [ ...Object.entries(attributes) .filter(([, value]) => !(0, utils_1.isEmpty)(value)) .map(([key, value]) => { if (stylesValueType === 'object' && key === 'style') { return `${key}={${value}}`; } return `${key}="${value}"`; }), ] .filter(Boolean) .join(' '); return { openTag: `<${wrappingElementName} ${attrs}>`, closeTag: `</${wrappingElementName}>`, }; } function isPathCodeSrcPath(path) { if (!path) return false; const info = (0, file_utils_1.getPathType)(path); if (info.type !== 'absolute') { // doing this to double-check w/TS const _codeSrcPath = info.path; return true; } return false; } //# sourceMappingURL=renderer-utils.js.map