@platform/css
Version:
Helpers for working with inline CSS.
136 lines (135 loc) • 4.77 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.toRadius = exports.toAbsolute = exports.toPosition = exports.toShadow = exports.toPadding = exports.toMargins = exports.prefixEdges = exports.toEdges = void 0;
var tslib_1 = require("tslib");
var common_1 = require("../common");
var color_1 = require("../color");
function isBlank(value) {
if (value === undefined || value === null)
return true;
if (typeof value === 'string' && common_1.valueUtil.isBlank(value))
return true;
if (Array.isArray(value) && common_1.valueUtil.compact(value).length === 0)
return true;
return false;
}
var toEdges = function (input, options) {
if (options === void 0) { options = {}; }
if ((Array.isArray(input) && input.length === 0) || isBlank(input)) {
var defaultValue = options.defaultValue;
if (defaultValue && !isBlank(defaultValue)) {
input = defaultValue;
}
else {
return {};
}
}
input = input || 0;
if (!Array.isArray(input)) {
input = input.toString().split(' ');
}
var edges = input
.map(function (item) {
return typeof item === 'string' && item.endsWith('px') ? item.replace(/px$/, '') : item;
})
.map(function (item) { return common_1.valueUtil.toNumber(item); });
var top;
var right;
var bottom;
var left;
var getEdge = function (index) {
var edge = edges[index];
if (edge === null || edge === 'null' || edge === '') {
return undefined;
}
return edge;
};
switch (edges.length) {
case 1:
top = getEdge(0);
bottom = getEdge(0);
left = getEdge(0);
right = getEdge(0);
break;
case 2:
top = getEdge(0);
bottom = getEdge(0);
left = getEdge(1);
right = getEdge(1);
break;
case 3:
top = getEdge(0);
left = getEdge(1);
right = getEdge(1);
bottom = getEdge(2);
break;
default:
top = getEdge(0);
right = getEdge(1);
bottom = getEdge(2);
left = getEdge(3);
}
if (top === undefined && right === undefined && bottom === undefined && left === undefined) {
return {};
}
else {
return { top: top, right: right, bottom: bottom, left: left };
}
};
exports.toEdges = toEdges;
function prefixEdges(prefix, edges) {
return Object.keys(edges).reduce(function (acc, key) {
var _a;
var value = edges[key];
key = "".concat(prefix).concat(key[0].toUpperCase()).concat(key.substring(1));
return tslib_1.__assign(tslib_1.__assign({}, acc), (_a = {}, _a[key] = value, _a));
}, {});
}
exports.prefixEdges = prefixEdges;
var toMargins = function (input, options) {
if (options === void 0) { options = {}; }
return prefixEdges('margin', (0, exports.toEdges)(input, options));
};
exports.toMargins = toMargins;
var toPadding = function (input, options) {
if (options === void 0) { options = {}; }
return prefixEdges('padding', (0, exports.toEdges)(input, options));
};
exports.toPadding = toPadding;
var toShadow = function (input) {
if (input === undefined)
return undefined;
var blur = input.blur;
var x = input.x ? "".concat(input.x, "px") : '0';
var y = input.y ? "".concat(input.y, "px") : '0';
var col = color_1.color.format(input.color);
var inset = input.inner ? 'inset ' : '';
return "".concat(inset).concat(x, " ").concat(y, " ").concat(blur, "px 0 ").concat(col);
};
exports.toShadow = toShadow;
var toPosition = function (position, edges) {
return tslib_1.__assign({ position: position }, (0, exports.toEdges)(edges));
};
exports.toPosition = toPosition;
var toAbsolute = function (edges) { return (0, exports.toPosition)('absolute', edges); };
exports.toAbsolute = toAbsolute;
var toRadius = function (edges) {
if (edges === null || edges === undefined)
return undefined;
if (typeof edges === 'number')
return "".concat(edges, "px");
if (typeof edges === 'string')
return edges;
if (Array.isArray(edges)) {
var convert = function (input) {
if (typeof input === 'number')
return input === 0 ? 0 : "".concat(input, "px");
if (input === null || input === undefined)
return 0;
return input || 0;
};
return tslib_1.__spreadArray(tslib_1.__spreadArray([], edges.slice(0, 4), true), [0, 0, 0, 0], false).slice(0, 4).map(convert).join(' ');
}
return undefined;
};
exports.toRadius = toRadius;