feat: allow boolean as string in vcn

This commit is contained in:
p-sw 2024-05-19 10:01:50 +09:00
parent 28f84778bf
commit c1a1f5da1b

View File

@ -1,5 +1,15 @@
import { twMerge } from "tailwind-merge"; import { twMerge } from "tailwind-merge";
type BooleanString<T extends string> = T extends "true"
? true
: T extends "false"
? false
: T;
type RawVariantProps<V extends Record<string, Record<string, string>>> = {
[VariantKey in keyof V]?: BooleanString<keyof V[VariantKey] & string>;
};
export function vcn<V extends Record<string, Record<string, string>>>({ export function vcn<V extends Record<string, Record<string, string>>>({
base, base,
variants, variants,
@ -7,10 +17,10 @@ export function vcn<V extends Record<string, Record<string, string>>>({
}: { }: {
base: string; base: string;
variants: V; variants: V;
defaults: { [VariantKey in keyof V]: keyof V[VariantKey] }; defaults: {
}): ( [VariantKey in keyof V]: BooleanString<keyof V[VariantKey] & string>;
variantProps: Partial<typeof defaults> & { className?: string } };
) => string { }): (variantProps: RawVariantProps<V> & { className?: string }) => string {
return ({ className, ...variantProps }) => { return ({ className, ...variantProps }) => {
return twMerge( return twMerge(
base, base,
@ -19,7 +29,7 @@ export function vcn<V extends Record<string, Record<string, string>>>({
).map<string>( ).map<string>(
([variantKey, defaultValue]) => ([variantKey, defaultValue]) =>
variants[variantKey][ variants[variantKey][
(variantProps as unknown as Partial<typeof defaults>)[variantKey] ?? (variantProps as unknown as RawVariantProps<V>)[variantKey] ??
defaultValue defaultValue
] ]
), ),