Replies: 4 comments 3 replies
-
Neat idea! Maybe we could just allow arrays within i.e. any variants that match either get the additional class? compoundVariants: [
{
size: ['md', 'lg'],
shape: ['default', 'square'],
class: 'rounded',
},
] Could be a little fiddly to understand though, and it may also increase the bundle size 🤔 |
Beta Was this translation helpful? Give feedback.
-
That looks pretty clean to me at least 👍 I actually ended up whipping up a similar home grown utility that accepts regexes and functions. Partially because with arrays, it may be ambiguous whether it needs all of them to match, or just any of them. In your example, it basically uses So personally, I found this more explicit: {
size: /md|lg/,
shape: /default|square/,
classes: 'rounded-md',
}, A bit nerdier of course 🤓 For reference, here's the bit that adds compound classes in my version: // Add compound variants to `res`
compound.forEach((def) => {
const { className, ...rest } = def;
if (className && Object.keys(rest).every((key) => {
const propValue = props[key] || defaults[key];
const matchValue = rest[key];
if (typeof matchValue === 'function') {
return matchValue(propValue);
}
if (matchValue instanceof RegExp) {
return matchValue.test(propValue);
}
if (typeof matchValue === 'boolean') {
return Boolean(matchValue) === Boolean(propValue);
}
return matchValue === propValue;
})) {
res += ` ${className}`;
}
}); |
Beta Was this translation helpful? Give feedback.
-
In practice I haven't needed the boolean coercion to be fair, since I can just use functions for that. And I've barely used functions, but I liked having the option so I can always just pass a component's props straight in and do more complex checks directly in the class composer. |
Beta Was this translation helpful? Give feedback.
-
Shipped a workaround in #76 |
Beta Was this translation helpful? Give feedback.
-
First off, thanks for this! I've been writing similar utilities myself, but never quite landed on an API I liked. So far I'm enjoying the simplicity of this one.
Now maybe I'm just missing something obvious, but I can't quite figure out how to solve this use case. Say I have a button component with these properties:
default
/square
/rounded
/circle
xs
/sm
/md
/lg
If the shape is
rounded
orcircle
, I want to applyrounded-full
. But otherwise, I need separate border radiuses for the two smallest and two biggest sizes.Is there a way to do that with compound variants, without creating one for every possible combination of shape and size? What I was hoping to do was something like:
I hope that makes sense. The same thing would apply to things like the width as well, where the square/circle buttons need an explicit width for each size.
Or is there a smarter way to handle these types of compound variants?
Beta Was this translation helpful? Give feedback.
All reactions