Rollup plugin for dynamic safelisting #12277
benjaminpreiss
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hey y'all
I have been thinking about possibilities to dynamically safelist classes for a component library based on how they are used.
This is about solving the problem of providing responsive component variants for different screens. More specifically:
Should render a "medium" sized component on a tailwind
md
breakpoint and a "small" sized component on mobile screens.There are several ways to achieve this (to my knowledge), two of which I analyzed in more detail:
Utilizing css variables
We can create css variables inline to achieve the desired effect: #12204 (comment)
The problem is, that we can only offer breakpoints that we have thought of before (no custom values allowed) and writing styles this way is very cumbersome as we need to write a lot of code to achieve this effect.
The amount of code required to be written can be reduced by creating components in tailwind, but that creates the possibilty of duplication for atomic styles:
Will generate:
Safelisting variants
The other variant is simply safelisting the used classes that cannot be detected by tailwind:
So let's say the component is a button with the following mapping of component version to mapping:
In that case, the above props should result in the following component
To my knowledge the comment about the safelist in the markup should be sufficient to make tailwind generate this class.
So I wonder now - could we automatically generate this comment by some rollup plugin magic?
Rollup plugin idea
Each component should export a
sizes()
function that takes in the sizes and just simply returns them.That way, using a rollup plugin and some smart AST transforms, we could possibly detect which variants are used on which breakpoint and inject them into the safelist. (?)
Then, by using this in our component, we can do:
I am curious what you folks think about this!
Beta Was this translation helpful? Give feedback.
All reactions