UNPKG

@lorenzo.franzone/tws

Version:

Tailwind 4 Styles Generator

80 lines (79 loc) 2.84 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.processSpacing = processSpacing; const clamp_1 = require("../../utils/clamp"); /** * ======================================== * processSpacing * ======================================== * Transforms a spacing config into CSS custom properties * using `clamp()` values. Handles nested structures recursively. * Gracefully reports invalid values with helpful messages. */ function processSpacing(input) { const errors = new Set(); validateSpacingConfig(input, errors); if (errors.size > 0) { console.error("Hmm, looks like your spacing config has some issues:"); errors.forEach(error => console.error("•", error)); return { outDir: "", data: [] }; } const { outDir, data } = input; const { sizes } = data; const theme = {}; /** * Recursively processes nested spacing definitions and builds * CSS custom properties prefixed with `--text-`. */ const processRecursive = (obj, prefix = '') => { for (const [key, value] of Object.entries(obj)) { const fullKey = `${prefix}${key}`; if (Array.isArray(value)) { try { const clamped = (0, clamp_1.clamp)(value); theme[`--text-${fullKey}`] = typeof clamped === 'number' ? `${clamped}rem` : clamped; } catch { errors.add(`spacing: Invalid clamp value at "sizes.${fullKey}".`); } } else if (typeof value === 'object' && value !== null) { // Recurse into nested groups (e.g. sizes.large.mobile) processRecursive(value, `${fullKey}-`); } else { errors.add(`spacing: Invalid value at "sizes.${fullKey}". Expected array or nested object.`); } } }; processRecursive(sizes); // Return early if any errors were collected during processing if (errors.size > 0) { console.error("Some values couldn't be processed due to errors:"); errors.forEach(error => console.error("•", error)); return { outDir: "", data: [] }; } return { outDir, data: [ { '@theme': theme } ], }; } /** * ======================================== * validateSpacingConfig * ======================================== * Performs basic structural validation of the spacing config. */ function validateSpacingConfig(config, errors) { if (!config.data) { errors.add("spacing: Missing 'data' field."); return; } const { sizes } = config.data; if (!sizes || typeof sizes !== 'object') { errors.add("spacing: 'sizes' must be a valid object."); } }