Skip to content

Commit 3ae2422

Browse files
authored
Handle undefined in recipes, improve recipe debug IDs (#380)
1 parent 0ac48dd commit 3ae2422

File tree

7 files changed

+96
-8
lines changed

7 files changed

+96
-8
lines changed

.changeset/early-baboons-serve.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+
Add variant group names to debug IDs

.changeset/rude-actors-obey.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+
Return default variants when selection is `undefined`

packages/recipes/src/createRuntimeFn.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ export const createRuntimeFn =
2525
(options) => {
2626
let className = config.defaultClassName;
2727

28-
// @ts-expect-error
2928
const selections: VariantSelection<Variants> = {
3029
...config.defaultVariants,
3130
...options,
3231
};
3332
for (const variantName in selections) {
34-
const variantSelection = selections[variantName];
33+
const variantSelection =
34+
selections[variantName] ?? config.defaultVariants[variantName];
3535

3636
if (variantSelection) {
3737
className += ` ${

packages/recipes/src/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ import type {
1212

1313
function mapValues<Input extends Record<string, any>, OutputValue>(
1414
input: Input,
15-
fn: (value: Input[keyof Input]) => OutputValue,
15+
fn: (value: Input[keyof Input], key: keyof Input) => OutputValue,
1616
): Record<keyof Input, OutputValue> {
1717
const result: any = {};
1818

1919
for (const key in input) {
20-
result[key] = fn(input[key]);
20+
result[key] = fn(input[key], key);
2121
}
2222

2323
return result;
@@ -29,7 +29,7 @@ export function recipe<Variants extends VariantGroups>(
2929
): RuntimeFn<Variants> {
3030
const {
3131
variants = {},
32-
defaultVariants,
32+
defaultVariants = {},
3333
compoundVariants = [],
3434
base = '',
3535
} = options;
@@ -39,12 +39,12 @@ export function recipe<Variants extends VariantGroups>(
3939

4040
// @ts-expect-error
4141
const variantClassNames: PatternResult<Variants>['variantClassNames'] =
42-
mapValues(variants, (variantGroup) =>
42+
mapValues(variants, (variantGroup, variantGroupName) =>
4343
styleVariants(
4444
variantGroup,
4545
(styleRule) =>
4646
typeof styleRule === 'string' ? [styleRule] : styleRule,
47-
debugId,
47+
debugId ? `${debugId}_${variantGroupName}` : variantGroupName,
4848
),
4949
);
5050

packages/recipes/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export type PatternResult<Variants extends VariantGroups> = {
1616
variantClassNames: {
1717
[P in keyof Variants]: { [P in keyof Variants[keyof Variants]]: string };
1818
};
19-
defaultVariants?: VariantSelection<Variants>;
19+
defaultVariants: VariantSelection<Variants>;
2020
compoundVariants: Array<[VariantSelection<Variants>, string]>;
2121
};
2222

tests/recipes/recipes.css.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { recipe } from '@vanilla-extract/recipes';
2+
3+
export const basic = recipe({
4+
base: {},
5+
6+
variants: {
7+
spaceWithDefault: {
8+
small: {},
9+
large: {},
10+
},
11+
spaceWithoutDefault: {
12+
small: {},
13+
large: {},
14+
},
15+
color: {
16+
red: {},
17+
blue: {},
18+
},
19+
},
20+
21+
defaultVariants: {
22+
spaceWithDefault: 'small',
23+
},
24+
25+
compoundVariants: [
26+
{
27+
variants: { color: 'red', spaceWithDefault: 'small' },
28+
style: {},
29+
},
30+
],
31+
});

tests/recipes/recipes.test.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { basic } from './recipes.css';
2+
3+
describe('recipes', () => {
4+
it('should return default variants for no options', () => {
5+
expect(basic()).toMatchInlineSnapshot(
6+
`"recipes_basic__niwegb0 recipes_basic_spaceWithDefault_small__niwegb1"`,
7+
);
8+
});
9+
10+
it('should return default variants for empty options', () => {
11+
expect(basic({})).toMatchInlineSnapshot(
12+
`"recipes_basic__niwegb0 recipes_basic_spaceWithDefault_small__niwegb1"`,
13+
);
14+
});
15+
16+
it('should return default variants for undefined options', () => {
17+
expect(basic({ spaceWithDefault: undefined })).toMatchInlineSnapshot(
18+
`"recipes_basic__niwegb0 recipes_basic_spaceWithDefault_small__niwegb1"`,
19+
);
20+
});
21+
22+
it('should return requested variants', () => {
23+
expect(
24+
basic({
25+
spaceWithDefault: 'large',
26+
spaceWithoutDefault: 'small',
27+
color: 'blue',
28+
}),
29+
).toMatchInlineSnapshot(
30+
`"recipes_basic__niwegb0 recipes_basic_spaceWithDefault_large__niwegb2 recipes_basic_spaceWithoutDefault_small__niwegb3 recipes_basic_color_blue__niwegb6"`,
31+
);
32+
});
33+
34+
it('should return requested compound variants', () => {
35+
expect(
36+
basic({ spaceWithDefault: 'small', color: 'red' }),
37+
).toMatchInlineSnapshot(
38+
`"recipes_basic__niwegb0 recipes_basic_spaceWithDefault_small__niwegb1 recipes_basic_color_red__niwegb5 recipes_basic_compound_0__niwegb7"`,
39+
);
40+
});
41+
42+
it('should return compound variants via defaultVariants', () => {
43+
expect(basic({ color: 'red' })).toMatchInlineSnapshot(
44+
`"recipes_basic__niwegb0 recipes_basic_spaceWithDefault_small__niwegb1 recipes_basic_color_red__niwegb5 recipes_basic_compound_0__niwegb7"`,
45+
);
46+
});
47+
});

0 commit comments

Comments
 (0)