Comparing version 0.3.1 to 0.4.0
@@ -1,3 +0,19 @@ | ||
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 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 |
@@ -13,7 +13,4 @@ "use strict"; | ||
cx: function() { | ||
return _cx.cx; | ||
return cx; | ||
}, | ||
mergeVariants: function() { | ||
return _mergevariants.mergeVariants; | ||
}, | ||
cn: function() { | ||
@@ -24,7 +21,18 @@ return cn; | ||
return cvax; | ||
}, | ||
mergeVariants: function() { | ||
return mergeVariants; | ||
}, | ||
merge: function() { | ||
return merge; | ||
} | ||
}); | ||
const _tailwindmerge = require("tailwind-merge"); | ||
const _cx = require("./cx"); | ||
const _mergevariants = require("./merge-variants"); | ||
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) { | ||
@@ -39,2 +47,36 @@ if (typeof value === "boolean") { | ||
} | ||
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() { | ||
@@ -44,9 +86,9 @@ for(var _len = arguments.length, inputs = new Array(_len), _key = 0; _key < _len; _key++){ | ||
} | ||
return (0, _tailwindmerge.twMerge)((0, _cx.cx)(inputs)); | ||
return (0, _tailwindmerge.twMerge)(cx(inputs)); | ||
} | ||
function cvax(config) { | ||
if (config.variants == null) return (props)=>(0, _cx.cx)(config?.base, props?.className); | ||
if (config.variants == null) return (props)=>cx(config?.base, props?.className); | ||
return (props)=>{ | ||
const { variants , defaultVariants } = config; | ||
if (!variants) return (0, _cx.cx)(props?.className); | ||
if (!variants) return cx(props?.className); | ||
const getVariantClassNames = Object.keys(variants).map((variant)=>{ | ||
@@ -83,5 +125,89 @@ const variantProp = props?.[variant]; | ||
}, []); | ||
return (0, _cx.cx)(config?.base, getVariantClassNames, getCompoundVariantClassNames, props?.className); | ||
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; | ||
} | ||
@@ -1,5 +0,5 @@ | ||
import type { ClassProp, ClassValue, OmitUndefined, StringToBoolean } from "./types"; | ||
import { cx } from "./cx"; | ||
export { cx }; | ||
export { mergeVariants } from "./merge-variants"; | ||
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; | ||
@@ -22,1 +22,26 @@ export type VariantProps<Component extends (...args: any) => any> = Omit<OmitUndefined<Parameters<Component>[0]>, "class" | "className">; | ||
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>; | ||
}; | ||
export {}; |
import { twMerge } from "tailwind-merge"; | ||
import { cx } from "./cx"; | ||
export { cx }; | ||
export { mergeVariants } from "./merge-variants"; | ||
import isEqual from "lodash.isequal"; // FIXME: find the way to not to use lodash | ||
function falsyToString(value) { | ||
@@ -14,2 +12,36 @@ if (typeof value === "boolean") { | ||
} | ||
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 | ||
@@ -61,2 +93,88 @@ ============================================ */ export function cn() { | ||
} | ||
// 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; | ||
} | ||
@@ -1,9 +0,9 @@ | ||
export type ClassPropKey = "class" | "className"; | ||
export type ClassValue = string | null | undefined | ClassValue[]; | ||
export type ClassPropKey = "class" | "className" | ||
export type ClassValue = string | null | undefined | ClassValue[] | ||
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; | ||
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 |
{ | ||
"name": "cvax", | ||
"version": "0.3.1", | ||
"version": "0.4.0", | ||
"description": "Customized CVA. fork 'class-variance-authority'", | ||
@@ -5,0 +5,0 @@ "repository": "https://github.com/alexvyber/cvax.git", |
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
25707
505
8
1