Best practices for distributing React component libraries with Tailwind v4 classes via npm packages #18545
-
Hi Tailwind team and community, I'm developing a React component library that uses Tailwind CSS v4 and distribute it as an npm package. Current workaroundMy current solution consists of:
// Auto-generated safelist component
export const TailwindSafelist = () => {
return (
<div className="hidden">
<div className="bg-blue-500 text-white p-4 rounded-lg shadow-md hover:bg-blue-600 transition-colors" />
{/* ...other classes used in the library */}
</div>
);
}; This works, but feels somewhat hacky and manual. My questions
I’d greatly appreciate any input or direction on whether this approach makes sense — or if there are better alternatives I should consider. Thanks for your time and for the amazing work on Tailwind! Best regards |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Simply add the ./css/globals.css @import "tailwindcss";
@source "../node_modules/my-package-name/dist; or ./css/globals.css @import "tailwindcss";
@source "../node_modules/my-package-name/dist/**/*.{js,ts,jsx,tsx}; or just ./css/globals.css @import "tailwindcss";
@source "../node_modules/my-package-name; This expands the source file discovery to include not only the local project but also your UI components. Important note: It's important not to include the entire Live examples:
Another:
|
Beta Was this translation helpful? Give feedback.
-
Suggested answer for others running into similar issues Thanks to @rozsazoltan’s input, I was able to solve the issue in most consuming apps. The actual problem turned out to be a mismatch in Tailwind versions — some apps still used Tailwind v3 while our component library was built with v4. Once we upgraded those apps to Tailwind CSS v4 and used the @source directive to explicitly include the library files, the purging issue was resolved. This avoided the need for a “safelist” workaround in most cases. If you’re running into similar problems with missing styles from a Tailwind-based component library, double-check: Thanks again to the Tailwind community — especially @rozsazoltan — for the great support! |
Beta Was this translation helpful? Give feedback.
Suggested answer for others running into similar issues
Thanks to @rozsazoltan’s input, I was able to solve the issue in most consuming apps. The actual problem turned out to be a mismatch in Tailwind versions — some apps still used Tailwind v3 while our component library was built with v4.
Once we upgraded those apps to Tailwind CSS v4 and used the @source directive to explicitly include the library files, the purging issue was resolved. This avoided the need for a “safelist” workaround in most cases.
If you’re running into similar problems with missing styles from a Tailwind-based component library, double-check:
• the Tailwind version in all consuming apps
• and use @source in your mai…