Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update VariantProp type merging logic for Compose interface #276

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions packages/cva/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,52 @@ describe("compose", () => {
"shadow-md gap-3 adhoc-class",
);
});
test("should correctly merge variant props", () => {
const A = cva({
base: "A",
variants: {
intent: {
primary: "primary-A",
},
},
});

const B = cva({
base: "B",
variants: {
intent: {
primary: "primary-B",
secondary: "secondary-B",
},
},
});

const C = cva({
base: "C",
variants: {
intent: {
tertiary: "tertiary-C",
},
},
});

const composedABC = compose(A, B, C);

expectTypeOf(composedABC).toBeFunction();
expectTypeOf(composedABC).parameter(0).toMatchTypeOf<
| {
intent?: "primary" | "secondary" | "tertiary";
}
| undefined
>();

expect(composedABC()).toBe("A B C");
expect(composedABC({ intent: "primary" })).toBe(
"A primary-A B primary-B C",
);
expect(composedABC({ intent: "secondary" })).toBe("A B secondary-B C");
expect(composedABC({ intent: "tertiary" })).toBe("A B C tertiary-C");
});
});

describe("cva", () => {
Expand Down
42 changes: 25 additions & 17 deletions packages/cva/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,36 @@ type ClassValue =
| null
| boolean
| undefined;
type ClassDictionary = Record<string, any>;
type ClassDictionary = Record<string, unknown>;
type ClassArray = ClassValue[];

/* Utils
---------------------------------- */

type OmitUndefined<T> = T extends undefined ? never : T;
type StringToBoolean<T> = T extends "true" | "false" ? boolean : T;
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (
k: infer I,
) => void
? I
: never;
type MergeVariantProps<Types extends object[]> = Types extends [
infer First,
...infer Rest,
]
? First extends object
? Rest extends object[]
? {
[K in
| keyof First
| keyof MergeVariantProps<Rest>]: K extends keyof First
? K extends keyof MergeVariantProps<Rest>
? First[K] | Exclude<MergeVariantProps<Rest>[K], First[K]>
: First[K]
: K extends keyof MergeVariantProps<Rest>
? MergeVariantProps<Rest>[K]
: never;
}
: never
: never
: object;

export type VariantProps<Component extends (...args: any) => any> = Omit<
export type VariantProps<Component extends (...args: any[]) => unknown> = Omit<
OmitUndefined<Parameters<Component>[0]>,
"class" | "className"
>;
Expand All @@ -57,16 +72,9 @@ export type VariantProps<Component extends (...args: any) => any> = Omit<

export interface Compose {
<T extends ReturnType<CVA>[]>(
...components: [...T]
...components: T
): (
props?: (
| UnionToIntersection<
{
[K in keyof T]: VariantProps<T[K]>;
}[number]
>
| undefined
) &
props?: Partial<MergeVariantProps<{ [K in keyof T]: VariantProps<T[K]> }>> &
CVAClassProp,
) => string;
}
Expand Down Expand Up @@ -160,7 +168,7 @@ export interface DefineConfig {
/* Exports
============================================ */

const falsyToString = <T extends unknown>(value: T) =>
const falsyToString = (value: unknown) =>
typeof value === "boolean" ? `${value}` : value === 0 ? "0" : value;

export const defineConfig: DefineConfig = (options) => {
Expand Down
Loading