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

38 lines
1.6 KiB
JavaScript
Raw Normal View History

2024-05-13 21:57:53 +00:00
import { SharedUtils, Theme } 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 = '') => {
2024-05-14 08:35:40 +00:00
return Object.entries(_theme).reduce(
(acc, [key, value]) => {
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);
2024-05-14 08:35:40 +00:00
if (SharedUtils.object.isObject(v)) {
const { variables, tokens } = _toVariables(v, px);
2024-05-14 08:35:40 +00:00
SharedUtils.object.merge(acc['tokens'], tokens);
SharedUtils.object.merge(acc['variables'], variables);
} else {
acc['tokens'].push((prefix ? px.replace(`${prefix}-`, '') : px).replaceAll('-', '.'));
SharedUtils.object.setProperty(acc['variables'], SharedUtils.object.getVariableName(px), SharedUtils.object.getVariableValue(v, px, prefix, [excludedKeyRegex]));
}
2024-05-14 08:35:40 +00:00
return acc;
},
{ variables: [], tokens: [] }
);
};
2024-05-14 08:35:40 +00:00
const { variables, tokens } = _toVariables(theme, prefix);
return {
2024-05-14 08:35:40 +00:00
value: variables,
tokens,
declarations: variables.join(''),
css: SharedUtils.object.getRule(selector, variables.join(''))
};
}