Skip to content

Commit 7c9b6bd

Browse files
committed
feat: added support for multiples variants at same time within compound variants
1 parent 5af3ad1 commit 7c9b6bd

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

packages/jacaranda/src/index.test.ts

+51
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,57 @@ describe('sva', () => {
204204
padding: 4,
205205
});
206206
});
207+
208+
it('should support compound variants with array of values', () => {
209+
const { sva } = defineTokens({});
210+
const buttonStyles = sva({
211+
base: { display: 'flex' },
212+
variants: {
213+
visual: {
214+
solid: { backgroundColor: '#FC8181' },
215+
outline: { borderWidth: 1 },
216+
},
217+
size: {
218+
sm: { padding: 4 },
219+
md: { padding: 6 },
220+
lg: { padding: 8 },
221+
},
222+
},
223+
compoundVariants: [
224+
{
225+
variants: { visual: 'solid', size: ['md', 'lg'] },
226+
style: { fontWeight: 'bold' },
227+
},
228+
],
229+
defaultVariants: {
230+
visual: 'solid',
231+
size: 'sm',
232+
},
233+
});
234+
235+
// Test compound variant with size md (should match the array)
236+
expect(buttonStyles({ visual: 'solid', size: 'md' })).toEqual({
237+
display: 'flex',
238+
backgroundColor: '#FC8181',
239+
padding: 6,
240+
fontWeight: 'bold',
241+
});
242+
243+
// Test compound variant with size lg (should match the array)
244+
expect(buttonStyles({ visual: 'solid', size: 'lg' })).toEqual({
245+
display: 'flex',
246+
backgroundColor: '#FC8181',
247+
padding: 8,
248+
fontWeight: 'bold',
249+
});
250+
251+
// Test without triggering compound variant (size sm not in array)
252+
expect(buttonStyles({ visual: 'solid', size: 'sm' })).toEqual({
253+
display: 'flex',
254+
backgroundColor: '#FC8181',
255+
padding: 4,
256+
});
257+
});
207258
});
208259

209260
describe('defineTokens', () => {

packages/jacaranda/src/index.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
// // Optional: Add compound variants for specific combinations
3131
// compoundVariants: [
3232
// {
33-
// variants: { visual: 'solid', size: 'lg' },
33+
// variants: { visual: 'solid', size: ['lg'] },
3434
// style: { fontWeight: 'bold' },
3535
// },
3636
// ],
@@ -66,7 +66,7 @@ type BooleanVariantKey = 'true' | 'false';
6666

6767
type CompoundVariant<V> = {
6868
variants: Partial<{
69-
[P in keyof V]: keyof V[P] | boolean;
69+
[P in keyof V]: keyof V[P] | boolean | Array<keyof V[P]>;
7070
}>;
7171
style: StyleObject;
7272
};
@@ -152,6 +152,13 @@ function styles<V extends VariantOptions<V>>(config: VariantStyleConfig<V>) {
152152
if (typeof value === 'boolean') {
153153
return mergedProps[propName as keyof V] === value;
154154
}
155+
156+
// Handle array of values
157+
if (Array.isArray(value)) {
158+
return value.includes(mergedProps[propName as keyof V]);
159+
}
160+
161+
// Handle single value (string/enum)
155162
return mergedProps[propName as keyof V] === value;
156163
})
157164
) {

0 commit comments

Comments
 (0)