Fixed #6452 - Add style keyword to themes section to inject custom styles

pull/6454/head
Mert Sincan 2024-09-22 14:22:07 +01:00
parent 9c7ac6c05a
commit d959cb0493
6 changed files with 30 additions and 14 deletions

View File

@ -164,8 +164,8 @@ export default {
loadCSS(options = {}) { loadCSS(options = {}) {
return this.load(this.css, options); return this.load(this.css, options);
}, },
loadTheme(options = {}) { loadTheme(options = {}, style = '') {
return this.load(this.theme, options, (computedStyle) => Theme.transformCSS(options.name || this.name, computedStyle)); return this.load(this.theme, options, (computedStyle) => Theme.transformCSS(options.name || this.name, `${computedStyle}${style}`));
}, },
getCommonTheme(params) { getCommonTheme(params) {
return Theme.getCommon(this.name, params); return Theme.getCommon(this.name, params);

View File

@ -159,21 +159,21 @@ export default {
// common // common
if (!Theme.isStyleNameLoaded('common')) { if (!Theme.isStyleNameLoaded('common')) {
const { primitive, semantic } = this.$style?.getCommonTheme?.() || {}; const { primitive, semantic, style } = this.$style?.getCommonTheme?.() || {};
BaseStyle.load(primitive?.css, { name: 'primitive-variables', ...this.$styleOptions }); BaseStyle.load(primitive?.css, { name: 'primitive-variables', ...this.$styleOptions });
BaseStyle.load(semantic?.css, { name: 'semantic-variables', ...this.$styleOptions }); BaseStyle.load(semantic?.css, { name: 'semantic-variables', ...this.$styleOptions });
BaseStyle.loadTheme({ name: 'global-style', ...this.$styleOptions }); BaseStyle.loadTheme({ name: 'global-style', ...this.$styleOptions }, style);
Theme.setLoadedStyleName('common'); Theme.setLoadedStyleName('common');
} }
// component // component
if (!Theme.isStyleNameLoaded(this.$style?.name) && this.$style?.name) { if (!Theme.isStyleNameLoaded(this.$style?.name) && this.$style?.name) {
const { css } = this.$style?.getComponentTheme?.() || {}; const { css, style } = this.$style?.getComponentTheme?.() || {};
this.$style?.load(css, { name: `${this.$style.name}-variables`, ...this.$styleOptions }); this.$style?.load(css, { name: `${this.$style.name}-variables`, ...this.$styleOptions });
this.$style?.loadTheme({ name: `${this.$style.name}-style`, ...this.$styleOptions }); this.$style?.loadTheme({ name: `${this.$style.name}-style`, ...this.$styleOptions }, style);
Theme.setLoadedStyleName(this.$style.name); Theme.setLoadedStyleName(this.$style.name);
} }

View File

@ -91,21 +91,21 @@ const BaseDirective = {
// common // common
if (!Theme.isStyleNameLoaded('common')) { if (!Theme.isStyleNameLoaded('common')) {
const { primitive, semantic } = instance.$style?.getCommonTheme?.() || {}; const { primitive, semantic, style } = instance.$style?.getCommonTheme?.() || {};
BaseStyle.load(primitive?.css, { name: 'primitive-variables', ...useStyleOptions }); BaseStyle.load(primitive?.css, { name: 'primitive-variables', ...useStyleOptions });
BaseStyle.load(semantic?.css, { name: 'semantic-variables', ...useStyleOptions }); BaseStyle.load(semantic?.css, { name: 'semantic-variables', ...useStyleOptions });
BaseStyle.loadTheme({ name: 'global-style', ...useStyleOptions }); BaseStyle.loadTheme({ name: 'global-style', ...useStyleOptions }, style);
Theme.setLoadedStyleName('common'); Theme.setLoadedStyleName('common');
} }
// directive // directive
if (!Theme.isStyleNameLoaded(instance.$style?.name) && instance.$style?.name) { if (!Theme.isStyleNameLoaded(instance.$style?.name) && instance.$style?.name) {
const { css } = instance.$style?.getDirectiveTheme?.() || {}; const { css, style } = instance.$style?.getDirectiveTheme?.() || {};
instance.$style?.load(css, { name: `${instance.$style.name}-variables`, ...useStyleOptions }); instance.$style?.load(css, { name: `${instance.$style.name}-variables`, ...useStyleOptions });
instance.$style?.loadTheme({ name: `${instance.$style.name}-style`, ...useStyleOptions }); instance.$style?.loadTheme({ name: `${instance.$style.name}-style`, ...useStyleOptions }, style);
Theme.setLoadedStyleName(instance.$style.name); Theme.setLoadedStyleName(instance.$style.name);
} }

View File

@ -195,12 +195,12 @@ export function setupConfig(app, PrimeVue) {
const loadCommonTheme = () => { const loadCommonTheme = () => {
// common // common
if (!Theme.isStyleNameLoaded('common')) { if (!Theme.isStyleNameLoaded('common')) {
const { primitive, semantic } = BaseStyle.getCommonTheme?.() || {}; const { primitive, semantic, style } = BaseStyle.getCommonTheme?.() || {};
const styleOptions = { nonce: PrimeVue.config?.csp?.nonce }; const styleOptions = { nonce: PrimeVue.config?.csp?.nonce };
BaseStyle.load(primitive?.css, { name: 'primitive-variables', ...styleOptions }); BaseStyle.load(primitive?.css, { name: 'primitive-variables', ...styleOptions });
BaseStyle.load(semantic?.css, { name: 'semantic-variables', ...styleOptions }); BaseStyle.load(semantic?.css, { name: 'semantic-variables', ...styleOptions });
BaseStyle.loadTheme({ name: 'global-style', ...styleOptions }); BaseStyle.loadTheme({ name: 'global-style', ...styleOptions }, style);
Theme.setLoadedStyleName('common'); Theme.setLoadedStyleName('common');
} }

View File

@ -177,5 +177,12 @@ export default {
directives: { directives: {
tooltip, tooltip,
ripple ripple
} },
// @todo: REMOVE THIS BLOCK
style: ({ dt }) => `
.p-button {
background: blue !important;
padding: ${dt('inputtext.test')} ${dt('inputtext.test')} !important;
}
`
}; };

View File

@ -14,6 +14,7 @@ export default {
shadow: '{form.field.shadow}', shadow: '{form.field.shadow}',
paddingX: '{form.field.padding.x}', paddingX: '{form.field.padding.x}',
paddingY: '{form.field.padding.y}', paddingY: '{form.field.padding.y}',
test: '2rem', // @todo: REMOVE THIS LINE
borderRadius: '{form.field.border.radius}', borderRadius: '{form.field.border.radius}',
focusRing: { focusRing: {
width: '{form.field.focus.ring.width}', width: '{form.field.focus.ring.width}',
@ -33,5 +34,13 @@ export default {
paddingX: '0.875rem', paddingX: '0.875rem',
paddingY: '0.625rem' paddingY: '0.625rem'
} }
} },
// @todo: REMOVE THIS BLOCK
style: ({ dt }) => `
.p-inputtext {
background: yellow;
padding: ${dt('inputtext.test')} ${dt('inputtext.test')};
border: 5px solid red;
}
`
}; };