primevue-mirror/components/lib/themes/helpers/toVariables.js

32 lines
1.2 KiB
JavaScript
Raw Normal View History

2024-03-13 12:05:23 +00:00
import Theme, { SharedUtils } from 'primevue/themes';
export default function (theme, options = {}) {
const VARIABLE = Theme.defaults.variable;
2024-03-13 12:05:23 +00:00
const { prefix = VARIABLE.prefix, selector = VARIABLE.selector, excludedKeyRegex = VARIABLE.excludedKeyRegex } = options;
const _toVariables = (_theme, _prefix = '') => {
return Object.entries(_theme).reduce((acc, [key, value]) => {
2024-03-05 09:22:33 +00:00
const px = SharedUtils.object.test(excludedKeyRegex, key) ? SharedUtils.object.toNormalizeVariable(_prefix) : SharedUtils.object.toNormalizeVariable(_prefix, SharedUtils.object.toKebabCase(key));
const v = SharedUtils.object.toValue(value);
if (SharedUtils.object.isObject(v)) {
const variables = _toVariables(v, px);
SharedUtils.object.merge(acc, variables);
} else {
2024-03-05 09:22:33 +00:00
SharedUtils.object.setProperty(acc, SharedUtils.object.getVariableName(px), SharedUtils.object.getVariableValue(v, px, prefix, [excludedKeyRegex]));
}
return acc;
}, []);
};
const value = _toVariables(theme, prefix);
return {
value,
2024-03-05 09:22:33 +00:00
declarations: value.join(''),
css: SharedUtils.object.getRule(selector, value.join(''))
};
}