Skip to content

Commit 64378b0

Browse files
authored
Fix compound variants not applying when default variants get undefined (#704)
1 parent aebf934 commit 64378b0

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

.changeset/quiet-games-explain.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@vanilla-extract/recipes': patch
3+
---
4+
5+
Fix compound variants not applying when default variants get passed undefined

packages/recipes/src/createRuntimeFn.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ import type {
88
const shouldApplyCompound = <Variants extends VariantGroups>(
99
compoundCheck: VariantSelection<Variants>,
1010
selections: VariantSelection<Variants>,
11+
defaultVariants: VariantSelection<Variants>,
1112
) => {
1213
for (const key of Object.keys(compoundCheck)) {
13-
if (compoundCheck[key] !== selections[key]) {
14+
if (compoundCheck[key] !== (selections[key] ?? defaultVariants[key])) {
1415
return false;
1516
}
1617
}
@@ -52,7 +53,9 @@ export const createRuntimeFn =
5253
}
5354

5455
for (const [compoundCheck, compoundClassName] of config.compoundVariants) {
55-
if (shouldApplyCompound(compoundCheck, selections)) {
56+
if (
57+
shouldApplyCompound(compoundCheck, selections, config.defaultVariants)
58+
) {
5659
className += ' ' + compoundClassName;
5760
}
5861
}

tests/recipes/recipes.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ describe('recipes', () => {
4545
);
4646
});
4747

48+
it('should return compound variants via defaultVariants, even when undefined is passed', () => {
49+
expect(
50+
basic({ color: 'red', spaceWithDefault: undefined }),
51+
).toMatchInlineSnapshot(
52+
`"recipes_basic__niwegb0 recipes_basic_spaceWithDefault_small__niwegb1 recipes_basic_color_red__niwegb5 recipes_basic_compound_0__niwegb8"`,
53+
);
54+
});
55+
4856
it('should return boolean variants', () => {
4957
expect(basic({ rounded: true })).toMatchInlineSnapshot(
5058
`"recipes_basic__niwegb0 recipes_basic_spaceWithDefault_small__niwegb1 recipes_basic_rounded_true__niwegb7"`,

0 commit comments

Comments
 (0)