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

34 lines
1.1 KiB
JavaScript
Raw Normal View History

2024-05-13 21:57:53 +00:00
import { SharedUtils, Theme } from 'primevue/themes';
2024-03-05 09:22:33 +00:00
export const $dt = (tokenPath) => {
2024-03-31 04:44:48 +00:00
const theme = Theme.getTheme();
2024-03-05 09:22:33 +00:00
const variable = dtwt(theme, tokenPath, undefined, 'variable');
const name = variable.match(/--[\w-]+/g)?.[0];
const value = dtwt(theme, tokenPath, undefined, 'value');
return {
name,
variable,
value
};
2024-03-05 09:22:33 +00:00
};
export const dt = (...args) => {
return dtwt(Theme.getTheme(), ...args);
};
export const dtwt = (theme = {}, tokenPath, fallback, type = 'variable') => {
2024-03-05 09:22:33 +00:00
if (tokenPath) {
const VARIABLE = Theme.defaults.variable;
2024-03-05 09:22:33 +00:00
const { prefix, transform } = theme?.options || {};
const regex = /{([^}]*)}/g;
const token = SharedUtils.object.test(regex, tokenPath) ? tokenPath : `{${tokenPath}}`;
const isStrictTransform = type === 'value' || transform === 'strict'; // @todo - TRANSFORM: strict | lenient(default)
2024-03-05 09:22:33 +00:00
return isStrictTransform ? Theme.getTokenValue(tokenPath) : SharedUtils.object.getVariableValue(token, undefined, prefix, [VARIABLE.excludedKeyRegex], fallback);
2024-03-05 09:22:33 +00:00
}
return '';
};