Skip to content

Instantly share code, notes, and snippets.

@CGamesPlay
Last active August 22, 2024 17:10
Show Gist options
  • Save CGamesPlay/bcf5b57c4a49cc487c92ecca61ae47f3 to your computer and use it in GitHub Desktop.
Save CGamesPlay/bcf5b57c4a49cc487c92ecca61ae47f3 to your computer and use it in GitHub Desktop.
CVA prop splitting
import { cva } from "cva";
export * from "cva";
type Base = Parameters<typeof cva<unknown>>[0];
type Config<T> = Parameters<typeof cva<T>>[1];
type Simplify<T> = { [KeyType in keyof T]: T[KeyType] } & {};
type Props<T> = Simplify<
Omit<
Exclude<Parameters<ReturnType<typeof cva<T>>>[0], undefined>,
"className" | "class"
>
>;
export const cvaSplit =
<T,>(base?: Base, config?: Config<T>) =>
<U,>(
props: Props<T> & U,
): Omit<U, keyof Props<T> | "className" | "class"> & {
className: string;
} => {
let rest: any = {};
if (config?.variants) {
for (const k in props) {
if (!(k in config.variants) && k !== "class" && k !== "className") {
rest[k] = (props as any)[k];
}
}
} else {
rest = { ...props };
}
rest.className = cva(base, config)(props as any);
return rest;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment