tailwindcss-capsize
Version:
TailwindCSS leading-trim utility classes.
89 lines (88 loc) • 3.25 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isRelativeValue = isRelativeValue;
exports.getRelativeValue = getRelativeValue;
exports.normalizeValue = normalizeValue;
exports.getValueAndUnit = getValueAndUnit;
exports.isPlainObject = isPlainObject;
exports.normalizeThemeValue = normalizeThemeValue;
exports.round = round;
exports.lineHeightProperties = lineHeightProperties;
function isRelativeValue(value) {
value = String(value);
let isPercentValue = value.endsWith('%');
let isUnitlessValue = /\d$/.test(value);
return isPercentValue || isUnitlessValue;
}
function getRelativeValue(value) {
value = String(value);
let isPercentValue = value.endsWith('%');
return isPercentValue
? Number.parseInt(value.replace('%', ''), 10) / 100
: Number.parseFloat(value);
}
function normalizeValue(value, root) {
value = Array.isArray(value) ? String(value[0]) : String(value);
if (value.endsWith('px'))
return Number.parseFloat(value.replace('px', ''));
if (value.endsWith('rem'))
return root * Number.parseFloat(value.replace('rem', ''));
return Number.parseInt(value, 10);
}
const cssRegex = /^([+-]?(?:\d+|\d*\.\d+))([a-z]*|%)$/;
function getValueAndUnit(value) {
if (typeof value !== 'string')
return [value, ''];
let matchedValue = cssRegex.exec(value);
if (matchedValue !== null)
return [Number.parseFloat(value), matchedValue[2]];
return [Number.parseInt(value, 10), undefined];
}
function isPlainObject(value) {
if (Object.prototype.toString.call(value) !== '[object Object]') {
return false;
}
let prototype = Object.getPrototypeOf(value);
return prototype === null || prototype === Object.prototype;
}
/**
* Returns a value based on the core theme section. Adapted from Tailwind
* internals.
* @see https://github.com/tailwindlabs/tailwindcss/blob/30ea5b14a631b6f68e56740c3d09bb54fcbad08a/src/util/transformThemeValue.js
*/
function normalizeThemeValue(key, value) {
switch (key) {
case 'fontSize':
case 'lineHeight':
if (typeof value === 'function')
value = value({});
if (Array.isArray(value))
value = value[0];
return value;
case 'fontFamily':
if (typeof value === 'function')
value = value({});
if (Array.isArray(value))
value = value.join(', ');
return value;
default:
if (typeof value === 'function')
value = value({});
return value;
}
}
function round(value) {
return Number.parseFloat(value.toFixed(4)).toString();
}
/**
* Takes a theme value for lineHeight and returns the `--line-height-offset`
* custom property.
*/
function lineHeightProperties(lineHeight, rootSize = 16) {
let lineHeightActual = isRelativeValue(lineHeight)
? `calc(${getRelativeValue(lineHeight).toString()} * var(--font-size-px))`
: normalizeValue(lineHeight, rootSize).toString();
return {
'--line-height-offset': `calc((((var(--line-height-scale) * var(--font-size-px)) - ${lineHeightActual}) / 2) / var(--font-size-px))`,
};
}