UNPKG

5.46 kBTypeScriptView Raw
1import type { DistributiveOmit, Pretty } from "../utils";
2import type { ConditionalValue, SystemStyleObject } from "./css.types";
3import type { ColorPalette } from "./generated/token.gen";
4type StringToBoolean<T> = T extends "true" | "false" ? boolean : T;
5export type RecipeVariantRecord = Record<any, Record<any, SystemStyleObject>>;
6export type RecipeSelection<T extends RecipeVariantRecord | SlotRecipeVariantRecord<string>> = keyof any extends keyof T ? {} : {
7 [K in keyof T]?: ConditionalValue<StringToBoolean<keyof T[K]> | undefined>;
8};
9export type RecipeVariantFn<T extends RecipeVariantRecord> = (props?: RecipeSelection<T>) => string;
10export type RecipeVariantProps<T extends RecipeDefinition | SlotRecipeDefinition> = T extends RecipeDefinition<infer U> ? RecipeSelection<U> : T extends SlotRecipeDefinition<string, infer U> ? RecipeSelection<U> : never;
11export type RecipeVariantMap<T extends RecipeVariantRecord> = {
12 [K in keyof T]: Array<keyof T[K]>;
13};
14export interface RecipeRuntimeFn<T extends RecipeVariantRecord> extends RecipeVariantFn<T> {
15 __type: RecipeSelection<T>;
16 variantKeys: (keyof T)[];
17 variantMap: RecipeVariantMap<T>;
18 config: RecipeDefinition<T>;
19 splitVariantProps<Props extends RecipeSelection<T>>(props: Props): [RecipeSelection<T>, Pretty<DistributiveOmit<Props, keyof T>>];
20 merge: any;
21}
22type OneOrMore<T> = T | Array<T>;
23export type RecipeCompoundSelection<T> = {
24 [K in keyof T]?: OneOrMore<StringToBoolean<keyof T[K]>> | undefined;
25} & {
26 colorPalette?: OneOrMore<ColorPalette> | undefined;
27};
28export type RecipeCompoundVariant<T> = T & {
29 css: SystemStyleObject;
30};
31export interface RecipeDefinition<T extends RecipeVariantRecord = RecipeVariantRecord> {
32 /**
33 * The class name of the recipe.
34 */
35 className?: string;
36 /**
37 * The base styles of the recipe.
38 */
39 base?: SystemStyleObject;
40 /**
41 * The multi-variant styles of the recipe.
42 */
43 variants?: T;
44 /**
45 * The default variants of the recipe.
46 */
47 defaultVariants?: RecipeSelection<T> & {
48 colorPalette?: ColorPalette;
49 };
50 /**
51 * The styles to apply when a combination of variants is selected.
52 */
53 compoundVariants?: Pretty<RecipeCompoundVariant<RecipeCompoundSelection<T>>>[];
54}
55export type RecipeCreatorFn = <T extends RecipeVariantRecord>(config: RecipeDefinition<T>) => RecipeRuntimeFn<T>;
56export type RecipeIdentityFn = <T extends RecipeVariantRecord>(config: RecipeDefinition<T>) => RecipeDefinition<T>;
57type SlotRecord<S extends string, T> = Partial<Record<S, T>>;
58export type SlotRecipeVariantRecord<S extends string> = Record<any, Record<any, SlotRecord<S, SystemStyleObject>>>;
59export type SlotRecipeVariantFn<S extends string, T extends RecipeVariantRecord> = (props?: RecipeSelection<T>) => SlotRecord<S, string>;
60export interface SlotRecipeRuntimeFn<S extends string, T extends SlotRecipeVariantRecord<S>> extends SlotRecipeVariantFn<S, T> {
61 classNameMap: Record<S, string>;
62 variantKeys: (keyof T)[];
63 variantMap: RecipeVariantMap<T>;
64 splitVariantProps<Props extends RecipeSelection<T>>(props: Props): [RecipeSelection<T>, Pretty<Omit<Props, keyof T>>];
65}
66export type SlotRecipeCompoundVariant<S extends string, T> = T & {
67 css: SlotRecord<S, SystemStyleObject>;
68};
69export interface SlotRecipeDefinition<S extends string = string, T extends SlotRecipeVariantRecord<S> = SlotRecipeVariantRecord<S>> {
70 /**
71 * The class name of the recipe. Useful for targeting slots.
72 *
73 * Say the recipe has slots like `root`, `control` and the class name is 'checkbox'
74 * Each slot will have a class name like `checkbox__root`, `checkbox__control`
75 */
76 className?: string;
77 /**
78 * The parts/slots of the recipe.
79 */
80 slots: S[] | Readonly<S[]>;
81 /**
82 * The base styles of the recipe.
83 */
84 base?: SlotRecord<S, SystemStyleObject>;
85 /**
86 * The multi-variant styles of the recipe.
87 */
88 variants?: T;
89 /**
90 * The default variants of the recipe.
91 */
92 defaultVariants?: RecipeSelection<T> & {
93 colorPalette?: ColorPalette;
94 };
95 /**
96 * The styles to apply when a combination of variants is selected.
97 */
98 compoundVariants?: Pretty<SlotRecipeCompoundVariant<S, RecipeCompoundSelection<T>>>[];
99}
100export type SlotRecipeCreatorFn = <S extends string, T extends SlotRecipeVariantRecord<S>>(config: SlotRecipeDefinition<S, T>) => SlotRecipeRuntimeFn<S, T>;
101export type SlotRecipeIdentityFn = <S extends string, T extends SlotRecipeVariantRecord<S>>(config: SlotRecipeDefinition<S, T>) => SlotRecipeDefinition<S, T>;
102export type SlotRecipeConfig<S extends string = string, T extends SlotRecipeVariantRecord<S> = SlotRecipeVariantRecord<S>> = SlotRecipeDefinition<S, T>;
103export interface SystemRecipeFn<VP, VM> {
104 __type: Partial<VP>;
105 (props?: Partial<VP>): SystemStyleObject;
106 className: string;
107 variantMap: VM;
108 variantKeys: Array<keyof VP>;
109 splitVariantProps<P extends VP>(props: P): [VP, Pretty<DistributiveOmit<P, keyof VP>>];
110}
111export interface SystemSlotRecipeFn<S extends string, VP, VM> {
112 __type: Partial<VP>;
113 (props?: Partial<VP>): Record<S, SystemStyleObject>;
114 classNameMap: Record<S, string>;
115 variantMap: VM;
116 variantKeys: Array<keyof VP>;
117 splitVariantProps<P extends VP & {
118 recipe?: any;
119 }>(props: P): [VP, Pretty<DistributiveOmit<P, keyof VP | "recipe">>];
120}
121export {};