feat: add dynamic className parameter in vcn

This commit is contained in:
p-sw 2024-06-29 21:53:11 +09:00
parent f4f2f2b820
commit a47e9b8427

View File

@ -62,6 +62,21 @@ type VariantKV<V extends VariantType> = {
*/ */
type VariantKVEntry<V extends VariantType> = [keyof V, BooleanString<keyof V[keyof V] & string>][] type VariantKVEntry<V extends VariantType> = [keyof V, BooleanString<keyof V[keyof V] & string>][]
/**
* Takes VariantKV as parameter, return className string.
*
* @example
* vcn({
* /* ... *\/
* dynamics: [
* ({ a, b }) => {
* return a === "something" ? "asdf" : b
* },
* ]
* })
*/
type DynamicClassName<V extends VariantType> = (variantProps: VariantKV<V>) => string
/** /**
* Takes VariantType, and returns a type that represents the preset object. * Takes VariantType, and returns a type that represents the preset object.
* *
@ -99,6 +114,7 @@ export function vcn<V extends VariantType>(param: {
*/ */
base?: string | undefined; base?: string | undefined;
variants: V; variants: V;
dynamics?: DynamicClassName<V>[];
defaults: VariantKV<V>; defaults: VariantKV<V>;
presets?: undefined; presets?: undefined;
}): [ }): [
@ -129,6 +145,7 @@ export function vcn<V extends VariantType, P extends PresetType<V>>(param: {
*/ */
base?: string | undefined; base?: string | undefined;
variants: V /* VariantType */; variants: V /* VariantType */;
dynamics?: DynamicClassName<V>[];
defaults: VariantKV<V>; defaults: VariantKV<V>;
presets: P; presets: P;
}): [ }): [
@ -164,11 +181,13 @@ export function vcn<
>({ >({
base, base,
variants, variants,
dynamics = [],
defaults, defaults,
presets, presets,
}: { }: {
base?: string | undefined; base?: string | undefined;
variants: V; variants: V;
dynamics?: DynamicClassName<V>[];
defaults: VariantKV<V>; defaults: VariantKV<V>;
presets?: P; presets?: P;
}) { }) {
@ -227,6 +246,10 @@ export function vcn<
// make dynamics result // make dynamics result
const dynamicClasses: string[] = [] const dynamicClasses: string[] = []
for (const dynamicFunction of dynamics) {
dynamicClasses.push(dynamicFunction(kv));
}
return __transformer__(kv, dynamicClasses, className); return __transformer__(kv, dynamicClasses, className);
}, },