Typescript and automatically generating variants with sprinkles #947
Unanswered
maaniksingh92
asked this question in
Q&A
Replies: 1 comment
-
is this what you want ? I made it generic even for the sprinkles part interface AnySprinklesFn {
(...args: any): string;
properties: Set<string>;
}
type SprinklesProp<Fn extends AnySprinklesFn> = Parameters<Fn>[0];
type NotUndefined<T> = Exclude<T, undefined>
function generateAllVariants<
SprinklesFn extends AnySprinklesFn,
PropName extends keyof SprinklesProp<SprinklesFn>,
Values extends Record<string, unknown>
>(sprinklesFn: SprinklesFn, propName: PropName, propValues: Values) {
return keys(propValues).reduce(
(variants, value) => ({
...variants,
[value]: sprinklesFn({ [propName]: value }),
}),
{} as {
[K in NotUndefined<SprinklesProp<SprinklesFn>[PropName]>]: ReturnType<SprinklesFn>;
}
);
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
TLDR; Is there an easy way to create a variant object mapped from all the custom values for a sprinkle property with the benefits of Typescript?
Hi,
I have a map of custom values for spacing:
const space = { none: 0, xs: 8, sm: 16, md: 24, lg: 32, xl: 40 };
which i'm reusing for defining sprinkle properties of
margin
,padding
andgap
.I also intend to define common recipe variants for each of the above properties to the custom values, e.g. for
gap
:and then do something similar for the
margin
andpadding
properties as well.The intention is to reuse these variants while writing recipes for common building block components.
I thought that I could take things one step further by creating a function that generates the variant map, e.g.
gapVariants
, like this:However, I lose the benefit of Typescript typings with this.
I feel like this might be a common use case for other projects and would love to know if there is an inbuilt function within the library to achieve this, or if any one else has a simpler solution to implement this.
Thanks
Beta Was this translation helpful? Give feedback.
All reactions