tailwindcss-rem2px-preset
Version:
tailwindcss-rem2px-preset, allow configuration for using px instead of rem.
60 lines (55 loc) • 1.96 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
// https://github.com/tailwindlabs/tailwindcss/issues/1232
function rem2px(input, fontSize = 16, unit = 'px') {
if (input == null) {
return input;
}
switch (typeof input) {
case 'object':
if (Array.isArray(input)) {
return input.map((val) => rem2px(val, fontSize, unit));
}
else {
const ret = {};
for (const key in input) {
ret[key] = rem2px(input[key], fontSize, unit);
}
return ret;
}
case 'string':
return input.replace(/(\d*\.?\d+)rem$/, (_, val) => parseFloat(val) * fontSize + unit);
default:
return input;
}
}
const defaultTheme = require('tailwindcss/defaultTheme');
const defaultOptions = {
fontSize: 16,
unit: 'px'
};
function createRem2px(option = {}) {
return (input) => {
return rem2px(input, option.fontSize, option.unit);
};
}
function createPreset(option = {}) {
const opt = Object.assign({}, defaultOptions, option);
const customRem2px = createRem2px(opt);
const config = {
content: ['src/**/*.{vue,js,ts,jsx,tsx}'],
theme: {
borderRadius: customRem2px(defaultTheme.borderRadius),
columns: customRem2px(defaultTheme.columns),
fontSize: customRem2px(defaultTheme.fontSize),
lineHeight: customRem2px(defaultTheme.lineHeight),
maxWidth: ({ theme, breakpoints }) => (Object.assign({}, customRem2px(defaultTheme.maxWidth({ theme, breakpoints })))),
spacing: customRem2px(defaultTheme.spacing)
}
};
return config;
}
exports = module.exports = createPreset();
exports.createPreset = createPreset;
exports.createRem2px = createRem2px;
exports.rem2px = rem2px;