I really don't like TailwindCSS and recently I discovered that llms are good at writing ViteJS plugins and did some experiments that came out pretty well in opinion.
Note. I know about styled-components, EmotionCSS and vanilla-extract and others but I don't like to write CSS-as-JS and some of the libraries do not support ViteJS or are too much tied to the React ecosystem and can't be used with Preact.
I really like to write CSS by hand but I get that Tailwind gives the ability to write everything directly in the same files keeping related things together. In an ideal world I think I would just ditch HTML+CSS and have the separation at components+layout and colors+styles, or just have a single language like in SwiftUI or Jetpack Compose.
This micro-library gives the best from Tailwind of having the styles directly in the code without having to learn a completely new language and syntax on top of CSS.
<button
classList={[
css`
background-color: blue;
color: white;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
&:hover {
background-color: darkblue;
}
`,
]}
>
Click Me
</button>This is also very composable. Commonly used utilities like the following can also be easily moved to a separate module
const cssFontBold = css`
font-weight: bold;
`
const cssCardShadow = css`
box-shadow:
0 4px 6px -1px rgb(0 0 0 / 0.1),
0 2px 4px -2px rgb(0 0 0 / 0.1);
`As these are just compiled to class names these can just be used with the classList custom attribute that simply calls directly clsx.
<button classList={[
css`
background-color: blue;
color: white;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointert;
&:hover {
background-color: darkblue;
}
`,
// just an example of a conditional class
Math.random() > 0.5 && cssFontBold
]}>
Click Here
</button>-
Instead of hashing use a sequential counter and give a custom uuid to each css snippet
-
Due to the regex extraction technique we can't extract complex template literals with interpolations inside, this would be usefull for example to define multiple variants of a class at the same time. A possible solution could be to have a function as follows
cosnt prova = css.variant({ regular: { bg: 'gray', }, primary: { bg: 'royalblue', }, }, ({ bg }) => css` font-size: 14px; color: #333; background: ${bg}; &:hover { background: hsl(from ${bg} h s calc(l + 10)); } `)
Then the lambda inside can be extracted* and evaluated "at compile time" from the vite js plugin environment. Another idea can be to not use interpolation altogether and just generate some css variables for each constant.
*At this point maybe not with regex extraction but with a fully fledged
{j,t}sx?parser.