Comparing version 0.4.0 to 0.5.0
@@ -1,19 +0,4 @@ | ||
export type ClassDictionary = Record< | ||
string, | ||
| ClassValue[] | ||
| string | ||
| number | ||
| null | ||
| boolean | ||
| undefined | ||
| Record<string, ClassValue[] | string | number | null | boolean | undefined> | ||
> | ||
export type ClassValue = | ||
| ClassValue[] | ||
| ClassDictionary | ||
| string | ||
| number | ||
| null | ||
| boolean | ||
| undefined | ||
export declare function cx(...inputs: ClassValue[]): string | ||
import { ClassValue } from "./types"; | ||
export type CxOptions = Parameters<typeof cx>; | ||
export type CxReturn = ReturnType<typeof cx>; | ||
export declare function cx(...inputs: ClassValue[]): string; |
@@ -1,208 +0,3 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
function _export(target, all) { | ||
for(var name in all)Object.defineProperty(target, name, { | ||
enumerable: true, | ||
get: all[name] | ||
}); | ||
} | ||
_export(exports, { | ||
cx: function() { | ||
return cx; | ||
}, | ||
cn: function() { | ||
return cn; | ||
}, | ||
cvax: function() { | ||
return cvax; | ||
}, | ||
mergeVariants: function() { | ||
return mergeVariants; | ||
}, | ||
merge: function() { | ||
return merge; | ||
} | ||
}); | ||
const _tailwindmerge = require("tailwind-merge"); | ||
const _lodashisequal = /*#__PURE__*/ _interop_require_default(require("lodash.isequal" // FIXME: find the way to not to use lodash | ||
)); | ||
function _interop_require_default(obj) { | ||
return obj && obj.__esModule ? obj : { | ||
default: obj | ||
}; | ||
} | ||
function falsyToString(value) { | ||
if (typeof value === "boolean") { | ||
return `${value}`; | ||
} | ||
if (typeof value === "number") { | ||
return value === 0 ? "0" : value; | ||
} | ||
return value; | ||
} | ||
function cx() { | ||
let i = 0, str = "", tmp, { length } = arguments; | ||
while(i < length){ | ||
if (tmp = arguments[i++]) { | ||
str += getStr(tmp); | ||
} | ||
} | ||
return str.replace(/\s+/g, " ").trim(); | ||
} | ||
function getStr(classes) { | ||
if (!classes || typeof classes === "boolean") return ""; | ||
if (typeof classes === "number") return classes.toString() + " "; | ||
if (typeof classes === "object") { | ||
let str = ""; | ||
if (Array.isArray(classes)) { | ||
if (classes.length === 0) return ""; | ||
for (const item of classes.flat(Infinity)){ | ||
if (item) { | ||
str += getStr(item); | ||
} | ||
} | ||
} else { | ||
for(const key in classes){ | ||
if (key === "class" || key === "className") { | ||
str += getStr(classes[key]) + " "; | ||
} else if (classes[key]) { | ||
str += key + " "; | ||
} | ||
} | ||
} | ||
return str; | ||
} | ||
return classes + " "; | ||
} | ||
function cn() { | ||
for(var _len = arguments.length, inputs = new Array(_len), _key = 0; _key < _len; _key++){ | ||
inputs[_key] = arguments[_key]; | ||
} | ||
return (0, _tailwindmerge.twMerge)(cx(inputs)); | ||
} | ||
function cvax(config) { | ||
if (config.variants == null) return (props)=>cx(config?.base, props?.className); | ||
return (props)=>{ | ||
const { variants , defaultVariants } = config; | ||
if (!variants) return cx(props?.className); | ||
const getVariantClassNames = Object.keys(variants).map((variant)=>{ | ||
const variantProp = props?.[variant]; | ||
const defaultVariantProp = defaultVariants?.[variant]; | ||
if (variantProp === null) return null; | ||
const variantKey = falsyToString(variantProp) || falsyToString(defaultVariantProp); | ||
return variants[variant][variantKey]; | ||
}); | ||
const propsWithoutUndefined = props && Object.entries(props).reduce((acc, param)=>{ | ||
let [key, value] = param; | ||
if (value === undefined) { | ||
return acc; | ||
} | ||
acc[key] = value; | ||
return acc; | ||
}, {}); | ||
const getCompoundVariantClassNames = config?.compoundVariants?.reduce((acc, param)=>{ | ||
let { className: cvClassName , ...compoundVariantOptions } = param; | ||
return Object.entries(compoundVariantOptions).every((param)=>{ | ||
let [key, value] = param; | ||
return Array.isArray(value) ? value.includes({ | ||
...defaultVariants, | ||
...propsWithoutUndefined | ||
}[key]) : ({ | ||
...defaultVariants, | ||
...propsWithoutUndefined | ||
})[key] === value; | ||
}) ? [ | ||
...acc, | ||
cvClassName | ||
] : acc; | ||
}, []); | ||
return cx(config?.base, getVariantClassNames, getCompoundVariantClassNames, props?.className); | ||
}; | ||
} | ||
function mergeVariants(baseVariants, newVariants) { | ||
const base_ = getAbsentKeys(baseVariants); | ||
const new_ = getAbsentKeys(newVariants); | ||
let base = ""; | ||
if (baseVariants.base || newVariants.base) { | ||
base = cn(baseVariants.base, newVariants.base); | ||
} | ||
const variants = getVariants(base_.variants, new_.variants); | ||
const defaultVariants = getDefaultVariants(base_.defaultVariants, new_.defaultVariants); | ||
const compoundVariants = getCompoundVariants(base_.compoundVariants, new_.compoundVariants); | ||
return { | ||
base, | ||
variants, | ||
defaultVariants, | ||
compoundVariants | ||
}; | ||
// return { | ||
// ...(base && { base }), | ||
// ...(Object.keys(variants).length > 0 && { variants }), | ||
// ...(Object.keys(defaultVariants).length > 0 && { defaultVariants }), | ||
// ...(compoundVariants.length > 0 && { compoundVariants }), | ||
// } | ||
} | ||
function getAbsentKeys(config) { | ||
const obj = Object.assign({}, config); | ||
if (!("variants" in config)) Object.assign(obj, { | ||
variants: {} | ||
}); | ||
if (!("defaultVariants" in config)) Object.assign(obj, { | ||
defaultVariants: {} | ||
}); | ||
if (!("compoundVariants" in config)) Object.assign(obj, { | ||
compoundVariants: [] | ||
}); | ||
return obj; | ||
} | ||
function getVariants(baseVariants, newVariants) { | ||
const variants = { | ||
...baseVariants | ||
}; | ||
Object.entries(newVariants).map((param)=>{ | ||
let [variant, value] = param; | ||
return Object.entries(value).map((param)=>{ | ||
let [key, classes] = param; | ||
if (!(variant in variants)) Object.assign(variants, { | ||
[variant]: {} | ||
}); | ||
Object.assign(variants[variant], { | ||
[key]: cn(variants?.[variant]?.[key], classes) | ||
}); | ||
}); | ||
}); | ||
return variants; | ||
} | ||
function getDefaultVariants(baseVariants, newVariants) { | ||
return merge(baseVariants, newVariants); | ||
} | ||
// FIXME: make newVariants as first priopity | ||
// TODO: optimize algorithm | ||
function getCompoundVariants(baseVariants, newVariants) { | ||
const arr = [ | ||
...baseVariants, | ||
...newVariants | ||
]; | ||
const markArr = []; | ||
for (const [key, { className , ...rest }] of arr.entries()){ | ||
for(let i = key + 1; i < arr.length; i++){ | ||
const { className , ...arrRest } = arr[i]; | ||
if ((0, _lodashisequal.default)(rest, arrRest)) markArr[i] = null; | ||
} | ||
} | ||
return arr.map((item, index)=>markArr[index] === undefined ? item : null).filter(Boolean); | ||
} | ||
function merge() { | ||
for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){ | ||
args[_key] = arguments[_key]; | ||
} | ||
return Object.assign({}, ...args.filter(cleanObjects)); | ||
} | ||
function cleanObjects(element) { | ||
if (element === null) return false; | ||
if (Array.isArray(element)) return false; | ||
return Object.keys(element).length !== 0; | ||
} | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:true});function _export(target,all){for(var name in all)Object.defineProperty(target,name,{enumerable:true,get:all[name]})}_export(exports,{createVariant:function(){return createVariant},cvax:function(){return cvax}});const _cx=require("./cx");function createVariant(arg){return arg}function cvax(config){if(!config)return props=>(0,_cx.cx)(props?.class,props?.className);if(!config.variants)return props=>(0,_cx.cx)(config.base,props?.class,props?.className);return props=>{let classes=(0,_cx.cx)(config.base);let tmp="";if(!props){if(!("defaultVariants"in config)||!config?.defaultVariants)return classes;for(const variant in config.variants){const key=toString(config?.defaultVariants?.[variant]);if(tmp=config.variants[variant][key]){classes=(0,_cx.cx)(classes,tmp)}}if(!config?.compoundVariants)return classes;let adding=true;for(const{class:Class,className,...compound}of config.compoundVariants){for(const prop in compound){assertsKeyof(prop);if(config?.defaultVariants?.[prop]!==compound[prop]){adding=false;break}}if(adding)classes=(0,_cx.cx)(classes,Class,className);adding=true}return classes}for(const variant in config.variants){const value=toString(props[variant])||toString(config.defaultVariants?.[variant]);if(tmp=config.variants?.[variant][value]){classes=(0,_cx.cx)(classes,tmp)}}if(!config.compoundVariants)return(0,_cx.cx)(classes,props.class,props.className);let adding=true;for(const{class:Class,className,...compound}of config.compoundVariants){for(const prop in compound){assertsKeyof(prop);if(Array.isArray(compound[prop])){if(!compound[prop].includes(props[prop])){adding=false}}else{const some=prop in props?props?.[prop]:config?.defaultVariants?.[prop];if(some!==compound[prop]){adding=false;break}}}if(adding)classes=(0,_cx.cx)(classes,Class,className);adding=true}return(0,_cx.cx)(classes,props?.class,props?.className)}}function assertsKeyof(arg){}function toString(value){if(typeof value==="boolean"||typeof value==="number"){return value.toString()}if(!value)return"";return value.toString()} | ||
//# sourceMappingURL=index.cjs.js.map |
@@ -1,46 +0,28 @@ | ||
import type { ClassProp, OmitUndefined, StringToBoolean } from "./types"; | ||
export type ClassDictionary = Record<string, ClassValue[] | string | number | null | boolean | undefined | Record<string, ClassValue[] | string | number | null | boolean | undefined>>; | ||
export type ClassValue = ClassValue[] | ClassDictionary | string | number | null | boolean | undefined; | ||
export declare function cx(...inputs: ClassValue[]): string; | ||
export declare function cn(...inputs: ClassValue[]): string; | ||
export type VariantProps<Component extends (...args: any) => any> = Omit<OmitUndefined<Parameters<Component>[0]>, "class" | "className">; | ||
export type ConfigSchema = Record<string, Record<string, ClassValue>>; | ||
export type ConfigVariants<T extends ConfigSchema> = { | ||
[Variant in keyof T]?: StringToBoolean<keyof T[Variant]> | null; | ||
import type { ClassProp, ClassValue, ExcludeUndefined, Prettify, StringToBoolean } from "./types"; | ||
export declare function createVariant<T>(arg: Config<T>): Prettify<Config<T>>; | ||
export type VariantProps<T> = T extends (props: infer U) => string ? Omit<ExcludeUndefined<U>, keyof ClassProp> : never; | ||
export type VariantShape = Record<string, Record<string, ClassValue>>; | ||
export type ConfigVariantsMulti<V extends VariantShape> = { | ||
[Variant in keyof V]?: StringToBoolean<keyof V[Variant]> | StringToBoolean<keyof V[Variant]>[]; | ||
}; | ||
export type ConfigVariantsMulti<T extends ConfigSchema> = { | ||
[Variant in keyof T]?: StringToBoolean<keyof T[Variant]> | StringToBoolean<keyof T[Variant]>[]; | ||
export type VariantSchema<V extends VariantShape> = { | ||
[Variant in keyof V]?: StringToBoolean<keyof V[Variant]> | "unset" | undefined | null; | ||
}; | ||
export type Config<T> = T extends ConfigSchema ? { | ||
export type Config<V> = V extends VariantShape ? ConfigBase & { | ||
variants?: V; | ||
defaultVariants?: VariantSchema<V>; | ||
compoundVariants?: (V extends VariantShape ? (VariantSchema<V> | VariantSchemaMultiple<V>) & ClassProp : ClassProp)[]; | ||
} : ConfigBase & { | ||
variants?: never; | ||
defaultVariants?: never; | ||
compoundVariants?: never; | ||
}; | ||
type VariantSchemaMultiple<V extends VariantShape> = { | ||
[Variant in keyof V]?: StringToBoolean<keyof V[Variant]> | StringToBoolean<keyof V[Variant]>[] | undefined; | ||
}; | ||
type ConfigBase = { | ||
base?: ClassValue; | ||
variants?: T; | ||
defaultVariants?: ConfigVariants<T>; | ||
compoundVariants?: readonly (T extends ConfigSchema ? (ConfigVariants<T> | ConfigVariantsMulti<T>) & ClassProp : ClassProp)[]; | ||
} : never; | ||
type Props<T> = T extends ConfigSchema ? ConfigVariants<T> & ClassProp : ClassProp; | ||
export declare function cvax<T>(config: Config<T>): (props?: Props<T>) => string; | ||
export declare function mergeVariants<T, U>(baseVariants: Config<T>, newVariants: Config<U>): { | ||
base: string; | ||
variants: import("type-fest/source/conditional-simplify").ConditionalSimplifyDeep<[undefined] extends [import("type-fest/source/conditional-simplify").ConditionalSimplifyDeep<T, Function | Iterable<unknown>, object> | import("type-fest/source/conditional-simplify").ConditionalSimplifyDeep<U, Function | Iterable<unknown>, object>] ? never : import("type-fest/source/conditional-simplify").ConditionalSimplifyDeep<T, Function | Iterable<unknown>, object> extends infer T_1 ? T_1 extends import("type-fest/source/conditional-simplify").ConditionalSimplifyDeep<T, Function | Iterable<unknown>, object> ? T_1 extends import("type-fest/source/internal").UnknownRecord ? import("type-fest/source/conditional-simplify").ConditionalSimplifyDeep<U, Function | Iterable<unknown>, object> extends infer T_2 ? T_2 extends import("type-fest/source/conditional-simplify").ConditionalSimplifyDeep<U, Function | Iterable<unknown>, object> ? T_2 extends import("type-fest/source/internal").UnknownRecord ? import("type-fest/source/merge-deep").MergeDeepRecord<T_1, T_2, { | ||
arrayMergeMode: "replace"; | ||
recurseIntoArrays: false; | ||
spreadTopLevelArrays: true; | ||
}> : never : never : never : T_1 extends import("type-fest/source/internal").UnknownArrayOrTuple ? import("type-fest/source/conditional-simplify").ConditionalSimplifyDeep<U, Function | Iterable<unknown>, object> extends infer T_3 ? T_3 extends import("type-fest/source/conditional-simplify").ConditionalSimplifyDeep<U, Function | Iterable<unknown>, object> ? T_3 extends import("type-fest/source/internal").UnknownArrayOrTuple ? (Exclude<T_1, undefined>[number] | Exclude<T_3, undefined>[number])[] : never : never : never : never : never : never, Function | Iterable<unknown>, object>; | ||
defaultVariants: Identity<Pick<ConfigVariants<T>, Exclude<keyof T, keyof Identity<Pick<ConfigVariants<U>, Exclude<keyof U, never>> & Pick<unknown, never> & SpreadProperties<ConfigVariants<U>, unknown, never>>>> & Pick<Identity<Pick<ConfigVariants<U>, Exclude<keyof U, never>> & Pick<unknown, never> & SpreadProperties<ConfigVariants<U>, unknown, never>>, Exclude<keyof Identity<Pick<ConfigVariants<U>, Exclude<keyof U, never>> & Pick<unknown, never> & SpreadProperties<ConfigVariants<U>, unknown, never>>, OptionalPropertyNames<Identity<Pick<ConfigVariants<U>, Exclude<keyof U, never>> & Pick<unknown, never> & SpreadProperties<ConfigVariants<U>, unknown, never>>>>> & Pick<Identity<Pick<ConfigVariants<U>, Exclude<keyof U, never>> & Pick<unknown, never> & SpreadProperties<ConfigVariants<U>, unknown, never>>, Exclude<OptionalPropertyNames<Identity<Pick<ConfigVariants<U>, Exclude<keyof U, never>> & Pick<unknown, never> & SpreadProperties<ConfigVariants<U>, unknown, never>>>, keyof T>> & SpreadProperties<ConfigVariants<T>, Identity<Pick<ConfigVariants<U>, Exclude<keyof U, never>> & Pick<unknown, never> & SpreadProperties<ConfigVariants<U>, unknown, never>>, OptionalPropertyNames<Identity<Pick<ConfigVariants<U>, Exclude<keyof U, never>> & Pick<unknown, never> & SpreadProperties<ConfigVariants<U>, unknown, never>>> & keyof T>>; | ||
compoundVariants: ((T extends ConfigSchema ? (ConfigVariants<T> | ConfigVariantsMulti<T>) & ClassProp : ClassProp) | (U extends ConfigSchema ? (ConfigVariants<U> | ConfigVariantsMulti<U>) & ClassProp : ClassProp))[]; | ||
}; | ||
export declare function merge<Args extends object[]>(...args: [...Args]): Spread<Args>; | ||
type Spread<Args extends readonly [...any]> = Args extends [infer Left, ...infer Right] ? SpreadTwo<Left, Spread<Right>> : unknown; | ||
type SpreadTwo<Left, Right> = Identity<Pick<Left, Exclude<keyof Left, keyof Right>> & Pick<Right, Exclude<keyof Right, OptionalPropertyNames<Right>>> & Pick<Right, Exclude<OptionalPropertyNames<Right>, keyof Left>> & SpreadProperties<Left, Right, OptionalPropertyNames<Right> & keyof Left>>; | ||
type Identity<T> = T extends infer U ? { | ||
[Key in keyof U]: U[Key]; | ||
} : never; | ||
type OptionalPropertyNames<T> = { | ||
[Key in keyof T]-?: {} extends { | ||
[P in Key]: T[Key]; | ||
} ? Key : never; | ||
}[keyof T]; | ||
type SpreadProperties<Left, Right, Key extends keyof Left & keyof Right> = { | ||
[P in Key]: Left[P] | Exclude<Right[P], undefined>; | ||
}; | ||
type Props<T> = T extends VariantShape ? Prettify<VariantSchema<T>> & ClassProp : ClassProp; | ||
export declare function cvax<_ extends "internal use only.", T>(config: Config<T>): (props?: Props<T>) => string; | ||
export {}; |
@@ -1,178 +0,3 @@ | ||
import { twMerge } from "tailwind-merge"; | ||
import isEqual from "lodash.isequal"; // FIXME: find the way to not to use lodash | ||
function falsyToString(value) { | ||
if (typeof value === "boolean") { | ||
return `${value}`; | ||
} | ||
if (typeof value === "number") { | ||
return value === 0 ? "0" : value; | ||
} | ||
return value; | ||
} | ||
export function cx() { | ||
let i = 0, str = "", tmp, { length } = arguments; | ||
while(i < length){ | ||
if (tmp = arguments[i++]) { | ||
str += getStr(tmp); | ||
} | ||
} | ||
return str.replace(/\s+/g, " ").trim(); | ||
} | ||
function getStr(classes) { | ||
if (!classes || typeof classes === "boolean") return ""; | ||
if (typeof classes === "number") return classes.toString() + " "; | ||
if (typeof classes === "object") { | ||
let str = ""; | ||
if (Array.isArray(classes)) { | ||
if (classes.length === 0) return ""; | ||
for (const item of classes.flat(Infinity)){ | ||
if (item) { | ||
str += getStr(item); | ||
} | ||
} | ||
} else { | ||
for(const key in classes){ | ||
if (key === "class" || key === "className") { | ||
str += getStr(classes[key]) + " "; | ||
} else if (classes[key]) { | ||
str += key + " "; | ||
} | ||
} | ||
} | ||
return str; | ||
} | ||
return classes + " "; | ||
} | ||
/* cn | ||
============================================ */ export function cn() { | ||
for(var _len = arguments.length, inputs = new Array(_len), _key = 0; _key < _len; _key++){ | ||
inputs[_key] = arguments[_key]; | ||
} | ||
return twMerge(cx(inputs)); | ||
} | ||
export function cvax(config) { | ||
if (config.variants == null) return (props)=>cx(config?.base, props?.className); | ||
return (props)=>{ | ||
const { variants , defaultVariants } = config; | ||
if (!variants) return cx(props?.className); | ||
const getVariantClassNames = Object.keys(variants).map((variant)=>{ | ||
const variantProp = props?.[variant]; | ||
const defaultVariantProp = defaultVariants?.[variant]; | ||
if (variantProp === null) return null; | ||
const variantKey = falsyToString(variantProp) || falsyToString(defaultVariantProp); | ||
return variants[variant][variantKey]; | ||
}); | ||
const propsWithoutUndefined = props && Object.entries(props).reduce((acc, param)=>{ | ||
let [key, value] = param; | ||
if (value === undefined) { | ||
return acc; | ||
} | ||
acc[key] = value; | ||
return acc; | ||
}, {}); | ||
const getCompoundVariantClassNames = config?.compoundVariants?.reduce((acc, param)=>{ | ||
let { className: cvClassName , ...compoundVariantOptions } = param; | ||
return Object.entries(compoundVariantOptions).every((param)=>{ | ||
let [key, value] = param; | ||
return Array.isArray(value) ? value.includes({ | ||
...defaultVariants, | ||
...propsWithoutUndefined | ||
}[key]) : ({ | ||
...defaultVariants, | ||
...propsWithoutUndefined | ||
})[key] === value; | ||
}) ? [ | ||
...acc, | ||
cvClassName | ||
] : acc; | ||
}, []); | ||
return cx(config?.base, getVariantClassNames, getCompoundVariantClassNames, props?.className); | ||
}; | ||
} | ||
// TODO: merge non-tailwind classes?.. | ||
export function mergeVariants(baseVariants, newVariants) { | ||
const base_ = getAbsentKeys(baseVariants); | ||
const new_ = getAbsentKeys(newVariants); | ||
let base = ""; | ||
if (baseVariants.base || newVariants.base) { | ||
base = cn(baseVariants.base, newVariants.base); | ||
} | ||
const variants = getVariants(base_.variants, new_.variants); | ||
const defaultVariants = getDefaultVariants(base_.defaultVariants, new_.defaultVariants); | ||
const compoundVariants = getCompoundVariants(base_.compoundVariants, new_.compoundVariants); | ||
return { | ||
base, | ||
variants, | ||
defaultVariants, | ||
compoundVariants | ||
}; | ||
// return { | ||
// ...(base && { base }), | ||
// ...(Object.keys(variants).length > 0 && { variants }), | ||
// ...(Object.keys(defaultVariants).length > 0 && { defaultVariants }), | ||
// ...(compoundVariants.length > 0 && { compoundVariants }), | ||
// } | ||
} | ||
function getAbsentKeys(config) { | ||
const obj = Object.assign({}, config); | ||
if (!("variants" in config)) Object.assign(obj, { | ||
variants: {} | ||
}); | ||
if (!("defaultVariants" in config)) Object.assign(obj, { | ||
defaultVariants: {} | ||
}); | ||
if (!("compoundVariants" in config)) Object.assign(obj, { | ||
compoundVariants: [] | ||
}); | ||
return obj; | ||
} | ||
function getVariants(baseVariants, newVariants) { | ||
const variants = { | ||
...baseVariants | ||
}; | ||
Object.entries(newVariants).map((param)=>{ | ||
let [variant, value] = param; | ||
return Object.entries(value).map((param)=>{ | ||
let [key, classes] = param; | ||
if (!(variant in variants)) Object.assign(variants, { | ||
[variant]: {} | ||
}); | ||
Object.assign(variants[variant], { | ||
[key]: cn(variants?.[variant]?.[key], classes) | ||
}); | ||
}); | ||
}); | ||
return variants; | ||
} | ||
function getDefaultVariants(baseVariants, newVariants) { | ||
return merge(baseVariants, newVariants); | ||
} | ||
// FIXME: make newVariants as first priopity | ||
// TODO: optimize algorithm | ||
function getCompoundVariants(baseVariants, newVariants) { | ||
const arr = [ | ||
...baseVariants, | ||
...newVariants | ||
]; | ||
const markArr = []; | ||
for (const [key, { className , ...rest }] of arr.entries()){ | ||
for(let i = key + 1; i < arr.length; i++){ | ||
const { className , ...arrRest } = arr[i]; | ||
if (isEqual(rest, arrRest)) markArr[i] = null; | ||
} | ||
} | ||
return arr.map((item, index)=>markArr[index] === undefined ? item : null).filter(Boolean); | ||
} | ||
/* merge | ||
============================================ */ export function merge() { | ||
for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){ | ||
args[_key] = arguments[_key]; | ||
} | ||
return Object.assign({}, ...args.filter(cleanObjects)); | ||
} | ||
function cleanObjects(element) { | ||
if (element === null) return false; | ||
if (Array.isArray(element)) return false; | ||
return Object.keys(element).length !== 0; | ||
} | ||
import{cx}from"./cx";export function createVariant(arg){return arg}export function cvax(config){if(!config)return props=>cx(props?.class,props?.className);if(!config.variants)return props=>cx(config.base,props?.class,props?.className);return props=>{let classes=cx(config.base);let tmp="";if(!props){if(!("defaultVariants"in config)||!config?.defaultVariants)return classes;for(const variant in config.variants){const key=toString(config?.defaultVariants?.[variant]);if(tmp=config.variants[variant][key]){classes=cx(classes,tmp)}}if(!config?.compoundVariants)return classes;let adding=true;for(const{class:Class,className,...compound}of config.compoundVariants){for(const prop in compound){assertsKeyof(prop);if(config?.defaultVariants?.[prop]!==compound[prop]){adding=false;break}}if(adding)classes=cx(classes,Class,className);adding=true}return classes}for(const variant in config.variants){const value=toString(props[variant])||toString(config.defaultVariants?.[variant]);if(tmp=config.variants?.[variant][value]){classes=cx(classes,tmp)}}if(!config.compoundVariants)return cx(classes,props.class,props.className);let adding=true;for(const{class:Class,className,...compound}of config.compoundVariants){for(const prop in compound){assertsKeyof(prop);if(Array.isArray(compound[prop])){if(!compound[prop].includes(props[prop])){adding=false}}else{const some=prop in props?props?.[prop]:config?.defaultVariants?.[prop];if(some!==compound[prop]){adding=false;break}}}if(adding)classes=cx(classes,Class,className);adding=true}return cx(classes,props?.class,props?.className)}}function assertsKeyof(arg){}function toString(value){if(typeof value==="boolean"||typeof value==="number"){return value.toString()}if(!value)return"";return value.toString()} | ||
//# sourceMappingURL=index.esm.js.map |
@@ -1,27 +0,20 @@ | ||
import type { ClassProp } from "./types"; | ||
import { Config, ConfigSchema, ConfigVariants, ConfigVariantsMulti } from "."; | ||
export declare function mergeVariants<T, U>(baseVariants: Config<T>, newVariants: Config<U>): { | ||
base: string; | ||
variants: import("type-fest/source/conditional-simplify").ConditionalSimplifyDeep<[undefined] extends [import("type-fest/source/conditional-simplify").ConditionalSimplifyDeep<T, Function | Iterable<unknown>, object> | import("type-fest/source/conditional-simplify").ConditionalSimplifyDeep<U, Function | Iterable<unknown>, object>] ? never : import("type-fest/source/conditional-simplify").ConditionalSimplifyDeep<T, Function | Iterable<unknown>, object> extends infer T_1 ? T_1 extends import("type-fest/source/conditional-simplify").ConditionalSimplifyDeep<T, Function | Iterable<unknown>, object> ? T_1 extends import("type-fest/source/internal").UnknownRecord ? import("type-fest/source/conditional-simplify").ConditionalSimplifyDeep<U, Function | Iterable<unknown>, object> extends infer T_2 ? T_2 extends import("type-fest/source/conditional-simplify").ConditionalSimplifyDeep<U, Function | Iterable<unknown>, object> ? T_2 extends import("type-fest/source/internal").UnknownRecord ? import("type-fest/source/merge-deep").MergeDeepRecord<T_1, T_2, { | ||
arrayMergeMode: "replace"; | ||
recurseIntoArrays: false; | ||
spreadTopLevelArrays: true; | ||
}> : never : never : never : T_1 extends import("type-fest/source/internal").UnknownArrayOrTuple ? import("type-fest/source/conditional-simplify").ConditionalSimplifyDeep<U, Function | Iterable<unknown>, object> extends infer T_3 ? T_3 extends import("type-fest/source/conditional-simplify").ConditionalSimplifyDeep<U, Function | Iterable<unknown>, object> ? T_3 extends import("type-fest/source/internal").UnknownArrayOrTuple ? (Exclude<T_1, undefined>[number] | Exclude<T_3, undefined>[number])[] : never : never : never : never : never : never, Function | Iterable<unknown>, object>; | ||
defaultVariants: Identity<Pick<ConfigVariants<T>, Exclude<keyof T, keyof Identity<Pick<ConfigVariants<U>, Exclude<keyof U, never>> & Pick<unknown, never> & SpreadProperties<ConfigVariants<U>, unknown, never>>>> & Pick<Identity<Pick<ConfigVariants<U>, Exclude<keyof U, never>> & Pick<unknown, never> & SpreadProperties<ConfigVariants<U>, unknown, never>>, Exclude<keyof Identity<Pick<ConfigVariants<U>, Exclude<keyof U, never>> & Pick<unknown, never> & SpreadProperties<ConfigVariants<U>, unknown, never>>, OptionalPropertyNames<Identity<Pick<ConfigVariants<U>, Exclude<keyof U, never>> & Pick<unknown, never> & SpreadProperties<ConfigVariants<U>, unknown, never>>>>> & Pick<Identity<Pick<ConfigVariants<U>, Exclude<keyof U, never>> & Pick<unknown, never> & SpreadProperties<ConfigVariants<U>, unknown, never>>, Exclude<OptionalPropertyNames<Identity<Pick<ConfigVariants<U>, Exclude<keyof U, never>> & Pick<unknown, never> & SpreadProperties<ConfigVariants<U>, unknown, never>>>, keyof T>> & SpreadProperties<ConfigVariants<T>, Identity<Pick<ConfigVariants<U>, Exclude<keyof U, never>> & Pick<unknown, never> & SpreadProperties<ConfigVariants<U>, unknown, never>>, OptionalPropertyNames<Identity<Pick<ConfigVariants<U>, Exclude<keyof U, never>> & Pick<unknown, never> & SpreadProperties<ConfigVariants<U>, unknown, never>>> & keyof T>>; | ||
compoundVariants: ((T extends ConfigSchema ? (ConfigVariants<T> | ConfigVariantsMulti<T>) & ClassProp : ClassProp) | (U extends ConfigSchema ? (ConfigVariants<U> | ConfigVariantsMulti<U>) & ClassProp : ClassProp))[]; | ||
import type { Prettify } from "./types"; | ||
import type { Config } from "."; | ||
type MergeVariants<Left, Right> = { | ||
[Key in keyof Left & keyof Right]: MergeObjects<Left[Key], Right[Key]>; | ||
} & MergeObjects<Left, Right>; | ||
type ToString<T> = T extends string ? string : T extends string[] ? string[] : T; | ||
type MergeObjects<Left, Right> = { | ||
[Prop in keyof Left | keyof Right]: Prop extends keyof Right ? Right[Prop] : Prop extends keyof Left ? ToString<Left[Prop]> : never; | ||
}; | ||
export declare function merge<Args extends object[]>(...args: [...Args]): Spread<Args>; | ||
type Spread<Args extends readonly [...any]> = Args extends [infer Left, ...infer Right] ? SpreadTwo<Left, Spread<Right>> : unknown; | ||
type SpreadTwo<Left, Right> = Identity<Pick<Left, Exclude<keyof Left, keyof Right>> & Pick<Right, Exclude<keyof Right, OptionalPropertyNames<Right>>> & Pick<Right, Exclude<OptionalPropertyNames<Right>, keyof Left>> & SpreadProperties<Left, Right, OptionalPropertyNames<Right> & keyof Left>>; | ||
type Identity<T> = T extends infer U ? { | ||
[Key in keyof U]: U[Key]; | ||
} : never; | ||
type OptionalPropertyNames<T> = { | ||
[Key in keyof T]-?: {} extends { | ||
[P in Key]: T[Key]; | ||
} ? Key : never; | ||
}[keyof T]; | ||
type SpreadProperties<Left, Right, Key extends keyof Left & keyof Right> = { | ||
[P in Key]: Left[P] | Exclude<Right[P], undefined>; | ||
type DefaultVariants<T> = { | ||
[Key in keyof T]?: keyof T[Key]; | ||
}; | ||
export declare function mergeVariants<T, U>(baseVariants: Config<T>, newVariants: Config<U>): Prettify<{ | ||
base: string; | ||
variants: Prettify<MergeVariants<T, U>>; | ||
defaultVariants: DefaultVariants<MergeVariants<T, U>>; | ||
compoundVariants: []; | ||
}>; | ||
export declare function mergeTwoObjects<Left extends object, Right extends object>(left: Left, right: Right): Prettify<MergeObjects<Left, Right>>; | ||
export {}; |
@@ -1,9 +0,20 @@ | ||
export type ClassPropKey = "class" | "className" | ||
export type ClassValue = string | null | undefined | ClassValue[] | ||
export type ClassValue = ClassValue[] | ClassDictionary | string | number | null | boolean | undefined; | ||
export type ClassDictionary = Record<string, ClassValue[] | string | number | null | boolean | undefined | Record<string, ClassValue[] | string | number | null | boolean | undefined>>; | ||
export type ClassPropKey = "class" | "className"; | ||
export type ClassProp = { | ||
className?: ClassValue | undefined | ||
} | ||
export type OmitUndefined<T> = T extends undefined ? never : T | ||
export type StringToBoolean<T> = T extends "true" | "false" ? boolean : T | ||
export type CxOptions = ClassValue[] | ||
export type CxReturn = string | ||
class: ClassValue; | ||
className?: never; | ||
} | { | ||
class?: never; | ||
className: ClassValue; | ||
} | { | ||
class?: never; | ||
className?: never; | ||
}; | ||
export type ExcludeUndefined<T> = T extends undefined ? never : T; | ||
export type StringToBoolean<T> = T extends "true" | "false" ? boolean : T; | ||
export type CxOptions = ClassValue[]; | ||
export type CxReturn = string; | ||
export type Prettify<T> = { | ||
[K in keyof T]: T[K] extends object ? Prettify<T[K]> : T[K]; | ||
} & {}; |
{ | ||
"name": "cvax", | ||
"version": "0.4.0", | ||
"version": "0.5.0", | ||
"description": "Customized CVA. fork 'class-variance-authority'", | ||
@@ -14,2 +14,7 @@ "repository": "https://github.com/alexvyber/cvax.git", | ||
], | ||
"exports": { | ||
"import": "./dist/*.mjs", | ||
"require": "./dist/*.js", | ||
"types": "./dist/*.d.ts" | ||
}, | ||
"scripts": { | ||
@@ -19,2 +24,8 @@ "build": "run-p build:**", | ||
"build:esm": "swc ./src/index.ts --config-file ./.config/.swcrc -o dist/index.esm.js -C module.type=es6 ", | ||
"build:cjs-cx": "swc ./src/cx.ts --config-file ./.config/.swcrc -o dist/cx.cjs.js -C module.type=commonjs", | ||
"build:esm-cx": "swc ./src/cx.ts --config-file ./.config/.swcrc -o dist/cx.esm.js -C module.type=es6 ", | ||
"build:cjs-cn": "swc ./src/cn.ts --config-file ./.config/.swcrc -o dist/cn.cjs.js -C module.type=commonjs", | ||
"build:esm-cn": "swc ./src/cn.ts --config-file ./.config/.swcrc -o dist/cn.esm.js -C module.type=es6 ", | ||
"build:cjs-merge": "swc ./src/merge-variants.ts --config-file ./.config/.swcrc -o dist/merge-variants.cjs.js -C module.type=commonjs", | ||
"build:esm-merge": "swc ./src/merge-variants.ts --config-file ./.config/.swcrc -o dist/merge-variants.esm.js -C module.type=es6 ", | ||
"build:tsc": "tsc --project .config/tsconfig.build.json", | ||
@@ -25,3 +36,3 @@ "dev": "vitest", | ||
"test:vitest": "vitest", | ||
"test:size": "pnpm build && bundlesize -f 'dist/*.js' -s 1KB", | ||
"test:size": "pnpm build && bundlesize -f 'dist/*.js' -s 1.1KB", | ||
"tsc": "tsc --project tsconfig.json --noEmit" | ||
@@ -31,8 +42,8 @@ }, | ||
"lodash.isequal": "^4.5.0", | ||
"tailwind-merge": "^1.12.0", | ||
"type-fest": "^3.9.0" | ||
"tailwind-merge": "^1.13.1", | ||
"type-fest": "^3.11.1" | ||
}, | ||
"devDependencies": { | ||
"@swc/cli": "0.1.62", | ||
"@swc/core": "1.3.56", | ||
"@swc/core": "1.3.62", | ||
"@types/lodash.isequal": "^4.5.6", | ||
@@ -42,4 +53,4 @@ "bundlesize": "^0.18.1", | ||
"react": "18.2.0", | ||
"typescript": "5.0.4", | ||
"vitest": "^0.30.1" | ||
"typescript": "5.1.3", | ||
"vitest": "^0.32.0" | ||
}, | ||
@@ -46,0 +57,0 @@ "peerDependencies": { |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 2 instances in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
47459
23
116
3
Updatedtailwind-merge@^1.13.1
Updatedtype-fest@^3.11.1