@yaredfall/class-variants 0.0.6 → 0.0.8
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-BNE2ZOTW.js → chunk-5UWIPH6K.js} +2 -2
- package/build/chunk-5UWIPH6K.js.map +1 -0
- package/build/{chunk-3RDDXOQ3.js → chunk-AQSN2Y5C.js} +6 -2
- package/build/chunk-AQSN2Y5C.js.map +1 -0
- package/build/index.cjs.map +1 -1
- package/build/index.d.cts +9 -8
- package/build/index.d.ts +9 -8
- package/build/index.js +2 -3
- package/build/utils.cjs +7 -2
- package/build/utils.cjs.map +1 -1
- package/build/utils.d.cts +12 -4
- package/build/utils.d.ts +12 -4
- package/build/utils.js +5 -4
- package/build/vue.cjs +16 -8
- package/build/vue.cjs.map +1 -1
- package/build/vue.d.cts +13 -9
- package/build/vue.d.ts +13 -9
- package/build/vue.js +16 -10
- package/build/vue.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +11 -7
- package/src/utils.ts +13 -2
- package/src/vue.ts +22 -16
- package/build/chunk-3RDDXOQ3.js.map +0 -1
- package/build/chunk-BNE2ZOTW.js.map +0 -1
- package/build/chunk-PLDDJCW6.js +0 -49
- package/build/chunk-PLDDJCW6.js.map +0 -1
- package/build/index.test.cjs +0 -16251
- package/build/index.test.cjs.map +0 -1
- package/build/index.test.d.cts +0 -2
- package/build/index.test.d.ts +0 -2
- package/build/index.test.js +0 -14902
- package/build/index.test.js.map +0 -1
- package/build/magic-string.es-4OEJ3555.js +0 -1275
- package/build/magic-string.es-4OEJ3555.js.map +0 -1
- package/src/index.test.ts +0 -65
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
2
|
getEntries,
|
|
3
3
|
getKeys
|
|
4
|
-
} from "./chunk-
|
|
4
|
+
} from "./chunk-AQSN2Y5C.js";
|
|
5
5
|
|
|
6
6
|
// src/index.ts
|
|
7
7
|
import { clsx } from "clsx";
|
|
@@ -36,4 +36,4 @@ var cv = (config, options = {}) => {
|
|
|
36
36
|
export {
|
|
37
37
|
cv
|
|
38
38
|
};
|
|
39
|
-
//# sourceMappingURL=chunk-
|
|
39
|
+
//# sourceMappingURL=chunk-5UWIPH6K.js.map
|
|
@@ -0,0 +1 @@
|
|
|
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":[]}
|
|
@@ -8,10 +8,14 @@ function getEntries(obj) {
|
|
|
8
8
|
function getVariantsOptions(variants) {
|
|
9
9
|
return Object.fromEntries(getEntries(variants).map(([cvKey, cvVal]) => [cvKey, getKeys(cvVal)]));
|
|
10
10
|
}
|
|
11
|
+
function objectFromEntries(entries) {
|
|
12
|
+
return Object.fromEntries(entries);
|
|
13
|
+
}
|
|
11
14
|
|
|
12
15
|
export {
|
|
13
16
|
getKeys,
|
|
14
17
|
getEntries,
|
|
15
|
-
getVariantsOptions
|
|
18
|
+
getVariantsOptions,
|
|
19
|
+
objectFromEntries
|
|
16
20
|
};
|
|
17
|
-
//# sourceMappingURL=chunk-
|
|
21
|
+
//# sourceMappingURL=chunk-AQSN2Y5C.js.map
|
|
@@ -0,0 +1 @@
|
|
|
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/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 { Prettify } from \"ts-essentials\";\r\nimport { getEntries, getKeys } from \"./utils\";\r\n\r\nexport type VariantProps<
|
|
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":[]}
|
package/build/index.d.cts
CHANGED
|
@@ -1,31 +1,32 @@
|
|
|
1
1
|
import { ClassValue } from 'clsx';
|
|
2
|
-
import { Prettify } from 'ts-essentials';
|
|
2
|
+
import { Prettify, MarkRequired } from 'ts-essentials';
|
|
3
3
|
|
|
4
|
-
type VariantProps<
|
|
4
|
+
type VariantProps<CVR extends (args?: any) => string> = Prettify<Omit<NonNullable<Exclude<Parameters<CVR>[0], void>>, "class">>;
|
|
5
5
|
type CVVariantsSchema = Record<string, Record<string, ClassValue>>;
|
|
6
|
-
type WithClassProp<T extends Record<string,
|
|
6
|
+
type WithClassProp<T extends Record<string, any>> = T & {
|
|
7
7
|
class?: ClassValue;
|
|
8
8
|
};
|
|
9
9
|
type CVCompoundVariantsSchema<V extends CVVariantsSchema> = WithClassProp<{
|
|
10
10
|
[K in keyof V]?: (keyof V[K] & string) | (keyof V[K])[] | boolean;
|
|
11
11
|
}>[];
|
|
12
|
-
interface CVConfig<V extends CVVariantsSchema> {
|
|
12
|
+
interface CVConfig<V extends CVVariantsSchema, R extends keyof V> {
|
|
13
13
|
baseClass?: ClassValue;
|
|
14
14
|
variants?: V;
|
|
15
15
|
compoundVariants?: CVCompoundVariantsSchema<NoInfer<V>> | ((variants: NoInfer<V>) => CVCompoundVariantsSchema<NoInfer<V>>);
|
|
16
16
|
defaultVariant?: Partial<{
|
|
17
17
|
[K in keyof V]?: keyof V[K];
|
|
18
18
|
}>;
|
|
19
|
+
requiredProps?: R[];
|
|
19
20
|
}
|
|
20
|
-
type CVVariantProps<V extends CVVariantsSchema> = WithClassProp<{
|
|
21
|
+
type CVVariantProps<V extends CVVariantsSchema, R extends (keyof V)> = WithClassProp<MarkRequired<{
|
|
21
22
|
[K in keyof V]?: keyof V[K];
|
|
22
|
-
}
|
|
23
|
-
type CVReturn<V extends CVVariantsSchema> = (props
|
|
23
|
+
}, R>>;
|
|
24
|
+
type CVReturn<V extends CVVariantsSchema, R extends keyof V = never> = (props: Prettify<CVVariantProps<V, R>> | ([R] extends [never] ? void : never)) => string;
|
|
24
25
|
interface CVOptions {
|
|
25
26
|
merge?: (...args: ClassValue[]) => string;
|
|
26
27
|
}
|
|
27
28
|
interface CV {
|
|
28
|
-
<V extends CVVariantsSchema>(config: CVConfig<V>, options?: CVOptions): CVReturn<V>;
|
|
29
|
+
<V extends CVVariantsSchema, R extends keyof V = never>(config: CVConfig<V, R>, options?: CVOptions): CVReturn<V, R>;
|
|
29
30
|
}
|
|
30
31
|
declare const cv: CV;
|
|
31
32
|
|
package/build/index.d.ts
CHANGED
|
@@ -1,31 +1,32 @@
|
|
|
1
1
|
import { ClassValue } from 'clsx';
|
|
2
|
-
import { Prettify } from 'ts-essentials';
|
|
2
|
+
import { Prettify, MarkRequired } from 'ts-essentials';
|
|
3
3
|
|
|
4
|
-
type VariantProps<
|
|
4
|
+
type VariantProps<CVR extends (args?: any) => string> = Prettify<Omit<NonNullable<Exclude<Parameters<CVR>[0], void>>, "class">>;
|
|
5
5
|
type CVVariantsSchema = Record<string, Record<string, ClassValue>>;
|
|
6
|
-
type WithClassProp<T extends Record<string,
|
|
6
|
+
type WithClassProp<T extends Record<string, any>> = T & {
|
|
7
7
|
class?: ClassValue;
|
|
8
8
|
};
|
|
9
9
|
type CVCompoundVariantsSchema<V extends CVVariantsSchema> = WithClassProp<{
|
|
10
10
|
[K in keyof V]?: (keyof V[K] & string) | (keyof V[K])[] | boolean;
|
|
11
11
|
}>[];
|
|
12
|
-
interface CVConfig<V extends CVVariantsSchema> {
|
|
12
|
+
interface CVConfig<V extends CVVariantsSchema, R extends keyof V> {
|
|
13
13
|
baseClass?: ClassValue;
|
|
14
14
|
variants?: V;
|
|
15
15
|
compoundVariants?: CVCompoundVariantsSchema<NoInfer<V>> | ((variants: NoInfer<V>) => CVCompoundVariantsSchema<NoInfer<V>>);
|
|
16
16
|
defaultVariant?: Partial<{
|
|
17
17
|
[K in keyof V]?: keyof V[K];
|
|
18
18
|
}>;
|
|
19
|
+
requiredProps?: R[];
|
|
19
20
|
}
|
|
20
|
-
type CVVariantProps<V extends CVVariantsSchema> = WithClassProp<{
|
|
21
|
+
type CVVariantProps<V extends CVVariantsSchema, R extends (keyof V)> = WithClassProp<MarkRequired<{
|
|
21
22
|
[K in keyof V]?: keyof V[K];
|
|
22
|
-
}
|
|
23
|
-
type CVReturn<V extends CVVariantsSchema> = (props
|
|
23
|
+
}, R>>;
|
|
24
|
+
type CVReturn<V extends CVVariantsSchema, R extends keyof V = never> = (props: Prettify<CVVariantProps<V, R>> | ([R] extends [never] ? void : never)) => string;
|
|
24
25
|
interface CVOptions {
|
|
25
26
|
merge?: (...args: ClassValue[]) => string;
|
|
26
27
|
}
|
|
27
28
|
interface CV {
|
|
28
|
-
<V extends CVVariantsSchema>(config: CVConfig<V>, options?: CVOptions): CVReturn<V>;
|
|
29
|
+
<V extends CVVariantsSchema, R extends keyof V = never>(config: CVConfig<V, R>, options?: CVOptions): CVReturn<V, R>;
|
|
29
30
|
}
|
|
30
31
|
declare const cv: CV;
|
|
31
32
|
|
package/build/index.js
CHANGED
package/build/utils.cjs
CHANGED
|
@@ -22,7 +22,8 @@ var utils_exports = {};
|
|
|
22
22
|
__export(utils_exports, {
|
|
23
23
|
getEntries: () => getEntries,
|
|
24
24
|
getKeys: () => getKeys,
|
|
25
|
-
getVariantsOptions: () => getVariantsOptions
|
|
25
|
+
getVariantsOptions: () => getVariantsOptions,
|
|
26
|
+
objectFromEntries: () => objectFromEntries
|
|
26
27
|
});
|
|
27
28
|
module.exports = __toCommonJS(utils_exports);
|
|
28
29
|
function getKeys(obj) {
|
|
@@ -34,10 +35,14 @@ function getEntries(obj) {
|
|
|
34
35
|
function getVariantsOptions(variants) {
|
|
35
36
|
return Object.fromEntries(getEntries(variants).map(([cvKey, cvVal]) => [cvKey, getKeys(cvVal)]));
|
|
36
37
|
}
|
|
38
|
+
function objectFromEntries(entries) {
|
|
39
|
+
return Object.fromEntries(entries);
|
|
40
|
+
}
|
|
37
41
|
// Annotate the CommonJS export names for ESM import in node:
|
|
38
42
|
0 && (module.exports = {
|
|
39
43
|
getEntries,
|
|
40
44
|
getKeys,
|
|
41
|
-
getVariantsOptions
|
|
45
|
+
getVariantsOptions,
|
|
46
|
+
objectFromEntries
|
|
42
47
|
});
|
|
43
48
|
//# sourceMappingURL=utils.cjs.map
|
package/build/utils.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/utils.ts"],"sourcesContent":["import { 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
|
|
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":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;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/build/utils.d.cts
CHANGED
|
@@ -1,12 +1,20 @@
|
|
|
1
|
+
import { Prettify, UnionToIntersection, DeepReadonly, DeepWritable } from 'ts-essentials';
|
|
1
2
|
import { CVVariantsSchema } from './index.cjs';
|
|
2
3
|
import 'clsx';
|
|
3
|
-
import 'ts-essentials';
|
|
4
4
|
|
|
5
5
|
declare function getKeys<O extends Record<string, unknown>>(obj: O): (keyof O)[];
|
|
6
|
-
type
|
|
6
|
+
type EntriesOf<O extends Record<string, unknown>> = {
|
|
7
7
|
[K in keyof O]: [K, O[K]];
|
|
8
8
|
};
|
|
9
|
-
declare function getEntries<O extends Record<string, unknown>>(obj: O): (
|
|
9
|
+
declare function getEntries<O extends Record<string, unknown>>(obj: O): (EntriesOf<O>[keyof EntriesOf<O>])[];
|
|
10
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>>>>;
|
|
11
19
|
|
|
12
|
-
export { getEntries, getKeys, getVariantsOptions };
|
|
20
|
+
export { getEntries, getKeys, getVariantsOptions, objectFromEntries };
|
package/build/utils.d.ts
CHANGED
|
@@ -1,12 +1,20 @@
|
|
|
1
|
+
import { Prettify, UnionToIntersection, DeepReadonly, DeepWritable } from 'ts-essentials';
|
|
1
2
|
import { CVVariantsSchema } from './index.js';
|
|
2
3
|
import 'clsx';
|
|
3
|
-
import 'ts-essentials';
|
|
4
4
|
|
|
5
5
|
declare function getKeys<O extends Record<string, unknown>>(obj: O): (keyof O)[];
|
|
6
|
-
type
|
|
6
|
+
type EntriesOf<O extends Record<string, unknown>> = {
|
|
7
7
|
[K in keyof O]: [K, O[K]];
|
|
8
8
|
};
|
|
9
|
-
declare function getEntries<O extends Record<string, unknown>>(obj: O): (
|
|
9
|
+
declare function getEntries<O extends Record<string, unknown>>(obj: O): (EntriesOf<O>[keyof EntriesOf<O>])[];
|
|
10
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>>>>;
|
|
11
19
|
|
|
12
|
-
export { getEntries, getKeys, getVariantsOptions };
|
|
20
|
+
export { getEntries, getKeys, getVariantsOptions, objectFromEntries };
|
package/build/utils.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import {
|
|
2
2
|
getEntries,
|
|
3
3
|
getKeys,
|
|
4
|
-
getVariantsOptions
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
getVariantsOptions,
|
|
5
|
+
objectFromEntries
|
|
6
|
+
} from "./chunk-AQSN2Y5C.js";
|
|
7
7
|
export {
|
|
8
8
|
getEntries,
|
|
9
9
|
getKeys,
|
|
10
|
-
getVariantsOptions
|
|
10
|
+
getVariantsOptions,
|
|
11
|
+
objectFromEntries
|
|
11
12
|
};
|
|
12
13
|
//# sourceMappingURL=utils.js.map
|
package/build/vue.cjs
CHANGED
|
@@ -20,7 +20,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/vue.ts
|
|
21
21
|
var vue_exports = {};
|
|
22
22
|
__export(vue_exports, {
|
|
23
|
-
cvWithProps: () => cvWithProps
|
|
23
|
+
cvWithProps: () => cvWithProps,
|
|
24
|
+
getPropsDeclaration: () => getPropsDeclaration
|
|
24
25
|
});
|
|
25
26
|
module.exports = __toCommonJS(vue_exports);
|
|
26
27
|
|
|
@@ -34,6 +35,9 @@ function getKeys(obj) {
|
|
|
34
35
|
function getEntries(obj) {
|
|
35
36
|
return Object.entries(obj);
|
|
36
37
|
}
|
|
38
|
+
function objectFromEntries(entries) {
|
|
39
|
+
return Object.fromEntries(entries);
|
|
40
|
+
}
|
|
37
41
|
|
|
38
42
|
// src/index.ts
|
|
39
43
|
var cv = (config, options = {}) => {
|
|
@@ -65,24 +69,28 @@ var cv = (config, options = {}) => {
|
|
|
65
69
|
};
|
|
66
70
|
|
|
67
71
|
// src/vue.ts
|
|
68
|
-
|
|
69
|
-
const getClass = cv(config, options);
|
|
72
|
+
function getPropsDeclaration(config) {
|
|
70
73
|
if (typeof config?.variants === "undefined") {
|
|
71
|
-
return {
|
|
74
|
+
return {};
|
|
72
75
|
}
|
|
73
|
-
|
|
74
|
-
|
|
76
|
+
return objectFromEntries(
|
|
77
|
+
getKeys(config.variants).map((key) => [
|
|
75
78
|
key,
|
|
76
79
|
{
|
|
77
80
|
type: String,
|
|
78
|
-
|
|
81
|
+
required: config.requiredProps?.includes(key)
|
|
79
82
|
}
|
|
80
83
|
])
|
|
81
84
|
);
|
|
85
|
+
}
|
|
86
|
+
var cvWithProps = (config, options) => {
|
|
87
|
+
const getClass = cv(config, options);
|
|
88
|
+
const props = getPropsDeclaration(config);
|
|
82
89
|
return { getClass, props };
|
|
83
90
|
};
|
|
84
91
|
// Annotate the CommonJS export names for ESM import in node:
|
|
85
92
|
0 && (module.exports = {
|
|
86
|
-
cvWithProps
|
|
93
|
+
cvWithProps,
|
|
94
|
+
getPropsDeclaration
|
|
87
95
|
});
|
|
88
96
|
//# 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 {
|
|
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":[]}
|
package/build/vue.d.cts
CHANGED
|
@@ -1,18 +1,22 @@
|
|
|
1
|
-
import { CVVariantsSchema, CVReturn, CVConfig, CVOptions } from './index.cjs';
|
|
2
1
|
import { Prettify } from 'ts-essentials';
|
|
3
|
-
import {
|
|
2
|
+
import { PropType } from 'vue';
|
|
3
|
+
import { CVVariantsSchema, CVReturn, CVConfig, CVOptions } from './index.cjs';
|
|
4
4
|
import 'clsx';
|
|
5
5
|
|
|
6
|
-
type PropsDeclaration<V extends CVVariantsSchema> = Prettify<{
|
|
7
|
-
[K in keyof V]:
|
|
6
|
+
type PropsDeclaration<V extends CVVariantsSchema, R extends keyof V = never> = Prettify<{
|
|
7
|
+
[K in keyof V]: {
|
|
8
|
+
type: PropType<keyof V[K] | (K extends R ? never : undefined)>;
|
|
9
|
+
required: K extends R ? true : false;
|
|
10
|
+
};
|
|
8
11
|
}>;
|
|
9
|
-
interface CVWithPropsReturn<V extends CVVariantsSchema> {
|
|
10
|
-
getClass: CVReturn<V>;
|
|
11
|
-
props: PropsDeclaration<V
|
|
12
|
+
interface CVWithPropsReturn<V extends CVVariantsSchema, R extends keyof V = never> {
|
|
13
|
+
getClass: CVReturn<V, R>;
|
|
14
|
+
props: PropsDeclaration<NoInfer<V>, NoInfer<R>>;
|
|
12
15
|
}
|
|
13
16
|
interface CVWithProps {
|
|
14
|
-
<V extends CVVariantsSchema>(config: CVConfig<V>, options?: CVOptions): CVWithPropsReturn<V>;
|
|
17
|
+
<V extends CVVariantsSchema, R extends keyof V = never>(config: CVConfig<V, R>, options?: CVOptions): CVWithPropsReturn<V, R>;
|
|
15
18
|
}
|
|
19
|
+
declare function getPropsDeclaration<V extends CVVariantsSchema, R extends keyof V = never>(config: CVConfig<V, R>): {};
|
|
16
20
|
declare const cvWithProps: CVWithProps;
|
|
17
21
|
|
|
18
|
-
export { type CVWithProps, type CVWithPropsReturn, cvWithProps };
|
|
22
|
+
export { type CVWithProps, type CVWithPropsReturn, cvWithProps, getPropsDeclaration };
|
package/build/vue.d.ts
CHANGED
|
@@ -1,18 +1,22 @@
|
|
|
1
|
-
import { CVVariantsSchema, CVReturn, CVConfig, CVOptions } from './index.js';
|
|
2
1
|
import { Prettify } from 'ts-essentials';
|
|
3
|
-
import {
|
|
2
|
+
import { PropType } from 'vue';
|
|
3
|
+
import { CVVariantsSchema, CVReturn, CVConfig, CVOptions } from './index.js';
|
|
4
4
|
import 'clsx';
|
|
5
5
|
|
|
6
|
-
type PropsDeclaration<V extends CVVariantsSchema> = Prettify<{
|
|
7
|
-
[K in keyof V]:
|
|
6
|
+
type PropsDeclaration<V extends CVVariantsSchema, R extends keyof V = never> = Prettify<{
|
|
7
|
+
[K in keyof V]: {
|
|
8
|
+
type: PropType<keyof V[K] | (K extends R ? never : undefined)>;
|
|
9
|
+
required: K extends R ? true : false;
|
|
10
|
+
};
|
|
8
11
|
}>;
|
|
9
|
-
interface CVWithPropsReturn<V extends CVVariantsSchema> {
|
|
10
|
-
getClass: CVReturn<V>;
|
|
11
|
-
props: PropsDeclaration<V
|
|
12
|
+
interface CVWithPropsReturn<V extends CVVariantsSchema, R extends keyof V = never> {
|
|
13
|
+
getClass: CVReturn<V, R>;
|
|
14
|
+
props: PropsDeclaration<NoInfer<V>, NoInfer<R>>;
|
|
12
15
|
}
|
|
13
16
|
interface CVWithProps {
|
|
14
|
-
<V extends CVVariantsSchema>(config: CVConfig<V>, options?: CVOptions): CVWithPropsReturn<V>;
|
|
17
|
+
<V extends CVVariantsSchema, R extends keyof V = never>(config: CVConfig<V, R>, options?: CVOptions): CVWithPropsReturn<V, R>;
|
|
15
18
|
}
|
|
19
|
+
declare function getPropsDeclaration<V extends CVVariantsSchema, R extends keyof V = never>(config: CVConfig<V, R>): {};
|
|
16
20
|
declare const cvWithProps: CVWithProps;
|
|
17
21
|
|
|
18
|
-
export { type CVWithProps, type CVWithPropsReturn, cvWithProps };
|
|
22
|
+
export { type CVWithProps, type CVWithPropsReturn, cvWithProps, getPropsDeclaration };
|
package/build/vue.js
CHANGED
|
@@ -1,27 +1,33 @@
|
|
|
1
1
|
import {
|
|
2
2
|
cv
|
|
3
|
-
} from "./chunk-
|
|
4
|
-
import
|
|
5
|
-
|
|
3
|
+
} from "./chunk-5UWIPH6K.js";
|
|
4
|
+
import {
|
|
5
|
+
getKeys,
|
|
6
|
+
objectFromEntries
|
|
7
|
+
} from "./chunk-AQSN2Y5C.js";
|
|
6
8
|
|
|
7
9
|
// src/vue.ts
|
|
8
|
-
|
|
9
|
-
const getClass = cv(config, options);
|
|
10
|
+
function getPropsDeclaration(config) {
|
|
10
11
|
if (typeof config?.variants === "undefined") {
|
|
11
|
-
return {
|
|
12
|
+
return {};
|
|
12
13
|
}
|
|
13
|
-
|
|
14
|
-
|
|
14
|
+
return objectFromEntries(
|
|
15
|
+
getKeys(config.variants).map((key) => [
|
|
15
16
|
key,
|
|
16
17
|
{
|
|
17
18
|
type: String,
|
|
18
|
-
|
|
19
|
+
required: config.requiredProps?.includes(key)
|
|
19
20
|
}
|
|
20
21
|
])
|
|
21
22
|
);
|
|
23
|
+
}
|
|
24
|
+
var cvWithProps = (config, options) => {
|
|
25
|
+
const getClass = cv(config, options);
|
|
26
|
+
const props = getPropsDeclaration(config);
|
|
22
27
|
return { getClass, props };
|
|
23
28
|
};
|
|
24
29
|
export {
|
|
25
|
-
cvWithProps
|
|
30
|
+
cvWithProps,
|
|
31
|
+
getPropsDeclaration
|
|
26
32
|
};
|
|
27
33
|
//# sourceMappingURL=vue.js.map
|
package/build/vue.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/vue.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"sources":["../src/vue.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"],"mappings":";;;;;;;;;AAmBO,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":[]}
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import { ClassValue, clsx } from "clsx";
|
|
2
|
-
import { Prettify } from "ts-essentials";
|
|
2
|
+
import { MarkRequired, Prettify } from "ts-essentials";
|
|
3
3
|
import { getEntries, getKeys } from "./utils";
|
|
4
4
|
|
|
5
|
-
|
|
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">>;
|
|
6
7
|
|
|
7
8
|
export type CVVariantsSchema = Record<string, Record<string, ClassValue>>;
|
|
8
9
|
|
|
9
|
-
|
|
10
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
11
|
+
export type WithClassProp<T extends Record<string, any>> = T & {
|
|
10
12
|
class?: ClassValue;
|
|
11
13
|
};
|
|
12
14
|
|
|
@@ -14,16 +16,18 @@ export type CVCompoundVariantsSchema<V extends CVVariantsSchema> = WithClassProp
|
|
|
14
16
|
[K in keyof V]?: (keyof V[K] & string) | (keyof V[K])[] | boolean
|
|
15
17
|
}>[];
|
|
16
18
|
|
|
17
|
-
export interface CVConfig<V extends CVVariantsSchema> {
|
|
19
|
+
export interface CVConfig<V extends CVVariantsSchema, R extends keyof V> {
|
|
18
20
|
baseClass?: ClassValue;
|
|
19
21
|
variants?: V;
|
|
20
22
|
compoundVariants?: CVCompoundVariantsSchema<NoInfer<V>> | ((variants: NoInfer<V>) => CVCompoundVariantsSchema<NoInfer<V>>);
|
|
21
23
|
defaultVariant?: Partial<{ [K in keyof V]?: keyof V[K] }>;
|
|
24
|
+
requiredProps?: R[];
|
|
22
25
|
}
|
|
23
26
|
|
|
24
|
-
export type CVVariantProps<V extends CVVariantsSchema
|
|
27
|
+
export type CVVariantProps<V extends CVVariantsSchema, R extends (keyof V)> =
|
|
28
|
+
WithClassProp<MarkRequired<{ [K in keyof V]?: keyof V[K] }, R>>;
|
|
25
29
|
|
|
26
|
-
export type CVReturn<V extends CVVariantsSchema> = (props
|
|
30
|
+
export type CVReturn<V extends CVVariantsSchema, R extends keyof V = never> = (props: Prettify<CVVariantProps<V, R>> | ([R] extends [never] ? void : never)) => string;
|
|
27
31
|
|
|
28
32
|
export interface CVOptions {
|
|
29
33
|
merge?: (...args: ClassValue[]) => string;
|
|
@@ -31,7 +35,7 @@ export interface CVOptions {
|
|
|
31
35
|
|
|
32
36
|
export interface CV {
|
|
33
37
|
// eslint-disable-next-line @typescript-eslint/prefer-function-type
|
|
34
|
-
<V extends CVVariantsSchema>(config: CVConfig<V>, options?: CVOptions): CVReturn<V>;
|
|
38
|
+
<V extends CVVariantsSchema, R extends keyof V = never>(config: CVConfig<V, R>, options?: CVOptions): CVReturn<V, R>;
|
|
35
39
|
}
|
|
36
40
|
|
|
37
41
|
export const cv: CV = (config, options = {}) => {
|
package/src/utils.ts
CHANGED
|
@@ -1,14 +1,25 @@
|
|
|
1
|
+
import { DeepReadonly, DeepWritable, Prettify, UnionToIntersection } from "ts-essentials";
|
|
1
2
|
import { CVVariantsSchema } from ".";
|
|
2
3
|
|
|
3
4
|
export function getKeys<O extends Record<string, unknown>>(obj: O) {
|
|
4
5
|
return Object.keys(obj) as (keyof O)[];
|
|
5
6
|
}
|
|
6
7
|
|
|
7
|
-
type
|
|
8
|
+
type EntriesOf<O extends Record<string, unknown>> = { [K in keyof O]: [K, O[K]] };
|
|
8
9
|
export function getEntries<O extends Record<string, unknown>>(obj: O) {
|
|
9
|
-
return Object.entries(obj) as (
|
|
10
|
+
return Object.entries(obj) as (EntriesOf<O>[keyof EntriesOf<O>])[];
|
|
10
11
|
}
|
|
11
12
|
|
|
12
13
|
export function getVariantsOptions<V extends CVVariantsSchema>(variants: V) {
|
|
13
14
|
return Object.fromEntries(getEntries(variants).map(([cvKey, cvVal]) => [cvKey, getKeys(cvVal)])) as { [K in keyof V]: (keyof V[K])[] };
|
|
14
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
|
+
}
|