Comparing version 0.2.2 to 0.2.3
@@ -5,3 +5,2 @@ "use strict"; | ||
}); | ||
exports.cx = cx; | ||
exports.cn = cn; | ||
@@ -11,2 +10,3 @@ exports.cvax = cvax; | ||
exports.merge = merge; | ||
exports.cx = cx; | ||
var _tailwindMerge = require("tailwind-merge"); | ||
@@ -21,10 +21,9 @@ var _lodashIsequal // FIXME: find the way to not to use lodash | ||
function falsyToString(value) { | ||
return typeof value === "boolean" ? "".concat(value) : value === 0 ? "0" : value; | ||
} | ||
function cx() { | ||
for(var _len = arguments.length, classes = new Array(_len), _key = 0; _key < _len; _key++){ | ||
classes[_key] = arguments[_key]; | ||
if (typeof value === "boolean") { | ||
return "".concat(value); | ||
} | ||
return classes.flat(Infinity) // HACK: hack around TS behavior | ||
.filter(Boolean).join(" "); | ||
if (typeof value === "number") { | ||
return value === 0 ? "0" : value; | ||
} | ||
return value; | ||
} | ||
@@ -38,6 +37,9 @@ function cn() { | ||
function cvax(config) { | ||
if (config.variants == null) return (props)=>{ | ||
return cx(config === null || config === void 0 ? void 0 : config.base, props === null || props === void 0 ? void 0 : props.className); | ||
}; | ||
return (props)=>{ | ||
var ref; | ||
if ((config === null || config === void 0 ? void 0 : config.variants) == null) return cx(config === null || config === void 0 ? void 0 : config.base, props === null || props === void 0 ? void 0 : props.className); | ||
const { variants , defaultVariants } = config; | ||
if (!variants) return cx(props === null || props === void 0 ? void 0 : props.className); | ||
const getVariantClassNames = Object.keys(variants).map((variant)=>{ | ||
@@ -74,3 +76,3 @@ const variantProp = props === null || props === void 0 ? void 0 : props[variant]; | ||
}, []); | ||
return cn(config === null || config === void 0 ? void 0 : config.base, getVariantClassNames, getCompoundVariantClassNames, props === null || props === void 0 ? void 0 : props.className); | ||
return cx(config === null || config === void 0 ? void 0 : config.base, getVariantClassNames, getCompoundVariantClassNames, props === null || props === void 0 ? void 0 : props.className); | ||
}; | ||
@@ -81,3 +83,6 @@ } | ||
const new_ = getAbsentKeys(newVariants); | ||
const base = cn(baseVariants.base, newVariants.base); | ||
let base = ""; | ||
if (baseVariants.base || newVariants.base) { | ||
base = cn(baseVariants.base, newVariants.base); | ||
} | ||
const variants = getVariants(base_.variants, new_.variants); | ||
@@ -92,14 +97,21 @@ const defaultVariants = getDefaultVariants(base_.defaultVariants, new_.defaultVariants); | ||
}; | ||
// return { | ||
// ...(base && { base }), | ||
// ...(Object.keys(variants).length > 0 && { variants }), | ||
// ...(Object.keys(defaultVariants).length > 0 && { defaultVariants }), | ||
// ...(compoundVariants.length > 0 && { compoundVariants }), | ||
// } | ||
} | ||
function getAbsentKeys(config) { | ||
if (!("variants" in config)) Object.assign(config, { | ||
const obj = Object.assign({}, config); | ||
if (!("variants" in config)) Object.assign(obj, { | ||
variants: {} | ||
}); | ||
if (!("defaultVariants" in config)) Object.assign(config, { | ||
if (!("defaultVariants" in config)) Object.assign(obj, { | ||
defaultVariants: {} | ||
}); | ||
if (!("compoundVariants" in config)) Object.assign(config, { | ||
if (!("compoundVariants" in config)) Object.assign(obj, { | ||
compoundVariants: [] | ||
}); | ||
return config; | ||
return obj; | ||
} | ||
@@ -144,5 +156,2 @@ function getVariants(baseVariants, newVariants) { | ||
} | ||
function removeEmptyObjects(element) { | ||
return Object.keys(element).length === 0; | ||
} | ||
function merge() { | ||
@@ -152,6 +161,86 @@ for(var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++){ | ||
} | ||
return Object.assign({}, ...args.filter(removeEmptyObjects)); | ||
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; | ||
} | ||
function cx() { | ||
for(var _len = arguments.length, classes = new Array(_len), _key = 0; _key < _len; _key++){ | ||
classes[_key] = arguments[_key]; | ||
} | ||
// let tmp, | ||
// x, | ||
// i = 0, | ||
// str = "" | ||
// while (i < classes.length) { | ||
// if ((tmp = classes[i++])) { | ||
// if ((x = toVal(tmp))) { | ||
// str && (str += " ") | ||
// str += x | ||
// } | ||
// } | ||
// } | ||
return classes.flat(Infinity).filter(Boolean).join(" "); | ||
} | ||
function toVal(mix) { | ||
if (!mix) return ""; | ||
if (typeof mix === "string" || typeof mix === "number") { | ||
return mix.toString(); | ||
} | ||
if (Array.isArray(mix)) { | ||
let str = ""; | ||
for (const item of mix){ | ||
if (item) { | ||
str += " " + toVal(item); | ||
} | ||
} | ||
return str; | ||
} | ||
if (typeof mix === "object") { | ||
let str = ""; | ||
for(const key in mix){ | ||
if (mix[key]) { | ||
str += " " + key; | ||
} | ||
} | ||
return str; | ||
} | ||
} | ||
var hasOwn = {}.hasOwnProperty; | ||
function classNames() { | ||
for(var _len = arguments.length, classesArr = new Array(_len), _key = 0; _key < _len; _key++){ | ||
classesArr[_key] = arguments[_key]; | ||
} | ||
var classes = []; | ||
for(var i = 0; i < classesArr.length; i++){ | ||
var arg = classesArr[i]; | ||
if (!arg) continue; | ||
const argType = typeof arg; | ||
if (argType === "string" || argType === "number") { | ||
classes.push(arg); | ||
} else if (Array.isArray(arg)) { | ||
if (arg.length) { | ||
var inner = classNames.apply(null, arg); | ||
if (inner) { | ||
classes.push(inner); | ||
} | ||
} | ||
} else if (argType === "object") { | ||
if (arg.toString !== Object.prototype.toString && !arg.toString.toString().includes("[native code]")) { | ||
classes.push(arg.toString()); | ||
continue; | ||
} | ||
// for (const key in arg) { | ||
// if (hasOwn.call(arg, key) && arg[key]) { | ||
// classes.push(key) | ||
// } | ||
// } | ||
} | ||
} | ||
return classes.join(" "); | ||
} | ||
//# sourceMappingURL=index.cjs.js.map |
import type { ClassProp, ClassValue, CxOptions, CxReturn, OmitUndefined, StringToBoolean } from "./types"; | ||
export declare type VariantProps<Component extends (...args: any) => any> = Omit<OmitUndefined<Parameters<Component>[0]>, "className">; | ||
export declare function cx<T extends CxOptions>(...classes: T): CxReturn; | ||
export declare function cn(...inputs: ClassValue[]): string; | ||
@@ -12,3 +11,3 @@ declare type ConfigSchema = Record<string, Record<string, ClassValue>>; | ||
}; | ||
declare type Config<T> = T extends ConfigSchema ? { | ||
export declare type Config<T> = T extends ConfigSchema ? { | ||
base?: ClassValue; | ||
@@ -20,3 +19,3 @@ variants?: T; | ||
declare 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 cvax<T>(config: Config<T>): (props?: Props<T>) => string; | ||
export declare function mergeVariants<T, U>(baseVariants: Config<T>, newVariants: Config<U>): { | ||
@@ -32,2 +31,8 @@ base: string; | ||
}; | ||
export declare function merge<Args extends object[]>(...args: [...Args]): Spread<Args>; | ||
declare type Spread<Args extends readonly [...any]> = Args extends [infer Left, ...infer Right] ? SpreadTwo<Left, Spread<Right>> : unknown; | ||
declare 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>>; | ||
declare type Identity<T> = T extends infer U ? { | ||
[Key in keyof U]: U[Key]; | ||
} : never; | ||
declare type OptionalPropertyNames<T> = { | ||
@@ -41,8 +46,6 @@ [Key in keyof T]-?: {} extends { | ||
}; | ||
declare type Identity<T> = T extends infer U ? { | ||
[Key in keyof U]: U[Key]; | ||
} : never; | ||
declare 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>>; | ||
declare type Spread<Args extends readonly [...any]> = Args extends [infer Left, ...infer Right] ? SpreadTwo<Left, Spread<Right>> : unknown; | ||
export declare function merge<Args extends object[]>(...args: [...Args]): Spread<Args>; | ||
export declare type XClassValue = XClassArray | XClassDictionary | string | number | null | boolean | undefined; | ||
export declare type XClassDictionary = Record<string, any>; | ||
export declare type XClassArray = ClassValue[]; | ||
export declare function cx<T extends CxOptions>(...classes: T): CxReturn; | ||
export {}; |
import { twMerge } from "tailwind-merge"; | ||
import isEqual from "lodash.isequal"; // FIXME: find the way to not to use lodash | ||
function falsyToString(value) { | ||
return typeof value === "boolean" ? "".concat(value) : value === 0 ? "0" : value; | ||
} | ||
/* cx | ||
============================================ */ export function cx() { | ||
for(var _len = arguments.length, classes = new Array(_len), _key = 0; _key < _len; _key++){ | ||
classes[_key] = arguments[_key]; | ||
if (typeof value === "boolean") { | ||
return "".concat(value); | ||
} | ||
return classes.flat(Infinity) // HACK: hack around TS behavior | ||
.filter(Boolean).join(" "); | ||
if (typeof value === "number") { | ||
return value === 0 ? "0" : value; | ||
} | ||
return value; | ||
} | ||
@@ -22,6 +20,9 @@ /* cn | ||
export function cvax(config) { | ||
if (config.variants == null) return (props)=>{ | ||
return cx(config === null || config === void 0 ? void 0 : config.base, props === null || props === void 0 ? void 0 : props.className); | ||
}; | ||
return (props)=>{ | ||
var ref; | ||
if ((config === null || config === void 0 ? void 0 : config.variants) == null) return cx(config === null || config === void 0 ? void 0 : config.base, props === null || props === void 0 ? void 0 : props.className); | ||
const { variants , defaultVariants } = config; | ||
if (!variants) return cx(props === null || props === void 0 ? void 0 : props.className); | ||
const getVariantClassNames = Object.keys(variants).map((variant)=>{ | ||
@@ -58,10 +59,14 @@ const variantProp = props === null || props === void 0 ? void 0 : props[variant]; | ||
}, []); | ||
return cn(config === null || config === void 0 ? void 0 : config.base, getVariantClassNames, getCompoundVariantClassNames, props === null || props === void 0 ? void 0 : props.className); | ||
return cx(config === null || config === void 0 ? void 0 : config.base, getVariantClassNames, getCompoundVariantClassNames, props === null || props === void 0 ? void 0 : props.className); | ||
}; | ||
} | ||
/* mergeVariants | ||
============================================ */ export function mergeVariants(baseVariants, newVariants) { | ||
============================================ */ // TODO: merge non-tailwind classes?.. | ||
export function mergeVariants(baseVariants, newVariants) { | ||
const base_ = getAbsentKeys(baseVariants); | ||
const new_ = getAbsentKeys(newVariants); | ||
const base = cn(baseVariants.base, newVariants.base); | ||
let base = ""; | ||
if (baseVariants.base || newVariants.base) { | ||
base = cn(baseVariants.base, newVariants.base); | ||
} | ||
const variants = getVariants(base_.variants, new_.variants); | ||
@@ -76,14 +81,21 @@ const defaultVariants = getDefaultVariants(base_.defaultVariants, new_.defaultVariants); | ||
}; | ||
// return { | ||
// ...(base && { base }), | ||
// ...(Object.keys(variants).length > 0 && { variants }), | ||
// ...(Object.keys(defaultVariants).length > 0 && { defaultVariants }), | ||
// ...(compoundVariants.length > 0 && { compoundVariants }), | ||
// } | ||
} | ||
function getAbsentKeys(config) { | ||
if (!("variants" in config)) Object.assign(config, { | ||
const obj = Object.assign({}, config); | ||
if (!("variants" in config)) Object.assign(obj, { | ||
variants: {} | ||
}); | ||
if (!("defaultVariants" in config)) Object.assign(config, { | ||
if (!("defaultVariants" in config)) Object.assign(obj, { | ||
defaultVariants: {} | ||
}); | ||
if (!("compoundVariants" in config)) Object.assign(config, { | ||
if (!("compoundVariants" in config)) Object.assign(obj, { | ||
compoundVariants: [] | ||
}); | ||
return config; | ||
return obj; | ||
} | ||
@@ -128,13 +140,91 @@ function getVariants(baseVariants, newVariants) { | ||
} | ||
function removeEmptyObjects(element) { | ||
return Object.keys(element).length === 0; | ||
} | ||
export function merge() { | ||
/* 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(removeEmptyObjects)); | ||
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; | ||
} | ||
export function cx() { | ||
for(var _len = arguments.length, classes = new Array(_len), _key = 0; _key < _len; _key++){ | ||
classes[_key] = arguments[_key]; | ||
} | ||
// let tmp, | ||
// x, | ||
// i = 0, | ||
// str = "" | ||
// while (i < classes.length) { | ||
// if ((tmp = classes[i++])) { | ||
// if ((x = toVal(tmp))) { | ||
// str && (str += " ") | ||
// str += x | ||
// } | ||
// } | ||
// } | ||
return classes.flat(Infinity).filter(Boolean).join(" "); | ||
} | ||
function toVal(mix) { | ||
if (!mix) return ""; | ||
if (typeof mix === "string" || typeof mix === "number") { | ||
return mix.toString(); | ||
} | ||
if (Array.isArray(mix)) { | ||
let str = ""; | ||
for (const item of mix){ | ||
if (item) { | ||
str += " " + toVal(item); | ||
} | ||
} | ||
return str; | ||
} | ||
if (typeof mix === "object") { | ||
let str = ""; | ||
for(const key in mix){ | ||
if (mix[key]) { | ||
str += " " + key; | ||
} | ||
} | ||
return str; | ||
} | ||
} | ||
var hasOwn = {}.hasOwnProperty; | ||
function classNames() { | ||
for(var _len = arguments.length, classesArr = new Array(_len), _key = 0; _key < _len; _key++){ | ||
classesArr[_key] = arguments[_key]; | ||
} | ||
var classes = []; | ||
for(var i = 0; i < classesArr.length; i++){ | ||
var arg = classesArr[i]; | ||
if (!arg) continue; | ||
const argType = typeof arg; | ||
if (argType === "string" || argType === "number") { | ||
classes.push(arg); | ||
} else if (Array.isArray(arg)) { | ||
if (arg.length) { | ||
var inner = classNames.apply(null, arg); | ||
if (inner) { | ||
classes.push(inner); | ||
} | ||
} | ||
} else if (argType === "object") { | ||
if (arg.toString !== Object.prototype.toString && !arg.toString.toString().includes("[native code]")) { | ||
classes.push(arg.toString()); | ||
continue; | ||
} | ||
// for (const key in arg) { | ||
// if (hasOwn.call(arg, key) && arg[key]) { | ||
// classes.push(key) | ||
// } | ||
// } | ||
} | ||
} | ||
return classes.join(" "); | ||
} | ||
//# sourceMappingURL=index.esm.js.map |
export declare type ClassPropKey = "className"; | ||
export declare type ClassValue = string | null | undefined | readonly ClassValue[]; | ||
export declare type ClassProp = { | ||
className: ClassValue; | ||
className?: ClassValue; | ||
} | { | ||
@@ -6,0 +6,0 @@ className?: never; |
{ | ||
"name": "cvax", | ||
"version": "0.2.2", | ||
"version": "0.2.3", | ||
"description": "Customized CVA. fork 'class-variance-authority'", | ||
@@ -5,0 +5,0 @@ "repository": "https://github.com/alexvyber/cvax.git", |
24928
9
528