Skip to content

Commit 3163abc

Browse files
Expose variants from recipe (#1044)
1 parent c4b4da6 commit 3163abc

File tree

5 files changed

+33
-7
lines changed

5 files changed

+33
-7
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@vanilla-extract/recipes": minor
3+
---
4+
5+
Add `variants` function for accessing variant names at runtime

packages/recipes/src/createRuntimeFn.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,10 @@ const shouldApplyCompound = <Variants extends VariantGroups>(
1919
return true;
2020
};
2121

22-
export const createRuntimeFn =
23-
<Variants extends VariantGroups>(
24-
config: PatternResult<Variants>,
25-
): RuntimeFn<Variants> =>
26-
(options) => {
22+
export const createRuntimeFn = <Variants extends VariantGroups>(
23+
config: PatternResult<Variants>,
24+
): RuntimeFn<Variants> => {
25+
const runtimeFn: RuntimeFn<Variants> = (options) => {
2726
let className = config.defaultClassName;
2827

2928
const selections: VariantSelection<Variants> = {
@@ -62,3 +61,8 @@ export const createRuntimeFn =
6261

6362
return className;
6463
};
64+
65+
runtimeFn.variants = () => Object.keys(config.variantClassNames);
66+
67+
return runtimeFn;
68+
};

packages/recipes/src/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ export type PatternOptions<Variants extends VariantGroups> = {
3232
compoundVariants?: Array<CompoundVariant<Variants>>;
3333
};
3434

35-
export type RuntimeFn<Variants extends VariantGroups> = (
35+
export type RuntimeFn<Variants extends VariantGroups> = ((
3636
options?: VariantSelection<Variants>,
37-
) => string;
37+
) => string) & { variants: () => (keyof Variants)[] };
3838

3939
export type RecipeVariants<RecipeFn extends RuntimeFn<VariantGroups>> =
4040
Parameters<RecipeFn>[0];

tests/recipes/recipes-type-tests.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,14 @@ type AssertIsString<S> = S extends string ? true : never;
3838
const recipeStyles = textRecipes({ size: 'small' });
3939
const recipeShouldReturnString: AssertIsString<typeof recipeStyles> = true;
4040

41+
const variants: ReturnType<typeof textRecipes.variants> = ['size'];
42+
// @ts-expect-error Type '"foo"' is not assignable to type '"size"'.
43+
const invalidVariants: ReturnType<typeof textRecipes.variants> = ['foo'];
44+
4145
noop(invalidVariantValue);
4246
noop(invalidVariantName);
4347
noop(validTextVariant);
48+
noop(variants);
49+
noop(invalidVariants);
4450
noop(recipeShouldReturnString);
4551
};

tests/recipes/recipes.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,15 @@ describe('recipes', () => {
6464
`"recipes_basic__niwegb0 recipes_basic_spaceWithDefault_small__niwegb1"`,
6565
);
6666
});
67+
68+
it('should expose a function returning list of variants', () => {
69+
expect(basic.variants()).toMatchInlineSnapshot(`
70+
[
71+
"spaceWithDefault",
72+
"spaceWithoutDefault",
73+
"color",
74+
"rounded",
75+
]
76+
`);
77+
});
6778
});

0 commit comments

Comments
 (0)