PrimeVue v4 issue with styling and wrapper components. #2158
-
Beta Was this translation helpful? Give feedback.
Replies: 9 comments 11 replies
-
You've hit upon a tricky aspect of how PrimeVue handles its CSS styles, and it's definitely related to how it dynamically injects styles based on component usage. Understanding the Problem: PrimeVue uses a clever technique to only inject the CSS styles needed for components that are actually used in your application. This is a performance optimization, but it creates a few challenges:
Why Your Approach Isn't Working: While hiding components with
Potential Solutions:
Additional Considerations:
Debugging Tips:
Remember: PrimeVue's style injection mechanism is a balance between performance and flexibility. You might need to experiment with different techniques to find the best solution for your specific use case. Let me know if you have any further questions or would like more code examples! |
Beta Was this translation helpful? Give feedback.
-
Thanks for the reply @vieira-brz ! I've tried a lot of the things you have suggested, but to no avail. In the end, the only thing that really worked for me was pre-loading all of the controls that I utilize in my control library, in my main app's App.vue. I just slapped a v-show="false" so that they rendered, but so that they were hidden. For applying the theme dynamically, I force a reload of the page after the user chooses another palette. Kind of lame, but it works. Maybe there's a way to provide/inject some sort of 'theme' service from PrimeVue into my control library, but as of now I haven't found any examples of that. Thanks again! |
Beta Was this translation helpful? Give feedback.
-
@MonkFox I have been stuck with this problem for a couple of hours now... Did you manage to find a real solution? |
Beta Was this translation helpful? Give feedback.
-
@savonbeldi If my component code is local to the dependent project, things work as expected. It's when the components are referenced from the package that styling breaks. Reuseable, custom components integrating PrimeVue components seems pretty logical to me. Reuseable grid toolbars, module/page/section headers, custom DataGrid components with specific behavior and OData protocol, for example. In regard to the styling mechanism, this seems to not be supported out of the box with versions 3.45.0+. If you're able to solve this somehow, I would love to be notified. Thanks |
Beta Was this translation helpful? Give feedback.
-
Hi @MonkFox, Do you use @primevue/nuxt-module? |
Beta Was this translation helpful? Give feedback.
-
Thanks @vieira-brz for the possible solutions provided. I am interested in the solution 1 that you provided, but I didn't get it right yet. My case is similar that the one in the discussion, custom components that use PrimeVue's native components in a separate npm package. The styles are shown well in the Storybook project where the PrimeVue is installed but not in the projects where the npm package is installed. The problem is that all component variables are missing (example We found a very manual workaround, copying all the variables component by component in Storybook, put them in an CSS file and export it along with the library, but there should be a more maintenable way to do it, I opened a discussion about this some months ago, just in case it helps: https://github.com/orgs/primefaces/discussions/2436#discussioncomment-10272692 |
Beta Was this translation helpful? Give feedback.
-
I have a similar issue in my custom component i can't use primevue Toast |
Beta Was this translation helpful? Give feedback.
-
I am in the same boat as @cristinafernandez1234 where any usage of primevue in a separate npm package will not cause primevue to detect a component being used and therefore all the css variables files for that component will not be loaded. This has prevented my from upgrading the codebase of one of my products to primevue v4 and I'll be staying on v3 until it gets resolved. |
Beta Was this translation helpful? Give feedback.
-
Importing and using the component library's PrimeVue instance is what solved it for me (I assume that's what's going on): //vue-component-library index.ts
export { PrimeVue as PrimeVueConfig }; //consuming application main.ts
import { PrimeVueConfig } from 'vue-component-library'
app.use(PrimeVueConfig, {
theme: {
preset: Aura,
options: {
prefix: 'p',
darkModeSelector: 'system',
cssLayer: false
}
},
zIndex: {
modal: 1100,
overlay: 1500,
menu: 1000,
tooltip: 1100
}
}) In addition to: //vue-component-library package.json
{
//...
"peerDependencies": {
"vue": "^3.5.13",
"primeicons": "^7.0.0",
"primevue": "^4.2.5",
"@primeuix/styled": "^0.4.1",
"@primevue/themes": "^4.2.5"
}
} //component library vite.config.ts
export default defineConfig({
//...
rollupOptions: {
external: ["vue", "@primeuix/styled", "@primevue/themes", "primevue"],
//...
},
}); For local testing before publishing to the Azure artifact, I'm referencing my component library like so in my consuming app: //consuming app's package.json
{
//...
"dependencies": {
//....
"vue-component-library": "file:../../../../vue-jc-library-v4/vue-jc-library-v4"
}
} In my case, I didn't need the following dedupe entry in my consuming app's vite.config.ts //consuming app's vite.config.ts
export default defineConfig({
resolve: {
dedupe: [
'primevue'
],
},
// etc
}) Thank you so much for your time and patience @itsalic and @cristinafernandez1234!! |
Beta Was this translation helpful? Give feedback.
Right now I don't have too much time to create an example repo, but I can share the files, maybe it helps:
The index.js where we have all components exported: