Skip to content

Commit

Permalink
update Compose interface with new VariantProp type merging logic; cle…
Browse files Browse the repository at this point in the history
…an up some other types
  • Loading branch information
lunelson committed Apr 3, 2024
1 parent 5e4b5b8 commit 506d2ad
Showing 1 changed file with 26 additions and 18 deletions.
44 changes: 26 additions & 18 deletions packages/cva/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,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 @@ -42,16 +57,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 All @@ -74,7 +82,7 @@ type CVAVariantShape = Record<string, Record<string, ClassValue>>;
type CVAVariantSchema<V extends CVAVariantShape> = {
[Variant in keyof V]?: StringToBoolean<keyof V[Variant]> | undefined;
};
type CVAClassProp =
export type CVAClassProp =
| {
class?: ClassValue;
className?: never;
Expand Down Expand Up @@ -145,7 +153,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

0 comments on commit 506d2ad

Please sign in to comment.