Theming API: update nuxt-primevue module

pull/5507/head
mertsincan 2024-03-29 11:25:15 +00:00
parent b5e53e6fa3
commit a5dff7722e
11 changed files with 46 additions and 50 deletions

View File

@ -40,14 +40,14 @@ export default {
loadTheme(theme, options = {}) { loadTheme(theme, options = {}) {
return theme ? useStyle(ObjectUtils.minifyCSS(theme), { name: `${this.name}-style`, ...options }) : {}; return theme ? useStyle(ObjectUtils.minifyCSS(theme), { name: `${this.name}-style`, ...options }) : {};
}, },
getCommonThemeCSS(theme, params) { getCommonThemeCSS(params) {
return Theme.getCommonCSS(this.name, theme, params); return Theme.getCommonCSS(this.name, params);
}, },
getComponentThemeCSS(theme, params) { getComponentThemeCSS(params) {
return Theme.getComponentCSS(this.name, theme, params); return Theme.getComponentCSS(this.name, params);
}, },
getDirectiveThemeCSS(theme, params) { getDirectiveThemeCSS(params) {
return Theme.getDirectiveCSS(this.name, theme, params); return Theme.getDirectiveCSS(this.name, params);
}, },
getLayerOrderThemeCSS() { getLayerOrderThemeCSS() {
return Theme.getLayerOrderCSS(this.name); return Theme.getLayerOrderCSS(this.name);
@ -64,11 +64,11 @@ export default {
return ''; return '';
}, },
getCommonThemeStyleSheet(theme = {}, params, props = {}) { getCommonThemeStyleSheet(params, props = {}) {
return Theme.getCommonStyleSheet(this.name, theme, params, props); return Theme.getCommonStyleSheet(this.name, params, props);
}, },
getThemeStyleSheet(theme = {}, params, props = {}) { getThemeStyleSheet(params, props = {}) {
return Theme.getStyleSheet(this.name, theme, params, props); return Theme.getStyleSheet(this.name, params, props);
}, },
extend(style) { extend(style) {
return { ...this, css: undefined, ...style }; return { ...this, css: undefined, ...style };

View File

@ -40,7 +40,8 @@ export default {
deep: true, deep: true,
immediate: true, immediate: true,
handler(newValue) { handler(newValue) {
this._loadThemeStyles(newValue); Theme.setTheme(newValue);
this._loadThemeStyles();
} }
} }
}, },
@ -114,16 +115,16 @@ export default {
ObjectUtils.isNotEmpty(globalCSS) && BaseComponentStyle.loadGlobalStyle(globalCSS, this.$styleOptions); ObjectUtils.isNotEmpty(globalCSS) && BaseComponentStyle.loadGlobalStyle(globalCSS, this.$styleOptions);
}, },
_loadThemeStyles(theme) { _loadThemeStyles() {
// common // common
const { primitive, semantic, global } = this.$style?.getCommonThemeCSS?.(theme, this.$themeParams) || {}; const { primitive, semantic, global } = this.$style?.getCommonThemeCSS?.(this.$themeParams) || {};
BaseStyle.loadTheme(primitive, { name: 'primitive-variables', ...this.$styleOptions }); BaseStyle.loadTheme(primitive, { name: 'primitive-variables', ...this.$styleOptions });
BaseStyle.loadTheme(semantic, { name: 'semantic-variables', ...this.$styleOptions }); BaseStyle.loadTheme(semantic, { name: 'semantic-variables', ...this.$styleOptions });
BaseComponentStyle.loadGlobalTheme(global, this.$styleOptions); BaseComponentStyle.loadGlobalTheme(global, this.$styleOptions);
// component // component
const { variables, style } = this.$style?.getComponentThemeCSS?.(theme, this.$themeParams) || {}; const { variables, style } = this.$style?.getComponentThemeCSS?.(this.$themeParams) || {};
this.$style?.loadTheme(variables, { name: `${this.$style.name}-variables`, ...this.$styleOptions }); this.$style?.loadTheme(variables, { name: `${this.$style.name}-variables`, ...this.$styleOptions });
this.$style?.loadTheme(style, this.$styleOptions); this.$style?.loadTheme(style, this.$styleOptions);

View File

@ -82,20 +82,21 @@ const BaseDirective = {
// apply theme settings // apply theme settings
Theme.init(); Theme.init();
Theme.setTheme(el.$instance?.theme());
BaseDirective._loadThemeStyles(el.$instance, { nonce: config?.csp?.nonce }); BaseDirective._loadThemeStyles(el.$instance, { nonce: config?.csp?.nonce });
}, },
_loadThemeStyles: (instance = {}, useStyleOptions) => { _loadThemeStyles: (instance = {}, useStyleOptions) => {
const [theme, themeParams] = [instance.theme(), instance.themeParams()]; const themeParams = instance.themeParams();
// common // common
const { primitive, semantic, global } = instance.$style?.getCommonThemeCSS?.(theme, themeParams) || {}; const { primitive, semantic, global } = instance.$style?.getCommonThemeCSS?.(themeParams) || {};
BaseStyle.loadTheme(primitive, { name: 'primitive-variables', ...useStyleOptions }); BaseStyle.loadTheme(primitive, { name: 'primitive-variables', ...useStyleOptions });
BaseStyle.loadTheme(semantic, { name: 'semantic-variables', ...useStyleOptions }); BaseStyle.loadTheme(semantic, { name: 'semantic-variables', ...useStyleOptions });
BaseStyle.loadTheme(global, { name: 'global-style', ...useStyleOptions }); BaseStyle.loadTheme(global, { name: 'global-style', ...useStyleOptions });
// component // component
const { variables, style } = instance.$style?.getDirectiveThemeCSS?.(theme, themeParams) || {}; const { variables, style } = instance.$style?.getDirectiveThemeCSS?.(themeParams) || {};
instance.$style?.loadTheme(variables, { name: `${instance.$name}-directive-variables`, ...useStyleOptions }); instance.$style?.loadTheme(variables, { name: `${instance.$name}-directive-variables`, ...useStyleOptions });
instance.$style?.loadTheme(style, { name: `${instance.$name}-directive-style`, ...useStyleOptions }); instance.$style?.loadTheme(style, { name: `${instance.$name}-directive-style`, ...useStyleOptions });

View File

@ -141,15 +141,8 @@ export const defaultOptions = {
preset: Aura, preset: Aura,
options: { options: {
prefix: 'p', prefix: 'p',
darkModeSelector: '.p-dark', darkModeSelector: 'system',
cssLayer: false cssLayer: false
/*
darkModeSelector: '.p-dark | [data-p-dark="true"] | system(default)',
cssLayer: {
// cssLayer: true | false | undefined | object // default: undefined
name: '', // ({component_name, type=variable|style}) => layer_name // default: primevue
order: '' // ({layer_names}) => layer_order // default: @layer primevue
}*/
} }
}, },
pt: undefined, pt: undefined,

View File

@ -83,19 +83,19 @@ export default {
setLayerNames(layerName) { setLayerNames(layerName) {
this._layerNames?.add(layerName); this._layerNames?.add(layerName);
}, },
getCommonCSS(name = '', theme, params) { getCommonCSS(name = '', params) {
return ThemeUtils.getCommon({ name, theme: theme || this.theme, params, defaults: this.defaults, set: { layerNames: this.setLayerNames.bind(this) } }); return ThemeUtils.getCommon({ name, theme: this.theme, params, defaults: this.defaults, set: { layerNames: this.setLayerNames.bind(this) } });
}, },
getComponentCSS(name = '', theme, params) { getComponentCSS(name = '', params) {
const options = { name, theme: theme || this.theme, params, defaults: this.defaults, set: { layerNames: this.setLayerNames.bind(this) } }; const options = { name, theme: this.theme, params, defaults: this.defaults, set: { layerNames: this.setLayerNames.bind(this) } };
return { return {
style: ThemeUtils.getBaseC(options), style: ThemeUtils.getBaseC(options),
variables: ThemeUtils.getPresetC(options) variables: ThemeUtils.getPresetC(options)
}; };
}, },
getDirectiveCSS(name = '', theme, params) { getDirectiveCSS(name = '', params) {
const options = { name, theme: theme || this.theme, params, defaults: this.defaults, set: { layerNames: this.setLayerNames.bind(this) } }; const options = { name, theme: this.theme, params, defaults: this.defaults, set: { layerNames: this.setLayerNames.bind(this) } };
return { return {
style: ThemeUtils.getBaseD(options), style: ThemeUtils.getBaseD(options),
@ -105,10 +105,10 @@ export default {
getLayerOrderCSS(name = '') { getLayerOrderCSS(name = '') {
return ThemeUtils.getLayerOrder(name, this.options, { names: this.getLayerNames() }, this.defaults); return ThemeUtils.getLayerOrder(name, this.options, { names: this.getLayerNames() }, this.defaults);
}, },
getCommonStyleSheet(name = '', theme, params, props = {}) { getCommonStyleSheet(name = '', params, props = {}) {
return ThemeUtils.getCommonStyleSheet({ name, theme, params, props, defaults: this.defaults, set: { layerNames: this.setLayerNames.bind(this) } }); return ThemeUtils.getCommonStyleSheet({ name, theme: this.theme, params, props, defaults: this.defaults, set: { layerNames: this.setLayerNames.bind(this) } });
}, },
getStyleSheet(name, theme = {}, params, props = {}) { getStyleSheet(name, params, props = {}) {
return ThemeUtils.getStyleSheet({ name, theme, params, props, defaults: this.defaults, set: { layerNames: this.setLayerNames.bind(this) } }); return ThemeUtils.getStyleSheet({ name, theme: this.theme, params, props, defaults: this.defaults, set: { layerNames: this.setLayerNames.bind(this) } });
} }
}; };

View File

@ -5,7 +5,7 @@ const types = ['value', 'variable'];
export const $dt = (tokenPath, param1, param2) => { export const $dt = (tokenPath, param1, param2) => {
const config = Theme.getPConfig(); const config = Theme.getPConfig();
return types.includes(param1) ? dt(config?.theme, tokenPath, undefined, param2) : dt(config?.theme, tokenPath, param1, param2); return types.includes(param1) ? dt(config?.theme, tokenPath, undefined, param1) : dt(config?.theme, tokenPath, param1, param2);
}; };
export const dt = (theme = {}, tokenPath, fallback, type = 'variable') => { export const dt = (theme = {}, tokenPath, fallback, type = 'variable') => {

View File

@ -94,13 +94,7 @@ export default {
if (type === 'primary') this.selectedPrimaryColor = color.name; if (type === 'primary') this.selectedPrimaryColor = color.name;
else if (type === 'surface') this.selectedSurfaceColor = color.name; else if (type === 'surface') this.selectedSurfaceColor = color.name;
if (!document.startViewTransition) {
this.applyTheme(type, color); this.applyTheme(type, color);
return;
}
document.startViewTransition(() => this.applyTheme(type, color));
}, },
applyTheme(type, color) { applyTheme(type, color) {
if (type === 'primary') { if (type === 'primary') {

View File

@ -51,7 +51,12 @@ export default defineNuxtModule({
const styleContent = () => ` const styleContent = () => `
${registered.styles.map((style) => `import ${style.as} from '${style.from}';`).join('\n')} ${registered.styles.map((style) => `import ${style.as} from '${style.from}';`).join('\n')}
${importTheme ? `import ${importTheme.as} from '${importTheme.from}';\n` : ''} ${
importTheme
? `import Theme from 'primevue/themes';
import ${importTheme.as} from '${importTheme.from}';\n`
: ''
}
const styleProps = { const styleProps = {
${options?.csp?.nonce ? `nonce: ${options?.csp?.nonce}` : ''} ${options?.csp?.nonce ? `nonce: ${options?.csp?.nonce}` : ''}
@ -61,9 +66,11 @@ const styles = [
${registered.styles.map((item) => `${item.as} && ${item.as}.getStyleSheet ? ${item.as}.getStyleSheet(undefined, styleProps) : ''`).join(',')} ${registered.styles.map((item) => `${item.as} && ${item.as}.getStyleSheet ? ${item.as}.getStyleSheet(undefined, styleProps) : ''`).join(',')}
].join(''); ].join('');
${importTheme ? `Theme.setTheme(${importTheme.as})` : ''}
const themes = [ const themes = [
${importTheme ? `${registered.styles[0].as} && ${registered.styles[0].as}.getCommonThemeStyleSheet ? ${registered.styles[0].as}.getCommonThemeStyleSheet(${importTheme?.as}, undefined, styleProps) : ''` : ''}, ${importTheme ? `${registered.styles[0].as} && ${registered.styles[0].as}.getCommonThemeStyleSheet ? ${registered.styles[0].as}.getCommonThemeStyleSheet(undefined, styleProps) : ''` : ''},
${importTheme ? registered.styles.map((item) => `${item.as} && ${item.as}.getThemeStyleSheet ? ${item.as}.getThemeStyleSheet(${importTheme?.as}, undefined, styleProps) : ''`).join(',') : ''} ${importTheme ? registered.styles.map((item) => `${item.as} && ${item.as}.getThemeStyleSheet ? ${item.as}.getThemeStyleSheet(undefined, styleProps) : ''`).join(',') : ''}
].join(''); ].join('');
export { styles, themes }; export { styles, themes };

View File

@ -1,5 +1,5 @@
// @ts-expect-error // @ts-expect-error
import { styles } from '#primevue-style'; import { styles, themes } from '#primevue-style';
//import { useRuntimeConfig } from '#imports'; //import { useRuntimeConfig } from '#imports';
const defineNitroPlugin = (def) => def; const defineNitroPlugin = (def) => def;
@ -7,7 +7,7 @@ const defineNitroPlugin = (def) => def;
export default defineNitroPlugin(async (nitroApp) => { export default defineNitroPlugin(async (nitroApp) => {
nitroApp.hooks.hook('render:html', (html) => { nitroApp.hooks.hook('render:html', (html) => {
html.head.push(styles); html.head.push(styles);
//html.head.push(themes); html.head.push(themes);
//html.htmlAttrs.push('class="p-dark"'); // @todo //html.htmlAttrs.push('class="p-dark"'); // @todo
}); });
}); });

View File

@ -26,7 +26,7 @@ export default defineNuxtConfig({
ripple: true ripple: true
}, },
layerOrder: 'primevue', layerOrder: 'primevue',
//importTheme: { from: '@/mytheme.js' }, importTheme: { from: '@/themes/aura.js' },
resolvePath: function ({ as, from, type }) { resolvePath: function ({ as, from, type }) {
const resolvedPath = from.replace('primevue', '@/components/lib'); const resolvedPath = from.replace('primevue', '@/components/lib');

View File

@ -5,6 +5,6 @@ export default {
base: PrimeOne, base: PrimeOne,
preset: Aura, preset: Aura,
options: { options: {
prefix: 'p' darkModeSelector: '.p-dark'
} }
}; };