@amaui/style
Version:
CSS in JS styling solution
169 lines (147 loc) • 6.34 kB
JavaScript
import variationWithRepetition from '@amaui/utils/variationWithRepetition';
import getEnvironment from '@amaui/utils/getEnvironment';
import AmauiSubscription from '@amaui/subscription';
export const cammelCaseToKebabCase = value => is('string', value) ? value.replace(/[A-Z]/g, v => "-".concat(v[0])).toLowerCase() : value;
export const kebabCasetoCammelCase = value => is('string', value) ? value.replace(/-./g, v => v[1] !== undefined ? v[1].toUpperCase() : '') : value;
export const capitalizedCammelCase = value => capitalize(kebabCasetoCammelCase(value));
export const capitalize = value => is('string', value) ? value.charAt(0).toUpperCase() + value.slice(1) : value;
export const is = (version, value) => {
switch (version) {
case 'string':
return typeof value === 'string';
case 'number':
return typeof value === 'number' && !Number.isNaN(value);
case 'array':
return Array.isArray(value);
case 'boolean':
return typeof value === 'boolean';
case 'null':
return value === null;
case 'undefined':
return value === undefined;
case 'object':
const isObject = typeof value === 'object' && !!value && value.constructor === Object;
return isObject;
case 'function':
return !!(value && value instanceof Function);
case 'simple':
return is('string', value) || is('number', value) || is('boolean', value) || is('undefined', value) || is('null', value);
default:
return;
}
};
export const isAmauiSubscription = value => value instanceof AmauiSubscription || is('function', value === null || value === void 0 ? void 0 : value.emit);
export const getRefs = value => {
const items = [];
if (is('string', value)) {
const regex = /\$[a-zA-Z1-9_]+/g;
items.push(...(value.match(regex) || []).map(item => item.replace('$', '')));
}
return items;
};
export const valueResolve = (property, value, amauiStyle) => {
const response = {
value: [],
options: {}
}; // Mange all the values
if (is('string', property) && !!property.length && value !== undefined && amauiStyle) {
// String
if (is('string', value)) response.value = [value]; // Number
else if (is('number', value)) {
var _amauiStyle$subscript;
const unit = (_amauiStyle$subscript = amauiStyle.subscriptions.rule.unit.map({
property,
value
})) === null || _amauiStyle$subscript === void 0 ? void 0 : _amauiStyle$subscript.value;
response.value = [(unit === null || unit === void 0 ? void 0 : unit.value) || value];
} // Array of simple
else if (is('array', value) && value.every(item => is('simple', item))) {
response.value = [value.flatMap(item => valueResolve(property, item, amauiStyle).value).join(' ')];
} // Array of arrays
// Array of objects
else if (is('array', value) && value.every(item => is('array', item) || is('object', item))) {
response.value = [value.flatMap(item => valueResolve(property, item, amauiStyle).value).join(', ')];
} // Object
else if (is('object', value)) {
// Object value
if (value.value) {
const fallbacks = (value.fallbacks || []).flatMap(item => valueResolve(property, item, amauiStyle).value);
response.value = [fallbacks, valueResolve(property, value.value, amauiStyle).value].flat().filter(Boolean);
if (value.rule) response.options.rule = value.rule;
} else {
var _amauiStyle$subscript2;
// Value plugins
const value_ = (_amauiStyle$subscript2 = amauiStyle.subscriptions.rule.value.map({
property,
value
})) === null || _amauiStyle$subscript2 === void 0 ? void 0 : _amauiStyle$subscript2.value;
response.value = value_ || [];
}
} // Method
// AmauiSubscription
// For methods and AmauiSubscription leave as is
// these are only used during add method
else response.value = [value];
}
return response;
};
export const dynamic = value => is('function', value) || isAmauiSubscription(value) || is('object', value) && Object.keys(value).some(prop => dynamic(value[prop]));
export function* makeName() {
let length_ = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 2;
let input_ = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'abcdefghijklmnopqrstuvwxyz';
const input = is('array', input_) ? input_ : input_.split('');
let length = length_;
let value;
let methodNameGenerator = variationWithRepetition(input, length, {
response: 'yield'
})();
while (true) {
var _value;
value = methodNameGenerator.next();
if ((_value = value) !== null && _value !== void 0 && _value.done) {
methodNameGenerator = variationWithRepetition(input, ++length, {
response: 'yield'
})();
value = methodNameGenerator.next();
}
yield value.value.join('');
}
}
export const pxToRem = function (value) {
let htmlFontSize = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 16;
return Number((value / htmlFontSize).toFixed(4));
};
const env = getEnvironment();
env.amaui_methods = {
makeName: makeName()
};
export const names = value => {
if (is('object', value)) {
// Update styles, className and class
if (!value.hasOwnProperty('className')) Object.defineProperty(value, 'className', {
get: function () {
return Object.keys(value.classNames).map(item => value.classNames[item]).join(' ');
}
});
if (!value.hasOwnProperty('class')) Object.defineProperty(value, 'class', {
get: function () {
return Object.keys(value.classes).map(item => value.classes[item]).join(' ');
}
});
if (!value.hasOwnProperty('styles')) value.styles = function () {
const values = [];
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
args.forEach(arg => {
if (value.classes[arg]) values.push(value.classes[arg]);
});
return values.join(' ');
};
return value;
}
return value;
};
let i = 0;
export const getID = () => "".concat(i++, "-").concat(new Date().getTime());
export const minify = value => value.replace(/\n/g, '').replace(/ ?(\{|:|,|>|~) ?/g, '$1').replace(/;(\})/g, '$1');