I am building UI-kit with react tailwind.
I know that tailwind can’t find classes that made with template strings. I want to achieve this:
I have a few components in my ui kit and I want them to have same logic (for i.e. button):
Button has variant size:{
sm: "p-1",
md: "p-2"
}
I want to automatically generate:
{
sm: "md:p-1",
md: "md:p-2"
}
And etc for all screen sizes.
Finally want to do smth like this:
<Button size="sm" md={size:"md"} />
I think I can’t do this in function because tailwind couldn’t see this classes in runtime.
Maybe there is a library or a way to do this in build time. I’m kinda lost cause I already tried a lot of things. Will be glad to see your answer
Tried twind, but can’t really use it cause of deps conflicts
2
Answers
In Tailwind, the screen sizes are defined using responsive breakpoints. For each breakpoint you define, Tailwind generates responsive variants for most utilities. This results in a set of classes like
sm:text-2xl
,md:text-2xl
, etc.But by design, due to the way Tailwind’s PurgeCSS works, it’s not possible to define classes dynamically or programmatically at runtime through string concatenation or template strings as it would not be able to recognize those in its safelist.
However, you could achieve what you want by re-structuring your Button component to accept different sizes for different screen breakpoints as follows:
Then you can use it as follows:
It’s important to clarify that this approach requires the possible combinations of sizes and breakpoints to be defined beforehand in the component. It won’t allow creating new class combinations dynamically at runtime.
Therefore, I’d suggest considering the design and how many screen sizes you might need to handle specific cases. Most of the time, you can handle it within a few sizes comfortably and handle edge cases by creating custom classes when necessary.
Remember also to include these classes you used in your safelist when purging css for production with PurgeCSS.
Here’s a way to define them separately:
In this structure, you can use the
Button
component as follows:The configuration
{breakpoint: 'md', size: 'md'}
essentially means "when the screen size hits the ‘md’ breakpoint, apply the ‘md’ size". Make sure to add fallback checks for when the sizes or breakpoints passed inadaptiveSize
doesn’t exist in the SIZES/BREAKPOINTS.Remember to add all the classes to PurgeCSS safelist to avoid them being stripped out in production.