Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot apply PrimeReact styled theme to NextJS 14 with Tailwind #5291

Closed
gygabyte017 opened this issue Nov 12, 2023 · 12 comments
Closed

Cannot apply PrimeReact styled theme to NextJS 14 with Tailwind #5291

gygabyte017 opened this issue Nov 12, 2023 · 12 comments
Labels
Component: NextJS NextJS related issue Component: Tailwind Tailwind specific issue Resolution: Workaround Issue or pull request contains a workaround. It needs to be reviewed further by Core Team

Comments

@gygabyte017
Copy link

Describe the bug

I started a brand new NextJS 14 app with App Router and Tailwind automatically set up by NextJS.

Then I added PrimeReact, with the mdc theme, and configured Tailwind as explained here: https://primereact.org/tailwind/ in the styled part.

However I got something mixed that doesn't appear to be working, see reproducer: Tailwind works (the classes are successfully applied), PrimeReact components work, but the PrimeReact mdc theme doesn't seem to apply, the button is transparent, the inputs are weird as well.

Could you please check the reproducer to verify whether I did something wrong or if that is indeed a bug?
Thanks a lot

Reproducer

https://stackblitz.com/edit/stackblitz-starters-i1ylh3?file=package.json

PrimeReact version

10.0.9

React version

18.x

Language

TypeScript

Build / Runtime

Next.js

Browser(s)

No response

Steps to reproduce the behavior

No response

Expected behavior

No response

@gygabyte017 gygabyte017 added the Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible label Nov 12, 2023
@melloware melloware added Component: Tailwind Tailwind specific issue and removed Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible labels Nov 12, 2023
@melloware
Copy link
Member

I think its an issue with CodeSandbox/StackBlitz. See this solution: #4987 (comment)

@gygabyte017
Copy link
Author

gygabyte017 commented Nov 12, 2023

Ok I tried specifying imports in that order (tailwind with layers first, then primereact) (see reproducer):

import '../styles/global.css';
import 'primereact/resources/primereact.min.css';
import 'primereact/resources/themes/mdc-light-indigo/theme.css';

but so far no changes.

The truth is that first I tried on my local setup, and with npm run dev that setup works correctly (*), however when running rpm run build && npm start the production page appears exactly as in the StackBlitz, without the primereact theme.

(*) even though there is something weird like the red X in the Message component when the page loads appears in the top left corner and a few ms later it appears in the correct position, it is something ugly noticeable by the user.

Thank you

@melloware
Copy link
Member

Yep @SoyDiego has a solution for this

@SoyDiego
Copy link
Contributor

SoyDiego commented Nov 12, 2023

Try this: https://soydiego.notion.site/PrimeReact-How-to-fix-styles-NextJS-Tailwind-92bec3eb91594388a7be67d922460eaf
Is not the best solution, it's a workaround and works.
I hope someone can have a better fix/solution soon

@gygabyte017
Copy link
Author

gygabyte017 commented Nov 19, 2023

Thank you @SoyDiego, I confirm that (weirdly enough :)) it works on dev environment.
However when building for production, the production environment still behaves without primereact theme.

Can you try if workarounds 1 and 2 work for you on production env?

@SoyDiego
Copy link
Contributor

Thank you @SoyDiego, I confirm that (weirdly enough :)) it works on dev environment. However when building for production, the production environment still behaves without primereact theme.

Can you try if workarounds 1 and 2 work for you on production env?

Yes, I have in production. Check if the styles are being applied in your head tag, inspecting the code.

image

@gygabyte017
Copy link
Author

Ok with workaround 1 I can see them in production with the correct order, with workaround 2 they are inverted.
Seems a bit "hacky" but it works, please update the issue when there will be a definitive fix!

Thank you!

@igo
Copy link

igo commented Nov 22, 2023

I guess I have the same problem. No PrimeReact components were style properly even in dev mode. It looks like that Nextjs strips all classes that are not used directly in Nextjs app. Adding this to tailwind.config.ts seems to preserve all classes in css:

safelist: [
{
  pattern: /./ ,
  // variants: ['sm', 'md', 'lg', 'xl', '2xl'], // uncommenting this line makes next compile for too long
},
]

@gygabyte017
Copy link
Author

I guess I have the same problem. No PrimeReact components were style properly even in dev mode. It looks like that Nextjs strips all classes that are not used directly in Nextjs app. Adding this to tailwind.config.ts seems to preserve all classes in css:

safelist: [
{
  pattern: /./ ,
  // variants: ['sm', 'md', 'lg', 'xl', '2xl'], // uncommenting this line makes next compile for too long
},
]

doesn't work for me, also that pattern makes next to compile a lot longer :(

From what I have understood the css classes are not stripped, is the layer ordering that gets messed up so tailwind overwrite primereact styles

@melloware melloware added the Component: NextJS NextJS related issue label Nov 25, 2023
@kabalan-datapostie
Copy link

A few thoughts on this:

@melloware melloware added the Resolution: Workaround Issue or pull request contains a workaround. It needs to be reviewed further by Core Team label Aug 16, 2024
@gcko
Copy link
Contributor

gcko commented Sep 4, 2024

@kabalan-datapostie I believe this has been addressed and fixed in NextJS as of May 1, 2024 --> vercel/next.js#13092

CC @melloware

@melloware
Copy link
Member

Thanks @gcko closed!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: NextJS NextJS related issue Component: Tailwind Tailwind specific issue Resolution: Workaround Issue or pull request contains a workaround. It needs to be reviewed further by Core Team
Projects
None yet
Development

No branches or pull requests

6 participants