2024-05-13 21:57:53 +00:00
|
|
|
import { SharedUtils, Theme } from 'primevue/themes';
|
2024-02-19 22:53:42 +00:00
|
|
|
|
|
|
|
export default function (theme, options = {}) {
|
2024-03-27 13:12:22 +00:00
|
|
|
const VARIABLE = Theme.defaults.variable;
|
2024-03-13 12:05:23 +00:00
|
|
|
const { prefix = VARIABLE.prefix, selector = VARIABLE.selector, excludedKeyRegex = VARIABLE.excludedKeyRegex } = options;
|
2024-02-19 22:53:42 +00:00
|
|
|
|
|
|
|
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-02-19 22:53:42 +00:00
|
|
|
|
2024-05-14 08:35:40 +00:00
|
|
|
if (SharedUtils.object.isObject(v)) {
|
|
|
|
const { variables, tokens } = _toVariables(v, px);
|
2024-02-19 22:53:42 +00:00
|
|
|
|
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-02-19 22:53:42 +00:00
|
|
|
|
2024-05-14 08:35:40 +00:00
|
|
|
return acc;
|
|
|
|
},
|
|
|
|
{ variables: [], tokens: [] }
|
|
|
|
);
|
2024-02-19 22:53:42 +00:00
|
|
|
};
|
|
|
|
|
2024-05-14 08:35:40 +00:00
|
|
|
const { variables, tokens } = _toVariables(theme, prefix);
|
2024-02-19 22:53:42 +00:00
|
|
|
|
|
|
|
return {
|
2024-05-14 08:35:40 +00:00
|
|
|
value: variables,
|
|
|
|
tokens,
|
|
|
|
declarations: variables.join(''),
|
|
|
|
css: SharedUtils.object.getRule(selector, variables.join(''))
|
2024-02-19 22:53:42 +00:00
|
|
|
};
|
|
|
|
}
|