react-style-stringify
Version:
A utility for converting React CSSProperties and style maps into CSS strings, designed to simplify the management of inline styles in HTML email templates and React projects.
131 lines (126 loc) • 5.21 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var index_exports = {};
__export(index_exports, {
stringifyCSSProperties: () => stringifyCSSProperties,
stringifyStyleDeclaration: () => stringifyStyleDeclaration,
stringifyStyleMap: () => stringifyStyleMap,
stringifyStyleRule: () => stringifyStyleRule
});
module.exports = __toCommonJS(index_exports);
// src/helpers.ts
var import_unitless = __toESM(require("@emotion/unitless"));
var DEFAULT_UNIT = "px";
var isCSSPropertyValue = (value) => typeof value === "number" || typeof value === "string";
function camelToKebab(str) {
return str.replace(/[A-Z]/g, (match) => `-${match.toLowerCase()}`);
}
function trimCssSelector(selector) {
return selector.replace(/\s*([+~>])\s*/g, "$1").replace(/\s{2,}/g, " ").trim();
}
function applyCssUnits(property, value, unit = DEFAULT_UNIT) {
if (typeof value !== "string" && typeof value !== "number") {
throw new Error(
"Invalid input: value of 'cssProperties' must be string or number."
);
}
const isUnitless = import_unitless.default[property] === 1;
if (typeof value === "string" || value === 0 || isUnitless) {
return `${value}`;
}
const resolvedUnit = (typeof unit === "string" ? unit : unit[property]) || DEFAULT_UNIT;
return `${value}${resolvedUnit}`;
}
// src/stringifyStyleDeclaration.ts
function stringifyStyleDeclaration(styleDeclaration, options) {
if (typeof styleDeclaration !== "object" || styleDeclaration === null) {
throw new TypeError(
`[stringifyStyleDeclaration]: Expected 'styleDeclaration' to be a non-null object, but received ${styleDeclaration} (type:${typeof styleDeclaration}).`
);
}
const importantSuffix = options?.important ? "!important" : "";
return Object.entries(styleDeclaration).filter(([_, value]) => isCSSPropertyValue(value)).map(
([property, value]) => `${camelToKebab(property)}:${applyCssUnits(
property,
value,
options?.unit
)}${importantSuffix};`
).join("");
}
// src/stringifyStyleRule.ts
function stringifyStyleRule(styleRule, options) {
if (typeof styleRule !== "object" || styleRule === null) {
throw new TypeError(
`[stringifyStyleRule]: Expected 'styleRule' to be a non-null object, but received ${styleRule} (type:${typeof styleRule}).`
);
}
return Object.entries(styleRule).reduce((result, [selector, declaration]) => {
if (Object.keys(declaration).length > 0) {
result.push(
`${trimCssSelector(selector)}{${stringifyStyleDeclaration(
declaration,
options
)}}`
);
}
return result;
}, []).join("");
}
// src/stringify-react-styles.ts
function stringifyCSSProperties(cssProperties, optionsOrImportant = false) {
if (typeof cssProperties !== "object" || cssProperties === null) {
throw new TypeError(
`[stringifyCSSProperties]: Expected 'cssProperties' to be a non-null object, but received ${cssProperties} (type:${typeof cssProperties}).`
);
}
const options = typeof optionsOrImportant === "boolean" ? {
important: optionsOrImportant
} : optionsOrImportant;
return stringifyStyleDeclaration(cssProperties, options);
}
function stringifyStyleMap(styleMap, optionsOrImportant = false) {
if (typeof styleMap !== "object" || styleMap === null) {
throw new TypeError(
`[stringifyStyleMap]: Expected 'styleMap' to be a non-null object, but received ${styleMap} (type:${typeof styleMap}).`
);
}
const options = typeof optionsOrImportant === "boolean" ? {
important: optionsOrImportant
} : optionsOrImportant;
return stringifyStyleRule(styleMap, options);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
stringifyCSSProperties,
stringifyStyleDeclaration,
stringifyStyleMap,
stringifyStyleRule
});
//# sourceMappingURL=index.js.map