import { clsx } from 'rntwsc/tw/clsx' import { set } from 'rntwsc/libs/lodash' import type { StrMap } from 'rntwsc/libs/utility-types' type ClassNames = { [K in keyof Map]?: string } type AttrClassName = Map extends undefined ? string : ClassNames type Attr = StrMap> type Attrs = StrMap> type VariantOptions> = { // taken from cva to convert string to boolean if match [K in keyof A]?: keyof A[K] extends 'true' | 'false' ? boolean : keyof A[K] } type CompoundVariant> = VariantOptions & (Map extends undefined ? { className: string } : { classNames: ClassNames }) type GetCvaOptions> = VariantOptions type GetCva> = ( options: GetCvaOptions, ) => Map extends undefined ? string : ClassNames type CvaOptions> = { className?: string classNames?: Map attributes?: A compoundVariants?: CompoundVariant[] } type Cva = , A extends Attrs>( options: CvaOptions, ) => GetCva export type Variant> = Parameters[0] export const cva: Cva = ({ className, classNames, attributes, compoundVariants }) => variant => { const map: StrMap = {} const rootKey = '' pushClassName(map, rootKey, className) pushClassNames(map, classNames) let computedVariant: StrMap | undefined = undefined if (variant) { if (!computedVariant) { computedVariant = {} } for (const [k, v] of Object.entries(variant)) { if (v === undefined || v === null) { continue } set(computedVariant, k, v) } } if (computedVariant) { for (const [k, v] of Object.entries(computedVariant)) { const attr = attributes?.[k]?.[v as string] if (!attr) { continue } if (typeof attr === 'string') { pushClassName(map, rootKey, attr) } else { pushClassNames(map, attr) } } if (compoundVariants) { for (const { className: compoundClassName, classNames: compoundClassNames, ...compound } of compoundVariants) { let match: boolean | undefined = undefined for (const [k2, v2] of Object.entries(compound)) { if (computedVariant[k2] !== v2) { match = false } else if (match === undefined) { match = true } } if (match) { pushClassName(map, rootKey, compoundClassName) pushClassNames(map, compoundClassNames) } } } } if (className) { return clsx(map[rootKey]) } delete map[rootKey] for (const [k, v] of Object.entries(map)) { map[k] = clsx(v) } return map as any } const pushClassName = (map: StrMap, k: string, className: any) => { if (!className) { return } if (Array.isArray(className)) { for (const v of className) { pushClassName(map, k, v) } return } const arr = map[k] if (!arr) { map[k] = [className] } else { arr.push(className) } } const pushClassNames = (map: StrMap, classNames: any) => { if (!classNames) { return } for (const [k, v] of Object.entries(classNames)) { pushClassName(map, k, v) } }