@yaredfall/class-variants 0.0.8 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/chunk-3XL4COYB.js +2 -0
- package/build/chunk-3XL4COYB.js.map +1 -0
- package/build/chunk-BV75ULQK.js +2 -0
- package/build/chunk-BV75ULQK.js.map +1 -0
- package/build/index-DUG5hPfi.d.cts +79 -0
- package/build/index-DUG5hPfi.d.ts +79 -0
- package/build/index.cjs +1 -67
- package/build/index.cjs.map +1 -1
- package/build/index.d.cts +3 -33
- package/build/index.d.ts +3 -33
- package/build/index.js +1 -7
- package/build/utils.cjs +1 -47
- package/build/utils.cjs.map +1 -1
- package/build/utils.d.cts +2 -19
- package/build/utils.d.ts +2 -19
- package/build/utils.js +1 -12
- package/build/vue.cjs +1 -95
- package/build/vue.cjs.map +1 -1
- package/build/vue.d.cts +1 -1
- package/build/vue.d.ts +1 -1
- package/build/vue.js +1 -32
- package/build/vue.js.map +1 -1
- package/package.json +56 -52
- package/build/chunk-5UWIPH6K.js +0 -39
- package/build/chunk-5UWIPH6K.js.map +0 -1
- package/build/chunk-AQSN2Y5C.js +0 -21
- package/build/chunk-AQSN2Y5C.js.map +0 -1
- package/src/index.ts +0 -79
- package/src/utils.ts +0 -25
- package/src/vue.ts +0 -41
- package/tsconfig.json +0 -23
- package/tsup.config.js +0 -13
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{a as C,b as c,c as m}from"./chunk-BV75ULQK.js";import{clsx as v}from"clsx";var U=(a,x={})=>{let h=x.merge??v,{variants:e,defaultVariant:r,base:k}=a,V=e?C(e):[],l=e?m(e):{},f=r??{},R=e?typeof a.compoundVariants=="function"?a.compoundVariants({variants:e,keys:V,options:l,defaults:f}):a.compoundVariants:void 0;return Object.assign(s=>{let g=e?V.map(t=>{let i=s?.[t],d=r?.[t],o=i?.toString()||d?.toString();return o?e[t][o]:void 0}):void 0,O=R?.reduce((t,{class:i,className:d,...o})=>(c(o).every(([u,n])=>{let _=r?.[u],p=s?.[u];if(typeof n=="object"&&"__token"in n){if(n.__token===S.__token)return p!==void 0;if(n.__token===W.__token)return p===void 0}let y=p??_;return Array.isArray(n)?n.some(P=>y===P):y===n})&&t.push(i,d),t),new Array);return h(k,g,O,s?.class,s?.className)},{keys:V,options:l,defaults:f})},S={__token:"OnlyWhenDefined"},W={__token:"OnlyWhenUndefined"};export{U as a,S as b,W as c};
|
|
2
|
+
//# sourceMappingURL=chunk-3XL4COYB.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { ClassValue, clsx } from \"clsx\";\r\nimport { MarkRequired, Prettify, StrictExclude, Opaque } from \"ts-essentials\";\r\nimport { getEntries, getKeys, getVariantsOptions, KeysOf, VariantOptions } from \"./utils\";\r\n\r\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\r\nexport type VariantProps<CVR extends (args?: any) => string> = Prettify<\r\n Omit<NonNullable<StrictExclude<Parameters<CVR>[0], void>>, \"class\" | \"className\">\r\n>;\r\n\r\ntype ClassProp =\r\n | { class?: ClassValue; className?: never }\r\n | { class?: never; className?: ClassValue };\r\n\r\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\r\nexport type WithClassProp<T extends Record<string, any>> = T & ClassProp;\r\n\r\ntype BooleanStringToBoolean<T> = T extends \"true\" | \"false\" ? boolean : T;\r\n\r\nexport type CVVariantsSchema = Record<string, Record<string, ClassValue>>;\r\n\r\nexport type CVVariant<V extends CVVariantsSchema> = {\r\n [K in keyof V]?: BooleanStringToBoolean<keyof V[K]>;\r\n};\r\nexport type CVCompoundVariant<V extends CVVariantsSchema> = WithClassProp<{\r\n [K in keyof V]?:\r\n | BooleanStringToBoolean<keyof V[K]>\r\n | BooleanStringToBoolean<keyof V[K]>[]\r\n | CVToken;\r\n}>;\r\n\r\nexport type CVCompoundVariantsSchema<V extends CVVariantsSchema> = CVCompoundVariant<V>[];\r\n\r\nexport interface CVConfig<V extends CVVariantsSchema, R extends keyof V> {\r\n base?: ClassValue;\r\n variants?: V;\r\n compoundVariants?:\r\n | CVCompoundVariantsSchema<V>\r\n | ((arg: {\r\n variants: V;\r\n keys: KeysOf<V>;\r\n options: VariantOptions<V>;\r\n defaults: CVVariant<V>;\r\n }) => CVCompoundVariantsSchema<V>);\r\n defaultVariant?: CVVariant<V>;\r\n requiredProps?: R[];\r\n}\r\nexport interface CVOptions {\r\n merge?: (...args: ClassValue[]) => string;\r\n}\r\n\r\ntype CVReturnProps<V extends CVVariantsSchema, R extends keyof V = never> =\r\n | Prettify<WithClassProp<MarkRequired<CVVariant<V>, R>>>\r\n | ([R] extends [never] ? void : never);\r\nexport interface CVReturn<V extends CVVariantsSchema, R extends keyof V = never> {\r\n (props: CVReturnProps<V, R>): string;\r\n keys: KeysOf<V>;\r\n options: VariantOptions<V>;\r\n defaults: CVVariant<V>;\r\n}\r\n\r\nexport interface CV {\r\n // eslint-disable-next-line @typescript-eslint/prefer-function-type\r\n <V extends CVVariantsSchema, R extends keyof V = never>(\r\n config: CVConfig<V, R>,\r\n cvOptions?: CVOptions,\r\n ): CVReturn<V, R>;\r\n}\r\n\r\nexport const cv: CV = (config, cvOptions = {}) => {\r\n const merge = cvOptions.merge ?? clsx;\r\n\r\n const { variants, defaultVariant, base } = config;\r\n\r\n type ResolvedV = NonNullable<typeof variants>;\r\n\r\n const keys = variants ? getKeys(variants) : [];\r\n const options = variants ? getVariantsOptions(variants) : ({} as VariantOptions<ResolvedV>);\r\n const defaults = defaultVariant ?? ({} as CVVariant<ResolvedV>);\r\n\r\n const compoundVariants = variants\r\n ? typeof config.compoundVariants === \"function\"\r\n ? config.compoundVariants({ variants, keys, options, defaults })\r\n : config.compoundVariants\r\n : undefined;\r\n\r\n const getVariant = <V extends ResolvedV, R extends keyof V = never>(\r\n props: CVReturnProps<V, R>,\r\n ) => {\r\n const variantClassNames = variants\r\n ? keys.map((variant) => {\r\n const variantProp = props?.[variant];\r\n const defaultVariantProp = defaultVariant?.[variant];\r\n\r\n const variantKey = variantProp?.toString() || defaultVariantProp?.toString();\r\n\r\n return variantKey ? variants[variant][variantKey] : undefined;\r\n })\r\n : undefined;\r\n\r\n const compoundVariantClassNames = compoundVariants?.reduce(\r\n (acc, { class: cvClass, className: cvClassName, ...cvConfig }) => {\r\n const shouldApplyCompoundVariant = getEntries(cvConfig).every(\r\n ([cvKey, cvSelector]) => {\r\n const defaultsSelector =\r\n defaultVariant?.[cvKey as keyof typeof defaultVariant];\r\n const propsSelector = props?.[cvKey as keyof typeof props];\r\n\r\n if (typeof cvSelector === \"object\" && \"__token\" in cvSelector) {\r\n if (cvSelector.__token === onlyWhenDefined.__token) {\r\n return propsSelector !== undefined;\r\n } else if (cvSelector.__token === onlyWhenUndefined.__token) {\r\n return propsSelector === undefined;\r\n }\r\n }\r\n\r\n const selector = propsSelector ?? defaultsSelector;\r\n\r\n return Array.isArray(cvSelector)\r\n ? cvSelector.some((cvSelector) => selector === cvSelector)\r\n : selector === cvSelector;\r\n },\r\n );\r\n\r\n if (shouldApplyCompoundVariant) acc.push(cvClass, cvClassName);\r\n return acc;\r\n },\r\n new Array<ClassValue>(),\r\n );\r\n\r\n return merge(\r\n base,\r\n variantClassNames,\r\n compoundVariantClassNames,\r\n props?.class,\r\n props?.className,\r\n );\r\n };\r\n\r\n return Object.assign(getVariant, { keys, options, defaults });\r\n};\r\n\r\ntype OnlyWhenDefined = Opaque<{ __token: string }, \"OnlyWhenDefined\">;\r\ntype OnlyWhenUndefined = Opaque<{ __token: string }, \"OnlyWhenUndefined\">;\r\n\r\ntype CVToken = OnlyWhenDefined | OnlyWhenUndefined;\r\n\r\n/** Apply compound variant only if prop **is** specified */\r\nexport const onlyWhenDefined = { __token: \"OnlyWhenDefined\" } as OnlyWhenDefined;\r\n/** Apply compound variant only if prop **is not** specified */\r\nexport const onlyWhenUndefined = { __token: \"OnlyWhenUndefined\" } as OnlyWhenUndefined;\r\n"],"mappings":"sDAAA,OAAqB,QAAAA,MAAY,OAoE1B,IAAMC,EAAS,CAACC,EAAQC,EAAY,CAAC,IAAM,CAC9C,IAAMC,EAAQD,EAAU,OAASE,EAE3B,CAAE,SAAAC,EAAU,eAAAC,EAAgB,KAAAC,CAAK,EAAIN,EAIrCO,EAAOH,EAAWI,EAAQJ,CAAQ,EAAI,CAAC,EACvCK,EAAUL,EAAWM,EAAmBN,CAAQ,EAAK,CAAC,EACtDO,EAAWN,GAAmB,CAAC,EAE/BO,EAAmBR,EACnB,OAAOJ,EAAO,kBAAqB,WAC/BA,EAAO,iBAAiB,CAAE,SAAAI,EAAU,KAAAG,EAAM,QAAAE,EAAS,SAAAE,CAAS,CAAC,EAC7DX,EAAO,iBACX,OAuDN,OAAO,OAAO,OApDVa,GACC,CACD,IAAMC,EAAoBV,EACpBG,EAAK,IAAKQ,GAAY,CAClB,IAAMC,EAAcH,IAAQE,CAAO,EAC7BE,EAAqBZ,IAAiBU,CAAO,EAE7CG,EAAaF,GAAa,SAAS,GAAKC,GAAoB,SAAS,EAE3E,OAAOC,EAAad,EAASW,CAAO,EAAEG,CAAU,EAAI,MACxD,CAAC,EACD,OAEAC,EAA4BP,GAAkB,OAChD,CAACQ,EAAK,CAAE,MAAOC,EAAS,UAAWC,EAAa,GAAGC,CAAS,KACrBC,EAAWD,CAAQ,EAAE,MACpD,CAAC,CAACE,EAAOC,CAAU,IAAM,CACrB,IAAMC,EACFtB,IAAiBoB,CAAoC,EACnDG,EAAgBf,IAAQY,CAA2B,EAEzD,GAAI,OAAOC,GAAe,UAAY,YAAaA,EAAY,CAC3D,GAAIA,EAAW,UAAYG,EAAgB,QACvC,OAAOD,IAAkB,OACtB,GAAIF,EAAW,UAAYI,EAAkB,QAChD,OAAOF,IAAkB,MAEjC,CAEA,IAAMG,EAAWH,GAAiBD,EAElC,OAAO,MAAM,QAAQD,CAAU,EACzBA,EAAW,KAAMA,GAAeK,IAAaL,CAAU,EACvDK,IAAaL,CACvB,CACJ,GAEgCN,EAAI,KAAKC,EAASC,CAAW,EACtDF,GAEX,IAAI,KACR,EAEA,OAAOlB,EACHI,EACAQ,EACAK,EACAN,GAAO,MACPA,GAAO,SACX,CACJ,EAEiC,CAAE,KAAAN,EAAM,QAAAE,EAAS,SAAAE,CAAS,CAAC,CAChE,EAQakB,EAAkB,CAAE,QAAS,iBAAkB,EAE/CC,EAAoB,CAAE,QAAS,mBAAoB","names":["clsx","cv","config","cvOptions","merge","clsx","variants","defaultVariant","base","keys","getKeys","options","getVariantsOptions","defaults","compoundVariants","props","variantClassNames","variant","variantProp","defaultVariantProp","variantKey","compoundVariantClassNames","acc","cvClass","cvClassName","cvConfig","getEntries","cvKey","cvSelector","defaultsSelector","propsSelector","onlyWhenDefined","onlyWhenUndefined","selector"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/utils.ts"],"sourcesContent":["import { DeepReadonly, DeepWritable, Prettify, UnionToIntersection } from \"ts-essentials\";\nimport { CVVariantsSchema } from \".\";\n\nexport type KeysOf<O extends Record<string, unknown>> = (keyof O)[];\nexport function getKeys<O extends Record<string, unknown>>(obj: O) {\n return Object.keys(obj) as KeysOf<O>;\n}\n\nexport type EntriesOf<O extends Record<string, unknown>> = { [K in keyof O]: [K, O[K]] };\nexport function getEntries<O extends Record<string, unknown>>(obj: O) {\n return Object.entries(obj) as EntriesOf<O>[keyof EntriesOf<O>][];\n}\n\nexport type VariantOptions<V extends CVVariantsSchema> = {\n [K in keyof V]: KeysOf<V[K]>;\n};\nexport function getVariantsOptions<V extends CVVariantsSchema>(variants: V) {\n return Object.fromEntries(\n getEntries(variants).map(([cvKey, cvVal]) => [cvKey, getKeys(cvVal)]),\n ) as VariantOptions<V>;\n}\n\ntype Entries = [PropertyKey, unknown][] | DeepReadonly<[PropertyKey, unknown][]>;\ntype UnionFromPairs<TArr extends Entries> =\n DeepWritable<TArr> extends (infer R)[]\n ? R extends [infer key, infer val]\n ? { [prop in key & PropertyKey]: val }\n : never\n : never;\n\ntype MergeIntersectingObjects<O> = { [K in keyof O]: O[K] };\n\nexport function objectFromEntries<E extends Entries>(entries: E) {\n return Object.fromEntries(entries) as Prettify<\n MergeIntersectingObjects<UnionToIntersection<UnionFromPairs<E>>>\n >;\n}\n"],"mappings":"AAIO,SAASA,EAA2CC,EAAQ,CAC/D,OAAO,OAAO,KAAKA,CAAG,CAC1B,CAGO,SAASC,EAA8CD,EAAQ,CAClE,OAAO,OAAO,QAAQA,CAAG,CAC7B,CAKO,SAASE,EAA+CC,EAAa,CACxE,OAAO,OAAO,YACVF,EAAWE,CAAQ,EAAE,IAAI,CAAC,CAACC,EAAOC,CAAK,IAAM,CAACD,EAAOL,EAAQM,CAAK,CAAC,CAAC,CACxE,CACJ,CAYO,SAASC,EAAqCC,EAAY,CAC7D,OAAO,OAAO,YAAYA,CAAO,CAGrC","names":["getKeys","obj","getEntries","getVariantsOptions","variants","cvKey","cvVal","objectFromEntries","entries"]}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { ClassValue } from 'clsx';
|
|
2
|
+
import { Prettify, UnionToIntersection, DeepReadonly, DeepWritable, StrictExclude, MarkRequired, Opaque } from 'ts-essentials';
|
|
3
|
+
|
|
4
|
+
type KeysOf<O extends Record<string, unknown>> = (keyof O)[];
|
|
5
|
+
declare function getKeys<O extends Record<string, unknown>>(obj: O): KeysOf<O>;
|
|
6
|
+
type EntriesOf<O extends Record<string, unknown>> = {
|
|
7
|
+
[K in keyof O]: [K, O[K]];
|
|
8
|
+
};
|
|
9
|
+
declare function getEntries<O extends Record<string, unknown>>(obj: O): EntriesOf<O>[keyof EntriesOf<O>][];
|
|
10
|
+
type VariantOptions<V extends CVVariantsSchema> = {
|
|
11
|
+
[K in keyof V]: KeysOf<V[K]>;
|
|
12
|
+
};
|
|
13
|
+
declare function getVariantsOptions<V extends CVVariantsSchema>(variants: V): VariantOptions<V>;
|
|
14
|
+
type Entries = [PropertyKey, unknown][] | DeepReadonly<[PropertyKey, unknown][]>;
|
|
15
|
+
type UnionFromPairs<TArr extends Entries> = DeepWritable<TArr> extends (infer R)[] ? R extends [infer key, infer val] ? {
|
|
16
|
+
[prop in key & PropertyKey]: val;
|
|
17
|
+
} : never : never;
|
|
18
|
+
type MergeIntersectingObjects<O> = {
|
|
19
|
+
[K in keyof O]: O[K];
|
|
20
|
+
};
|
|
21
|
+
declare function objectFromEntries<E extends Entries>(entries: E): Prettify<MergeIntersectingObjects<UnionToIntersection<UnionFromPairs<E>>>>;
|
|
22
|
+
|
|
23
|
+
type VariantProps<CVR extends (args?: any) => string> = Prettify<Omit<NonNullable<StrictExclude<Parameters<CVR>[0], void>>, "class" | "className">>;
|
|
24
|
+
type ClassProp = {
|
|
25
|
+
class?: ClassValue;
|
|
26
|
+
className?: never;
|
|
27
|
+
} | {
|
|
28
|
+
class?: never;
|
|
29
|
+
className?: ClassValue;
|
|
30
|
+
};
|
|
31
|
+
type WithClassProp<T extends Record<string, any>> = T & ClassProp;
|
|
32
|
+
type BooleanStringToBoolean<T> = T extends "true" | "false" ? boolean : T;
|
|
33
|
+
type CVVariantsSchema = Record<string, Record<string, ClassValue>>;
|
|
34
|
+
type CVVariant<V extends CVVariantsSchema> = {
|
|
35
|
+
[K in keyof V]?: BooleanStringToBoolean<keyof V[K]>;
|
|
36
|
+
};
|
|
37
|
+
type CVCompoundVariant<V extends CVVariantsSchema> = WithClassProp<{
|
|
38
|
+
[K in keyof V]?: BooleanStringToBoolean<keyof V[K]> | BooleanStringToBoolean<keyof V[K]>[] | CVToken;
|
|
39
|
+
}>;
|
|
40
|
+
type CVCompoundVariantsSchema<V extends CVVariantsSchema> = CVCompoundVariant<V>[];
|
|
41
|
+
interface CVConfig<V extends CVVariantsSchema, R extends keyof V> {
|
|
42
|
+
base?: ClassValue;
|
|
43
|
+
variants?: V;
|
|
44
|
+
compoundVariants?: CVCompoundVariantsSchema<V> | ((arg: {
|
|
45
|
+
variants: V;
|
|
46
|
+
keys: KeysOf<V>;
|
|
47
|
+
options: VariantOptions<V>;
|
|
48
|
+
defaults: CVVariant<V>;
|
|
49
|
+
}) => CVCompoundVariantsSchema<V>);
|
|
50
|
+
defaultVariant?: CVVariant<V>;
|
|
51
|
+
requiredProps?: R[];
|
|
52
|
+
}
|
|
53
|
+
interface CVOptions {
|
|
54
|
+
merge?: (...args: ClassValue[]) => string;
|
|
55
|
+
}
|
|
56
|
+
type CVReturnProps<V extends CVVariantsSchema, R extends keyof V = never> = Prettify<WithClassProp<MarkRequired<CVVariant<V>, R>>> | ([R] extends [never] ? void : never);
|
|
57
|
+
interface CVReturn<V extends CVVariantsSchema, R extends keyof V = never> {
|
|
58
|
+
(props: CVReturnProps<V, R>): string;
|
|
59
|
+
keys: KeysOf<V>;
|
|
60
|
+
options: VariantOptions<V>;
|
|
61
|
+
defaults: CVVariant<V>;
|
|
62
|
+
}
|
|
63
|
+
interface CV {
|
|
64
|
+
<V extends CVVariantsSchema, R extends keyof V = never>(config: CVConfig<V, R>, cvOptions?: CVOptions): CVReturn<V, R>;
|
|
65
|
+
}
|
|
66
|
+
declare const cv: CV;
|
|
67
|
+
type OnlyWhenDefined = Opaque<{
|
|
68
|
+
__token: string;
|
|
69
|
+
}, "OnlyWhenDefined">;
|
|
70
|
+
type OnlyWhenUndefined = Opaque<{
|
|
71
|
+
__token: string;
|
|
72
|
+
}, "OnlyWhenUndefined">;
|
|
73
|
+
type CVToken = OnlyWhenDefined | OnlyWhenUndefined;
|
|
74
|
+
/** Apply compound variant only if prop **is** specified */
|
|
75
|
+
declare const onlyWhenDefined: OnlyWhenDefined;
|
|
76
|
+
/** Apply compound variant only if prop **is not** specified */
|
|
77
|
+
declare const onlyWhenUndefined: OnlyWhenUndefined;
|
|
78
|
+
|
|
79
|
+
export { type CVVariantsSchema as C, type EntriesOf as E, type KeysOf as K, type VariantOptions as V, type WithClassProp as W, type CVReturn as a, type CVConfig as b, type CVOptions as c, getEntries as d, getVariantsOptions as e, type VariantProps as f, getKeys as g, type CVVariant as h, type CVCompoundVariant as i, type CVCompoundVariantsSchema as j, type CV as k, cv as l, onlyWhenDefined as m, onlyWhenUndefined as n, objectFromEntries as o };
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { ClassValue } from 'clsx';
|
|
2
|
+
import { Prettify, UnionToIntersection, DeepReadonly, DeepWritable, StrictExclude, MarkRequired, Opaque } from 'ts-essentials';
|
|
3
|
+
|
|
4
|
+
type KeysOf<O extends Record<string, unknown>> = (keyof O)[];
|
|
5
|
+
declare function getKeys<O extends Record<string, unknown>>(obj: O): KeysOf<O>;
|
|
6
|
+
type EntriesOf<O extends Record<string, unknown>> = {
|
|
7
|
+
[K in keyof O]: [K, O[K]];
|
|
8
|
+
};
|
|
9
|
+
declare function getEntries<O extends Record<string, unknown>>(obj: O): EntriesOf<O>[keyof EntriesOf<O>][];
|
|
10
|
+
type VariantOptions<V extends CVVariantsSchema> = {
|
|
11
|
+
[K in keyof V]: KeysOf<V[K]>;
|
|
12
|
+
};
|
|
13
|
+
declare function getVariantsOptions<V extends CVVariantsSchema>(variants: V): VariantOptions<V>;
|
|
14
|
+
type Entries = [PropertyKey, unknown][] | DeepReadonly<[PropertyKey, unknown][]>;
|
|
15
|
+
type UnionFromPairs<TArr extends Entries> = DeepWritable<TArr> extends (infer R)[] ? R extends [infer key, infer val] ? {
|
|
16
|
+
[prop in key & PropertyKey]: val;
|
|
17
|
+
} : never : never;
|
|
18
|
+
type MergeIntersectingObjects<O> = {
|
|
19
|
+
[K in keyof O]: O[K];
|
|
20
|
+
};
|
|
21
|
+
declare function objectFromEntries<E extends Entries>(entries: E): Prettify<MergeIntersectingObjects<UnionToIntersection<UnionFromPairs<E>>>>;
|
|
22
|
+
|
|
23
|
+
type VariantProps<CVR extends (args?: any) => string> = Prettify<Omit<NonNullable<StrictExclude<Parameters<CVR>[0], void>>, "class" | "className">>;
|
|
24
|
+
type ClassProp = {
|
|
25
|
+
class?: ClassValue;
|
|
26
|
+
className?: never;
|
|
27
|
+
} | {
|
|
28
|
+
class?: never;
|
|
29
|
+
className?: ClassValue;
|
|
30
|
+
};
|
|
31
|
+
type WithClassProp<T extends Record<string, any>> = T & ClassProp;
|
|
32
|
+
type BooleanStringToBoolean<T> = T extends "true" | "false" ? boolean : T;
|
|
33
|
+
type CVVariantsSchema = Record<string, Record<string, ClassValue>>;
|
|
34
|
+
type CVVariant<V extends CVVariantsSchema> = {
|
|
35
|
+
[K in keyof V]?: BooleanStringToBoolean<keyof V[K]>;
|
|
36
|
+
};
|
|
37
|
+
type CVCompoundVariant<V extends CVVariantsSchema> = WithClassProp<{
|
|
38
|
+
[K in keyof V]?: BooleanStringToBoolean<keyof V[K]> | BooleanStringToBoolean<keyof V[K]>[] | CVToken;
|
|
39
|
+
}>;
|
|
40
|
+
type CVCompoundVariantsSchema<V extends CVVariantsSchema> = CVCompoundVariant<V>[];
|
|
41
|
+
interface CVConfig<V extends CVVariantsSchema, R extends keyof V> {
|
|
42
|
+
base?: ClassValue;
|
|
43
|
+
variants?: V;
|
|
44
|
+
compoundVariants?: CVCompoundVariantsSchema<V> | ((arg: {
|
|
45
|
+
variants: V;
|
|
46
|
+
keys: KeysOf<V>;
|
|
47
|
+
options: VariantOptions<V>;
|
|
48
|
+
defaults: CVVariant<V>;
|
|
49
|
+
}) => CVCompoundVariantsSchema<V>);
|
|
50
|
+
defaultVariant?: CVVariant<V>;
|
|
51
|
+
requiredProps?: R[];
|
|
52
|
+
}
|
|
53
|
+
interface CVOptions {
|
|
54
|
+
merge?: (...args: ClassValue[]) => string;
|
|
55
|
+
}
|
|
56
|
+
type CVReturnProps<V extends CVVariantsSchema, R extends keyof V = never> = Prettify<WithClassProp<MarkRequired<CVVariant<V>, R>>> | ([R] extends [never] ? void : never);
|
|
57
|
+
interface CVReturn<V extends CVVariantsSchema, R extends keyof V = never> {
|
|
58
|
+
(props: CVReturnProps<V, R>): string;
|
|
59
|
+
keys: KeysOf<V>;
|
|
60
|
+
options: VariantOptions<V>;
|
|
61
|
+
defaults: CVVariant<V>;
|
|
62
|
+
}
|
|
63
|
+
interface CV {
|
|
64
|
+
<V extends CVVariantsSchema, R extends keyof V = never>(config: CVConfig<V, R>, cvOptions?: CVOptions): CVReturn<V, R>;
|
|
65
|
+
}
|
|
66
|
+
declare const cv: CV;
|
|
67
|
+
type OnlyWhenDefined = Opaque<{
|
|
68
|
+
__token: string;
|
|
69
|
+
}, "OnlyWhenDefined">;
|
|
70
|
+
type OnlyWhenUndefined = Opaque<{
|
|
71
|
+
__token: string;
|
|
72
|
+
}, "OnlyWhenUndefined">;
|
|
73
|
+
type CVToken = OnlyWhenDefined | OnlyWhenUndefined;
|
|
74
|
+
/** Apply compound variant only if prop **is** specified */
|
|
75
|
+
declare const onlyWhenDefined: OnlyWhenDefined;
|
|
76
|
+
/** Apply compound variant only if prop **is not** specified */
|
|
77
|
+
declare const onlyWhenUndefined: OnlyWhenUndefined;
|
|
78
|
+
|
|
79
|
+
export { type CVVariantsSchema as C, type EntriesOf as E, type KeysOf as K, type VariantOptions as V, type WithClassProp as W, type CVReturn as a, type CVConfig as b, type CVOptions as c, getEntries as d, getVariantsOptions as e, type VariantProps as f, getKeys as g, type CVVariant as h, type CVCompoundVariant as i, type CVCompoundVariantsSchema as j, type CV as k, cv as l, onlyWhenDefined as m, onlyWhenUndefined as n, objectFromEntries as o };
|
package/build/index.cjs
CHANGED
|
@@ -1,68 +1,2 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/index.ts
|
|
21
|
-
var src_exports = {};
|
|
22
|
-
__export(src_exports, {
|
|
23
|
-
cv: () => cv
|
|
24
|
-
});
|
|
25
|
-
module.exports = __toCommonJS(src_exports);
|
|
26
|
-
var import_clsx = require("clsx");
|
|
27
|
-
|
|
28
|
-
// src/utils.ts
|
|
29
|
-
function getKeys(obj) {
|
|
30
|
-
return Object.keys(obj);
|
|
31
|
-
}
|
|
32
|
-
function getEntries(obj) {
|
|
33
|
-
return Object.entries(obj);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
// src/index.ts
|
|
37
|
-
var cv = (config, options = {}) => {
|
|
38
|
-
const { variants, defaultVariant, baseClass } = config;
|
|
39
|
-
const merge = options.merge ?? import_clsx.clsx;
|
|
40
|
-
if (variants == null) return (props) => merge(baseClass, props?.class);
|
|
41
|
-
const compoundVariants = typeof config.compoundVariants === "function" ? config.compoundVariants(variants) : config.compoundVariants;
|
|
42
|
-
return (props) => {
|
|
43
|
-
const variantClassNames = getKeys(variants).map((variant) => {
|
|
44
|
-
const variantProp = props?.[variant];
|
|
45
|
-
const defaultVariantProp = defaultVariant?.[variant];
|
|
46
|
-
const variantKey = variantProp || defaultVariantProp;
|
|
47
|
-
return variants[variant][variantKey];
|
|
48
|
-
});
|
|
49
|
-
const compoundVariantClassNames = compoundVariants?.reduce((acc, { class: cvClass, ...cvConfig }) => {
|
|
50
|
-
const shouldApplyCompoundVariant = getEntries(cvConfig).every(([cvKey, cvSelector]) => {
|
|
51
|
-
const defaultsSelector = defaultVariant?.[cvKey];
|
|
52
|
-
const propsSelector = props?.[cvKey];
|
|
53
|
-
if (cvSelector === true) return propsSelector !== void 0;
|
|
54
|
-
else if (cvSelector === false) return propsSelector === void 0;
|
|
55
|
-
const selector = propsSelector ?? defaultsSelector;
|
|
56
|
-
return Array.isArray(cvSelector) ? cvSelector.includes(selector) : selector === cvSelector;
|
|
57
|
-
});
|
|
58
|
-
if (shouldApplyCompoundVariant) acc.push(cvClass);
|
|
59
|
-
return acc;
|
|
60
|
-
}, new Array());
|
|
61
|
-
return merge(config?.baseClass, variantClassNames, compoundVariantClassNames, props?.class);
|
|
62
|
-
};
|
|
63
|
-
};
|
|
64
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
65
|
-
0 && (module.exports = {
|
|
66
|
-
cv
|
|
67
|
-
});
|
|
1
|
+
"use strict";var l=Object.defineProperty;var b=Object.getOwnPropertyDescriptor;var E=Object.getOwnPropertyNames;var T=Object.prototype.hasOwnProperty;var N=(e,n)=>{for(var s in n)l(e,s,{get:n[s],enumerable:!0})},D=(e,n,s,t)=>{if(n&&typeof n=="object"||typeof n=="function")for(let r of E(n))!T.call(e,r)&&r!==s&&l(e,r,{get:()=>n[r],enumerable:!(t=b(n,r))||t.enumerable});return e};var U=e=>D(l({},"__esModule",{value:!0}),e);var B={};N(B,{cv:()=>j,onlyWhenDefined:()=>R,onlyWhenUndefined:()=>h});module.exports=U(B);var g=require("clsx");function u(e){return Object.keys(e)}function c(e){return Object.entries(e)}function k(e){return Object.fromEntries(c(e).map(([n,s])=>[n,u(s)]))}var j=(e,n={})=>{let s=n.merge??g.clsx,{variants:t,defaultVariant:r,base:K}=e,p=t?u(t):[],C=t?k(t):{},m=r??{},P=t?typeof e.compoundVariants=="function"?e.compoundVariants({variants:t,keys:p,options:C,defaults:m}):e.compoundVariants:void 0;return Object.assign(i=>{let v=t?p.map(o=>{let d=i?.[o],y=r?.[o],V=d?.toString()||y?.toString();return V?t[o][V]:void 0}):void 0,S=P?.reduce((o,{class:d,className:y,...V})=>(c(V).every(([O,a])=>{let _=r?.[O],f=i?.[O];if(typeof a=="object"&&"__token"in a){if(a.__token===R.__token)return f!==void 0;if(a.__token===h.__token)return f===void 0}let x=f??_;return Array.isArray(a)?a.some(W=>x===W):x===a})&&o.push(d,y),o),new Array);return s(K,v,S,i?.class,i?.className)},{keys:p,options:C,defaults:m})},R={__token:"OnlyWhenDefined"},h={__token:"OnlyWhenUndefined"};0&&(module.exports={cv,onlyWhenDefined,onlyWhenUndefined});
|
|
68
2
|
//# sourceMappingURL=index.cjs.map
|
package/build/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/utils.ts"],"sourcesContent":["import { ClassValue, clsx } from \"clsx\";\r\nimport { MarkRequired, Prettify } from \"ts-essentials\";\r\nimport { getEntries, getKeys } from \"./utils\";\r\n\r\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\r\nexport type VariantProps<CVR extends (args?: any) => string> = Prettify<Omit<NonNullable<Exclude<Parameters<CVR>[0], void>>, \"class\">>;\r\n\r\nexport type CVVariantsSchema = Record<string, Record<string, ClassValue>>;\r\n\r\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\r\nexport type WithClassProp<T extends Record<string, any>> = T & {\r\n class?: ClassValue;\r\n};\r\n\r\nexport type CVCompoundVariantsSchema<V extends CVVariantsSchema> = WithClassProp<{\r\n [K in keyof V]?: (keyof V[K] & string) | (keyof V[K])[] | boolean\r\n}>[];\r\n\r\nexport interface CVConfig<V extends CVVariantsSchema, R extends keyof V> {\r\n baseClass?: ClassValue;\r\n variants?: V;\r\n compoundVariants?: CVCompoundVariantsSchema<NoInfer<V>> | ((variants: NoInfer<V>) => CVCompoundVariantsSchema<NoInfer<V>>);\r\n defaultVariant?: Partial<{ [K in keyof V]?: keyof V[K] }>;\r\n requiredProps?: R[];\r\n}\r\n\r\nexport type CVVariantProps<V extends CVVariantsSchema, R extends (keyof V)> =\r\nWithClassProp<MarkRequired<{ [K in keyof V]?: keyof V[K] }, R>>;\r\n\r\nexport type CVReturn<V extends CVVariantsSchema, R extends keyof V = never> = (props: Prettify<CVVariantProps<V, R>> | ([R] extends [never] ? void : never)) => string;\r\n\r\nexport interface CVOptions {\r\n merge?: (...args: ClassValue[]) => string;\r\n}\r\n\r\nexport interface CV {\r\n // eslint-disable-next-line @typescript-eslint/prefer-function-type\r\n <V extends CVVariantsSchema, R extends keyof V = never>(config: CVConfig<V, R>, options?: CVOptions): CVReturn<V, R>;\r\n}\r\n\r\nexport const cv: CV = (config, options = {}) => {\r\n const { variants, defaultVariant, baseClass } = config;\r\n\r\n const merge = options.merge ?? clsx;\r\n\r\n if (variants == null) return props => merge(baseClass, props?.class);\r\n\r\n const compoundVariants = typeof config.compoundVariants === \"function\" ? config.compoundVariants(variants) : config.compoundVariants;\r\n\r\n return (props) => {\r\n const variantClassNames = getKeys(variants).map((variant) => {\r\n const variantProp = props?.[variant];\r\n const defaultVariantProp = defaultVariant?.[variant];\r\n\r\n const variantKey = (variantProp || defaultVariantProp) as keyof (typeof variants)[typeof variant];\r\n\r\n return variants[variant][variantKey];\r\n });\r\n\r\n const compoundVariantClassNames = compoundVariants?.reduce((acc, { class: cvClass, ...cvConfig }) => {\r\n const shouldApplyCompoundVariant = getEntries(cvConfig).every(([cvKey, cvSelector]) => {\r\n const defaultsSelector = defaultVariant?.[cvKey as keyof typeof defaultVariant];\r\n const propsSelector = props?.[cvKey as keyof typeof props];\r\n\r\n if (cvSelector === true) return propsSelector !== undefined; // only when prop is defined\r\n else if (cvSelector === false) return propsSelector === undefined; // only when prop is not defined\r\n\r\n const selector = propsSelector ?? defaultsSelector;\r\n\r\n return Array.isArray(cvSelector) ? cvSelector.includes(selector as string) : selector === cvSelector;\r\n });\r\n\r\n if (shouldApplyCompoundVariant) acc.push(cvClass);\r\n return acc;\r\n }, new Array<ClassValue>());\r\n\r\n return merge(config?.baseClass, variantClassNames, compoundVariantClassNames, props?.class);\r\n };\r\n};\r\n","import { DeepReadonly, DeepWritable, Prettify, UnionToIntersection } from \"ts-essentials\";\r\nimport { CVVariantsSchema } from \".\";\r\n\r\nexport function getKeys<O extends Record<string, unknown>>(obj: O) {\r\n return Object.keys(obj) as (keyof O)[];\r\n}\r\n\r\ntype EntriesOf<O extends Record<string, unknown>> = { [K in keyof O]: [K, O[K]] };\r\nexport function getEntries<O extends Record<string, unknown>>(obj: O) {\r\n return Object.entries(obj) as (EntriesOf<O>[keyof EntriesOf<O>])[];\r\n}\r\n\r\nexport function getVariantsOptions<V extends CVVariantsSchema>(variants: V) {\r\n return Object.fromEntries(getEntries(variants).map(([cvKey, cvVal]) => [cvKey, getKeys(cvVal)])) as { [K in keyof V]: (keyof V[K])[] };\r\n}\r\n\r\ntype Entries = [PropertyKey, unknown][] | DeepReadonly<[PropertyKey, unknown][]>;\r\ntype UnionFromPairs<TArr extends Entries> =\r\n DeepWritable<TArr> extends (infer R)[] ? R extends [infer key, infer val] ? { [prop in key & PropertyKey]: val } : never : never;\r\n\r\ntype MergeIntersectingObjects<O> = { [K in keyof O]: O[K] };\r\n\r\nexport function objectFromEntries<E extends Entries>(entries: E) {\r\n return Object.fromEntries(entries) as Prettify<MergeIntersectingObjects<UnionToIntersection<UnionFromPairs<E>>>>;\r\n}\r\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,kBAAiC;;;ACG1B,SAAS,QAA2C,KAAQ;AAC/D,SAAO,OAAO,KAAK,GAAG;AAC1B;AAGO,SAAS,WAA8C,KAAQ;AAClE,SAAO,OAAO,QAAQ,GAAG;AAC7B;;;AD8BO,IAAM,KAAS,CAAC,QAAQ,UAAU,CAAC,MAAM;AAC5C,QAAM,EAAE,UAAU,gBAAgB,UAAU,IAAI;AAEhD,QAAM,QAAQ,QAAQ,SAAS;AAE/B,MAAI,YAAY,KAAM,QAAO,WAAS,MAAM,WAAW,OAAO,KAAK;AAEnE,QAAM,mBAAmB,OAAO,OAAO,qBAAqB,aAAa,OAAO,iBAAiB,QAAQ,IAAI,OAAO;AAEpH,SAAO,CAAC,UAAU;AACd,UAAM,oBAAoB,QAAQ,QAAQ,EAAE,IAAI,CAAC,YAAY;AACzD,YAAM,cAAc,QAAQ,OAAO;AACnC,YAAM,qBAAqB,iBAAiB,OAAO;AAEnD,YAAM,aAAc,eAAe;AAEnC,aAAO,SAAS,OAAO,EAAE,UAAU;AAAA,IACvC,CAAC;AAED,UAAM,4BAA4B,kBAAkB,OAAO,CAAC,KAAK,EAAE,OAAO,SAAS,GAAG,SAAS,MAAM;AACjG,YAAM,6BAA6B,WAAW,QAAQ,EAAE,MAAM,CAAC,CAAC,OAAO,UAAU,MAAM;AACnF,cAAM,mBAAmB,iBAAiB,KAAoC;AAC9E,cAAM,gBAAgB,QAAQ,KAA2B;AAEzD,YAAI,eAAe,KAAM,QAAO,kBAAkB;AAAA,iBACzC,eAAe,MAAO,QAAO,kBAAkB;AAExD,cAAM,WAAW,iBAAiB;AAElC,eAAO,MAAM,QAAQ,UAAU,IAAI,WAAW,SAAS,QAAkB,IAAI,aAAa;AAAA,MAC9F,CAAC;AAED,UAAI,2BAA4B,KAAI,KAAK,OAAO;AAChD,aAAO;AAAA,IACX,GAAG,IAAI,MAAkB,CAAC;AAE1B,WAAO,MAAM,QAAQ,WAAW,mBAAmB,2BAA2B,OAAO,KAAK;AAAA,EAC9F;AACJ;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/utils.ts"],"sourcesContent":["import { ClassValue, clsx } from \"clsx\";\r\nimport { MarkRequired, Prettify, StrictExclude, Opaque } from \"ts-essentials\";\r\nimport { getEntries, getKeys, getVariantsOptions, KeysOf, VariantOptions } from \"./utils\";\r\n\r\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\r\nexport type VariantProps<CVR extends (args?: any) => string> = Prettify<\r\n Omit<NonNullable<StrictExclude<Parameters<CVR>[0], void>>, \"class\" | \"className\">\r\n>;\r\n\r\ntype ClassProp =\r\n | { class?: ClassValue; className?: never }\r\n | { class?: never; className?: ClassValue };\r\n\r\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\r\nexport type WithClassProp<T extends Record<string, any>> = T & ClassProp;\r\n\r\ntype BooleanStringToBoolean<T> = T extends \"true\" | \"false\" ? boolean : T;\r\n\r\nexport type CVVariantsSchema = Record<string, Record<string, ClassValue>>;\r\n\r\nexport type CVVariant<V extends CVVariantsSchema> = {\r\n [K in keyof V]?: BooleanStringToBoolean<keyof V[K]>;\r\n};\r\nexport type CVCompoundVariant<V extends CVVariantsSchema> = WithClassProp<{\r\n [K in keyof V]?:\r\n | BooleanStringToBoolean<keyof V[K]>\r\n | BooleanStringToBoolean<keyof V[K]>[]\r\n | CVToken;\r\n}>;\r\n\r\nexport type CVCompoundVariantsSchema<V extends CVVariantsSchema> = CVCompoundVariant<V>[];\r\n\r\nexport interface CVConfig<V extends CVVariantsSchema, R extends keyof V> {\r\n base?: ClassValue;\r\n variants?: V;\r\n compoundVariants?:\r\n | CVCompoundVariantsSchema<V>\r\n | ((arg: {\r\n variants: V;\r\n keys: KeysOf<V>;\r\n options: VariantOptions<V>;\r\n defaults: CVVariant<V>;\r\n }) => CVCompoundVariantsSchema<V>);\r\n defaultVariant?: CVVariant<V>;\r\n requiredProps?: R[];\r\n}\r\nexport interface CVOptions {\r\n merge?: (...args: ClassValue[]) => string;\r\n}\r\n\r\ntype CVReturnProps<V extends CVVariantsSchema, R extends keyof V = never> =\r\n | Prettify<WithClassProp<MarkRequired<CVVariant<V>, R>>>\r\n | ([R] extends [never] ? void : never);\r\nexport interface CVReturn<V extends CVVariantsSchema, R extends keyof V = never> {\r\n (props: CVReturnProps<V, R>): string;\r\n keys: KeysOf<V>;\r\n options: VariantOptions<V>;\r\n defaults: CVVariant<V>;\r\n}\r\n\r\nexport interface CV {\r\n // eslint-disable-next-line @typescript-eslint/prefer-function-type\r\n <V extends CVVariantsSchema, R extends keyof V = never>(\r\n config: CVConfig<V, R>,\r\n cvOptions?: CVOptions,\r\n ): CVReturn<V, R>;\r\n}\r\n\r\nexport const cv: CV = (config, cvOptions = {}) => {\r\n const merge = cvOptions.merge ?? clsx;\r\n\r\n const { variants, defaultVariant, base } = config;\r\n\r\n type ResolvedV = NonNullable<typeof variants>;\r\n\r\n const keys = variants ? getKeys(variants) : [];\r\n const options = variants ? getVariantsOptions(variants) : ({} as VariantOptions<ResolvedV>);\r\n const defaults = defaultVariant ?? ({} as CVVariant<ResolvedV>);\r\n\r\n const compoundVariants = variants\r\n ? typeof config.compoundVariants === \"function\"\r\n ? config.compoundVariants({ variants, keys, options, defaults })\r\n : config.compoundVariants\r\n : undefined;\r\n\r\n const getVariant = <V extends ResolvedV, R extends keyof V = never>(\r\n props: CVReturnProps<V, R>,\r\n ) => {\r\n const variantClassNames = variants\r\n ? keys.map((variant) => {\r\n const variantProp = props?.[variant];\r\n const defaultVariantProp = defaultVariant?.[variant];\r\n\r\n const variantKey = variantProp?.toString() || defaultVariantProp?.toString();\r\n\r\n return variantKey ? variants[variant][variantKey] : undefined;\r\n })\r\n : undefined;\r\n\r\n const compoundVariantClassNames = compoundVariants?.reduce(\r\n (acc, { class: cvClass, className: cvClassName, ...cvConfig }) => {\r\n const shouldApplyCompoundVariant = getEntries(cvConfig).every(\r\n ([cvKey, cvSelector]) => {\r\n const defaultsSelector =\r\n defaultVariant?.[cvKey as keyof typeof defaultVariant];\r\n const propsSelector = props?.[cvKey as keyof typeof props];\r\n\r\n if (typeof cvSelector === \"object\" && \"__token\" in cvSelector) {\r\n if (cvSelector.__token === onlyWhenDefined.__token) {\r\n return propsSelector !== undefined;\r\n } else if (cvSelector.__token === onlyWhenUndefined.__token) {\r\n return propsSelector === undefined;\r\n }\r\n }\r\n\r\n const selector = propsSelector ?? defaultsSelector;\r\n\r\n return Array.isArray(cvSelector)\r\n ? cvSelector.some((cvSelector) => selector === cvSelector)\r\n : selector === cvSelector;\r\n },\r\n );\r\n\r\n if (shouldApplyCompoundVariant) acc.push(cvClass, cvClassName);\r\n return acc;\r\n },\r\n new Array<ClassValue>(),\r\n );\r\n\r\n return merge(\r\n base,\r\n variantClassNames,\r\n compoundVariantClassNames,\r\n props?.class,\r\n props?.className,\r\n );\r\n };\r\n\r\n return Object.assign(getVariant, { keys, options, defaults });\r\n};\r\n\r\ntype OnlyWhenDefined = Opaque<{ __token: string }, \"OnlyWhenDefined\">;\r\ntype OnlyWhenUndefined = Opaque<{ __token: string }, \"OnlyWhenUndefined\">;\r\n\r\ntype CVToken = OnlyWhenDefined | OnlyWhenUndefined;\r\n\r\n/** Apply compound variant only if prop **is** specified */\r\nexport const onlyWhenDefined = { __token: \"OnlyWhenDefined\" } as OnlyWhenDefined;\r\n/** Apply compound variant only if prop **is not** specified */\r\nexport const onlyWhenUndefined = { __token: \"OnlyWhenUndefined\" } as OnlyWhenUndefined;\r\n","import { DeepReadonly, DeepWritable, Prettify, UnionToIntersection } from \"ts-essentials\";\nimport { CVVariantsSchema } from \".\";\n\nexport type KeysOf<O extends Record<string, unknown>> = (keyof O)[];\nexport function getKeys<O extends Record<string, unknown>>(obj: O) {\n return Object.keys(obj) as KeysOf<O>;\n}\n\nexport type EntriesOf<O extends Record<string, unknown>> = { [K in keyof O]: [K, O[K]] };\nexport function getEntries<O extends Record<string, unknown>>(obj: O) {\n return Object.entries(obj) as EntriesOf<O>[keyof EntriesOf<O>][];\n}\n\nexport type VariantOptions<V extends CVVariantsSchema> = {\n [K in keyof V]: KeysOf<V[K]>;\n};\nexport function getVariantsOptions<V extends CVVariantsSchema>(variants: V) {\n return Object.fromEntries(\n getEntries(variants).map(([cvKey, cvVal]) => [cvKey, getKeys(cvVal)]),\n ) as VariantOptions<V>;\n}\n\ntype Entries = [PropertyKey, unknown][] | DeepReadonly<[PropertyKey, unknown][]>;\ntype UnionFromPairs<TArr extends Entries> =\n DeepWritable<TArr> extends (infer R)[]\n ? R extends [infer key, infer val]\n ? { [prop in key & PropertyKey]: val }\n : never\n : never;\n\ntype MergeIntersectingObjects<O> = { [K in keyof O]: O[K] };\n\nexport function objectFromEntries<E extends Entries>(entries: E) {\n return Object.fromEntries(entries) as Prettify<\n MergeIntersectingObjects<UnionToIntersection<UnionFromPairs<E>>>\n >;\n}\n"],"mappings":"yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,QAAAE,EAAA,oBAAAC,EAAA,sBAAAC,IAAA,eAAAC,EAAAL,GAAA,IAAAM,EAAiC,gBCI1B,SAASC,EAA2CC,EAAQ,CAC/D,OAAO,OAAO,KAAKA,CAAG,CAC1B,CAGO,SAASC,EAA8CD,EAAQ,CAClE,OAAO,OAAO,QAAQA,CAAG,CAC7B,CAKO,SAASE,EAA+CC,EAAa,CACxE,OAAO,OAAO,YACVF,EAAWE,CAAQ,EAAE,IAAI,CAAC,CAACC,EAAOC,CAAK,IAAM,CAACD,EAAOL,EAAQM,CAAK,CAAC,CAAC,CACxE,CACJ,CDgDO,IAAMC,EAAS,CAACC,EAAQC,EAAY,CAAC,IAAM,CAC9C,IAAMC,EAAQD,EAAU,OAAS,OAE3B,CAAE,SAAAE,EAAU,eAAAC,EAAgB,KAAAC,CAAK,EAAIL,EAIrCM,EAAOH,EAAWI,EAAQJ,CAAQ,EAAI,CAAC,EACvCK,EAAUL,EAAWM,EAAmBN,CAAQ,EAAK,CAAC,EACtDO,EAAWN,GAAmB,CAAC,EAE/BO,EAAmBR,EACnB,OAAOH,EAAO,kBAAqB,WAC/BA,EAAO,iBAAiB,CAAE,SAAAG,EAAU,KAAAG,EAAM,QAAAE,EAAS,SAAAE,CAAS,CAAC,EAC7DV,EAAO,iBACX,OAuDN,OAAO,OAAO,OApDVY,GACC,CACD,IAAMC,EAAoBV,EACpBG,EAAK,IAAKQ,GAAY,CAClB,IAAMC,EAAcH,IAAQE,CAAO,EAC7BE,EAAqBZ,IAAiBU,CAAO,EAE7CG,EAAaF,GAAa,SAAS,GAAKC,GAAoB,SAAS,EAE3E,OAAOC,EAAad,EAASW,CAAO,EAAEG,CAAU,EAAI,MACxD,CAAC,EACD,OAEAC,EAA4BP,GAAkB,OAChD,CAACQ,EAAK,CAAE,MAAOC,EAAS,UAAWC,EAAa,GAAGC,CAAS,KACrBC,EAAWD,CAAQ,EAAE,MACpD,CAAC,CAACE,EAAOC,CAAU,IAAM,CACrB,IAAMC,EACFtB,IAAiBoB,CAAoC,EACnDG,EAAgBf,IAAQY,CAA2B,EAEzD,GAAI,OAAOC,GAAe,UAAY,YAAaA,EAAY,CAC3D,GAAIA,EAAW,UAAYG,EAAgB,QACvC,OAAOD,IAAkB,OACtB,GAAIF,EAAW,UAAYI,EAAkB,QAChD,OAAOF,IAAkB,MAEjC,CAEA,IAAMG,EAAWH,GAAiBD,EAElC,OAAO,MAAM,QAAQD,CAAU,EACzBA,EAAW,KAAMA,GAAeK,IAAaL,CAAU,EACvDK,IAAaL,CACvB,CACJ,GAEgCN,EAAI,KAAKC,EAASC,CAAW,EACtDF,GAEX,IAAI,KACR,EAEA,OAAOjB,EACHG,EACAQ,EACAK,EACAN,GAAO,MACPA,GAAO,SACX,CACJ,EAEiC,CAAE,KAAAN,EAAM,QAAAE,EAAS,SAAAE,CAAS,CAAC,CAChE,EAQakB,EAAkB,CAAE,QAAS,iBAAkB,EAE/CC,EAAoB,CAAE,QAAS,mBAAoB","names":["src_exports","__export","cv","onlyWhenDefined","onlyWhenUndefined","__toCommonJS","import_clsx","getKeys","obj","getEntries","getVariantsOptions","variants","cvKey","cvVal","cv","config","cvOptions","merge","variants","defaultVariant","base","keys","getKeys","options","getVariantsOptions","defaults","compoundVariants","props","variantClassNames","variant","variantProp","defaultVariantProp","variantKey","compoundVariantClassNames","acc","cvClass","cvClassName","cvConfig","getEntries","cvKey","cvSelector","defaultsSelector","propsSelector","onlyWhenDefined","onlyWhenUndefined","selector"]}
|
package/build/index.d.cts
CHANGED
|
@@ -1,33 +1,3 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
type VariantProps<CVR extends (args?: any) => string> = Prettify<Omit<NonNullable<Exclude<Parameters<CVR>[0], void>>, "class">>;
|
|
5
|
-
type CVVariantsSchema = Record<string, Record<string, ClassValue>>;
|
|
6
|
-
type WithClassProp<T extends Record<string, any>> = T & {
|
|
7
|
-
class?: ClassValue;
|
|
8
|
-
};
|
|
9
|
-
type CVCompoundVariantsSchema<V extends CVVariantsSchema> = WithClassProp<{
|
|
10
|
-
[K in keyof V]?: (keyof V[K] & string) | (keyof V[K])[] | boolean;
|
|
11
|
-
}>[];
|
|
12
|
-
interface CVConfig<V extends CVVariantsSchema, R extends keyof V> {
|
|
13
|
-
baseClass?: ClassValue;
|
|
14
|
-
variants?: V;
|
|
15
|
-
compoundVariants?: CVCompoundVariantsSchema<NoInfer<V>> | ((variants: NoInfer<V>) => CVCompoundVariantsSchema<NoInfer<V>>);
|
|
16
|
-
defaultVariant?: Partial<{
|
|
17
|
-
[K in keyof V]?: keyof V[K];
|
|
18
|
-
}>;
|
|
19
|
-
requiredProps?: R[];
|
|
20
|
-
}
|
|
21
|
-
type CVVariantProps<V extends CVVariantsSchema, R extends (keyof V)> = WithClassProp<MarkRequired<{
|
|
22
|
-
[K in keyof V]?: keyof V[K];
|
|
23
|
-
}, R>>;
|
|
24
|
-
type CVReturn<V extends CVVariantsSchema, R extends keyof V = never> = (props: Prettify<CVVariantProps<V, R>> | ([R] extends [never] ? void : never)) => string;
|
|
25
|
-
interface CVOptions {
|
|
26
|
-
merge?: (...args: ClassValue[]) => string;
|
|
27
|
-
}
|
|
28
|
-
interface CV {
|
|
29
|
-
<V extends CVVariantsSchema, R extends keyof V = never>(config: CVConfig<V, R>, options?: CVOptions): CVReturn<V, R>;
|
|
30
|
-
}
|
|
31
|
-
declare const cv: CV;
|
|
32
|
-
|
|
33
|
-
export { type CV, type CVCompoundVariantsSchema, type CVConfig, type CVOptions, type CVReturn, type CVVariantProps, type CVVariantsSchema, type VariantProps, type WithClassProp, cv };
|
|
1
|
+
import 'clsx';
|
|
2
|
+
import 'ts-essentials';
|
|
3
|
+
export { k as CV, i as CVCompoundVariant, j as CVCompoundVariantsSchema, b as CVConfig, c as CVOptions, a as CVReturn, h as CVVariant, C as CVVariantsSchema, f as VariantProps, W as WithClassProp, l as cv, m as onlyWhenDefined, n as onlyWhenUndefined } from './index-DUG5hPfi.cjs';
|
package/build/index.d.ts
CHANGED
|
@@ -1,33 +1,3 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
type VariantProps<CVR extends (args?: any) => string> = Prettify<Omit<NonNullable<Exclude<Parameters<CVR>[0], void>>, "class">>;
|
|
5
|
-
type CVVariantsSchema = Record<string, Record<string, ClassValue>>;
|
|
6
|
-
type WithClassProp<T extends Record<string, any>> = T & {
|
|
7
|
-
class?: ClassValue;
|
|
8
|
-
};
|
|
9
|
-
type CVCompoundVariantsSchema<V extends CVVariantsSchema> = WithClassProp<{
|
|
10
|
-
[K in keyof V]?: (keyof V[K] & string) | (keyof V[K])[] | boolean;
|
|
11
|
-
}>[];
|
|
12
|
-
interface CVConfig<V extends CVVariantsSchema, R extends keyof V> {
|
|
13
|
-
baseClass?: ClassValue;
|
|
14
|
-
variants?: V;
|
|
15
|
-
compoundVariants?: CVCompoundVariantsSchema<NoInfer<V>> | ((variants: NoInfer<V>) => CVCompoundVariantsSchema<NoInfer<V>>);
|
|
16
|
-
defaultVariant?: Partial<{
|
|
17
|
-
[K in keyof V]?: keyof V[K];
|
|
18
|
-
}>;
|
|
19
|
-
requiredProps?: R[];
|
|
20
|
-
}
|
|
21
|
-
type CVVariantProps<V extends CVVariantsSchema, R extends (keyof V)> = WithClassProp<MarkRequired<{
|
|
22
|
-
[K in keyof V]?: keyof V[K];
|
|
23
|
-
}, R>>;
|
|
24
|
-
type CVReturn<V extends CVVariantsSchema, R extends keyof V = never> = (props: Prettify<CVVariantProps<V, R>> | ([R] extends [never] ? void : never)) => string;
|
|
25
|
-
interface CVOptions {
|
|
26
|
-
merge?: (...args: ClassValue[]) => string;
|
|
27
|
-
}
|
|
28
|
-
interface CV {
|
|
29
|
-
<V extends CVVariantsSchema, R extends keyof V = never>(config: CVConfig<V, R>, options?: CVOptions): CVReturn<V, R>;
|
|
30
|
-
}
|
|
31
|
-
declare const cv: CV;
|
|
32
|
-
|
|
33
|
-
export { type CV, type CVCompoundVariantsSchema, type CVConfig, type CVOptions, type CVReturn, type CVVariantProps, type CVVariantsSchema, type VariantProps, type WithClassProp, cv };
|
|
1
|
+
import 'clsx';
|
|
2
|
+
import 'ts-essentials';
|
|
3
|
+
export { k as CV, i as CVCompoundVariant, j as CVCompoundVariantsSchema, b as CVConfig, c as CVOptions, a as CVReturn, h as CVVariant, C as CVVariantsSchema, f as VariantProps, W as WithClassProp, l as cv, m as onlyWhenDefined, n as onlyWhenUndefined } from './index-DUG5hPfi.js';
|
package/build/index.js
CHANGED
|
@@ -1,8 +1,2 @@
|
|
|
1
|
-
import
|
|
2
|
-
cv
|
|
3
|
-
} from "./chunk-5UWIPH6K.js";
|
|
4
|
-
import "./chunk-AQSN2Y5C.js";
|
|
5
|
-
export {
|
|
6
|
-
cv
|
|
7
|
-
};
|
|
1
|
+
import{a,b,c}from"./chunk-3XL4COYB.js";import"./chunk-BV75ULQK.js";export{a as cv,b as onlyWhenDefined,c as onlyWhenUndefined};
|
|
8
2
|
//# sourceMappingURL=index.js.map
|
package/build/utils.cjs
CHANGED
|
@@ -1,48 +1,2 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/utils.ts
|
|
21
|
-
var utils_exports = {};
|
|
22
|
-
__export(utils_exports, {
|
|
23
|
-
getEntries: () => getEntries,
|
|
24
|
-
getKeys: () => getKeys,
|
|
25
|
-
getVariantsOptions: () => getVariantsOptions,
|
|
26
|
-
objectFromEntries: () => objectFromEntries
|
|
27
|
-
});
|
|
28
|
-
module.exports = __toCommonJS(utils_exports);
|
|
29
|
-
function getKeys(obj) {
|
|
30
|
-
return Object.keys(obj);
|
|
31
|
-
}
|
|
32
|
-
function getEntries(obj) {
|
|
33
|
-
return Object.entries(obj);
|
|
34
|
-
}
|
|
35
|
-
function getVariantsOptions(variants) {
|
|
36
|
-
return Object.fromEntries(getEntries(variants).map(([cvKey, cvVal]) => [cvKey, getKeys(cvVal)]));
|
|
37
|
-
}
|
|
38
|
-
function objectFromEntries(entries) {
|
|
39
|
-
return Object.fromEntries(entries);
|
|
40
|
-
}
|
|
41
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
42
|
-
0 && (module.exports = {
|
|
43
|
-
getEntries,
|
|
44
|
-
getKeys,
|
|
45
|
-
getVariantsOptions,
|
|
46
|
-
objectFromEntries
|
|
47
|
-
});
|
|
1
|
+
"use strict";var o=Object.defineProperty;var a=Object.getOwnPropertyDescriptor;var p=Object.getOwnPropertyNames;var y=Object.prototype.hasOwnProperty;var f=(e,n)=>{for(var t in n)o(e,t,{get:n[t],enumerable:!0})},c=(e,n,t,i)=>{if(n&&typeof n=="object"||typeof n=="function")for(let r of p(n))!y.call(e,r)&&r!==t&&o(e,r,{get:()=>n[r],enumerable:!(i=a(n,r))||i.enumerable});return e};var x=e=>c(o({},"__esModule",{value:!0}),e);var k={};f(k,{getEntries:()=>O,getKeys:()=>s,getVariantsOptions:()=>d,objectFromEntries:()=>V});module.exports=x(k);function s(e){return Object.keys(e)}function O(e){return Object.entries(e)}function d(e){return Object.fromEntries(O(e).map(([n,t])=>[n,s(t)]))}function V(e){return Object.fromEntries(e)}0&&(module.exports={getEntries,getKeys,getVariantsOptions,objectFromEntries});
|
|
48
2
|
//# sourceMappingURL=utils.cjs.map
|
package/build/utils.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/utils.ts"],"sourcesContent":["import { DeepReadonly, DeepWritable, Prettify, UnionToIntersection } from \"ts-essentials\";\
|
|
1
|
+
{"version":3,"sources":["../src/utils.ts"],"sourcesContent":["import { DeepReadonly, DeepWritable, Prettify, UnionToIntersection } from \"ts-essentials\";\nimport { CVVariantsSchema } from \".\";\n\nexport type KeysOf<O extends Record<string, unknown>> = (keyof O)[];\nexport function getKeys<O extends Record<string, unknown>>(obj: O) {\n return Object.keys(obj) as KeysOf<O>;\n}\n\nexport type EntriesOf<O extends Record<string, unknown>> = { [K in keyof O]: [K, O[K]] };\nexport function getEntries<O extends Record<string, unknown>>(obj: O) {\n return Object.entries(obj) as EntriesOf<O>[keyof EntriesOf<O>][];\n}\n\nexport type VariantOptions<V extends CVVariantsSchema> = {\n [K in keyof V]: KeysOf<V[K]>;\n};\nexport function getVariantsOptions<V extends CVVariantsSchema>(variants: V) {\n return Object.fromEntries(\n getEntries(variants).map(([cvKey, cvVal]) => [cvKey, getKeys(cvVal)]),\n ) as VariantOptions<V>;\n}\n\ntype Entries = [PropertyKey, unknown][] | DeepReadonly<[PropertyKey, unknown][]>;\ntype UnionFromPairs<TArr extends Entries> =\n DeepWritable<TArr> extends (infer R)[]\n ? R extends [infer key, infer val]\n ? { [prop in key & PropertyKey]: val }\n : never\n : never;\n\ntype MergeIntersectingObjects<O> = { [K in keyof O]: O[K] };\n\nexport function objectFromEntries<E extends Entries>(entries: E) {\n return Object.fromEntries(entries) as Prettify<\n MergeIntersectingObjects<UnionToIntersection<UnionFromPairs<E>>>\n >;\n}\n"],"mappings":"yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,gBAAAE,EAAA,YAAAC,EAAA,uBAAAC,EAAA,sBAAAC,IAAA,eAAAC,EAAAN,GAIO,SAASG,EAA2CI,EAAQ,CAC/D,OAAO,OAAO,KAAKA,CAAG,CAC1B,CAGO,SAASL,EAA8CK,EAAQ,CAClE,OAAO,OAAO,QAAQA,CAAG,CAC7B,CAKO,SAASH,EAA+CI,EAAa,CACxE,OAAO,OAAO,YACVN,EAAWM,CAAQ,EAAE,IAAI,CAAC,CAACC,EAAOC,CAAK,IAAM,CAACD,EAAON,EAAQO,CAAK,CAAC,CAAC,CACxE,CACJ,CAYO,SAASL,EAAqCM,EAAY,CAC7D,OAAO,OAAO,YAAYA,CAAO,CAGrC","names":["utils_exports","__export","getEntries","getKeys","getVariantsOptions","objectFromEntries","__toCommonJS","obj","variants","cvKey","cvVal","entries"]}
|
package/build/utils.d.cts
CHANGED
|
@@ -1,20 +1,3 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
1
|
+
import 'ts-essentials';
|
|
2
|
+
export { E as EntriesOf, K as KeysOf, V as VariantOptions, d as getEntries, g as getKeys, e as getVariantsOptions, o as objectFromEntries } from './index-DUG5hPfi.cjs';
|
|
3
3
|
import 'clsx';
|
|
4
|
-
|
|
5
|
-
declare function getKeys<O extends Record<string, unknown>>(obj: O): (keyof O)[];
|
|
6
|
-
type EntriesOf<O extends Record<string, unknown>> = {
|
|
7
|
-
[K in keyof O]: [K, O[K]];
|
|
8
|
-
};
|
|
9
|
-
declare function getEntries<O extends Record<string, unknown>>(obj: O): (EntriesOf<O>[keyof EntriesOf<O>])[];
|
|
10
|
-
declare function getVariantsOptions<V extends CVVariantsSchema>(variants: V): { [K in keyof V]: (keyof V[K])[]; };
|
|
11
|
-
type Entries = [PropertyKey, unknown][] | DeepReadonly<[PropertyKey, unknown][]>;
|
|
12
|
-
type UnionFromPairs<TArr extends Entries> = DeepWritable<TArr> extends (infer R)[] ? R extends [infer key, infer val] ? {
|
|
13
|
-
[prop in key & PropertyKey]: val;
|
|
14
|
-
} : never : never;
|
|
15
|
-
type MergeIntersectingObjects<O> = {
|
|
16
|
-
[K in keyof O]: O[K];
|
|
17
|
-
};
|
|
18
|
-
declare function objectFromEntries<E extends Entries>(entries: E): Prettify<MergeIntersectingObjects<UnionToIntersection<UnionFromPairs<E>>>>;
|
|
19
|
-
|
|
20
|
-
export { getEntries, getKeys, getVariantsOptions, objectFromEntries };
|
package/build/utils.d.ts
CHANGED
|
@@ -1,20 +1,3 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
1
|
+
import 'ts-essentials';
|
|
2
|
+
export { E as EntriesOf, K as KeysOf, V as VariantOptions, d as getEntries, g as getKeys, e as getVariantsOptions, o as objectFromEntries } from './index-DUG5hPfi.js';
|
|
3
3
|
import 'clsx';
|
|
4
|
-
|
|
5
|
-
declare function getKeys<O extends Record<string, unknown>>(obj: O): (keyof O)[];
|
|
6
|
-
type EntriesOf<O extends Record<string, unknown>> = {
|
|
7
|
-
[K in keyof O]: [K, O[K]];
|
|
8
|
-
};
|
|
9
|
-
declare function getEntries<O extends Record<string, unknown>>(obj: O): (EntriesOf<O>[keyof EntriesOf<O>])[];
|
|
10
|
-
declare function getVariantsOptions<V extends CVVariantsSchema>(variants: V): { [K in keyof V]: (keyof V[K])[]; };
|
|
11
|
-
type Entries = [PropertyKey, unknown][] | DeepReadonly<[PropertyKey, unknown][]>;
|
|
12
|
-
type UnionFromPairs<TArr extends Entries> = DeepWritable<TArr> extends (infer R)[] ? R extends [infer key, infer val] ? {
|
|
13
|
-
[prop in key & PropertyKey]: val;
|
|
14
|
-
} : never : never;
|
|
15
|
-
type MergeIntersectingObjects<O> = {
|
|
16
|
-
[K in keyof O]: O[K];
|
|
17
|
-
};
|
|
18
|
-
declare function objectFromEntries<E extends Entries>(entries: E): Prettify<MergeIntersectingObjects<UnionToIntersection<UnionFromPairs<E>>>>;
|
|
19
|
-
|
|
20
|
-
export { getEntries, getKeys, getVariantsOptions, objectFromEntries };
|
package/build/utils.js
CHANGED
|
@@ -1,13 +1,2 @@
|
|
|
1
|
-
import
|
|
2
|
-
getEntries,
|
|
3
|
-
getKeys,
|
|
4
|
-
getVariantsOptions,
|
|
5
|
-
objectFromEntries
|
|
6
|
-
} from "./chunk-AQSN2Y5C.js";
|
|
7
|
-
export {
|
|
8
|
-
getEntries,
|
|
9
|
-
getKeys,
|
|
10
|
-
getVariantsOptions,
|
|
11
|
-
objectFromEntries
|
|
12
|
-
};
|
|
1
|
+
import{a,b,c,d}from"./chunk-BV75ULQK.js";export{b as getEntries,a as getKeys,c as getVariantsOptions,d as objectFromEntries};
|
|
13
2
|
//# sourceMappingURL=utils.js.map
|
package/build/vue.cjs
CHANGED
|
@@ -1,96 +1,2 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/vue.ts
|
|
21
|
-
var vue_exports = {};
|
|
22
|
-
__export(vue_exports, {
|
|
23
|
-
cvWithProps: () => cvWithProps,
|
|
24
|
-
getPropsDeclaration: () => getPropsDeclaration
|
|
25
|
-
});
|
|
26
|
-
module.exports = __toCommonJS(vue_exports);
|
|
27
|
-
|
|
28
|
-
// src/index.ts
|
|
29
|
-
var import_clsx = require("clsx");
|
|
30
|
-
|
|
31
|
-
// src/utils.ts
|
|
32
|
-
function getKeys(obj) {
|
|
33
|
-
return Object.keys(obj);
|
|
34
|
-
}
|
|
35
|
-
function getEntries(obj) {
|
|
36
|
-
return Object.entries(obj);
|
|
37
|
-
}
|
|
38
|
-
function objectFromEntries(entries) {
|
|
39
|
-
return Object.fromEntries(entries);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
// src/index.ts
|
|
43
|
-
var cv = (config, options = {}) => {
|
|
44
|
-
const { variants, defaultVariant, baseClass } = config;
|
|
45
|
-
const merge = options.merge ?? import_clsx.clsx;
|
|
46
|
-
if (variants == null) return (props) => merge(baseClass, props?.class);
|
|
47
|
-
const compoundVariants = typeof config.compoundVariants === "function" ? config.compoundVariants(variants) : config.compoundVariants;
|
|
48
|
-
return (props) => {
|
|
49
|
-
const variantClassNames = getKeys(variants).map((variant) => {
|
|
50
|
-
const variantProp = props?.[variant];
|
|
51
|
-
const defaultVariantProp = defaultVariant?.[variant];
|
|
52
|
-
const variantKey = variantProp || defaultVariantProp;
|
|
53
|
-
return variants[variant][variantKey];
|
|
54
|
-
});
|
|
55
|
-
const compoundVariantClassNames = compoundVariants?.reduce((acc, { class: cvClass, ...cvConfig }) => {
|
|
56
|
-
const shouldApplyCompoundVariant = getEntries(cvConfig).every(([cvKey, cvSelector]) => {
|
|
57
|
-
const defaultsSelector = defaultVariant?.[cvKey];
|
|
58
|
-
const propsSelector = props?.[cvKey];
|
|
59
|
-
if (cvSelector === true) return propsSelector !== void 0;
|
|
60
|
-
else if (cvSelector === false) return propsSelector === void 0;
|
|
61
|
-
const selector = propsSelector ?? defaultsSelector;
|
|
62
|
-
return Array.isArray(cvSelector) ? cvSelector.includes(selector) : selector === cvSelector;
|
|
63
|
-
});
|
|
64
|
-
if (shouldApplyCompoundVariant) acc.push(cvClass);
|
|
65
|
-
return acc;
|
|
66
|
-
}, new Array());
|
|
67
|
-
return merge(config?.baseClass, variantClassNames, compoundVariantClassNames, props?.class);
|
|
68
|
-
};
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
// src/vue.ts
|
|
72
|
-
function getPropsDeclaration(config) {
|
|
73
|
-
if (typeof config?.variants === "undefined") {
|
|
74
|
-
return {};
|
|
75
|
-
}
|
|
76
|
-
return objectFromEntries(
|
|
77
|
-
getKeys(config.variants).map((key) => [
|
|
78
|
-
key,
|
|
79
|
-
{
|
|
80
|
-
type: String,
|
|
81
|
-
required: config.requiredProps?.includes(key)
|
|
82
|
-
}
|
|
83
|
-
])
|
|
84
|
-
);
|
|
85
|
-
}
|
|
86
|
-
var cvWithProps = (config, options) => {
|
|
87
|
-
const getClass = cv(config, options);
|
|
88
|
-
const props = getPropsDeclaration(config);
|
|
89
|
-
return { getClass, props };
|
|
90
|
-
};
|
|
91
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
92
|
-
0 && (module.exports = {
|
|
93
|
-
cvWithProps,
|
|
94
|
-
getPropsDeclaration
|
|
95
|
-
});
|
|
1
|
+
"use strict";var c=Object.defineProperty;var E=Object.getOwnPropertyDescriptor;var T=Object.getOwnPropertyNames;var D=Object.prototype.hasOwnProperty;var N=(e,n)=>{for(var r in n)c(e,r,{get:n[r],enumerable:!0})},j=(e,n,r,t)=>{if(n&&typeof n=="object"||typeof n=="function")for(let s of T(n))!D.call(e,s)&&s!==r&&c(e,s,{get:()=>n[s],enumerable:!(t=E(n,s))||t.enumerable});return e};var U=e=>j(c({},"__esModule",{value:!0}),e);var A={};N(A,{cvWithProps:()=>w,getPropsDeclaration:()=>P});module.exports=U(A);var g=require("clsx");function i(e){return Object.keys(e)}function l(e){return Object.entries(e)}function R(e){return Object.fromEntries(l(e).map(([n,r])=>[n,i(r)]))}function k(e){return Object.fromEntries(e)}var h=(e,n={})=>{let r=n.merge??g.clsx,{variants:t,defaultVariant:s,base:v}=e,d=t?i(t):[],C=t?R(t):{},m=s??{},K=t?typeof e.compoundVariants=="function"?e.compoundVariants({variants:t,keys:d,options:C,defaults:m}):e.compoundVariants:void 0;return Object.assign(V=>{let S=t?d.map(a=>{let f=V?.[a],y=s?.[a],p=f?.toString()||y?.toString();return p?t[a][p]:void 0}):void 0,W=K?.reduce((a,{class:f,className:y,...p})=>(l(p).every(([x,o])=>{let _=s?.[x],u=V?.[x];if(typeof o=="object"&&"__token"in o){if(o.__token===q.__token)return u!==void 0;if(o.__token===B.__token)return u===void 0}let O=u??_;return Array.isArray(o)?o.some(b=>O===b):O===o})&&a.push(f,y),a),new Array);return r(v,S,W,V?.class,V?.className)},{keys:d,options:C,defaults:m})},q={__token:"OnlyWhenDefined"},B={__token:"OnlyWhenUndefined"};function P(e){return typeof e?.variants>"u"?{}:k(i(e.variants).map(n=>[n,{type:String,required:e.requiredProps?.includes(n)}]))}var w=(e,n)=>{let r=h(e,n),t=P(e);return{getClass:r,props:t}};0&&(module.exports={cvWithProps,getPropsDeclaration});
|
|
96
2
|
//# sourceMappingURL=vue.cjs.map
|
package/build/vue.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/vue.ts","../src/index.ts","../src/utils.ts"],"sourcesContent":["import { Prettify } from \"ts-essentials\";\r\nimport { PropType } from \"vue\";\r\nimport { CVConfig, CVOptions, CVReturn, CVVariantsSchema, cv } from \".\";\r\nimport { getKeys, objectFromEntries } from \"./utils\";\r\n\r\ntype PropsDeclaration<V extends CVVariantsSchema, R extends keyof V = never> = Prettify<{ [K in keyof V]: {\r\n type: PropType<keyof V[K] | (K extends R ? never : undefined)>;\r\n required: K extends R ? true : false;\r\n} }>;\r\nexport interface CVWithPropsReturn<V extends CVVariantsSchema, R extends keyof V = never> {\r\n getClass: CVReturn<V, R>;\r\n props: PropsDeclaration<NoInfer<V>, NoInfer<R>>;\r\n}\r\n\r\nexport interface CVWithProps {\r\n // eslint-disable-next-line @typescript-eslint/prefer-function-type\r\n <V extends CVVariantsSchema, R extends keyof V = never>(config: CVConfig<V, R>, options?: CVOptions): CVWithPropsReturn<V, R>;\r\n}\r\n\r\nexport function getPropsDeclaration<V extends CVVariantsSchema, R extends keyof V = never>(config: CVConfig<V, R>) {\r\n if (typeof config?.variants === \"undefined\") {\r\n return {};\r\n }\r\n\r\n return objectFromEntries(\r\n getKeys(config.variants).map(key => [\r\n key,\r\n {\r\n type: String,\r\n required: config.requiredProps?.includes(key as R),\r\n },\r\n ] as const),\r\n );\r\n}\r\n\r\nexport const cvWithProps: CVWithProps = ((config, options) => {\r\n const getClass = cv(config, options);\r\n const props = getPropsDeclaration(config);\r\n\r\n return { getClass, props };\r\n}) as CVWithProps;\r\n","import { ClassValue, clsx } from \"clsx\";\r\nimport { MarkRequired, Prettify } from \"ts-essentials\";\r\nimport { getEntries, getKeys } from \"./utils\";\r\n\r\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\r\nexport type VariantProps<CVR extends (args?: any) => string> = Prettify<Omit<NonNullable<Exclude<Parameters<CVR>[0], void>>, \"class\">>;\r\n\r\nexport type CVVariantsSchema = Record<string, Record<string, ClassValue>>;\r\n\r\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\r\nexport type WithClassProp<T extends Record<string, any>> = T & {\r\n class?: ClassValue;\r\n};\r\n\r\nexport type CVCompoundVariantsSchema<V extends CVVariantsSchema> = WithClassProp<{\r\n [K in keyof V]?: (keyof V[K] & string) | (keyof V[K])[] | boolean\r\n}>[];\r\n\r\nexport interface CVConfig<V extends CVVariantsSchema, R extends keyof V> {\r\n baseClass?: ClassValue;\r\n variants?: V;\r\n compoundVariants?: CVCompoundVariantsSchema<NoInfer<V>> | ((variants: NoInfer<V>) => CVCompoundVariantsSchema<NoInfer<V>>);\r\n defaultVariant?: Partial<{ [K in keyof V]?: keyof V[K] }>;\r\n requiredProps?: R[];\r\n}\r\n\r\nexport type CVVariantProps<V extends CVVariantsSchema, R extends (keyof V)> =\r\nWithClassProp<MarkRequired<{ [K in keyof V]?: keyof V[K] }, R>>;\r\n\r\nexport type CVReturn<V extends CVVariantsSchema, R extends keyof V = never> = (props: Prettify<CVVariantProps<V, R>> | ([R] extends [never] ? void : never)) => string;\r\n\r\nexport interface CVOptions {\r\n merge?: (...args: ClassValue[]) => string;\r\n}\r\n\r\nexport interface CV {\r\n // eslint-disable-next-line @typescript-eslint/prefer-function-type\r\n <V extends CVVariantsSchema, R extends keyof V = never>(config: CVConfig<V, R>, options?: CVOptions): CVReturn<V, R>;\r\n}\r\n\r\nexport const cv: CV = (config, options = {}) => {\r\n const { variants, defaultVariant, baseClass } = config;\r\n\r\n const merge = options.merge ?? clsx;\r\n\r\n if (variants == null) return props => merge(baseClass, props?.class);\r\n\r\n const compoundVariants = typeof config.compoundVariants === \"function\" ? config.compoundVariants(variants) : config.compoundVariants;\r\n\r\n return (props) => {\r\n const variantClassNames = getKeys(variants).map((variant) => {\r\n const variantProp = props?.[variant];\r\n const defaultVariantProp = defaultVariant?.[variant];\r\n\r\n const variantKey = (variantProp || defaultVariantProp) as keyof (typeof variants)[typeof variant];\r\n\r\n return variants[variant][variantKey];\r\n });\r\n\r\n const compoundVariantClassNames = compoundVariants?.reduce((acc, { class: cvClass, ...cvConfig }) => {\r\n const shouldApplyCompoundVariant = getEntries(cvConfig).every(([cvKey, cvSelector]) => {\r\n const defaultsSelector = defaultVariant?.[cvKey as keyof typeof defaultVariant];\r\n const propsSelector = props?.[cvKey as keyof typeof props];\r\n\r\n if (cvSelector === true) return propsSelector !== undefined; // only when prop is defined\r\n else if (cvSelector === false) return propsSelector === undefined; // only when prop is not defined\r\n\r\n const selector = propsSelector ?? defaultsSelector;\r\n\r\n return Array.isArray(cvSelector) ? cvSelector.includes(selector as string) : selector === cvSelector;\r\n });\r\n\r\n if (shouldApplyCompoundVariant) acc.push(cvClass);\r\n return acc;\r\n }, new Array<ClassValue>());\r\n\r\n return merge(config?.baseClass, variantClassNames, compoundVariantClassNames, props?.class);\r\n };\r\n};\r\n","import { DeepReadonly, DeepWritable, Prettify, UnionToIntersection } from \"ts-essentials\";\r\nimport { CVVariantsSchema } from \".\";\r\n\r\nexport function getKeys<O extends Record<string, unknown>>(obj: O) {\r\n return Object.keys(obj) as (keyof O)[];\r\n}\r\n\r\ntype EntriesOf<O extends Record<string, unknown>> = { [K in keyof O]: [K, O[K]] };\r\nexport function getEntries<O extends Record<string, unknown>>(obj: O) {\r\n return Object.entries(obj) as (EntriesOf<O>[keyof EntriesOf<O>])[];\r\n}\r\n\r\nexport function getVariantsOptions<V extends CVVariantsSchema>(variants: V) {\r\n return Object.fromEntries(getEntries(variants).map(([cvKey, cvVal]) => [cvKey, getKeys(cvVal)])) as { [K in keyof V]: (keyof V[K])[] };\r\n}\r\n\r\ntype Entries = [PropertyKey, unknown][] | DeepReadonly<[PropertyKey, unknown][]>;\r\ntype UnionFromPairs<TArr extends Entries> =\r\n DeepWritable<TArr> extends (infer R)[] ? R extends [infer key, infer val] ? { [prop in key & PropertyKey]: val } : never : never;\r\n\r\ntype MergeIntersectingObjects<O> = { [K in keyof O]: O[K] };\r\n\r\nexport function objectFromEntries<E extends Entries>(entries: E) {\r\n return Object.fromEntries(entries) as Prettify<MergeIntersectingObjects<UnionToIntersection<UnionFromPairs<E>>>>;\r\n}\r\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,kBAAiC;;;ACG1B,SAAS,QAA2C,KAAQ;AAC/D,SAAO,OAAO,KAAK,GAAG;AAC1B;AAGO,SAAS,WAA8C,KAAQ;AAClE,SAAO,OAAO,QAAQ,GAAG;AAC7B;AAYO,SAAS,kBAAqC,SAAY;AAC7D,SAAO,OAAO,YAAY,OAAO;AACrC;;;ADgBO,IAAM,KAAS,CAAC,QAAQ,UAAU,CAAC,MAAM;AAC5C,QAAM,EAAE,UAAU,gBAAgB,UAAU,IAAI;AAEhD,QAAM,QAAQ,QAAQ,SAAS;AAE/B,MAAI,YAAY,KAAM,QAAO,WAAS,MAAM,WAAW,OAAO,KAAK;AAEnE,QAAM,mBAAmB,OAAO,OAAO,qBAAqB,aAAa,OAAO,iBAAiB,QAAQ,IAAI,OAAO;AAEpH,SAAO,CAAC,UAAU;AACd,UAAM,oBAAoB,QAAQ,QAAQ,EAAE,IAAI,CAAC,YAAY;AACzD,YAAM,cAAc,QAAQ,OAAO;AACnC,YAAM,qBAAqB,iBAAiB,OAAO;AAEnD,YAAM,aAAc,eAAe;AAEnC,aAAO,SAAS,OAAO,EAAE,UAAU;AAAA,IACvC,CAAC;AAED,UAAM,4BAA4B,kBAAkB,OAAO,CAAC,KAAK,EAAE,OAAO,SAAS,GAAG,SAAS,MAAM;AACjG,YAAM,6BAA6B,WAAW,QAAQ,EAAE,MAAM,CAAC,CAAC,OAAO,UAAU,MAAM;AACnF,cAAM,mBAAmB,iBAAiB,KAAoC;AAC9E,cAAM,gBAAgB,QAAQ,KAA2B;AAEzD,YAAI,eAAe,KAAM,QAAO,kBAAkB;AAAA,iBACzC,eAAe,MAAO,QAAO,kBAAkB;AAExD,cAAM,WAAW,iBAAiB;AAElC,eAAO,MAAM,QAAQ,UAAU,IAAI,WAAW,SAAS,QAAkB,IAAI,aAAa;AAAA,MAC9F,CAAC;AAED,UAAI,2BAA4B,KAAI,KAAK,OAAO;AAChD,aAAO;AAAA,IACX,GAAG,IAAI,MAAkB,CAAC;AAE1B,WAAO,MAAM,QAAQ,WAAW,mBAAmB,2BAA2B,OAAO,KAAK;AAAA,EAC9F;AACJ;;;AD3DO,SAAS,oBAA2E,QAAwB;AAC/G,MAAI,OAAO,QAAQ,aAAa,aAAa;AACzC,WAAO,CAAC;AAAA,EACZ;AAEA,SAAO;AAAA,IACH,QAAQ,OAAO,QAAQ,EAAE,IAAI,SAAO;AAAA,MAChC;AAAA,MACA;AAAA,QACI,MAAM;AAAA,QACN,UAAU,OAAO,eAAe,SAAS,GAAQ;AAAA,MACrD;AAAA,IACJ,CAAU;AAAA,EACd;AACJ;AAEO,IAAM,cAA4B,CAAC,QAAQ,YAAY;AAC1D,QAAM,WAAW,GAAG,QAAQ,OAAO;AACnC,QAAM,QAAQ,oBAAoB,MAAM;AAExC,SAAO,EAAE,UAAU,MAAM;AAC7B;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/vue.ts","../src/index.ts","../src/utils.ts"],"sourcesContent":["import { Prettify } from \"ts-essentials\";\nimport { PropType } from \"vue\";\nimport { CVConfig, CVOptions, CVReturn, CVVariantsSchema, cv } from \".\";\nimport { getKeys, objectFromEntries } from \"./utils\";\n\ntype PropsDeclaration<V extends CVVariantsSchema, R extends keyof V = never> = Prettify<{\n [K in keyof V]: {\n type: PropType<keyof V[K] | (K extends R ? never : undefined)>;\n required: K extends R ? true : false;\n };\n}>;\nexport interface CVWithPropsReturn<V extends CVVariantsSchema, R extends keyof V = never> {\n getClass: CVReturn<V, R>;\n props: PropsDeclaration<NoInfer<V>, NoInfer<R>>;\n}\n\nexport interface CVWithProps {\n // eslint-disable-next-line @typescript-eslint/prefer-function-type\n <V extends CVVariantsSchema, R extends keyof V = never>(\n config: CVConfig<V, R>,\n options?: CVOptions,\n ): CVWithPropsReturn<V, R>;\n}\n\nexport function getPropsDeclaration<V extends CVVariantsSchema, R extends keyof V = never>(\n config: CVConfig<V, R>,\n) {\n if (typeof config?.variants === \"undefined\") {\n return {};\n }\n\n return objectFromEntries(\n getKeys(config.variants).map(\n (key) =>\n [\n key,\n {\n type: String,\n required: config.requiredProps?.includes(key as R),\n },\n ] as const,\n ),\n );\n}\n\nexport const cvWithProps: CVWithProps = ((config, options) => {\n const getClass = cv(config, options);\n const props = getPropsDeclaration(config);\n\n return { getClass, props };\n}) as CVWithProps;\n","import { ClassValue, clsx } from \"clsx\";\r\nimport { MarkRequired, Prettify, StrictExclude, Opaque } from \"ts-essentials\";\r\nimport { getEntries, getKeys, getVariantsOptions, KeysOf, VariantOptions } from \"./utils\";\r\n\r\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\r\nexport type VariantProps<CVR extends (args?: any) => string> = Prettify<\r\n Omit<NonNullable<StrictExclude<Parameters<CVR>[0], void>>, \"class\" | \"className\">\r\n>;\r\n\r\ntype ClassProp =\r\n | { class?: ClassValue; className?: never }\r\n | { class?: never; className?: ClassValue };\r\n\r\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\r\nexport type WithClassProp<T extends Record<string, any>> = T & ClassProp;\r\n\r\ntype BooleanStringToBoolean<T> = T extends \"true\" | \"false\" ? boolean : T;\r\n\r\nexport type CVVariantsSchema = Record<string, Record<string, ClassValue>>;\r\n\r\nexport type CVVariant<V extends CVVariantsSchema> = {\r\n [K in keyof V]?: BooleanStringToBoolean<keyof V[K]>;\r\n};\r\nexport type CVCompoundVariant<V extends CVVariantsSchema> = WithClassProp<{\r\n [K in keyof V]?:\r\n | BooleanStringToBoolean<keyof V[K]>\r\n | BooleanStringToBoolean<keyof V[K]>[]\r\n | CVToken;\r\n}>;\r\n\r\nexport type CVCompoundVariantsSchema<V extends CVVariantsSchema> = CVCompoundVariant<V>[];\r\n\r\nexport interface CVConfig<V extends CVVariantsSchema, R extends keyof V> {\r\n base?: ClassValue;\r\n variants?: V;\r\n compoundVariants?:\r\n | CVCompoundVariantsSchema<V>\r\n | ((arg: {\r\n variants: V;\r\n keys: KeysOf<V>;\r\n options: VariantOptions<V>;\r\n defaults: CVVariant<V>;\r\n }) => CVCompoundVariantsSchema<V>);\r\n defaultVariant?: CVVariant<V>;\r\n requiredProps?: R[];\r\n}\r\nexport interface CVOptions {\r\n merge?: (...args: ClassValue[]) => string;\r\n}\r\n\r\ntype CVReturnProps<V extends CVVariantsSchema, R extends keyof V = never> =\r\n | Prettify<WithClassProp<MarkRequired<CVVariant<V>, R>>>\r\n | ([R] extends [never] ? void : never);\r\nexport interface CVReturn<V extends CVVariantsSchema, R extends keyof V = never> {\r\n (props: CVReturnProps<V, R>): string;\r\n keys: KeysOf<V>;\r\n options: VariantOptions<V>;\r\n defaults: CVVariant<V>;\r\n}\r\n\r\nexport interface CV {\r\n // eslint-disable-next-line @typescript-eslint/prefer-function-type\r\n <V extends CVVariantsSchema, R extends keyof V = never>(\r\n config: CVConfig<V, R>,\r\n cvOptions?: CVOptions,\r\n ): CVReturn<V, R>;\r\n}\r\n\r\nexport const cv: CV = (config, cvOptions = {}) => {\r\n const merge = cvOptions.merge ?? clsx;\r\n\r\n const { variants, defaultVariant, base } = config;\r\n\r\n type ResolvedV = NonNullable<typeof variants>;\r\n\r\n const keys = variants ? getKeys(variants) : [];\r\n const options = variants ? getVariantsOptions(variants) : ({} as VariantOptions<ResolvedV>);\r\n const defaults = defaultVariant ?? ({} as CVVariant<ResolvedV>);\r\n\r\n const compoundVariants = variants\r\n ? typeof config.compoundVariants === \"function\"\r\n ? config.compoundVariants({ variants, keys, options, defaults })\r\n : config.compoundVariants\r\n : undefined;\r\n\r\n const getVariant = <V extends ResolvedV, R extends keyof V = never>(\r\n props: CVReturnProps<V, R>,\r\n ) => {\r\n const variantClassNames = variants\r\n ? keys.map((variant) => {\r\n const variantProp = props?.[variant];\r\n const defaultVariantProp = defaultVariant?.[variant];\r\n\r\n const variantKey = variantProp?.toString() || defaultVariantProp?.toString();\r\n\r\n return variantKey ? variants[variant][variantKey] : undefined;\r\n })\r\n : undefined;\r\n\r\n const compoundVariantClassNames = compoundVariants?.reduce(\r\n (acc, { class: cvClass, className: cvClassName, ...cvConfig }) => {\r\n const shouldApplyCompoundVariant = getEntries(cvConfig).every(\r\n ([cvKey, cvSelector]) => {\r\n const defaultsSelector =\r\n defaultVariant?.[cvKey as keyof typeof defaultVariant];\r\n const propsSelector = props?.[cvKey as keyof typeof props];\r\n\r\n if (typeof cvSelector === \"object\" && \"__token\" in cvSelector) {\r\n if (cvSelector.__token === onlyWhenDefined.__token) {\r\n return propsSelector !== undefined;\r\n } else if (cvSelector.__token === onlyWhenUndefined.__token) {\r\n return propsSelector === undefined;\r\n }\r\n }\r\n\r\n const selector = propsSelector ?? defaultsSelector;\r\n\r\n return Array.isArray(cvSelector)\r\n ? cvSelector.some((cvSelector) => selector === cvSelector)\r\n : selector === cvSelector;\r\n },\r\n );\r\n\r\n if (shouldApplyCompoundVariant) acc.push(cvClass, cvClassName);\r\n return acc;\r\n },\r\n new Array<ClassValue>(),\r\n );\r\n\r\n return merge(\r\n base,\r\n variantClassNames,\r\n compoundVariantClassNames,\r\n props?.class,\r\n props?.className,\r\n );\r\n };\r\n\r\n return Object.assign(getVariant, { keys, options, defaults });\r\n};\r\n\r\ntype OnlyWhenDefined = Opaque<{ __token: string }, \"OnlyWhenDefined\">;\r\ntype OnlyWhenUndefined = Opaque<{ __token: string }, \"OnlyWhenUndefined\">;\r\n\r\ntype CVToken = OnlyWhenDefined | OnlyWhenUndefined;\r\n\r\n/** Apply compound variant only if prop **is** specified */\r\nexport const onlyWhenDefined = { __token: \"OnlyWhenDefined\" } as OnlyWhenDefined;\r\n/** Apply compound variant only if prop **is not** specified */\r\nexport const onlyWhenUndefined = { __token: \"OnlyWhenUndefined\" } as OnlyWhenUndefined;\r\n","import { DeepReadonly, DeepWritable, Prettify, UnionToIntersection } from \"ts-essentials\";\nimport { CVVariantsSchema } from \".\";\n\nexport type KeysOf<O extends Record<string, unknown>> = (keyof O)[];\nexport function getKeys<O extends Record<string, unknown>>(obj: O) {\n return Object.keys(obj) as KeysOf<O>;\n}\n\nexport type EntriesOf<O extends Record<string, unknown>> = { [K in keyof O]: [K, O[K]] };\nexport function getEntries<O extends Record<string, unknown>>(obj: O) {\n return Object.entries(obj) as EntriesOf<O>[keyof EntriesOf<O>][];\n}\n\nexport type VariantOptions<V extends CVVariantsSchema> = {\n [K in keyof V]: KeysOf<V[K]>;\n};\nexport function getVariantsOptions<V extends CVVariantsSchema>(variants: V) {\n return Object.fromEntries(\n getEntries(variants).map(([cvKey, cvVal]) => [cvKey, getKeys(cvVal)]),\n ) as VariantOptions<V>;\n}\n\ntype Entries = [PropertyKey, unknown][] | DeepReadonly<[PropertyKey, unknown][]>;\ntype UnionFromPairs<TArr extends Entries> =\n DeepWritable<TArr> extends (infer R)[]\n ? R extends [infer key, infer val]\n ? { [prop in key & PropertyKey]: val }\n : never\n : never;\n\ntype MergeIntersectingObjects<O> = { [K in keyof O]: O[K] };\n\nexport function objectFromEntries<E extends Entries>(entries: E) {\n return Object.fromEntries(entries) as Prettify<\n MergeIntersectingObjects<UnionToIntersection<UnionFromPairs<E>>>\n >;\n}\n"],"mappings":"yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,iBAAAE,EAAA,wBAAAC,IAAA,eAAAC,EAAAJ,GCAA,IAAAK,EAAiC,gBCI1B,SAASC,EAA2CC,EAAQ,CAC/D,OAAO,OAAO,KAAKA,CAAG,CAC1B,CAGO,SAASC,EAA8CD,EAAQ,CAClE,OAAO,OAAO,QAAQA,CAAG,CAC7B,CAKO,SAASE,EAA+CC,EAAa,CACxE,OAAO,OAAO,YACVF,EAAWE,CAAQ,EAAE,IAAI,CAAC,CAACC,EAAOC,CAAK,IAAM,CAACD,EAAOL,EAAQM,CAAK,CAAC,CAAC,CACxE,CACJ,CAYO,SAASC,EAAqCC,EAAY,CAC7D,OAAO,OAAO,YAAYA,CAAO,CAGrC,CDgCO,IAAMC,EAAS,CAACC,EAAQC,EAAY,CAAC,IAAM,CAC9C,IAAMC,EAAQD,EAAU,OAAS,OAE3B,CAAE,SAAAE,EAAU,eAAAC,EAAgB,KAAAC,CAAK,EAAIL,EAIrCM,EAAOH,EAAWI,EAAQJ,CAAQ,EAAI,CAAC,EACvCK,EAAUL,EAAWM,EAAmBN,CAAQ,EAAK,CAAC,EACtDO,EAAWN,GAAmB,CAAC,EAE/BO,EAAmBR,EACnB,OAAOH,EAAO,kBAAqB,WAC/BA,EAAO,iBAAiB,CAAE,SAAAG,EAAU,KAAAG,EAAM,QAAAE,EAAS,SAAAE,CAAS,CAAC,EAC7DV,EAAO,iBACX,OAuDN,OAAO,OAAO,OApDVY,GACC,CACD,IAAMC,EAAoBV,EACpBG,EAAK,IAAKQ,GAAY,CAClB,IAAMC,EAAcH,IAAQE,CAAO,EAC7BE,EAAqBZ,IAAiBU,CAAO,EAE7CG,EAAaF,GAAa,SAAS,GAAKC,GAAoB,SAAS,EAE3E,OAAOC,EAAad,EAASW,CAAO,EAAEG,CAAU,EAAI,MACxD,CAAC,EACD,OAEAC,EAA4BP,GAAkB,OAChD,CAACQ,EAAK,CAAE,MAAOC,EAAS,UAAWC,EAAa,GAAGC,CAAS,KACrBC,EAAWD,CAAQ,EAAE,MACpD,CAAC,CAACE,EAAOC,CAAU,IAAM,CACrB,IAAMC,EACFtB,IAAiBoB,CAAoC,EACnDG,EAAgBf,IAAQY,CAA2B,EAEzD,GAAI,OAAOC,GAAe,UAAY,YAAaA,EAAY,CAC3D,GAAIA,EAAW,UAAYG,EAAgB,QACvC,OAAOD,IAAkB,OACtB,GAAIF,EAAW,UAAYI,EAAkB,QAChD,OAAOF,IAAkB,MAEjC,CAEA,IAAMG,EAAWH,GAAiBD,EAElC,OAAO,MAAM,QAAQD,CAAU,EACzBA,EAAW,KAAMA,GAAeK,IAAaL,CAAU,EACvDK,IAAaL,CACvB,CACJ,GAEgCN,EAAI,KAAKC,EAASC,CAAW,EACtDF,GAEX,IAAI,KACR,EAEA,OAAOjB,EACHG,EACAQ,EACAK,EACAN,GAAO,MACPA,GAAO,SACX,CACJ,EAEiC,CAAE,KAAAN,EAAM,QAAAE,EAAS,SAAAE,CAAS,CAAC,CAChE,EAQakB,EAAkB,CAAE,QAAS,iBAAkB,EAE/CC,EAAoB,CAAE,QAAS,mBAAoB,ED7HzD,SAASE,EACZC,EACF,CACE,OAAI,OAAOA,GAAQ,SAAa,IACrB,CAAC,EAGLC,EACHC,EAAQF,EAAO,QAAQ,EAAE,IACpBG,GACG,CACIA,EACA,CACI,KAAM,OACN,SAAUH,EAAO,eAAe,SAASG,CAAQ,CACrD,CACJ,CACR,CACJ,CACJ,CAEO,IAAMC,EAA4B,CAACJ,EAAQK,IAAY,CAC1D,IAAMC,EAAWC,EAAGP,EAAQK,CAAO,EAC7BG,EAAQT,EAAoBC,CAAM,EAExC,MAAO,CAAE,SAAAM,EAAU,MAAAE,CAAM,CAC7B","names":["vue_exports","__export","cvWithProps","getPropsDeclaration","__toCommonJS","import_clsx","getKeys","obj","getEntries","getVariantsOptions","variants","cvKey","cvVal","objectFromEntries","entries","cv","config","cvOptions","merge","variants","defaultVariant","base","keys","getKeys","options","getVariantsOptions","defaults","compoundVariants","props","variantClassNames","variant","variantProp","defaultVariantProp","variantKey","compoundVariantClassNames","acc","cvClass","cvClassName","cvConfig","getEntries","cvKey","cvSelector","defaultsSelector","propsSelector","onlyWhenDefined","onlyWhenUndefined","selector","getPropsDeclaration","config","objectFromEntries","getKeys","key","cvWithProps","options","getClass","cv","props"]}
|
package/build/vue.d.cts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Prettify } from 'ts-essentials';
|
|
2
2
|
import { PropType } from 'vue';
|
|
3
|
-
import { CVVariantsSchema, CVReturn, CVConfig, CVOptions } from './index.cjs';
|
|
3
|
+
import { C as CVVariantsSchema, a as CVReturn, b as CVConfig, c as CVOptions } from './index-DUG5hPfi.cjs';
|
|
4
4
|
import 'clsx';
|
|
5
5
|
|
|
6
6
|
type PropsDeclaration<V extends CVVariantsSchema, R extends keyof V = never> = Prettify<{
|
package/build/vue.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Prettify } from 'ts-essentials';
|
|
2
2
|
import { PropType } from 'vue';
|
|
3
|
-
import { CVVariantsSchema, CVReturn, CVConfig, CVOptions } from './index.js';
|
|
3
|
+
import { C as CVVariantsSchema, a as CVReturn, b as CVConfig, c as CVOptions } from './index-DUG5hPfi.js';
|
|
4
4
|
import 'clsx';
|
|
5
5
|
|
|
6
6
|
type PropsDeclaration<V extends CVVariantsSchema, R extends keyof V = never> = Prettify<{
|
package/build/vue.js
CHANGED
|
@@ -1,33 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
cv
|
|
3
|
-
} from "./chunk-5UWIPH6K.js";
|
|
4
|
-
import {
|
|
5
|
-
getKeys,
|
|
6
|
-
objectFromEntries
|
|
7
|
-
} from "./chunk-AQSN2Y5C.js";
|
|
8
|
-
|
|
9
|
-
// src/vue.ts
|
|
10
|
-
function getPropsDeclaration(config) {
|
|
11
|
-
if (typeof config?.variants === "undefined") {
|
|
12
|
-
return {};
|
|
13
|
-
}
|
|
14
|
-
return objectFromEntries(
|
|
15
|
-
getKeys(config.variants).map((key) => [
|
|
16
|
-
key,
|
|
17
|
-
{
|
|
18
|
-
type: String,
|
|
19
|
-
required: config.requiredProps?.includes(key)
|
|
20
|
-
}
|
|
21
|
-
])
|
|
22
|
-
);
|
|
23
|
-
}
|
|
24
|
-
var cvWithProps = (config, options) => {
|
|
25
|
-
const getClass = cv(config, options);
|
|
26
|
-
const props = getPropsDeclaration(config);
|
|
27
|
-
return { getClass, props };
|
|
28
|
-
};
|
|
29
|
-
export {
|
|
30
|
-
cvWithProps,
|
|
31
|
-
getPropsDeclaration
|
|
32
|
-
};
|
|
1
|
+
import{a as o}from"./chunk-3XL4COYB.js";import{a as t,d as n}from"./chunk-BV75ULQK.js";function V(e){return typeof e?.variants>"u"?{}:n(t(e.variants).map(r=>[r,{type:String,required:e.requiredProps?.includes(r)}]))}var R=(e,r)=>{let s=o(e,r),i=V(e);return{getClass:s,props:i}};export{R as cvWithProps,V as getPropsDeclaration};
|
|
33
2
|
//# sourceMappingURL=vue.js.map
|
package/build/vue.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/vue.ts"],"sourcesContent":["import { Prettify } from \"ts-essentials\";\
|
|
1
|
+
{"version":3,"sources":["../src/vue.ts"],"sourcesContent":["import { Prettify } from \"ts-essentials\";\nimport { PropType } from \"vue\";\nimport { CVConfig, CVOptions, CVReturn, CVVariantsSchema, cv } from \".\";\nimport { getKeys, objectFromEntries } from \"./utils\";\n\ntype PropsDeclaration<V extends CVVariantsSchema, R extends keyof V = never> = Prettify<{\n [K in keyof V]: {\n type: PropType<keyof V[K] | (K extends R ? never : undefined)>;\n required: K extends R ? true : false;\n };\n}>;\nexport interface CVWithPropsReturn<V extends CVVariantsSchema, R extends keyof V = never> {\n getClass: CVReturn<V, R>;\n props: PropsDeclaration<NoInfer<V>, NoInfer<R>>;\n}\n\nexport interface CVWithProps {\n // eslint-disable-next-line @typescript-eslint/prefer-function-type\n <V extends CVVariantsSchema, R extends keyof V = never>(\n config: CVConfig<V, R>,\n options?: CVOptions,\n ): CVWithPropsReturn<V, R>;\n}\n\nexport function getPropsDeclaration<V extends CVVariantsSchema, R extends keyof V = never>(\n config: CVConfig<V, R>,\n) {\n if (typeof config?.variants === \"undefined\") {\n return {};\n }\n\n return objectFromEntries(\n getKeys(config.variants).map(\n (key) =>\n [\n key,\n {\n type: String,\n required: config.requiredProps?.includes(key as R),\n },\n ] as const,\n ),\n );\n}\n\nexport const cvWithProps: CVWithProps = ((config, options) => {\n const getClass = cv(config, options);\n const props = getPropsDeclaration(config);\n\n return { getClass, props };\n}) as CVWithProps;\n"],"mappings":"uFAwBO,SAASA,EACZC,EACF,CACE,OAAI,OAAOA,GAAQ,SAAa,IACrB,CAAC,EAGLC,EACHC,EAAQF,EAAO,QAAQ,EAAE,IACpBG,GACG,CACIA,EACA,CACI,KAAM,OACN,SAAUH,EAAO,eAAe,SAASG,CAAQ,CACrD,CACJ,CACR,CACJ,CACJ,CAEO,IAAMC,EAA4B,CAACJ,EAAQK,IAAY,CAC1D,IAAMC,EAAWC,EAAGP,EAAQK,CAAO,EAC7BG,EAAQT,EAAoBC,CAAM,EAExC,MAAO,CAAE,SAAAM,EAAU,MAAAE,CAAM,CAC7B","names":["getPropsDeclaration","config","objectFromEntries","getKeys","key","cvWithProps","options","getClass","cv","props"]}
|
package/package.json
CHANGED
|
@@ -1,57 +1,61 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
},
|
|
8
|
-
"version": "0.0.8",
|
|
9
|
-
"type": "module",
|
|
10
|
-
"main": "build/index.cjs",
|
|
11
|
-
"module": "build/index.js",
|
|
12
|
-
"types": "build/index.d.ts",
|
|
13
|
-
"exports": {
|
|
14
|
-
".": {
|
|
15
|
-
"import": {
|
|
16
|
-
"types": "./build/index.d.ts",
|
|
17
|
-
"default": "./build/index.js"
|
|
18
|
-
},
|
|
19
|
-
"require": {
|
|
20
|
-
"types": "./build/index.d.cts",
|
|
21
|
-
"default": "./build/index.cjs"
|
|
22
|
-
}
|
|
2
|
+
"name": "@yaredfall/class-variants",
|
|
3
|
+
"author": "yaredfall",
|
|
4
|
+
"private": false,
|
|
5
|
+
"publishConfig": {
|
|
6
|
+
"access": "public"
|
|
23
7
|
},
|
|
24
|
-
"
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
"
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
8
|
+
"version": "0.1.1",
|
|
9
|
+
"type": "module",
|
|
10
|
+
"files": [
|
|
11
|
+
"build"
|
|
12
|
+
],
|
|
13
|
+
"main": "build/index.cjs",
|
|
14
|
+
"module": "build/index.js",
|
|
15
|
+
"types": "build/index.d.ts",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"import": {
|
|
19
|
+
"types": "./build/index.d.ts",
|
|
20
|
+
"default": "./build/index.js"
|
|
21
|
+
},
|
|
22
|
+
"require": {
|
|
23
|
+
"types": "./build/index.d.cts",
|
|
24
|
+
"default": "./build/index.cjs"
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"./utils": {
|
|
28
|
+
"import": {
|
|
29
|
+
"types": "./build/utils.d.ts",
|
|
30
|
+
"default": "./build/utils.js"
|
|
31
|
+
},
|
|
32
|
+
"require": {
|
|
33
|
+
"types": "./build/utils.d.cts",
|
|
34
|
+
"default": "./build/utils.cjs"
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
"./vue": {
|
|
38
|
+
"import": {
|
|
39
|
+
"types": "./build/vue.d.ts",
|
|
40
|
+
"default": "./build/vue.js"
|
|
41
|
+
},
|
|
42
|
+
"require": {
|
|
43
|
+
"types": "./build/vue.d.cts",
|
|
44
|
+
"default": "./build/vue.cjs"
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
"./package.json": "./package.json"
|
|
33
48
|
},
|
|
34
|
-
"
|
|
35
|
-
|
|
36
|
-
"
|
|
37
|
-
"default": "./build/vue.js"
|
|
38
|
-
},
|
|
39
|
-
"require": {
|
|
40
|
-
"types": "./build/vue.d.cts",
|
|
41
|
-
"default": "./build/vue.cjs"
|
|
42
|
-
}
|
|
49
|
+
"scripts": {
|
|
50
|
+
"build": "tsc && tsup",
|
|
51
|
+
"prepack": "npm run build"
|
|
43
52
|
},
|
|
44
|
-
"
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
"vue": "^3.4.29"
|
|
53
|
-
},
|
|
54
|
-
"dependencies": {
|
|
55
|
-
"clsx": "^2.1.1"
|
|
56
|
-
}
|
|
53
|
+
"devDependencies": {
|
|
54
|
+
"tsup": "^8.1.0",
|
|
55
|
+
"typescript": "^5.2.2",
|
|
56
|
+
"vue": "^3.4.29"
|
|
57
|
+
},
|
|
58
|
+
"dependencies": {
|
|
59
|
+
"clsx": "^2.1.1"
|
|
60
|
+
}
|
|
57
61
|
}
|
package/build/chunk-5UWIPH6K.js
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
getEntries,
|
|
3
|
-
getKeys
|
|
4
|
-
} from "./chunk-AQSN2Y5C.js";
|
|
5
|
-
|
|
6
|
-
// src/index.ts
|
|
7
|
-
import { clsx } from "clsx";
|
|
8
|
-
var cv = (config, options = {}) => {
|
|
9
|
-
const { variants, defaultVariant, baseClass } = config;
|
|
10
|
-
const merge = options.merge ?? clsx;
|
|
11
|
-
if (variants == null) return (props) => merge(baseClass, props?.class);
|
|
12
|
-
const compoundVariants = typeof config.compoundVariants === "function" ? config.compoundVariants(variants) : config.compoundVariants;
|
|
13
|
-
return (props) => {
|
|
14
|
-
const variantClassNames = getKeys(variants).map((variant) => {
|
|
15
|
-
const variantProp = props?.[variant];
|
|
16
|
-
const defaultVariantProp = defaultVariant?.[variant];
|
|
17
|
-
const variantKey = variantProp || defaultVariantProp;
|
|
18
|
-
return variants[variant][variantKey];
|
|
19
|
-
});
|
|
20
|
-
const compoundVariantClassNames = compoundVariants?.reduce((acc, { class: cvClass, ...cvConfig }) => {
|
|
21
|
-
const shouldApplyCompoundVariant = getEntries(cvConfig).every(([cvKey, cvSelector]) => {
|
|
22
|
-
const defaultsSelector = defaultVariant?.[cvKey];
|
|
23
|
-
const propsSelector = props?.[cvKey];
|
|
24
|
-
if (cvSelector === true) return propsSelector !== void 0;
|
|
25
|
-
else if (cvSelector === false) return propsSelector === void 0;
|
|
26
|
-
const selector = propsSelector ?? defaultsSelector;
|
|
27
|
-
return Array.isArray(cvSelector) ? cvSelector.includes(selector) : selector === cvSelector;
|
|
28
|
-
});
|
|
29
|
-
if (shouldApplyCompoundVariant) acc.push(cvClass);
|
|
30
|
-
return acc;
|
|
31
|
-
}, new Array());
|
|
32
|
-
return merge(config?.baseClass, variantClassNames, compoundVariantClassNames, props?.class);
|
|
33
|
-
};
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
export {
|
|
37
|
-
cv
|
|
38
|
-
};
|
|
39
|
-
//# sourceMappingURL=chunk-5UWIPH6K.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { ClassValue, clsx } from \"clsx\";\r\nimport { MarkRequired, Prettify } from \"ts-essentials\";\r\nimport { getEntries, getKeys } from \"./utils\";\r\n\r\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\r\nexport type VariantProps<CVR extends (args?: any) => string> = Prettify<Omit<NonNullable<Exclude<Parameters<CVR>[0], void>>, \"class\">>;\r\n\r\nexport type CVVariantsSchema = Record<string, Record<string, ClassValue>>;\r\n\r\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\r\nexport type WithClassProp<T extends Record<string, any>> = T & {\r\n class?: ClassValue;\r\n};\r\n\r\nexport type CVCompoundVariantsSchema<V extends CVVariantsSchema> = WithClassProp<{\r\n [K in keyof V]?: (keyof V[K] & string) | (keyof V[K])[] | boolean\r\n}>[];\r\n\r\nexport interface CVConfig<V extends CVVariantsSchema, R extends keyof V> {\r\n baseClass?: ClassValue;\r\n variants?: V;\r\n compoundVariants?: CVCompoundVariantsSchema<NoInfer<V>> | ((variants: NoInfer<V>) => CVCompoundVariantsSchema<NoInfer<V>>);\r\n defaultVariant?: Partial<{ [K in keyof V]?: keyof V[K] }>;\r\n requiredProps?: R[];\r\n}\r\n\r\nexport type CVVariantProps<V extends CVVariantsSchema, R extends (keyof V)> =\r\nWithClassProp<MarkRequired<{ [K in keyof V]?: keyof V[K] }, R>>;\r\n\r\nexport type CVReturn<V extends CVVariantsSchema, R extends keyof V = never> = (props: Prettify<CVVariantProps<V, R>> | ([R] extends [never] ? void : never)) => string;\r\n\r\nexport interface CVOptions {\r\n merge?: (...args: ClassValue[]) => string;\r\n}\r\n\r\nexport interface CV {\r\n // eslint-disable-next-line @typescript-eslint/prefer-function-type\r\n <V extends CVVariantsSchema, R extends keyof V = never>(config: CVConfig<V, R>, options?: CVOptions): CVReturn<V, R>;\r\n}\r\n\r\nexport const cv: CV = (config, options = {}) => {\r\n const { variants, defaultVariant, baseClass } = config;\r\n\r\n const merge = options.merge ?? clsx;\r\n\r\n if (variants == null) return props => merge(baseClass, props?.class);\r\n\r\n const compoundVariants = typeof config.compoundVariants === \"function\" ? config.compoundVariants(variants) : config.compoundVariants;\r\n\r\n return (props) => {\r\n const variantClassNames = getKeys(variants).map((variant) => {\r\n const variantProp = props?.[variant];\r\n const defaultVariantProp = defaultVariant?.[variant];\r\n\r\n const variantKey = (variantProp || defaultVariantProp) as keyof (typeof variants)[typeof variant];\r\n\r\n return variants[variant][variantKey];\r\n });\r\n\r\n const compoundVariantClassNames = compoundVariants?.reduce((acc, { class: cvClass, ...cvConfig }) => {\r\n const shouldApplyCompoundVariant = getEntries(cvConfig).every(([cvKey, cvSelector]) => {\r\n const defaultsSelector = defaultVariant?.[cvKey as keyof typeof defaultVariant];\r\n const propsSelector = props?.[cvKey as keyof typeof props];\r\n\r\n if (cvSelector === true) return propsSelector !== undefined; // only when prop is defined\r\n else if (cvSelector === false) return propsSelector === undefined; // only when prop is not defined\r\n\r\n const selector = propsSelector ?? defaultsSelector;\r\n\r\n return Array.isArray(cvSelector) ? cvSelector.includes(selector as string) : selector === cvSelector;\r\n });\r\n\r\n if (shouldApplyCompoundVariant) acc.push(cvClass);\r\n return acc;\r\n }, new Array<ClassValue>());\r\n\r\n return merge(config?.baseClass, variantClassNames, compoundVariantClassNames, props?.class);\r\n };\r\n};\r\n"],"mappings":";;;;;;AAAA,SAAqB,YAAY;AAwC1B,IAAM,KAAS,CAAC,QAAQ,UAAU,CAAC,MAAM;AAC5C,QAAM,EAAE,UAAU,gBAAgB,UAAU,IAAI;AAEhD,QAAM,QAAQ,QAAQ,SAAS;AAE/B,MAAI,YAAY,KAAM,QAAO,WAAS,MAAM,WAAW,OAAO,KAAK;AAEnE,QAAM,mBAAmB,OAAO,OAAO,qBAAqB,aAAa,OAAO,iBAAiB,QAAQ,IAAI,OAAO;AAEpH,SAAO,CAAC,UAAU;AACd,UAAM,oBAAoB,QAAQ,QAAQ,EAAE,IAAI,CAAC,YAAY;AACzD,YAAM,cAAc,QAAQ,OAAO;AACnC,YAAM,qBAAqB,iBAAiB,OAAO;AAEnD,YAAM,aAAc,eAAe;AAEnC,aAAO,SAAS,OAAO,EAAE,UAAU;AAAA,IACvC,CAAC;AAED,UAAM,4BAA4B,kBAAkB,OAAO,CAAC,KAAK,EAAE,OAAO,SAAS,GAAG,SAAS,MAAM;AACjG,YAAM,6BAA6B,WAAW,QAAQ,EAAE,MAAM,CAAC,CAAC,OAAO,UAAU,MAAM;AACnF,cAAM,mBAAmB,iBAAiB,KAAoC;AAC9E,cAAM,gBAAgB,QAAQ,KAA2B;AAEzD,YAAI,eAAe,KAAM,QAAO,kBAAkB;AAAA,iBACzC,eAAe,MAAO,QAAO,kBAAkB;AAExD,cAAM,WAAW,iBAAiB;AAElC,eAAO,MAAM,QAAQ,UAAU,IAAI,WAAW,SAAS,QAAkB,IAAI,aAAa;AAAA,MAC9F,CAAC;AAED,UAAI,2BAA4B,KAAI,KAAK,OAAO;AAChD,aAAO;AAAA,IACX,GAAG,IAAI,MAAkB,CAAC;AAE1B,WAAO,MAAM,QAAQ,WAAW,mBAAmB,2BAA2B,OAAO,KAAK;AAAA,EAC9F;AACJ;","names":[]}
|
package/build/chunk-AQSN2Y5C.js
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
// src/utils.ts
|
|
2
|
-
function getKeys(obj) {
|
|
3
|
-
return Object.keys(obj);
|
|
4
|
-
}
|
|
5
|
-
function getEntries(obj) {
|
|
6
|
-
return Object.entries(obj);
|
|
7
|
-
}
|
|
8
|
-
function getVariantsOptions(variants) {
|
|
9
|
-
return Object.fromEntries(getEntries(variants).map(([cvKey, cvVal]) => [cvKey, getKeys(cvVal)]));
|
|
10
|
-
}
|
|
11
|
-
function objectFromEntries(entries) {
|
|
12
|
-
return Object.fromEntries(entries);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export {
|
|
16
|
-
getKeys,
|
|
17
|
-
getEntries,
|
|
18
|
-
getVariantsOptions,
|
|
19
|
-
objectFromEntries
|
|
20
|
-
};
|
|
21
|
-
//# sourceMappingURL=chunk-AQSN2Y5C.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/utils.ts"],"sourcesContent":["import { DeepReadonly, DeepWritable, Prettify, UnionToIntersection } from \"ts-essentials\";\r\nimport { CVVariantsSchema } from \".\";\r\n\r\nexport function getKeys<O extends Record<string, unknown>>(obj: O) {\r\n return Object.keys(obj) as (keyof O)[];\r\n}\r\n\r\ntype EntriesOf<O extends Record<string, unknown>> = { [K in keyof O]: [K, O[K]] };\r\nexport function getEntries<O extends Record<string, unknown>>(obj: O) {\r\n return Object.entries(obj) as (EntriesOf<O>[keyof EntriesOf<O>])[];\r\n}\r\n\r\nexport function getVariantsOptions<V extends CVVariantsSchema>(variants: V) {\r\n return Object.fromEntries(getEntries(variants).map(([cvKey, cvVal]) => [cvKey, getKeys(cvVal)])) as { [K in keyof V]: (keyof V[K])[] };\r\n}\r\n\r\ntype Entries = [PropertyKey, unknown][] | DeepReadonly<[PropertyKey, unknown][]>;\r\ntype UnionFromPairs<TArr extends Entries> =\r\n DeepWritable<TArr> extends (infer R)[] ? R extends [infer key, infer val] ? { [prop in key & PropertyKey]: val } : never : never;\r\n\r\ntype MergeIntersectingObjects<O> = { [K in keyof O]: O[K] };\r\n\r\nexport function objectFromEntries<E extends Entries>(entries: E) {\r\n return Object.fromEntries(entries) as Prettify<MergeIntersectingObjects<UnionToIntersection<UnionFromPairs<E>>>>;\r\n}\r\n"],"mappings":";AAGO,SAAS,QAA2C,KAAQ;AAC/D,SAAO,OAAO,KAAK,GAAG;AAC1B;AAGO,SAAS,WAA8C,KAAQ;AAClE,SAAO,OAAO,QAAQ,GAAG;AAC7B;AAEO,SAAS,mBAA+C,UAAa;AACxE,SAAO,OAAO,YAAY,WAAW,QAAQ,EAAE,IAAI,CAAC,CAAC,OAAO,KAAK,MAAM,CAAC,OAAO,QAAQ,KAAK,CAAC,CAAC,CAAC;AACnG;AAQO,SAAS,kBAAqC,SAAY;AAC7D,SAAO,OAAO,YAAY,OAAO;AACrC;","names":[]}
|
package/src/index.ts
DELETED
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
import { ClassValue, clsx } from "clsx";
|
|
2
|
-
import { MarkRequired, Prettify } from "ts-essentials";
|
|
3
|
-
import { getEntries, getKeys } from "./utils";
|
|
4
|
-
|
|
5
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
6
|
-
export type VariantProps<CVR extends (args?: any) => string> = Prettify<Omit<NonNullable<Exclude<Parameters<CVR>[0], void>>, "class">>;
|
|
7
|
-
|
|
8
|
-
export type CVVariantsSchema = Record<string, Record<string, ClassValue>>;
|
|
9
|
-
|
|
10
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
11
|
-
export type WithClassProp<T extends Record<string, any>> = T & {
|
|
12
|
-
class?: ClassValue;
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
export type CVCompoundVariantsSchema<V extends CVVariantsSchema> = WithClassProp<{
|
|
16
|
-
[K in keyof V]?: (keyof V[K] & string) | (keyof V[K])[] | boolean
|
|
17
|
-
}>[];
|
|
18
|
-
|
|
19
|
-
export interface CVConfig<V extends CVVariantsSchema, R extends keyof V> {
|
|
20
|
-
baseClass?: ClassValue;
|
|
21
|
-
variants?: V;
|
|
22
|
-
compoundVariants?: CVCompoundVariantsSchema<NoInfer<V>> | ((variants: NoInfer<V>) => CVCompoundVariantsSchema<NoInfer<V>>);
|
|
23
|
-
defaultVariant?: Partial<{ [K in keyof V]?: keyof V[K] }>;
|
|
24
|
-
requiredProps?: R[];
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export type CVVariantProps<V extends CVVariantsSchema, R extends (keyof V)> =
|
|
28
|
-
WithClassProp<MarkRequired<{ [K in keyof V]?: keyof V[K] }, R>>;
|
|
29
|
-
|
|
30
|
-
export type CVReturn<V extends CVVariantsSchema, R extends keyof V = never> = (props: Prettify<CVVariantProps<V, R>> | ([R] extends [never] ? void : never)) => string;
|
|
31
|
-
|
|
32
|
-
export interface CVOptions {
|
|
33
|
-
merge?: (...args: ClassValue[]) => string;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export interface CV {
|
|
37
|
-
// eslint-disable-next-line @typescript-eslint/prefer-function-type
|
|
38
|
-
<V extends CVVariantsSchema, R extends keyof V = never>(config: CVConfig<V, R>, options?: CVOptions): CVReturn<V, R>;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export const cv: CV = (config, options = {}) => {
|
|
42
|
-
const { variants, defaultVariant, baseClass } = config;
|
|
43
|
-
|
|
44
|
-
const merge = options.merge ?? clsx;
|
|
45
|
-
|
|
46
|
-
if (variants == null) return props => merge(baseClass, props?.class);
|
|
47
|
-
|
|
48
|
-
const compoundVariants = typeof config.compoundVariants === "function" ? config.compoundVariants(variants) : config.compoundVariants;
|
|
49
|
-
|
|
50
|
-
return (props) => {
|
|
51
|
-
const variantClassNames = getKeys(variants).map((variant) => {
|
|
52
|
-
const variantProp = props?.[variant];
|
|
53
|
-
const defaultVariantProp = defaultVariant?.[variant];
|
|
54
|
-
|
|
55
|
-
const variantKey = (variantProp || defaultVariantProp) as keyof (typeof variants)[typeof variant];
|
|
56
|
-
|
|
57
|
-
return variants[variant][variantKey];
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
const compoundVariantClassNames = compoundVariants?.reduce((acc, { class: cvClass, ...cvConfig }) => {
|
|
61
|
-
const shouldApplyCompoundVariant = getEntries(cvConfig).every(([cvKey, cvSelector]) => {
|
|
62
|
-
const defaultsSelector = defaultVariant?.[cvKey as keyof typeof defaultVariant];
|
|
63
|
-
const propsSelector = props?.[cvKey as keyof typeof props];
|
|
64
|
-
|
|
65
|
-
if (cvSelector === true) return propsSelector !== undefined; // only when prop is defined
|
|
66
|
-
else if (cvSelector === false) return propsSelector === undefined; // only when prop is not defined
|
|
67
|
-
|
|
68
|
-
const selector = propsSelector ?? defaultsSelector;
|
|
69
|
-
|
|
70
|
-
return Array.isArray(cvSelector) ? cvSelector.includes(selector as string) : selector === cvSelector;
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
if (shouldApplyCompoundVariant) acc.push(cvClass);
|
|
74
|
-
return acc;
|
|
75
|
-
}, new Array<ClassValue>());
|
|
76
|
-
|
|
77
|
-
return merge(config?.baseClass, variantClassNames, compoundVariantClassNames, props?.class);
|
|
78
|
-
};
|
|
79
|
-
};
|
package/src/utils.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { DeepReadonly, DeepWritable, Prettify, UnionToIntersection } from "ts-essentials";
|
|
2
|
-
import { CVVariantsSchema } from ".";
|
|
3
|
-
|
|
4
|
-
export function getKeys<O extends Record<string, unknown>>(obj: O) {
|
|
5
|
-
return Object.keys(obj) as (keyof O)[];
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
type EntriesOf<O extends Record<string, unknown>> = { [K in keyof O]: [K, O[K]] };
|
|
9
|
-
export function getEntries<O extends Record<string, unknown>>(obj: O) {
|
|
10
|
-
return Object.entries(obj) as (EntriesOf<O>[keyof EntriesOf<O>])[];
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export function getVariantsOptions<V extends CVVariantsSchema>(variants: V) {
|
|
14
|
-
return Object.fromEntries(getEntries(variants).map(([cvKey, cvVal]) => [cvKey, getKeys(cvVal)])) as { [K in keyof V]: (keyof V[K])[] };
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
type Entries = [PropertyKey, unknown][] | DeepReadonly<[PropertyKey, unknown][]>;
|
|
18
|
-
type UnionFromPairs<TArr extends Entries> =
|
|
19
|
-
DeepWritable<TArr> extends (infer R)[] ? R extends [infer key, infer val] ? { [prop in key & PropertyKey]: val } : never : never;
|
|
20
|
-
|
|
21
|
-
type MergeIntersectingObjects<O> = { [K in keyof O]: O[K] };
|
|
22
|
-
|
|
23
|
-
export function objectFromEntries<E extends Entries>(entries: E) {
|
|
24
|
-
return Object.fromEntries(entries) as Prettify<MergeIntersectingObjects<UnionToIntersection<UnionFromPairs<E>>>>;
|
|
25
|
-
}
|
package/src/vue.ts
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import { Prettify } from "ts-essentials";
|
|
2
|
-
import { PropType } from "vue";
|
|
3
|
-
import { CVConfig, CVOptions, CVReturn, CVVariantsSchema, cv } from ".";
|
|
4
|
-
import { getKeys, objectFromEntries } from "./utils";
|
|
5
|
-
|
|
6
|
-
type PropsDeclaration<V extends CVVariantsSchema, R extends keyof V = never> = Prettify<{ [K in keyof V]: {
|
|
7
|
-
type: PropType<keyof V[K] | (K extends R ? never : undefined)>;
|
|
8
|
-
required: K extends R ? true : false;
|
|
9
|
-
} }>;
|
|
10
|
-
export interface CVWithPropsReturn<V extends CVVariantsSchema, R extends keyof V = never> {
|
|
11
|
-
getClass: CVReturn<V, R>;
|
|
12
|
-
props: PropsDeclaration<NoInfer<V>, NoInfer<R>>;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export interface CVWithProps {
|
|
16
|
-
// eslint-disable-next-line @typescript-eslint/prefer-function-type
|
|
17
|
-
<V extends CVVariantsSchema, R extends keyof V = never>(config: CVConfig<V, R>, options?: CVOptions): CVWithPropsReturn<V, R>;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export function getPropsDeclaration<V extends CVVariantsSchema, R extends keyof V = never>(config: CVConfig<V, R>) {
|
|
21
|
-
if (typeof config?.variants === "undefined") {
|
|
22
|
-
return {};
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
return objectFromEntries(
|
|
26
|
-
getKeys(config.variants).map(key => [
|
|
27
|
-
key,
|
|
28
|
-
{
|
|
29
|
-
type: String,
|
|
30
|
-
required: config.requiredProps?.includes(key as R),
|
|
31
|
-
},
|
|
32
|
-
] as const),
|
|
33
|
-
);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export const cvWithProps: CVWithProps = ((config, options) => {
|
|
37
|
-
const getClass = cv(config, options);
|
|
38
|
-
const props = getPropsDeclaration(config);
|
|
39
|
-
|
|
40
|
-
return { getClass, props };
|
|
41
|
-
}) as CVWithProps;
|
package/tsconfig.json
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "ES2020",
|
|
4
|
-
"useDefineForClassFields": true,
|
|
5
|
-
"module": "ESNext",
|
|
6
|
-
"lib": ["ES2020", "DOM", "DOM.Iterable"],
|
|
7
|
-
"skipLibCheck": true,
|
|
8
|
-
|
|
9
|
-
/* Bundler mode */
|
|
10
|
-
"moduleResolution": "bundler",
|
|
11
|
-
"allowImportingTsExtensions": true,
|
|
12
|
-
"resolveJsonModule": true,
|
|
13
|
-
"isolatedModules": true,
|
|
14
|
-
"noEmit": true,
|
|
15
|
-
|
|
16
|
-
/* Linting */
|
|
17
|
-
"strict": true,
|
|
18
|
-
"noUnusedLocals": true,
|
|
19
|
-
"noUnusedParameters": true,
|
|
20
|
-
"noFallthroughCasesInSwitch": true
|
|
21
|
-
},
|
|
22
|
-
"include": ["src"]
|
|
23
|
-
}
|
package/tsup.config.js
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { defineConfig } from "tsup";
|
|
2
|
-
|
|
3
|
-
export default defineConfig(
|
|
4
|
-
{
|
|
5
|
-
entry: ["src/*.ts"],
|
|
6
|
-
format: ["cjs", "esm"],
|
|
7
|
-
target: ["chrome91", "firefox90", "edge91", "safari15", "ios15", "opera77"],
|
|
8
|
-
outDir: "build",
|
|
9
|
-
dts: true,
|
|
10
|
-
sourcemap: true,
|
|
11
|
-
clean: true,
|
|
12
|
-
},
|
|
13
|
-
);
|