2024-05-02 22:12:36 +00:00
|
|
|
import { SharedUtils, toVariables } from 'primevue/themes';
|
2024-03-13 12:05:23 +00:00
|
|
|
|
|
|
|
export default {
|
2024-03-21 09:07:51 +00:00
|
|
|
regex: {
|
|
|
|
rules: {
|
|
|
|
class: {
|
|
|
|
pattern: /^\.([a-zA-Z][\w-]*)$/,
|
|
|
|
resolve(value) {
|
2024-03-22 13:02:01 +00:00
|
|
|
return { type: 'class', selector: value, matched: this.pattern.test(value.trim()) };
|
2024-03-21 09:07:51 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
attr: {
|
|
|
|
pattern: /^\[(.*)\]$/,
|
|
|
|
resolve(value) {
|
2024-03-22 13:02:01 +00:00
|
|
|
return { type: 'attr', selector: `:root${value}`, matched: this.pattern.test(value.trim()) };
|
2024-03-21 09:07:51 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
media: {
|
|
|
|
pattern: /^@media (.*)$/,
|
|
|
|
resolve(value) {
|
2024-03-22 13:02:01 +00:00
|
|
|
return { type: 'media', selector: `${value}{:root{[CSS]}}`, matched: this.pattern.test(value.trim()) };
|
2024-03-21 09:07:51 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
system: {
|
|
|
|
pattern: /^system$/,
|
|
|
|
resolve(value) {
|
2024-03-22 13:02:01 +00:00
|
|
|
return { type: 'system', selector: '@media (prefers-color-scheme: dark){:root{[CSS]}}', matched: this.pattern.test(value.trim()) };
|
2024-03-21 09:07:51 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
custom: {
|
|
|
|
resolve(value) {
|
2024-03-22 13:02:01 +00:00
|
|
|
return { type: 'custom', selector: value, matched: true };
|
2024-03-21 09:07:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
resolve(value) {
|
|
|
|
const rules = Object.keys(this.rules)
|
|
|
|
.filter((k) => k !== 'custom')
|
|
|
|
.map((r) => this.rules[r]);
|
|
|
|
|
2024-03-22 13:02:01 +00:00
|
|
|
return [value].flat().map((v) => rules.map((r) => r.resolve(v)).find((rr) => rr.matched) ?? this.rules.custom.resolve(v));
|
2024-03-21 09:07:51 +00:00
|
|
|
}
|
|
|
|
},
|
2024-05-02 22:12:36 +00:00
|
|
|
_toVariables(theme, options) {
|
|
|
|
return toVariables(theme, { prefix: options?.prefix });
|
|
|
|
},
|
2024-03-18 12:23:53 +00:00
|
|
|
getCommon({ name = '', theme = {}, params, set, defaults }) {
|
2024-05-02 22:12:36 +00:00
|
|
|
const { preset, options } = theme;
|
2024-05-14 08:35:40 +00:00
|
|
|
let primitive_css, primitive_tokens, semantic_css, semantic_tokens;
|
2024-03-13 12:05:23 +00:00
|
|
|
|
|
|
|
if (SharedUtils.object.isNotEmpty(preset)) {
|
|
|
|
const { primitive, semantic } = preset;
|
|
|
|
const { colorScheme, ...sRest } = semantic || {};
|
|
|
|
const { dark, ...csRest } = colorScheme || {};
|
2024-05-14 08:35:40 +00:00
|
|
|
const prim_var = SharedUtils.object.isNotEmpty(primitive) ? this._toVariables({ primitive }, options) : {};
|
|
|
|
const sRest_var = SharedUtils.object.isNotEmpty(sRest) ? this._toVariables({ semantic: sRest }, options) : {};
|
|
|
|
const csRest_var = SharedUtils.object.isNotEmpty(csRest) ? this._toVariables({ light: csRest }, options) : {};
|
|
|
|
const dark_var = SharedUtils.object.isNotEmpty(dark) ? this._toVariables({ dark }, options) : {};
|
|
|
|
|
|
|
|
const [prim_css, prim_tokens] = [prim_var.declarations ?? '', prim_var.tokens];
|
|
|
|
const [sRest_css, sRest_tokens] = [sRest_var.declarations ?? '', sRest_var.tokens || []];
|
|
|
|
const [csRest_css, csRest_tokens] = [csRest_var.declarations ?? '', csRest_var.tokens || []];
|
|
|
|
const [dark_css, dark_tokens] = [dark_var.declarations ?? '', dark_var.tokens || []];
|
2024-03-13 12:05:23 +00:00
|
|
|
|
2024-05-02 22:12:36 +00:00
|
|
|
primitive_css = this.transformCSS(name, prim_css, 'light', 'variable', options, set, defaults);
|
2024-05-14 08:35:40 +00:00
|
|
|
primitive_tokens = prim_tokens;
|
2024-03-27 13:13:42 +00:00
|
|
|
|
2024-05-02 22:12:36 +00:00
|
|
|
const semantic_light_css = this.transformCSS(name, `${sRest_css}${csRest_css}color-scheme:light`, 'light', 'variable', options, set, defaults);
|
|
|
|
const semantic_dark_css = this.transformCSS(name, `${dark_css}color-scheme:dark`, 'dark', 'variable', options, set, defaults);
|
2024-03-27 13:13:42 +00:00
|
|
|
|
|
|
|
semantic_css = `${semantic_light_css}${semantic_dark_css}`;
|
2024-05-14 08:35:40 +00:00
|
|
|
semantic_tokens = [...new Set([...sRest_tokens, ...csRest_tokens, ...dark_tokens])];
|
2024-03-13 12:05:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2024-05-14 08:35:40 +00:00
|
|
|
primitive: {
|
|
|
|
css: primitive_css,
|
|
|
|
tokens: primitive_tokens
|
|
|
|
},
|
|
|
|
semantic: {
|
|
|
|
css: semantic_css,
|
|
|
|
tokens: semantic_tokens
|
|
|
|
}
|
2024-03-13 12:05:23 +00:00
|
|
|
};
|
|
|
|
},
|
2024-04-01 13:08:53 +00:00
|
|
|
getPreset({ name = '', preset = {}, options, params, set, defaults, selector }) {
|
2024-04-01 15:03:24 +00:00
|
|
|
const _name = name.replace('-directive', '');
|
2024-04-01 13:08:53 +00:00
|
|
|
const { colorScheme, ...vRest } = preset;
|
2024-03-13 12:05:23 +00:00
|
|
|
const { dark, ...csRest } = colorScheme || {};
|
2024-05-14 08:35:40 +00:00
|
|
|
const vRest_var = SharedUtils.object.isNotEmpty(vRest) ? this._toVariables({ [_name]: vRest }, options) : {};
|
|
|
|
const csRest_var = SharedUtils.object.isNotEmpty(csRest) ? this._toVariables({ [_name]: csRest }, options) : {};
|
|
|
|
const dark_var = SharedUtils.object.isNotEmpty(dark) ? this._toVariables({ [_name]: dark }, options) : {};
|
|
|
|
|
|
|
|
const [vRest_css, vRest_tokens] = [vRest_var.declarations ?? '', vRest_var.tokens || []];
|
|
|
|
const [csRest_css, csRest_tokens] = [csRest_var.declarations ?? '', csRest_var.tokens || []];
|
|
|
|
const [dark_css, dark_tokens] = [dark_var.declarations ?? '', dark_var.tokens || []];
|
|
|
|
const tokens = [...new Set([...vRest_tokens, ...csRest_tokens, ...dark_tokens])];
|
2024-03-13 12:05:23 +00:00
|
|
|
|
2024-05-02 22:12:36 +00:00
|
|
|
const light_variable_css = this.transformCSS(_name, `${vRest_css}${csRest_css}`, 'light', 'variable', options, set, defaults, selector);
|
|
|
|
const dark_variable_css = this.transformCSS(_name, dark_css, 'dark', 'variable', options, set, defaults, selector);
|
2024-03-31 05:10:59 +00:00
|
|
|
|
2024-05-14 08:35:40 +00:00
|
|
|
return {
|
|
|
|
css: `${light_variable_css}${dark_variable_css}`,
|
|
|
|
tokens
|
|
|
|
};
|
2024-03-13 12:05:23 +00:00
|
|
|
},
|
2024-04-01 13:08:53 +00:00
|
|
|
getPresetC({ name = '', theme = {}, params, set, defaults }) {
|
|
|
|
const { preset, options } = theme;
|
|
|
|
const cPreset = preset?.components?.[name];
|
|
|
|
|
|
|
|
return this.getPreset({ name, preset: cPreset, options, params, set, defaults });
|
|
|
|
},
|
2024-03-18 12:23:53 +00:00
|
|
|
getPresetD({ name = '', theme = {}, params, set, defaults }) {
|
2024-03-31 15:25:39 +00:00
|
|
|
const dName = name.replace('-directive', '');
|
2024-03-22 13:02:01 +00:00
|
|
|
const { preset, options } = theme;
|
2024-04-01 13:08:53 +00:00
|
|
|
const dPreset = preset?.directives?.[dName];
|
2024-03-13 12:05:23 +00:00
|
|
|
|
2024-04-01 13:08:53 +00:00
|
|
|
return this.getPreset({ name: dName, preset: dPreset, options, params, set, defaults });
|
2024-03-13 12:05:23 +00:00
|
|
|
},
|
2024-03-21 09:07:51 +00:00
|
|
|
getColorSchemeOption(options, defaults) {
|
2024-03-22 13:02:01 +00:00
|
|
|
return this.regex.resolve(options.darkModeSelector ?? defaults.options.darkModeSelector);
|
2024-03-13 12:05:23 +00:00
|
|
|
},
|
2024-03-18 12:23:53 +00:00
|
|
|
getLayerOrder(name, options = {}, params, defaults) {
|
2024-03-21 09:07:51 +00:00
|
|
|
const { cssLayer } = options;
|
2024-03-18 10:57:17 +00:00
|
|
|
|
2024-03-21 09:07:51 +00:00
|
|
|
if (cssLayer) {
|
2024-03-31 05:10:59 +00:00
|
|
|
const order = SharedUtils.object.getItemValue(cssLayer.order || 'primeui', params);
|
2024-03-18 10:57:17 +00:00
|
|
|
|
|
|
|
return `@layer ${order}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return '';
|
|
|
|
},
|
2024-03-18 12:23:53 +00:00
|
|
|
getCommonStyleSheet({ name = '', theme = {}, params, props = {}, set, defaults }) {
|
2024-05-14 08:35:40 +00:00
|
|
|
const { primitive, semantic } = this.getCommon({ name, theme, params, set, defaults });
|
|
|
|
const common_css = `${primitive.css}${semantic.css}`;
|
2024-03-13 12:05:23 +00:00
|
|
|
const _props = Object.entries(props)
|
|
|
|
.reduce((acc, [k, v]) => acc.push(`${k}="${v}"`) && acc, [])
|
|
|
|
.join(' ');
|
|
|
|
|
|
|
|
return Object.entries(common_css || {})
|
|
|
|
.reduce((acc, [key, value]) => {
|
|
|
|
if (value) {
|
|
|
|
const _css = SharedUtils.object.minifyCSS(value);
|
2024-05-02 22:12:36 +00:00
|
|
|
const id = `${key}-variables`;
|
2024-03-13 12:05:23 +00:00
|
|
|
|
2024-03-30 09:08:00 +00:00
|
|
|
acc.push(`<style type="text/css" data-primevue-style-id="${id}" ${_props}>${_css}</style>`);
|
2024-03-13 12:05:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
}, [])
|
|
|
|
.join('');
|
|
|
|
},
|
2024-03-18 12:23:53 +00:00
|
|
|
getStyleSheet({ name = '', theme = {}, params, props = {}, set, defaults }) {
|
2024-05-14 08:35:40 +00:00
|
|
|
const presetC_css = this.getPresetC({ name, theme, params, set, defaults })?.css;
|
2024-03-13 12:05:23 +00:00
|
|
|
const _props = Object.entries(props)
|
|
|
|
.reduce((acc, [k, v]) => acc.push(`${k}="${v}"`) && acc, [])
|
|
|
|
.join(' ');
|
|
|
|
|
2024-05-02 22:12:36 +00:00
|
|
|
return presetC_css ? `<style type="text/css" data-primevue-style-id="${name}-variables" ${_props}>${SharedUtils.object.minifyCSS(presetC_css)}</style>` : '';
|
2024-03-13 12:05:23 +00:00
|
|
|
},
|
2024-03-22 13:02:01 +00:00
|
|
|
createTokens(obj = {}, defaults, parentKey = '', parentPath = '', tokens = {}) {
|
2024-03-19 12:50:54 +00:00
|
|
|
Object.entries(obj).forEach(([key, value]) => {
|
|
|
|
const currentKey = SharedUtils.object.test(defaults.variable.excludedKeyRegex, key) ? parentKey : parentKey ? `${parentKey}.${SharedUtils.object.toTokenKey(key)}` : SharedUtils.object.toTokenKey(key);
|
|
|
|
const currentPath = parentPath ? `${parentPath}.${key}` : key;
|
|
|
|
|
|
|
|
if (SharedUtils.object.isObject(value)) {
|
2024-03-22 13:02:01 +00:00
|
|
|
this.createTokens(value, defaults, currentKey, currentPath, tokens);
|
2024-03-19 12:50:54 +00:00
|
|
|
} else {
|
|
|
|
tokens[currentKey] ||= {
|
|
|
|
paths: [],
|
2024-03-25 13:28:24 +00:00
|
|
|
computed(colorScheme, tokenPathMap = {}) {
|
|
|
|
if (colorScheme) {
|
|
|
|
const path = this.paths.find((p) => p.scheme === colorScheme) || this.paths.find((p) => p.scheme === 'none');
|
2024-03-19 12:50:54 +00:00
|
|
|
|
2024-04-15 09:17:29 +00:00
|
|
|
return path?.computed(colorScheme, tokenPathMap['binding']);
|
2024-03-25 13:28:24 +00:00
|
|
|
}
|
|
|
|
|
2024-04-15 09:17:29 +00:00
|
|
|
return this.paths.map((p) => p.computed(p.scheme, tokenPathMap[p.scheme]));
|
2024-03-19 12:50:54 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
tokens[currentKey].paths.push({
|
|
|
|
path: currentPath,
|
|
|
|
value,
|
|
|
|
scheme: currentPath.includes('colorScheme.light') ? 'light' : currentPath.includes('colorScheme.dark') ? 'dark' : 'none',
|
2024-03-25 13:28:24 +00:00
|
|
|
computed(colorScheme, tokenPathMap = {}) {
|
2024-03-19 12:50:54 +00:00
|
|
|
const regex = /{([^}]*)}/g;
|
2024-03-25 13:28:24 +00:00
|
|
|
let computedValue = value;
|
|
|
|
|
2024-04-15 09:17:29 +00:00
|
|
|
tokenPathMap['name'] = this.path;
|
|
|
|
tokenPathMap['binding'] ||= {};
|
2024-03-19 12:50:54 +00:00
|
|
|
|
|
|
|
if (SharedUtils.object.test(regex, value)) {
|
|
|
|
const val = value.trim();
|
|
|
|
const _val = val.replaceAll(regex, (v) => {
|
|
|
|
const path = v.replace(/{|}/g, '');
|
|
|
|
|
2024-03-25 13:28:24 +00:00
|
|
|
return tokens[path]?.computed(colorScheme, tokenPathMap)?.value;
|
2024-03-19 12:50:54 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const calculationRegex = /(\d+\w*\s+[\+\-\*\/]\s+\d+\w*)/g;
|
|
|
|
const cleanedVarRegex = /var\([^)]+\)/g;
|
|
|
|
|
2024-03-25 13:28:24 +00:00
|
|
|
computedValue = SharedUtils.object.test(calculationRegex, _val.replace(cleanedVarRegex, '0')) ? `calc(${_val})` : _val;
|
2024-03-19 12:50:54 +00:00
|
|
|
}
|
|
|
|
|
2024-04-15 09:17:29 +00:00
|
|
|
SharedUtils.object.isEmpty(tokenPathMap['binding']) && delete tokenPathMap['binding'];
|
2024-03-25 13:28:24 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
colorScheme,
|
2024-04-15 09:17:29 +00:00
|
|
|
path: this.path,
|
|
|
|
paths: tokenPathMap,
|
2024-03-25 13:28:24 +00:00
|
|
|
value: computedValue.includes('undefined') ? undefined : computedValue
|
|
|
|
};
|
2024-03-19 12:50:54 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return tokens;
|
|
|
|
},
|
|
|
|
getTokenValue(tokens, path, defaults) {
|
|
|
|
const normalizePath = (str) => {
|
|
|
|
const strArr = str.split('.');
|
|
|
|
|
|
|
|
return strArr.filter((s) => !SharedUtils.object.test(defaults.variable.excludedKeyRegex, s.toLowerCase())).join('.');
|
|
|
|
};
|
|
|
|
|
|
|
|
const token = normalizePath(path);
|
|
|
|
const colorScheme = path.includes('colorScheme.light') ? 'light' : path.includes('colorScheme.dark') ? 'dark' : undefined;
|
2024-04-15 09:17:29 +00:00
|
|
|
const computedValues = [tokens[token]?.computed(colorScheme)].flat().filter((computed) => computed);
|
2024-03-19 12:50:54 +00:00
|
|
|
|
2024-04-15 09:17:29 +00:00
|
|
|
return computedValues.length === 1
|
|
|
|
? computedValues[0].value
|
|
|
|
: computedValues.reduce((acc = {}, computed) => {
|
|
|
|
const { colorScheme: cs, ...rest } = computed;
|
|
|
|
|
|
|
|
acc[cs] = rest;
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
}, undefined);
|
2024-03-21 09:07:51 +00:00
|
|
|
},
|
2024-05-02 22:12:36 +00:00
|
|
|
transformCSS(name, css, mode, type, options = {}, set, defaults, selector) {
|
2024-03-21 09:07:51 +00:00
|
|
|
if (SharedUtils.object.isNotEmpty(css)) {
|
|
|
|
const { cssLayer } = options;
|
|
|
|
|
|
|
|
if (type !== 'style') {
|
|
|
|
const colorSchemeOption = this.getColorSchemeOption(options, defaults);
|
2024-04-01 13:08:53 +00:00
|
|
|
const _css = selector ? SharedUtils.object.getRule(selector, css) : css;
|
2024-03-21 09:07:51 +00:00
|
|
|
|
|
|
|
css =
|
|
|
|
mode === 'dark'
|
2024-04-01 13:08:53 +00:00
|
|
|
? colorSchemeOption.reduce((acc, { selector: _selector }) => {
|
|
|
|
if (SharedUtils.object.isNotEmpty(_selector)) {
|
|
|
|
acc += _selector.includes('[CSS]') ? _selector.replace('[CSS]', _css) : SharedUtils.object.getRule(_selector, _css);
|
2024-03-21 09:07:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
}, '')
|
2024-04-01 13:08:53 +00:00
|
|
|
: SharedUtils.object.getRule(selector ?? ':root', css);
|
2024-03-21 09:07:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (cssLayer) {
|
2024-03-31 05:10:59 +00:00
|
|
|
const layerOptions = {
|
|
|
|
name: 'primeui',
|
|
|
|
order: 'primeui'
|
|
|
|
};
|
2024-03-21 09:07:51 +00:00
|
|
|
|
|
|
|
SharedUtils.object.isObject(cssLayer) && (layerOptions.name = SharedUtils.object.getItemValue(cssLayer.name, { name, type }));
|
|
|
|
|
|
|
|
if (SharedUtils.object.isNotEmpty(layerOptions.name)) {
|
|
|
|
css = SharedUtils.object.getRule(`@layer ${layerOptions.name}`, css);
|
|
|
|
set?.layerNames(layerOptions.name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return css;
|
|
|
|
}
|
|
|
|
|
|
|
|
return '';
|
2024-03-13 12:05:23 +00:00
|
|
|
}
|
|
|
|
};
|