cva 1.0.0-beta.7 → 1.0.0-beta.9
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/LICENSE +1 -1
- package/dist/config.cjs +3 -0
- package/dist/config.d.cts +2 -0
- package/dist/config.d.mts +2 -0
- package/dist/config.mjs +2 -0
- package/dist/core-BMF1ydiV.d.cts +188 -0
- package/dist/core-BMF1ydiV.d.mts +188 -0
- package/dist/core-DPc9m0ph.mjs +99 -0
- package/dist/core-DPc9m0ph.mjs.map +1 -0
- package/dist/core-iwLcNxRW.cjs +104 -0
- package/dist/core-iwLcNxRW.cjs.map +1 -0
- package/dist/index.cjs +20 -117
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +25 -115
- package/dist/index.d.mts +25 -115
- package/dist/index.mjs +20 -117
- package/dist/index.mjs.map +1 -1
- package/dist/utils.cjs +31 -0
- package/dist/utils.cjs.map +1 -0
- package/dist/utils.d.cts +6 -0
- package/dist/utils.d.mts +6 -0
- package/dist/utils.mjs +30 -0
- package/dist/utils.mjs.map +1 -0
- package/package.json +31 -3
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
//#region src/core.ts
|
|
2
|
+
const falsyToString = (value) => typeof value === "boolean" ? `${value}` : value === 0 ? "0" : value;
|
|
3
|
+
const emptyClassNames = [];
|
|
4
|
+
const defineConfig = ((options) => {
|
|
5
|
+
const cx = (...inputs) => {
|
|
6
|
+
var _options$hooks$cxDon, _options$hooks, _options$hooks2;
|
|
7
|
+
const className = options.cx(...inputs.filter((input) => input !== void 0));
|
|
8
|
+
const hook = (_options$hooks$cxDon = (_options$hooks = options.hooks) === null || _options$hooks === void 0 ? void 0 : _options$hooks["cx:done"]) !== null && _options$hooks$cxDon !== void 0 ? _options$hooks$cxDon : (_options$hooks2 = options.hooks) === null || _options$hooks2 === void 0 ? void 0 : _options$hooks2.onComplete;
|
|
9
|
+
return hook ? hook(className) : className;
|
|
10
|
+
};
|
|
11
|
+
const cva = ((config) => {
|
|
12
|
+
const components = (config === null || config === void 0 ? void 0 : config.composes) == null ? [] : Array.isArray(config.composes) ? config.composes : [config.composes];
|
|
13
|
+
const mergeVariants = (acc, variants) => {
|
|
14
|
+
if (!variants) return acc;
|
|
15
|
+
const merged = { ...acc };
|
|
16
|
+
for (const key of Object.keys(variants)) merged[key] = {
|
|
17
|
+
...merged[key],
|
|
18
|
+
...variants[key]
|
|
19
|
+
};
|
|
20
|
+
return merged;
|
|
21
|
+
};
|
|
22
|
+
const mergedVariants = mergeVariants(components.reduce((acc, component) => {
|
|
23
|
+
var _component$config;
|
|
24
|
+
return mergeVariants(acc, (_component$config = component.config) === null || _component$config === void 0 ? void 0 : _component$config.variants);
|
|
25
|
+
}, {}), config === null || config === void 0 ? void 0 : config.variants);
|
|
26
|
+
const mergedDefaultVariants = {
|
|
27
|
+
...components.reduce((acc, component) => {
|
|
28
|
+
var _component$config2;
|
|
29
|
+
return {
|
|
30
|
+
...acc,
|
|
31
|
+
...(_component$config2 = component.config) === null || _component$config2 === void 0 ? void 0 : _component$config2.defaultVariants
|
|
32
|
+
};
|
|
33
|
+
}, {}),
|
|
34
|
+
...config === null || config === void 0 ? void 0 : config.defaultVariants
|
|
35
|
+
};
|
|
36
|
+
const component = (props) => {
|
|
37
|
+
var _config$variants, _config$compoundVaria, _config$compoundVaria2;
|
|
38
|
+
const definedPropsWithoutClass = components.length || (config === null || config === void 0 ? void 0 : config.compoundVariants) ? Object.fromEntries(Object.entries(props || {}).filter(([key, value]) => key !== "class" && key !== "className" && typeof value !== "undefined")) : {};
|
|
39
|
+
const getComposedClassNames = components.length ? components.map((component) => component({
|
|
40
|
+
...mergedDefaultVariants,
|
|
41
|
+
...definedPropsWithoutClass
|
|
42
|
+
})) : emptyClassNames;
|
|
43
|
+
if (!(config === null || config === void 0 ? void 0 : config.variants) && !(config === null || config === void 0 ? void 0 : config.compoundVariants)) return getComposedClassNames.length ? cx(...getComposedClassNames, config === null || config === void 0 ? void 0 : config.base, props === null || props === void 0 ? void 0 : props.class, props === null || props === void 0 ? void 0 : props.className) : cx(config === null || config === void 0 ? void 0 : config.base, props === null || props === void 0 ? void 0 : props.class, props === null || props === void 0 ? void 0 : props.className);
|
|
44
|
+
const variants = (_config$variants = config.variants) !== null && _config$variants !== void 0 ? _config$variants : {};
|
|
45
|
+
const getVariantClassNames = Object.keys(variants).map((variant) => {
|
|
46
|
+
const variantProp = props === null || props === void 0 ? void 0 : props[variant];
|
|
47
|
+
const defaultVariantProp = mergedDefaultVariants[variant];
|
|
48
|
+
const variantKey = falsyToString(variantProp) || falsyToString(defaultVariantProp);
|
|
49
|
+
return variants[variant][variantKey];
|
|
50
|
+
});
|
|
51
|
+
const defaultsAndProps = {
|
|
52
|
+
...mergedDefaultVariants,
|
|
53
|
+
...definedPropsWithoutClass
|
|
54
|
+
};
|
|
55
|
+
const getCompoundVariantClassNames = (_config$compoundVaria = config === null || config === void 0 || (_config$compoundVaria2 = config.compoundVariants) === null || _config$compoundVaria2 === void 0 ? void 0 : _config$compoundVaria2.reduce((acc, { class: cvClass, className: cvClassName, ...cvConfig }) => Object.entries(cvConfig).every(([cvKey, cvSelector]) => {
|
|
56
|
+
const selector = defaultsAndProps[cvKey];
|
|
57
|
+
return Array.isArray(cvSelector) ? cvSelector.includes(selector) : selector === cvSelector;
|
|
58
|
+
}) ? [
|
|
59
|
+
...acc,
|
|
60
|
+
cvClass,
|
|
61
|
+
cvClassName
|
|
62
|
+
] : acc, [])) !== null && _config$compoundVaria !== void 0 ? _config$compoundVaria : emptyClassNames;
|
|
63
|
+
return cx(...getComposedClassNames, config === null || config === void 0 ? void 0 : config.base, ...getVariantClassNames, ...getCompoundVariantClassNames, props === null || props === void 0 ? void 0 : props.class, props === null || props === void 0 ? void 0 : props.className);
|
|
64
|
+
};
|
|
65
|
+
component.config = {
|
|
66
|
+
...config,
|
|
67
|
+
variants: mergedVariants,
|
|
68
|
+
defaultVariants: mergedDefaultVariants
|
|
69
|
+
};
|
|
70
|
+
return component;
|
|
71
|
+
});
|
|
72
|
+
const compose = (...components) => {
|
|
73
|
+
const composedComponents = components;
|
|
74
|
+
const config = composedComponents.reduce((acc, { config }) => {
|
|
75
|
+
Object.entries(config || {}).forEach(([key, value]) => {
|
|
76
|
+
acc[key] = typeof value === "object" && value !== null && !Array.isArray(value) ? {
|
|
77
|
+
...acc[key],
|
|
78
|
+
...value
|
|
79
|
+
} : value;
|
|
80
|
+
});
|
|
81
|
+
return acc;
|
|
82
|
+
}, {});
|
|
83
|
+
const component = (props) => {
|
|
84
|
+
const propsWithoutClass = Object.fromEntries(Object.entries(props || {}).filter(([key]) => !["class", "className"].includes(key)));
|
|
85
|
+
return cx(...composedComponents.map((component) => component(propsWithoutClass)), props === null || props === void 0 ? void 0 : props.class, props === null || props === void 0 ? void 0 : props.className);
|
|
86
|
+
};
|
|
87
|
+
component.config = config;
|
|
88
|
+
return component;
|
|
89
|
+
};
|
|
90
|
+
return {
|
|
91
|
+
compose,
|
|
92
|
+
cva,
|
|
93
|
+
cx
|
|
94
|
+
};
|
|
95
|
+
});
|
|
96
|
+
//#endregion
|
|
97
|
+
Object.defineProperty(exports, "defineConfig", {
|
|
98
|
+
enumerable: true,
|
|
99
|
+
get: function() {
|
|
100
|
+
return defineConfig;
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
//# sourceMappingURL=core-iwLcNxRW.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"core-iwLcNxRW.cjs","names":[],"sources":["../src/core.ts"],"sourcesContent":["/**\n * Copyright 2022-present Joe Bell. All rights reserved.\n *\n * This file is licensed to you under the Apache License, Version 2.0\n * (the \"License\"); you may not use this file except in compliance with the\n * License. You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT\n * WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or implied. See the\n * License for the specific language governing permissions and limitations under\n * the License.\n */\n\n/* Types\n ============================================ */\n\n/* ClassValue\n ---------------------------------- */\n\n// Mirrors clsx's `ClassValue`; local to keep `core` dependency-free (TS2742).\n\nexport type ClassValue =\n | ClassArray\n | ClassDictionary\n | string\n | number\n | bigint\n | null\n | boolean\n | undefined;\nexport type ClassDictionary = Record<string, any>;\nexport type ClassArray = ClassValue[];\n\n/* CXInput\n ---------------------------------- */\n\n/**\n * Any function usable as a `cx` concatenator.\n */\nexport type AnyCX = (...inputs: any[]) => string;\n\n// Infer directly from a readonly rest pattern instead of `Parameters<T>[number]`:\n// the latter is `never` for zero-argument callbacks and loses readonly rest\n// element types.\ntype CXInputs<TCX extends AnyCX> = TCX extends (\n ...inputs: readonly [...infer Inputs]\n) => string\n ? Inputs\n : never;\ntype CXInputElement<TCX extends AnyCX> = CXInputs<TCX>[number];\n\n// cva may call `cx` with no values, or with any number of authored values and\n// composed component strings. A constant callback is safe; every other\n// callback must accept arbitrary lists of its own grammar plus strings.\ntype CXHasSafeArity<TCX extends AnyCX> =\n CXInputs<TCX> extends infer Inputs\n ? Inputs extends readonly unknown[]\n ? Inputs extends []\n ? true\n : [] extends Inputs\n ? number extends Inputs[\"length\"]\n ? true\n : false\n : false\n : false\n : false;\ntype CXConstraint<TCX extends AnyCX> =\n false extends CXHasSafeArity<TCX>\n ? \"cva's cx must accept zero arguments and an unbounded rest parameter.\"\n : [TCX] extends [(...inputs: (string | CXInput<TCX>)[]) => string]\n ? unknown\n : \"cva's cx must accept its inferred class values and composed strings.\";\n\n/**\n * The class value type a concatenator accepts, inferred from its\n * parameters — `defineConfig` uses this to type the authoring surface\n * (`base`, variant values, `class`/`className`) against the configured\n * concatenator's own input grammar.\n */\nexport type CXInput<TCX extends AnyCX> =\n CXInputElement<TCX> extends infer P\n ? // `0 extends 1 & P` detects `any`; `any`/`never` use `ClassValue`.\n 0 extends 1 & P\n ? ClassValue\n : [P] extends [never]\n ? ClassValue\n : [P] extends [ClassValue]\n ? P\n : // A concatenator accepting more than cva's grammar (e.g. `string |\n // URL`) narrows to the subset it shares with it, since the surface\n // can't widen beyond what the concatenator accepts; when nothing is\n // shared (e.g. `unknown`), the full grammar applies.\n [Extract<P, ClassValue>] extends [never]\n ? ClassValue\n : Extract<P, ClassValue>\n : ClassValue;\n\n/* Utils\n ---------------------------------- */\n\ntype OmitUndefined<T> = T extends undefined ? never : T;\n// Blocks inference from a site that merely checks a type parameter.\ntype Uninferred<T> = [T][T extends any ? 0 : never];\ntype StringToBoolean<T> = T extends \"true\" | \"false\" ? boolean : T;\ntype UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (\n k: infer I,\n) => void\n ? I\n : never;\n\n// `composes` accepts either a single component or a list of components. A\n// plain union (`CVAComponentShape | CVAComponentShape[]`) collapses an array\n// literal's element type to a union, silently dropping components whose\n// variants are a structural subtype of another composed component's (e.g.\n// `composes: [a, b]` where `b`'s variants are a superset of `a`'s). Splitting\n// inference across two type parameters preserves the array as a real tuple.\ntype ComposedTuple<\n S extends CVAComponentShape | undefined,\n L extends readonly CVAComponentShape[],\n> = [S] extends [CVAComponentShape] ? [S] : L;\n\ntype MergedVariants<T extends readonly unknown[]> = UnionToIntersection<\n {\n [K in keyof T]: T[K] extends {\n config: { variants?: infer V extends CVAVariantShape };\n }\n ? V\n : never;\n }[number]\n>;\n\n// Right-biased merge (`B`'s keys win on conflicts) implemented as a mapped\n// type rather than `Omit<A, keyof B> & B`: the latter stays an unresolved\n// deferred type when `A`/`B` are themselves generic (as they are here, via\n// `ReturnType<CVA>` with no concrete `Config`), which then breaks downstream\n// `any`-narrowing in unrelated code that consumes `ReturnType<CVA>` (e.g.\n// the deprecated `compose`). A mapped type resolves eagerly instead.\ntype RightMerge<A, B> = {\n [K in keyof A | keyof B]: K extends keyof B\n ? B[K]\n : K extends keyof A\n ? A[K]\n : never;\n};\n\n// `D` infers as `undefined` (not absent) when a component declares no\n// `defaultVariants` at all. `NonNullable<undefined>` would give `never`,\n// and `keyof never` is `string | number | symbol` (not `never`) — poisoning\n// `RightMerge`'s key union with every possible key. Normalize to `{}` instead,\n// matching a component that contributes nothing to the merge.\ntype DefaultsOf<Component> = Component extends {\n config: { defaultVariants?: infer D };\n}\n ? D extends undefined\n ? {}\n : D\n : {};\n\ntype MergedDefaultVariants<T extends readonly unknown[]> = T extends readonly [\n infer Head,\n ...infer Rest,\n]\n ? RightMerge<DefaultsOf<Head>, MergedDefaultVariants<Rest>>\n : {};\n\ntype ComponentProps<Component extends (...args: any) => any> = Omit<\n OmitUndefined<Parameters<Component>[0]>,\n \"class\" | \"className\"\n>;\n\n// A variant name prefixed with `_` is internal: the component still accepts\n// it, but `VariantProps` and `getSchema` omit it from the public surface.\ntype InternalVariantKey = `_${string}`;\n\nexport type VariantProps<Component extends (...args: any) => any> = Omit<\n ComponentProps<Component>,\n InternalVariantKey\n>;\n\n/* compose\n ---------------------------------- */\n\n/**\n * @deprecated Use the `composes` property inside `cva` instead.\n * @example\n * // Before\n * const card = compose(box, stack)\n * // After\n * const card = cva({ composes: [box, stack] })\n */\nexport interface Compose<T extends ClassValue = ClassValue> {\n <Components extends readonly unknown[]>(\n ...components: Components &\n (Components[number] extends CVAComponentShape ? unknown : never)\n ): (\n props?: (\n | UnionToIntersection<\n {\n [K in keyof Components]: Components[K] extends CVAComponentShape\n ? ComponentProps<Components[K]>\n : never;\n }[number]\n >\n | undefined\n ) &\n CVAClassProp<T>,\n ) => string;\n}\n\n/* cx\n ---------------------------------- */\n\nexport interface CX<T extends ClassValue = ClassValue> {\n (...inputs: T[]): string;\n}\n\nexport type CXOptions = Parameters<CX>;\nexport type CXReturn = ReturnType<CX>;\n\n/* cva\n ============================================ */\n\ntype CVAComponentConfigBase<T extends ClassValue = ClassValue> = { base?: T };\n/**\n * Exported so TypeScript can name this type in your generated declarations\n * (`declaration: true`) — you shouldn't really use it directly.\n */\nexport type CVAVariantShape = Record<string, Record<string, ClassValue>>;\n// Unconstrained so it can map over the merged (local plus composed)\n// variants, which is only known to be an intersection, not a\n// `CVAVariantShape`.\ntype CVAVariantSchema<V> = {\n [Variant in keyof V]?: StringToBoolean<keyof V[Variant]> | undefined;\n};\ntype CVAClassProp<T extends ClassValue = ClassValue> =\n | {\n class?: T;\n className?: never;\n }\n | {\n class?: never;\n className?: T;\n };\n\ntype InternalOnlyWarning =\n \"cva's generic parameters are restricted to internal use only.\";\n\ntype CVAComponentConfig<\n Config,\n Variants,\n ComposedSingle extends CVAComponentShape | undefined =\n | CVAComponentShape\n | undefined,\n ComposedList extends readonly CVAComponentShape[] =\n readonly CVAComponentShape[],\n T extends ClassValue = ClassValue,\n // `defaultVariants` and `compoundVariants` are checked against local variants\n // merged with composed variants. Defaults to local variants for introspection\n // guards.\n //\n // Gate on `keyof Merged` to preserve literal defaults, and wrap in\n // `Uninferred` to prevent reverse inference from authored values.\n Merged = Variants,\n> = Config & {\n composes?: ComposedSingle | readonly [...ComposedList];\n // The gate checks variant values against the configured concatenator's\n // input type, so e.g. object syntax fails here (on the `variants` key)\n // under a concatenator that doesn't accept objects.\n} & (Variants extends Record<string, Record<string, T>>\n ? CVAComponentConfigBase<T> & { variants?: Variants }\n : CVAComponentConfigBase<T> & { variants?: never }) &\n ([keyof Merged] extends [never]\n ? { compoundVariants?: never; defaultVariants?: never }\n : {\n compoundVariants?: ((\n | CVAVariantSchema<Uninferred<Merged>>\n | {\n [Variant in keyof Uninferred<Merged>]?:\n | StringToBoolean<keyof Uninferred<Merged>[Variant]>\n | StringToBoolean<keyof Uninferred<Merged>[Variant]>[]\n | undefined;\n }\n ) &\n CVAClassProp<T>)[];\n defaultVariants?: CVAVariantSchema<Uninferred<Merged>>;\n });\n\n/**\n * Exported so TypeScript can name this type in your generated declarations\n * (`declaration: true`) — you shouldn't really use it directly.\n */\nexport interface CVAComponent<\n Config,\n Variants,\n T extends ClassValue = ClassValue,\n> {\n (\n props?: Variants extends CVAVariantShape\n ? CVAVariantSchema<Variants> & CVAClassProp<T>\n : CVAClassProp<T>,\n ): string;\n /** @internal */\n config: Config;\n}\n\n// The loosest form a composable component can take, constraining `composes`\n// and the composition merge helpers above. Deriving it from `CVAComponent`\n// keeps the two from drifting: instantiated with `any`, the props conditional\n// and `config` both collapse to `any` (mapped types over `any` are `any`),\n// i.e. `{ (props?: any): string; config: any }`. The required `config`\n// property is what rejects plain functions and (deprecated) `compose`\n// results.\n//\n// The `any` arguments are deliberate, not lazy typing — a shaped `config`\n// (e.g. `{ variants?: CVAVariantShape }`) was tried and verifiably breaks:\n// a variant-less `cva({ base })` carries `variants: unknown`, and\n// `ReturnType<CVA>` instantiates this constraint inside the\n// `Compose`/`GetSchema` guards, where the shaped form rejects every real\n// component via props contravariance.\n//\n// Its class-value parameter must be `any`: narrowed components otherwise fail\n// props contravariance in `composes` and `getSchema`.\n/**\n * Exported so TypeScript can name this type in your generated declarations\n * (`declaration: true`) — you shouldn't really use it directly.\n */\nexport type CVAComponentShape = CVAComponent<any, any, any>;\n\ntype CVADefaultVariants<Config> = Config extends { defaultVariants?: infer D }\n ? D\n : {};\n\nexport interface CVA<T extends ClassValue = ClassValue> {\n <\n _ extends InternalOnlyWarning,\n Config,\n Variants,\n ComposedSingle extends CVAComponentShape | undefined = undefined,\n ComposedList extends readonly CVAComponentShape[] = [],\n >(\n config: CVAComponentConfig<\n Config,\n Variants,\n ComposedSingle,\n ComposedList,\n T,\n Variants & MergedVariants<ComposedTuple<ComposedSingle, ComposedList>>\n >,\n ): CVAComponent<\n Omit<Config, \"defaultVariants\"> & {\n variants: Variants &\n MergedVariants<ComposedTuple<ComposedSingle, ComposedList>>;\n // Local `defaultVariants` win over composed ones on key conflicts,\n // matching the runtime spread order. A plain intersection would collapse\n // a conflicting key's value to `never` (e.g. `\"sm\" & \"lg\"`), which then\n // silently drops the variant from `getSchema`'s inferred type.\n defaultVariants: Omit<\n MergedDefaultVariants<ComposedTuple<ComposedSingle, ComposedList>>,\n keyof CVADefaultVariants<Config>\n > &\n CVADefaultVariants<Config>;\n },\n Variants & MergedVariants<ComposedTuple<ComposedSingle, ComposedList>>,\n T\n >;\n}\n\n/* defineConfig\n ---------------------------------- */\n\nexport interface DefineConfigOptions<TCX extends AnyCX = CX> {\n /**\n * The class name concatenator used by `cva`, `cx`, and `compose`. It owns\n * the class name grammar entirely: cva assembles the authored values\n * (composed component outputs, `base`, matched variant and compound\n * variant values, `class`/`className`) and passes them through verbatim,\n * one argument each, without interpreting them.\n *\n * The authoring surface adopts the concatenator's own input type\n * automatically (see {@link CXInput}): pass `twMerge` and your variants\n * are checked against tailwind-merge's `ClassNameValue`; pass `clsx` (or\n * any function whose parameters don't narrow further) and you keep the\n * full clsx-flavored `ClassValue` grammar.\n */\n cx: TCX & CXConstraint<TCX>;\n hooks?: {\n /**\n * @deprecated please use the `cx` option instead\n */\n \"cx:done\"?: (className: string) => string;\n /**\n * @deprecated please use the `cx` option instead\n */\n onComplete?: (className: string) => string;\n };\n}\n\nexport interface DefineConfig {\n <TCX extends AnyCX>(\n options: DefineConfigOptions<TCX>,\n ): {\n /**\n * @deprecated Use the `composes` property inside `cva` instead.\n * @example\n * // Before\n * const card = compose(box, stack)\n * // After\n * const card = cva({ composes: [box, stack] })\n */\n compose: Compose<CXInput<TCX>>;\n cx: CX<CXInput<TCX>>;\n cva: CVA<CXInput<TCX>>;\n };\n}\n\n/* Exports\n ============================================ */\n\nconst falsyToString = <T extends unknown>(value: T) =>\n typeof value === \"boolean\" ? `${value}` : value === 0 ? \"0\" : value;\n\n// Shared across every non-composed call, rather than allocating a fresh `[]`\n// per call — spreading an empty array contributes no arguments.\nconst emptyClassNames: string[] = [];\n\n// Cast to `DefineConfig`: runtime uses `ClassValue`; `CXInput` is type-only.\nexport const defineConfig = ((options: DefineConfigOptions) => {\n const cx: CX = (...inputs) => {\n // Drop absent values so a narrower concatenator never receives `undefined`.\n const className = options.cx(\n ...inputs.filter((input) => input !== undefined),\n );\n\n const hook = options.hooks?.[\"cx:done\"] ?? options.hooks?.onComplete;\n return hook ? hook(className) : className;\n };\n\n const cva = (<\n _ extends InternalOnlyWarning,\n Config,\n Variants,\n ComposedSingle extends CVAComponentShape | undefined = undefined,\n ComposedList extends readonly CVAComponentShape[] = [],\n >(\n config: CVAComponentConfig<Config, Variants, ComposedSingle, ComposedList>,\n ) => {\n const components = (\n config?.composes == null\n ? []\n : Array.isArray(config.composes)\n ? config.composes\n : [config.composes]\n ) as CVAComponentShape[];\n // A one-level-deep merge per variant key, so overlapping variants (e.g.\n // multiple composed components declaring `style`) union their values\n // instead of the last component's values silently replacing the rest.\n const mergeVariants = (\n acc: CVAVariantShape,\n variants: CVAVariantShape | undefined,\n ): CVAVariantShape => {\n if (!variants) return acc;\n const merged: CVAVariantShape = { ...acc };\n for (const key of Object.keys(variants)) {\n merged[key] = { ...merged[key], ...variants[key] };\n }\n return merged;\n };\n const mergedVariantsFromComposed = components.reduce(\n (acc: CVAVariantShape, component: CVAComponentShape) =>\n mergeVariants(acc, component.config?.variants),\n {} as CVAVariantShape,\n );\n const mergedVariants = mergeVariants(\n mergedVariantsFromComposed,\n config?.variants as CVAVariantShape | undefined,\n );\n const mergedDefaultVariantsFromComposed = components.reduce(\n (acc: Record<string, unknown>, component: CVAComponentShape) => ({\n ...acc,\n ...component.config?.defaultVariants,\n }),\n {} as Record<string, unknown>,\n );\n // Local `defaultVariants` win over composed ones here too (last spread).\n const mergedDefaultVariants: Record<string, unknown> = {\n ...mergedDefaultVariantsFromComposed,\n ...config?.defaultVariants,\n };\n\n const component: CVAComponent<typeof config, typeof config.variants> = (\n props,\n ) => {\n // Strip `class`/`className` and explicit `undefined` from props once,\n // reused for both the composed-component calls and compound-variant\n // matching. An explicit `{ variant: undefined }` is dropped so it falls\n // back to the (possibly composed) default, matching variant resolution\n // below. Only built when something consumes it — a plain component with\n // no `composes` and no `compoundVariants` skips the work entirely.\n const definedPropsWithoutClass =\n components.length || config?.compoundVariants\n ? Object.fromEntries(\n Object.entries(props || {}).filter(\n ([key, value]) =>\n key !== \"class\" &&\n key !== \"className\" &&\n typeof value !== \"undefined\",\n ),\n )\n : {};\n\n const getComposedClassNames = components.length\n ? components.map((component: CVAComponentShape) =>\n component({\n ...mergedDefaultVariants,\n ...definedPropsWithoutClass,\n }),\n )\n : emptyClassNames;\n\n // Compound variants may target composed-only keys, so a component with\n // no local `variants` still resolves them.\n if (!config?.variants && !config?.compoundVariants) {\n return getComposedClassNames.length\n ? cx(\n ...getComposedClassNames,\n config?.base,\n props?.class,\n props?.className,\n )\n : cx(config?.base, props?.class, props?.className);\n }\n\n const variants = (config.variants ?? {}) as CVAVariantShape;\n\n // Resolve against the *merged* defaults (composed + local) so a variant\n // redeclared locally over a composed key uses the same effective default\n // the composed components and `getSchema` see.\n const getVariantClassNames = Object.keys(variants).map((variant) => {\n const variantProp = props?.[variant as keyof typeof props];\n const defaultVariantProp = mergedDefaultVariants[variant];\n\n const variantKey = (falsyToString(variantProp) ||\n falsyToString(defaultVariantProp)) as string;\n\n return variants[variant][variantKey];\n });\n\n const defaultsAndProps = {\n ...mergedDefaultVariants,\n ...definedPropsWithoutClass,\n };\n\n const getCompoundVariantClassNames =\n config?.compoundVariants?.reduce(\n (\n acc: ClassValue[],\n {\n class: cvClass,\n className: cvClassName,\n ...cvConfig\n }: CVAClassProp & Record<string, unknown>,\n ) =>\n Object.entries(cvConfig).every(([cvKey, cvSelector]) => {\n const selector =\n defaultsAndProps[cvKey as keyof typeof defaultsAndProps];\n\n return Array.isArray(cvSelector)\n ? cvSelector.includes(selector)\n : selector === cvSelector;\n })\n ? [...acc, cvClass, cvClassName]\n : acc,\n [] as ClassValue[],\n ) ?? emptyClassNames;\n\n return cx(\n ...getComposedClassNames,\n config?.base,\n ...getVariantClassNames,\n ...getCompoundVariantClassNames,\n props?.class,\n props?.className,\n );\n };\n\n component.config = {\n ...config,\n variants: mergedVariants,\n defaultVariants: mergedDefaultVariants,\n };\n\n return component as ReturnType<CVA>;\n }) as CVA;\n\n const compose: Compose = (...components) => {\n const composedComponents = components as CVAComponentShape[];\n const config = composedComponents.reduce(\n (acc, { config }) => {\n Object.entries(config || {}).forEach(([key, value]) => {\n acc[key] =\n typeof value === \"object\" && value !== null && !Array.isArray(value)\n ? {\n ...acc[key],\n ...value,\n }\n : value;\n });\n return acc;\n },\n // A loose accumulator: composed configs carry heterogeneous values\n // (base strings, variant maps, compoundVariant arrays), not just the\n // `CVAVariantShape` the merged `variants` key holds.\n {} as Record<string, any>,\n );\n\n const component: CVAComponent<typeof config, typeof config.variants> = (\n props,\n ) => {\n const propsWithoutClass = Object.fromEntries(\n Object.entries(props || {}).filter(\n ([key]) => ![\"class\", \"className\"].includes(key),\n ),\n );\n\n return cx(\n ...composedComponents.map((component) => component(propsWithoutClass)),\n props?.class,\n props?.className,\n );\n };\n\n component.config = config;\n\n return component;\n };\n\n return {\n compose,\n cva,\n cx,\n };\n}) as DefineConfig;\n\nexport interface GetSchema {\n <_ extends InternalOnlyWarning, Component, Config, Variants>(\n component: Component &\n (Component extends CVAComponentShape\n ? Component extends { config: { variants: infer V } }\n ? // A variant-less component carries `variants: unknown` and a\n // `defaultVariants: {}` that no `CVAComponentConfig` branch\n // accepts; leave `Variants` uninferred so the schema maps over\n // `keyof unknown` (`never`) to `{}`.\n unknown extends V\n ? unknown\n : { config: CVAComponentConfig<Config, Variants> }\n : { config: CVAComponentConfig<Config, Variants> }\n : never),\n ): {\n [Variant in keyof Variants as Variant extends InternalVariantKey\n ? never\n : Variant]: Config extends CVAComponentConfig<Config, Variants>\n ? Variant extends keyof Config[\"defaultVariants\"]\n ? Config[\"defaultVariants\"][Variant] extends undefined\n ? never\n : {\n values: ReadonlyArray<StringToBoolean<keyof Variants[Variant]>>;\n defaultValue: Readonly<\n StringToBoolean<Config[\"defaultVariants\"][Variant]>\n >;\n }\n : {\n values: ReadonlyArray<StringToBoolean<keyof Variants[Variant]>>;\n }\n : never;\n // Iterate over the returned schema and remove any keys that have no values\n } extends infer Schema\n ? {\n [K in keyof Schema as Schema[K] extends {\n values: readonly never[];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ACR,CAAC;GACD,OAAO;EACT,GAIA,CAAC,CACH;EAEA,MAAM,aACJ,UACG;GACH,MAAM,oBAAoB,OAAO,YAC/B,OAAO,QAAQ,SAAS,CAAC,CAAC,CAAC,CAAC,QACzB,CAAC,SAAS,CAAC,CAAC,SAAS,WAAW,CAAC,CAAC,SAAS,GAAG,CACjD,CACF;GAEA,OAAO,GACL,GAAG,mBAAmB,KAAK,cAAc,UAAU,iBAAiB,CAAC,GAAA,UAAA,QAAA,UAAA,KAAA,IAAA,KAAA,IACrE,MAAO,OAAA,UAAA,QAAA,UAAA,KAAA,IAAA,KAAA,IACP,MAAO,SACT;EACF;EAEA,UAAU,SAAS;EAEnB,OAAO;CACT;CAEA,OAAO;EACL;EACA;EACA;CACF;AACF"}
|
package/dist/index.cjs
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_core = require("./core-iwLcNxRW.cjs");
|
|
3
|
+
const require_utils = require("./utils.cjs");
|
|
2
4
|
let clsx = require("clsx");
|
|
3
5
|
//#region src/index.ts
|
|
4
6
|
/**
|
|
5
|
-
* Copyright 2022 Joe Bell. All rights reserved.
|
|
7
|
+
* Copyright 2022-present Joe Bell. All rights reserved.
|
|
6
8
|
*
|
|
7
9
|
* This file is licensed to you under the Apache License, Version 2.0
|
|
8
10
|
* (the "License"); you may not use this file except in compliance with the
|
|
@@ -16,123 +18,24 @@ let clsx = require("clsx");
|
|
|
16
18
|
* License for the specific language governing permissions and limitations under
|
|
17
19
|
* the License.
|
|
18
20
|
*/
|
|
19
|
-
|
|
20
|
-
const
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
if (!variants) return acc;
|
|
32
|
-
const merged = { ...acc };
|
|
33
|
-
for (const key of Object.keys(variants)) merged[key] = {
|
|
34
|
-
...merged[key],
|
|
35
|
-
...variants[key]
|
|
36
|
-
};
|
|
37
|
-
return merged;
|
|
38
|
-
};
|
|
39
|
-
const mergedVariants = mergeVariants(components.reduce((acc, component) => {
|
|
40
|
-
var _component$config;
|
|
41
|
-
return mergeVariants(acc, (_component$config = component.config) === null || _component$config === void 0 ? void 0 : _component$config.variants);
|
|
42
|
-
}, {}), config === null || config === void 0 ? void 0 : config.variants);
|
|
43
|
-
const mergedDefaultVariants = {
|
|
44
|
-
...components.reduce((acc, component) => {
|
|
45
|
-
var _component$config2;
|
|
46
|
-
return {
|
|
47
|
-
...acc,
|
|
48
|
-
...(_component$config2 = component.config) === null || _component$config2 === void 0 ? void 0 : _component$config2.defaultVariants
|
|
49
|
-
};
|
|
50
|
-
}, {}),
|
|
51
|
-
...config === null || config === void 0 ? void 0 : config.defaultVariants
|
|
52
|
-
};
|
|
53
|
-
const component = (props) => {
|
|
54
|
-
var _config$compoundVaria;
|
|
55
|
-
const definedPropsWithoutClass = components.length || (config === null || config === void 0 ? void 0 : config.variants) != null ? Object.fromEntries(Object.entries(props || {}).filter(([key, value]) => key !== "class" && key !== "className" && typeof value !== "undefined")) : {};
|
|
56
|
-
const getComposedClassNames = components.length ? components.map((component) => component({
|
|
57
|
-
...mergedDefaultVariants,
|
|
58
|
-
...definedPropsWithoutClass
|
|
59
|
-
})) : emptyClassNames;
|
|
60
|
-
if ((config === null || config === void 0 ? void 0 : config.variants) == null) return cx(getComposedClassNames, config === null || config === void 0 ? void 0 : config.base, props === null || props === void 0 ? void 0 : props.class, props === null || props === void 0 ? void 0 : props.className);
|
|
61
|
-
const { variants } = config;
|
|
62
|
-
const getVariantClassNames = Object.keys(variants).map((variant) => {
|
|
63
|
-
const variantProp = props === null || props === void 0 ? void 0 : props[variant];
|
|
64
|
-
const defaultVariantProp = mergedDefaultVariants[variant];
|
|
65
|
-
const variantKey = falsyToString(variantProp) || falsyToString(defaultVariantProp);
|
|
66
|
-
return variants[variant][variantKey];
|
|
67
|
-
});
|
|
68
|
-
const defaultsAndProps = {
|
|
69
|
-
...mergedDefaultVariants,
|
|
70
|
-
...definedPropsWithoutClass
|
|
71
|
-
};
|
|
72
|
-
const getCompoundVariantClassNames = config === null || config === void 0 || (_config$compoundVaria = config.compoundVariants) === null || _config$compoundVaria === void 0 ? void 0 : _config$compoundVaria.reduce((acc, { class: cvClass, className: cvClassName, ...cvConfig }) => Object.entries(cvConfig).every(([cvKey, cvSelector]) => {
|
|
73
|
-
const selector = defaultsAndProps[cvKey];
|
|
74
|
-
return Array.isArray(cvSelector) ? cvSelector.includes(selector) : selector === cvSelector;
|
|
75
|
-
}) ? [
|
|
76
|
-
...acc,
|
|
77
|
-
cvClass,
|
|
78
|
-
cvClassName
|
|
79
|
-
] : acc, []);
|
|
80
|
-
return cx(getComposedClassNames, config === null || config === void 0 ? void 0 : config.base, getVariantClassNames, getCompoundVariantClassNames, props === null || props === void 0 ? void 0 : props.class, props === null || props === void 0 ? void 0 : props.className);
|
|
81
|
-
};
|
|
82
|
-
component.config = {
|
|
83
|
-
...config,
|
|
84
|
-
variants: mergedVariants,
|
|
85
|
-
defaultVariants: mergedDefaultVariants
|
|
86
|
-
};
|
|
87
|
-
return component;
|
|
21
|
+
/** @deprecated Import `getSchema` from `cva/utils` instead. */
|
|
22
|
+
const getSchema = require_utils.getSchema;
|
|
23
|
+
/**
|
|
24
|
+
* @deprecated Import `defineConfig` from `cva/config` instead — the `cva`
|
|
25
|
+
* package is the clsx preset, while `cva/config` is where custom
|
|
26
|
+
* configuration (your own `cx` concatenator, hooks) lives.
|
|
27
|
+
*/
|
|
28
|
+
const defineConfig = ((options) => {
|
|
29
|
+
var _options$cx;
|
|
30
|
+
return require_core.defineConfig({
|
|
31
|
+
...options,
|
|
32
|
+
cx: (_options$cx = options === null || options === void 0 ? void 0 : options.cx) !== null && _options$cx !== void 0 ? _options$cx : clsx.clsx
|
|
88
33
|
});
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
...value
|
|
95
|
-
} : value;
|
|
96
|
-
});
|
|
97
|
-
return acc;
|
|
98
|
-
}, {});
|
|
99
|
-
const component = (props) => {
|
|
100
|
-
const propsWithoutClass = Object.fromEntries(Object.entries(props || {}).filter(([key]) => !["class", "className"].includes(key)));
|
|
101
|
-
return cx(components.map((component) => component(propsWithoutClass)), props === null || props === void 0 ? void 0 : props.class, props === null || props === void 0 ? void 0 : props.className);
|
|
102
|
-
};
|
|
103
|
-
component.config = config;
|
|
104
|
-
return component;
|
|
105
|
-
};
|
|
106
|
-
return {
|
|
107
|
-
compose,
|
|
108
|
-
cva,
|
|
109
|
-
cx
|
|
110
|
-
};
|
|
111
|
-
};
|
|
112
|
-
const { compose, cva, cx } = defineConfig();
|
|
113
|
-
const getSchema = (component) => {
|
|
114
|
-
var _component$config3;
|
|
115
|
-
if (!((_component$config3 = component.config) === null || _component$config3 === void 0 ? void 0 : _component$config3.variants)) return {};
|
|
116
|
-
return Object.entries(component.config.variants).reduce((acc, [key, value]) => {
|
|
117
|
-
var _component$config$def;
|
|
118
|
-
const defaultValue = (_component$config$def = component.config.defaultVariants) === null || _component$config$def === void 0 ? void 0 : _component$config$def[key];
|
|
119
|
-
const hasDefaultValue = defaultValue !== void 0;
|
|
120
|
-
const values = Object.keys(value).map((v) => {
|
|
121
|
-
if (v === "true") return true;
|
|
122
|
-
if (v === "false") return false;
|
|
123
|
-
const n = Number(v);
|
|
124
|
-
return Number.isFinite(n) && String(n) === v ? n : v;
|
|
125
|
-
});
|
|
126
|
-
const hasValues = values.length > 0;
|
|
127
|
-
return hasValues || hasDefaultValue ? {
|
|
128
|
-
...acc,
|
|
129
|
-
[key]: {
|
|
130
|
-
...hasValues ? { values } : {},
|
|
131
|
-
...hasDefaultValue ? { defaultValue } : {}
|
|
132
|
-
}
|
|
133
|
-
} : acc;
|
|
134
|
-
}, {});
|
|
135
|
-
};
|
|
34
|
+
});
|
|
35
|
+
const preset = require_core.defineConfig({ cx: clsx.clsx });
|
|
36
|
+
/** @deprecated Use the `composes` property inside `cva` instead. */
|
|
37
|
+
const compose = preset.compose;
|
|
38
|
+
const { cva, cx } = preset;
|
|
136
39
|
//#endregion
|
|
137
40
|
exports.compose = compose;
|
|
138
41
|
exports.cva = cva;
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","names":[],"sources":["../src/index.ts"],"sourcesContent":["/**\n * Copyright 2022 Joe Bell. All rights reserved.\n *\n * This file is licensed to you under the Apache License, Version 2.0\n * (the \"License\"); you may not use this file except in compliance with the\n * License. You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT\n * WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or implied. See the\n * License for the specific language governing permissions and limitations under\n * the License.\n */\nimport { clsx } from \"clsx\";\n\n/* Types\n ============================================ */\n\n/* clsx\n ---------------------------------- */\n\n// When compiling with `declaration: true`, many projects experience the dreaded\n// TS2742 error. To combat this, we copy clsx's types manually.\n// Should this project move to JSDoc, this workaround would no longer be needed.\n\nexport type ClassValue =\n | ClassArray\n | ClassDictionary\n | string\n | number\n | bigint\n | null\n | boolean\n | undefined;\nexport type ClassDictionary = Record<string, any>;\nexport type ClassArray = ClassValue[];\n\n/* Utils\n ---------------------------------- */\n\ntype OmitUndefined<T> = T extends undefined ? never : T;\ntype StringToBoolean<T> = T extends \"true\" | \"false\" ? boolean : T;\ntype UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (\n k: infer I,\n) => void\n ? I\n : never;\n\n// `composes` accepts either a single component or a list of components. A\n// plain union (`CVAComponentShape | CVAComponentShape[]`) collapses an array\n// literal's element type to a union, silently dropping components whose\n// variants are a structural subtype of another composed component's (e.g.\n// `composes: [a, b]` where `b`'s variants are a superset of `a`'s). Splitting\n// inference across two type parameters preserves the array as a real tuple.\ntype ComposedTuple<\n S extends CVAComponentShape | undefined,\n L extends readonly CVAComponentShape[],\n> = [S] extends [CVAComponentShape] ? [S] : L;\n\ntype MergedVariants<T extends readonly unknown[]> = UnionToIntersection<\n {\n [K in keyof T]: T[K] extends {\n config: { variants?: infer V extends CVAVariantShape };\n }\n ? V\n : never;\n }[number]\n>;\n\n// Right-biased merge (`B`'s keys win on conflicts) implemented as a mapped\n// type rather than `Omit<A, keyof B> & B`: the latter stays an unresolved\n// deferred type when `A`/`B` are themselves generic (as they are here, via\n// `ReturnType<CVA>` with no concrete `Config`), which then breaks downstream\n// `any`-narrowing in unrelated code that consumes `ReturnType<CVA>` (e.g.\n// the deprecated `compose`). A mapped type resolves eagerly instead.\ntype RightMerge<A, B> = {\n [K in keyof A | keyof B]: K extends keyof B\n ? B[K]\n : K extends keyof A\n ? A[K]\n : never;\n};\n\n// `D` infers as `undefined` (not absent) when a component declares no\n// `defaultVariants` at all. `NonNullable<undefined>` would give `never`,\n// and `keyof never` is `string | number | symbol` (not `never`) — poisoning\n// `RightMerge`'s key union with every possible key. Normalize to `{}` instead,\n// matching a component that contributes nothing to the merge.\ntype DefaultsOf<Component> = Component extends {\n config: { defaultVariants?: infer D };\n}\n ? D extends undefined\n ? {}\n : D\n : {};\n\ntype MergedDefaultVariants<T extends readonly unknown[]> = T extends readonly [\n infer Head,\n ...infer Rest,\n]\n ? RightMerge<DefaultsOf<Head>, MergedDefaultVariants<Rest>>\n : {};\n\nexport type VariantProps<Component extends (...args: any) => any> = Omit<\n OmitUndefined<Parameters<Component>[0]>,\n \"class\" | \"className\"\n>;\n\n/* compose\n ---------------------------------- */\n\n/**\n * @deprecated Use the `composes` property inside `cva` instead.\n * @example\n * // Before\n * const card = compose(box, stack)\n * // After\n * const card = cva({ composes: [box, stack] })\n */\nexport interface Compose {\n <T extends ReturnType<CVA>[]>(\n ...components: [...T]\n ): (\n props?: (\n | UnionToIntersection<\n {\n [K in keyof T]: VariantProps<T[K]>;\n }[number]\n >\n | undefined\n ) &\n CVAClassProp,\n ) => string;\n}\n\n/* cx\n ---------------------------------- */\n\nexport interface CX {\n (...inputs: ClassValue[]): string;\n}\n\nexport type CXOptions = Parameters<CX>;\nexport type CXReturn = ReturnType<CX>;\n\n/* cva\n ============================================ */\n\ntype CVAComponentConfigBase = { base?: ClassValue };\n/**\n * Exported so TypeScript can name this type in your generated declarations\n * (`declaration: true`) — you shouldn't really use it directly.\n */\nexport type CVAVariantShape = Record<string, Record<string, ClassValue>>;\ntype CVAVariantSchema<V extends CVAVariantShape> = {\n [Variant in keyof V]?: StringToBoolean<keyof V[Variant]> | undefined;\n};\ntype CVAClassProp =\n | {\n class?: ClassValue;\n className?: never;\n }\n | {\n class?: never;\n className?: ClassValue;\n };\n\ntype InternalOnlyWarning =\n \"cva's generic parameters are restricted to internal use only.\";\n\ntype CVAComponentConfig<\n Config,\n Variants,\n ComposedSingle extends CVAComponentShape | undefined =\n | CVAComponentShape\n | undefined,\n ComposedList extends readonly CVAComponentShape[] =\n readonly CVAComponentShape[],\n> = Config & {\n composes?: ComposedSingle | readonly [...ComposedList];\n} & (Variants extends CVAVariantShape\n ? CVAComponentConfigBase & {\n variants?: Variants;\n compoundVariants?: (Variants extends CVAVariantShape\n ? (\n | CVAVariantSchema<Variants>\n | {\n [Variant in keyof Variants]?:\n | StringToBoolean<keyof Variants[Variant]>\n | StringToBoolean<keyof Variants[Variant]>[]\n | undefined;\n }\n ) &\n CVAClassProp\n : CVAClassProp)[];\n defaultVariants?: CVAVariantSchema<Variants>;\n }\n : CVAComponentConfigBase & {\n variants?: never;\n compoundVariants?: never;\n defaultVariants?: never;\n });\n\n/**\n * Exported so TypeScript can name this type in your generated declarations\n * (`declaration: true`) — you shouldn't really use it directly.\n */\nexport interface CVAComponent<Config, Variants> {\n (\n props?: Variants extends CVAVariantShape\n ? CVAVariantSchema<Variants> & CVAClassProp\n : CVAClassProp,\n ): string;\n /** @internal */\n config: Config;\n}\n\n// The loosest form a composable component can take, constraining `composes`\n// and the composition merge helpers above. Deriving it from `CVAComponent`\n// keeps the two from drifting: instantiated with `any`, the props conditional\n// and `config` both collapse to `any` (mapped types over `any` are `any`),\n// i.e. `{ (props?: any): string; config: any }`. The required `config`\n// property is what rejects plain functions and (deprecated) `compose`\n// results.\n//\n// The `any` arguments are deliberate, not lazy typing — a shaped `config`\n// (e.g. `{ variants?: CVAVariantShape }`) was tried and verifiably breaks:\n// a variant-less `cva({ base })` carries `variants: unknown`, and\n// `ReturnType<CVA>` instantiates this constraint inside the\n// `Compose`/`GetSchema` guards, where the shaped form rejects every real\n// component via props contravariance.\n/**\n * Exported so TypeScript can name this type in your generated declarations\n * (`declaration: true`) — you shouldn't really use it directly.\n */\nexport type CVAComponentShape = CVAComponent<any, any>;\n\ntype CVADefaultVariants<Config> = Config extends { defaultVariants?: infer D }\n ? D\n : {};\n\nexport interface CVA {\n <\n _ extends InternalOnlyWarning,\n Config,\n Variants,\n ComposedSingle extends CVAComponentShape | undefined = undefined,\n ComposedList extends readonly CVAComponentShape[] = [],\n >(\n config: CVAComponentConfig<Config, Variants, ComposedSingle, ComposedList>,\n ): CVAComponent<\n Omit<Config, \"defaultVariants\"> & {\n variants: Variants &\n MergedVariants<ComposedTuple<ComposedSingle, ComposedList>>;\n // Local `defaultVariants` win over composed ones on key conflicts,\n // matching the runtime spread order. A plain intersection would collapse\n // a conflicting key's value to `never` (e.g. `\"sm\" & \"lg\"`), which then\n // silently drops the variant from `getSchema`'s inferred type.\n defaultVariants: Omit<\n MergedDefaultVariants<ComposedTuple<ComposedSingle, ComposedList>>,\n keyof CVADefaultVariants<Config>\n > &\n CVADefaultVariants<Config>;\n },\n Variants & MergedVariants<ComposedTuple<ComposedSingle, ComposedList>>\n >;\n}\n\n/* defineConfig\n ---------------------------------- */\n\nexport interface DefineConfigOptions {\n hooks?: {\n /**\n * @deprecated please use `onComplete`\n */\n \"cx:done\"?: (className: string) => string;\n /**\n * Returns the completed string of concatenated classes/classNames.\n */\n onComplete?: (className: string) => string;\n };\n}\n\nexport interface DefineConfig {\n (options?: DefineConfigOptions): {\n /**\n * @deprecated Use the `composes` property inside `cva` instead.\n * @example\n * // Before\n * const card = compose(box, stack)\n * // After\n * const card = cva({ composes: [box, stack] })\n */\n compose: Compose;\n cx: CX;\n cva: CVA;\n };\n}\n\n/* Exports\n ============================================ */\n\nconst falsyToString = <T extends unknown>(value: T) =>\n typeof value === \"boolean\" ? `${value}` : value === 0 ? \"0\" : value;\n\n// Shared across every non-composed call, rather than allocating a fresh `[]`\n// per call — `cx` (clsx) treats an empty array identically to an absent one.\nconst emptyClassNames: string[] = [];\n\nexport const defineConfig: DefineConfig = (options) => {\n const cx: CX = (...inputs) => {\n if (typeof options?.hooks?.[\"cx:done\"] !== \"undefined\")\n return options?.hooks[\"cx:done\"](clsx(inputs));\n\n if (typeof options?.hooks?.onComplete !== \"undefined\")\n return options?.hooks.onComplete(clsx(inputs));\n\n return clsx(inputs);\n };\n\n const cva = (<\n _ extends InternalOnlyWarning,\n Config,\n Variants,\n ComposedSingle extends CVAComponentShape | undefined = undefined,\n ComposedList extends readonly CVAComponentShape[] = [],\n >(\n config: CVAComponentConfig<Config, Variants, ComposedSingle, ComposedList>,\n ) => {\n const components = (\n config?.composes == null\n ? []\n : Array.isArray(config.composes)\n ? config.composes\n : [config.composes]\n ) as CVAComponentShape[];\n // A one-level-deep merge per variant key, so overlapping variants (e.g.\n // multiple composed components declaring `style`) union their values\n // instead of the last component's values silently replacing the rest.\n const mergeVariants = (\n acc: CVAVariantShape,\n variants: CVAVariantShape | undefined,\n ): CVAVariantShape => {\n if (!variants) return acc;\n const merged: CVAVariantShape = { ...acc };\n for (const key of Object.keys(variants)) {\n merged[key] = { ...merged[key], ...variants[key] };\n }\n return merged;\n };\n const mergedVariantsFromComposed = components.reduce(\n (acc: CVAVariantShape, component: CVAComponentShape) =>\n mergeVariants(acc, component.config?.variants),\n {} as CVAVariantShape,\n );\n const mergedVariants = mergeVariants(\n mergedVariantsFromComposed,\n config?.variants as CVAVariantShape | undefined,\n );\n const mergedDefaultVariantsFromComposed = components.reduce(\n (acc: Record<string, unknown>, component: CVAComponentShape) => ({\n ...acc,\n ...component.config?.defaultVariants,\n }),\n {} as Record<string, unknown>,\n );\n // Local `defaultVariants` win over composed ones here too (last spread).\n const mergedDefaultVariants: Record<string, unknown> = {\n ...mergedDefaultVariantsFromComposed,\n ...config?.defaultVariants,\n };\n\n const component: CVAComponent<typeof config, typeof config.variants> = (\n props,\n ) => {\n // Strip `class`/`className` and explicit `undefined` from props once,\n // reused for both the composed-component calls and compound-variant\n // matching. An explicit `{ variant: undefined }` is dropped so it falls\n // back to the (possibly composed) default, matching variant resolution\n // below. Only built when something consumes it — a plain component with\n // no `composes` and no `variants` skips the work entirely.\n const definedPropsWithoutClass =\n components.length || config?.variants != null\n ? Object.fromEntries(\n Object.entries(props || {}).filter(\n ([key, value]) =>\n key !== \"class\" &&\n key !== \"className\" &&\n typeof value !== \"undefined\",\n ),\n )\n : {};\n\n const getComposedClassNames = components.length\n ? components.map((component: CVAComponentShape) =>\n component({\n ...mergedDefaultVariants,\n ...definedPropsWithoutClass,\n }),\n )\n : emptyClassNames;\n\n if (config?.variants == null) {\n return cx(\n getComposedClassNames,\n config?.base,\n props?.class,\n props?.className,\n );\n }\n\n const { variants } = config;\n\n // Resolve against the *merged* defaults (composed + local) so a variant\n // redeclared locally over a composed key uses the same effective default\n // the composed components and `getSchema` see.\n const getVariantClassNames = Object.keys(variants).map(\n (variant: keyof typeof variants) => {\n const variantProp = props?.[variant as keyof typeof props];\n const defaultVariantProp = mergedDefaultVariants[variant as string];\n\n const variantKey = (falsyToString(variantProp) ||\n falsyToString(\n defaultVariantProp,\n )) as keyof (typeof variants)[typeof variant];\n\n return variants[variant][variantKey];\n },\n );\n\n const defaultsAndProps = {\n ...mergedDefaultVariants,\n ...definedPropsWithoutClass,\n };\n\n const getCompoundVariantClassNames = config?.compoundVariants?.reduce(\n (\n acc: ClassValue[],\n {\n class: cvClass,\n className: cvClassName,\n ...cvConfig\n }: CVAClassProp & Record<string, unknown>,\n ) =>\n Object.entries(cvConfig).every(([cvKey, cvSelector]) => {\n const selector =\n defaultsAndProps[cvKey as keyof typeof defaultsAndProps];\n\n return Array.isArray(cvSelector)\n ? cvSelector.includes(selector)\n : selector === cvSelector;\n })\n ? [...acc, cvClass, cvClassName]\n : acc,\n [] as ClassValue[],\n );\n\n return cx(\n getComposedClassNames,\n config?.base,\n getVariantClassNames,\n getCompoundVariantClassNames,\n props?.class,\n props?.className,\n );\n };\n\n component.config = {\n ...config,\n variants: mergedVariants,\n defaultVariants: mergedDefaultVariants,\n };\n\n return component as ReturnType<CVA>;\n }) as CVA;\n\n const compose: Compose = (...components) => {\n const config = components.reduce(\n (acc, { config }) => {\n Object.entries(config || {}).forEach(([key, value]) => {\n acc[key] =\n typeof value === \"object\" && value !== null && !Array.isArray(value)\n ? {\n ...acc[key],\n ...value,\n }\n : value;\n });\n return acc;\n },\n // A loose accumulator: composed configs carry heterogeneous values\n // (base strings, variant maps, compoundVariant arrays), not just the\n // `CVAVariantShape` the merged `variants` key holds.\n {} as Record<string, any>,\n );\n\n const component: CVAComponent<typeof config, typeof config.variants> = (\n props,\n ) => {\n const propsWithoutClass = Object.fromEntries(\n Object.entries(props || {}).filter(\n ([key]) => ![\"class\", \"className\"].includes(key),\n ),\n );\n\n return cx(\n components.map((component) => component(propsWithoutClass)),\n props?.class,\n props?.className,\n );\n };\n\n component.config = config;\n\n return component;\n };\n\n return {\n compose,\n cva,\n cx,\n };\n};\n\nexport const { compose, cva, cx } = defineConfig();\n\nexport interface GetSchema {\n <_ extends InternalOnlyWarning, Component, Config, Variants>(\n component: Component &\n (Component extends ReturnType<CVA>\n ? { config: CVAComponentConfig<Config, Variants> }\n : never),\n ): {\n [Variant in keyof Variants]: Config extends CVAComponentConfig<\n Config,\n Variants\n >\n ? Variant extends keyof Config[\"defaultVariants\"]\n ? Config[\"defaultVariants\"][Variant] extends undefined\n ? never\n : {\n values: ReadonlyArray<StringToBoolean<keyof Variants[Variant]>>;\n defaultValue: Readonly<\n StringToBoolean<Config[\"defaultVariants\"][Variant]>\n >;\n }\n : {\n values: ReadonlyArray<StringToBoolean<keyof Variants[Variant]>>;\n }\n : never;\n // Iterate over the returned schema and remove any keys that have no values\n } extends infer Schema\n ? {\n [K in keyof Schema as Schema[K] extends {\n values: readonly never[];\n }\n ? never\n : K]: Schema[K] extends { defaultValue: never } ? never : Schema[K];\n }\n : never;\n}\n\nexport const getSchema: GetSchema = (component) => {\n if (!component.config?.variants) return {} as any;\n\n return Object.entries(component.config.variants).reduce(\n (acc, [key, value]) => {\n const defaultValue = component.config.defaultVariants?.[key];\n const hasDefaultValue = defaultValue !== undefined;\n const values = Object.keys(value).map((v) => {\n if (v === \"true\") return true;\n if (v === \"false\") return false;\n // Normalize numeric-literal keys back to numbers, since that's how\n // they appear in variant prop types (`keyof { 1: ... }` is `1`, not\n // `\"1\"`) — object keys are always strings/symbols at runtime. The\n // `String(n) === v` round-trip only accepts canonical numeric forms\n // (so `\"01\"`, `\"\"`, `\" 1\"` stay strings), covering negatives too.\n const n = Number(v);\n return Number.isFinite(n) && String(n) === v ? n : v;\n }) as StringToBoolean<keyof typeof value>[];\n const hasValues = values.length > 0;\n\n return hasValues || hasDefaultValue\n ? {\n ...acc,\n [key]: {\n ...(hasValues ? { values } : {}),\n ...(hasDefaultValue ? { defaultValue } : {}),\n },\n }\n : acc;\n },\n {} as ReturnType<GetSchema>,\n );\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;AAiTA,MAAM,iBAAoC,UACxC,OAAO,UAAU,YAAY,GAAG,UAAU,UAAU,IAAI,MAAM;AAIhE,MAAM,kBAA4B,CAAC;AAEnC,MAAa,gBAA8B,YAAY;CACrD,MAAM,MAAU,GAAG,WAAW;;EAC5B,IAAI,QAAA,YAAA,QAAA,YAAA,KAAA,MAAA,iBAAO,QAAS,WAAA,QAAA,mBAAA,KAAA,IAAA,KAAA,IAAA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GAAA,qBAAC,UAAU,YAAA,QAAA,uBAAA,KAAA,IAAA,KAAA,IAAA,mBAAQ,WAAU,OAAO,CAAC;CAEzC,OAAO,OAAO,QAAQ,UAAU,OAAO,QAAQ,CAAC,CAAC,QAC9C,KAAK,CAAC,KAAK,WAAW;;EACrB,MAAM,gBAAA,wBAAe,UAAU,OAAO,qBAAA,QAAA,0BAAA,KAAA,IAAA,KAAA,IAAA,sBAAkB;EACxD,MAAM,kBAAkB,iBAAiB,KAAA;EACzC,MAAM,SAAS,OAAO,KAAK,KAAK,CAAC,CAAC,KAAK,MAAM;GAC3C,IAAI,MAAM,QAAQ,OAAO;GACzB,IAAI,MAAM,SAAS,OAAO;GAM1B,MAAM,IAAI,OAAO,CAAC;GAClB,OAAO,OAAO,SAAS,CAAC,KAAK,OAAO,CAAC,MAAM,IAAI,IAAI;EACrD,CAAC;EACD,MAAM,YAAY,OAAO,SAAS;EAElC,OAAO,aAAa,kBAChB;GACE,GAAG;IACF,MAAM;IACL,GAAI,YAAY,EAAE,OAAO,IAAI,CAAC;IAC9B,GAAI,kBAAkB,EAAE,aAAa,IAAI,CAAC;GAC5C;EACF,IACA;CACN,GACA,CAAC,CACH;AACF"}
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["getSchemaUtils","defineCoreConfig"],"sources":["../src/index.ts"],"sourcesContent":["/**\n * Copyright 2022-present Joe Bell. All rights reserved.\n *\n * This file is licensed to you under the Apache License, Version 2.0\n * (the \"License\"); you may not use this file except in compliance with the\n * License. You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT\n * WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or implied. See the\n * License for the specific language governing permissions and limitations under\n * the License.\n */\nimport { clsx } from \"clsx\";\nimport {\n defineConfig as defineCoreConfig,\n type AnyCX,\n type CVA,\n type CXInput,\n type Compose,\n type CX,\n type DefineConfigOptions as CoreDefineConfigOptions,\n} from \"./core.js\";\nimport {\n getSchema as getSchemaUtils,\n type GetSchema as UtilsGetSchema,\n} from \"./utils.js\";\n\nexport type {\n AnyCX,\n ClassValue,\n ClassDictionary,\n ClassArray,\n VariantProps,\n Compose,\n CX,\n CXInput,\n CXOptions,\n CXReturn,\n CVAVariantShape,\n CVAComponent,\n CVAComponentShape,\n CVA,\n} from \"./config.js\";\n\n/** @deprecated Import `GetSchema` from `cva/utils` instead. */\nexport type GetSchema = UtilsGetSchema;\n\n/** @deprecated Import `getSchema` from `cva/utils` instead. */\nexport const getSchema: GetSchema = getSchemaUtils;\n\n/**\n * @deprecated Import `DefineConfigOptions` from `cva/config` instead and provide\n * the required `cx` concatenator (for example, `clsx`).\n */\nexport interface DefineConfigOptions<TCX extends AnyCX = CX> extends Omit<\n CoreDefineConfigOptions<TCX>,\n \"cx\"\n> {\n /** Defaults to `clsx`. */\n cx?: CoreDefineConfigOptions<TCX>[\"cx\"];\n}\n\n/**\n * @deprecated Import `DefineConfig` from `cva/config` instead. Its options require\n * a `cx` concatenator (for example, `clsx`).\n */\nexport interface DefineConfig {\n <TCX extends AnyCX = CX>(\n options?: DefineConfigOptions<TCX>,\n ): {\n /**\n * @deprecated Use the `composes` property inside `cva` instead.\n * @example\n * // Before\n * const card = compose(box, stack)\n * // After\n * const card = cva({ composes: [box, stack] })\n */\n compose: Compose<CXInput<TCX>>;\n cx: CX<CXInput<TCX>>;\n cva: CVA<CXInput<TCX>>;\n };\n}\n\n/**\n * @deprecated Import `defineConfig` from `cva/config` instead — the `cva`\n * package is the clsx preset, while `cva/config` is where custom\n * configuration (your own `cx` concatenator, hooks) lives.\n */\nexport const defineConfig = ((options?: DefineConfigOptions) =>\n defineCoreConfig({\n ...options,\n cx: options?.cx ?? clsx,\n })) as DefineConfig;\n\nconst preset = defineCoreConfig({ cx: clsx });\n\n/** @deprecated Use the `composes` property inside `cva` instead. */\nexport const compose = preset.compose;\nexport const { cva, cx } = preset;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAmDA,MAAa,YAAuBA,cAAAA;;;;;;AAyCpC,MAAa,iBAAiB,YAC5B;;kCAAiB;EACf,GAAG;EACH,KAAA,cAAA,YAAA,QAAA,YAAA,KAAA,IAAA,KAAA,IAAI,QAAS,QAAA,QAAA,gBAAA,KAAA,IAAA,cAAM,KAAA;CACrB,CAAC;;AAEH,MAAM,SAASC,aAAAA,aAAiB,EAAE,IAAI,KAAA,KAAK,CAAC;;AAG5C,MAAa,UAAU,OAAO;AAC9B,MAAa,EAAE,KAAK,OAAO"}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,105 +1,23 @@
|
|
|
1
|
+
import { _ as VariantProps, a as CVAVariantShape, c as CXOptions, d as ClassDictionary, f as ClassValue, g as GetSchema$1, h as DefineConfigOptions$1, i as CVAComponentShape, l as CXReturn, n as CVA, o as CX, p as Compose, r as CVAComponent, s as CXInput, t as AnyCX, u as ClassArray } from "./core-BMF1ydiV.cjs";
|
|
1
2
|
//#region src/index.d.ts
|
|
2
|
-
|
|
3
|
-
type
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
type StringToBoolean<T> = T extends "true" | "false" ? boolean : T;
|
|
7
|
-
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never;
|
|
8
|
-
type ComposedTuple<S extends CVAComponentShape | undefined, L extends readonly CVAComponentShape[]> = [S] extends [CVAComponentShape] ? [S] : L;
|
|
9
|
-
type MergedVariants<T extends readonly unknown[]> = UnionToIntersection<{ [K in keyof T]: T[K] extends {
|
|
10
|
-
config: {
|
|
11
|
-
variants?: infer V extends CVAVariantShape;
|
|
12
|
-
};
|
|
13
|
-
} ? V : never; }[number]>;
|
|
14
|
-
type RightMerge<A, B> = { [K in keyof A | keyof B]: K extends keyof B ? B[K] : K extends keyof A ? A[K] : never; };
|
|
15
|
-
type DefaultsOf<Component> = Component extends {
|
|
16
|
-
config: {
|
|
17
|
-
defaultVariants?: infer D;
|
|
18
|
-
};
|
|
19
|
-
} ? D extends undefined ? {} : D : {};
|
|
20
|
-
type MergedDefaultVariants<T extends readonly unknown[]> = T extends readonly [infer Head, ...infer Rest] ? RightMerge<DefaultsOf<Head>, MergedDefaultVariants<Rest>> : {};
|
|
21
|
-
type VariantProps<Component extends (...args: any) => any> = Omit<OmitUndefined<Parameters<Component>[0]>, "class" | "className">;
|
|
22
|
-
/**
|
|
23
|
-
* @deprecated Use the `composes` property inside `cva` instead.
|
|
24
|
-
* @example
|
|
25
|
-
* // Before
|
|
26
|
-
* const card = compose(box, stack)
|
|
27
|
-
* // After
|
|
28
|
-
* const card = cva({ composes: [box, stack] })
|
|
29
|
-
*/
|
|
30
|
-
interface Compose {
|
|
31
|
-
<T extends ReturnType<CVA>[]>(...components: [...T]): (props?: (UnionToIntersection<{ [K in keyof T]: VariantProps<T[K]>; }[number]> | undefined) & CVAClassProp) => string;
|
|
32
|
-
}
|
|
33
|
-
interface CX {
|
|
34
|
-
(...inputs: ClassValue[]): string;
|
|
35
|
-
}
|
|
36
|
-
type CXOptions = Parameters<CX>;
|
|
37
|
-
type CXReturn = ReturnType<CX>;
|
|
38
|
-
type CVAComponentConfigBase = {
|
|
39
|
-
base?: ClassValue;
|
|
40
|
-
};
|
|
41
|
-
/**
|
|
42
|
-
* Exported so TypeScript can name this type in your generated declarations
|
|
43
|
-
* (`declaration: true`) — you shouldn't really use it directly.
|
|
44
|
-
*/
|
|
45
|
-
type CVAVariantShape = Record<string, Record<string, ClassValue>>;
|
|
46
|
-
type CVAVariantSchema<V extends CVAVariantShape> = { [Variant in keyof V]?: StringToBoolean<keyof V[Variant]> | undefined; };
|
|
47
|
-
type CVAClassProp = {
|
|
48
|
-
class?: ClassValue;
|
|
49
|
-
className?: never;
|
|
50
|
-
} | {
|
|
51
|
-
class?: never;
|
|
52
|
-
className?: ClassValue;
|
|
53
|
-
};
|
|
54
|
-
type InternalOnlyWarning = "cva's generic parameters are restricted to internal use only.";
|
|
55
|
-
type CVAComponentConfig<Config, Variants, ComposedSingle extends CVAComponentShape | undefined = CVAComponentShape | undefined, ComposedList extends readonly CVAComponentShape[] = readonly CVAComponentShape[]> = Config & {
|
|
56
|
-
composes?: ComposedSingle | readonly [...ComposedList];
|
|
57
|
-
} & (Variants extends CVAVariantShape ? CVAComponentConfigBase & {
|
|
58
|
-
variants?: Variants;
|
|
59
|
-
compoundVariants?: (Variants extends CVAVariantShape ? (CVAVariantSchema<Variants> | { [Variant in keyof Variants]?: StringToBoolean<keyof Variants[Variant]> | StringToBoolean<keyof Variants[Variant]>[] | undefined; }) & CVAClassProp : CVAClassProp)[];
|
|
60
|
-
defaultVariants?: CVAVariantSchema<Variants>;
|
|
61
|
-
} : CVAComponentConfigBase & {
|
|
62
|
-
variants?: never;
|
|
63
|
-
compoundVariants?: never;
|
|
64
|
-
defaultVariants?: never;
|
|
65
|
-
});
|
|
3
|
+
/** @deprecated Import `GetSchema` from `cva/utils` instead. */
|
|
4
|
+
type GetSchema = GetSchema$1;
|
|
5
|
+
/** @deprecated Import `getSchema` from `cva/utils` instead. */
|
|
6
|
+
declare const getSchema: GetSchema;
|
|
66
7
|
/**
|
|
67
|
-
*
|
|
68
|
-
*
|
|
8
|
+
* @deprecated Import `DefineConfigOptions` from `cva/config` instead and provide
|
|
9
|
+
* the required `cx` concatenator (for example, `clsx`).
|
|
69
10
|
*/
|
|
70
|
-
interface
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
config: Config;
|
|
11
|
+
interface DefineConfigOptions<TCX extends AnyCX = CX> extends Omit<DefineConfigOptions$1<TCX>, "cx"> {
|
|
12
|
+
/** Defaults to `clsx`. */
|
|
13
|
+
cx?: DefineConfigOptions$1<TCX>["cx"];
|
|
74
14
|
}
|
|
75
15
|
/**
|
|
76
|
-
*
|
|
77
|
-
*
|
|
16
|
+
* @deprecated Import `DefineConfig` from `cva/config` instead. Its options require
|
|
17
|
+
* a `cx` concatenator (for example, `clsx`).
|
|
78
18
|
*/
|
|
79
|
-
type CVAComponentShape = CVAComponent<any, any>;
|
|
80
|
-
type CVADefaultVariants<Config> = Config extends {
|
|
81
|
-
defaultVariants?: infer D;
|
|
82
|
-
} ? D : {};
|
|
83
|
-
interface CVA {
|
|
84
|
-
<_ extends InternalOnlyWarning, Config, Variants, ComposedSingle extends CVAComponentShape | undefined = undefined, ComposedList extends readonly CVAComponentShape[] = []>(config: CVAComponentConfig<Config, Variants, ComposedSingle, ComposedList>): CVAComponent<Omit<Config, "defaultVariants"> & {
|
|
85
|
-
variants: Variants & MergedVariants<ComposedTuple<ComposedSingle, ComposedList>>;
|
|
86
|
-
defaultVariants: Omit<MergedDefaultVariants<ComposedTuple<ComposedSingle, ComposedList>>, keyof CVADefaultVariants<Config>> & CVADefaultVariants<Config>;
|
|
87
|
-
}, Variants & MergedVariants<ComposedTuple<ComposedSingle, ComposedList>>>;
|
|
88
|
-
}
|
|
89
|
-
interface DefineConfigOptions {
|
|
90
|
-
hooks?: {
|
|
91
|
-
/**
|
|
92
|
-
* @deprecated please use `onComplete`
|
|
93
|
-
*/
|
|
94
|
-
"cx:done"?: (className: string) => string;
|
|
95
|
-
/**
|
|
96
|
-
* Returns the completed string of concatenated classes/classNames.
|
|
97
|
-
*/
|
|
98
|
-
onComplete?: (className: string) => string;
|
|
99
|
-
};
|
|
100
|
-
}
|
|
101
19
|
interface DefineConfig {
|
|
102
|
-
(options?: DefineConfigOptions): {
|
|
20
|
+
<TCX extends AnyCX = CX>(options?: DefineConfigOptions<TCX>): {
|
|
103
21
|
/**
|
|
104
22
|
* @deprecated Use the `composes` property inside `cva` instead.
|
|
105
23
|
* @example
|
|
@@ -108,28 +26,20 @@ interface DefineConfig {
|
|
|
108
26
|
* // After
|
|
109
27
|
* const card = cva({ composes: [box, stack] })
|
|
110
28
|
*/
|
|
111
|
-
compose: Compose
|
|
112
|
-
cx: CX
|
|
113
|
-
cva: CVA
|
|
29
|
+
compose: Compose<CXInput<TCX>>;
|
|
30
|
+
cx: CX<CXInput<TCX>>;
|
|
31
|
+
cva: CVA<CXInput<TCX>>;
|
|
114
32
|
};
|
|
115
33
|
}
|
|
34
|
+
/**
|
|
35
|
+
* @deprecated Import `defineConfig` from `cva/config` instead — the `cva`
|
|
36
|
+
* package is the clsx preset, while `cva/config` is where custom
|
|
37
|
+
* configuration (your own `cx` concatenator, hooks) lives.
|
|
38
|
+
*/
|
|
116
39
|
declare const defineConfig: DefineConfig;
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
config: CVAComponentConfig<Config, Variants>;
|
|
121
|
-
} : never)): { [Variant in keyof Variants]: Config extends CVAComponentConfig<Config, Variants> ? Variant extends keyof Config["defaultVariants"] ? Config["defaultVariants"][Variant] extends undefined ? never : {
|
|
122
|
-
values: ReadonlyArray<StringToBoolean<keyof Variants[Variant]>>;
|
|
123
|
-
defaultValue: Readonly<StringToBoolean<Config["defaultVariants"][Variant]>>;
|
|
124
|
-
} : {
|
|
125
|
-
values: ReadonlyArray<StringToBoolean<keyof Variants[Variant]>>;
|
|
126
|
-
} : never; } extends (infer Schema) ? { [K in keyof Schema as Schema[K] extends {
|
|
127
|
-
values: readonly never[];
|
|
128
|
-
} ? never : K]: Schema[K] extends {
|
|
129
|
-
defaultValue: never;
|
|
130
|
-
} ? never : Schema[K]; } : never;
|
|
131
|
-
}
|
|
132
|
-
declare const getSchema: GetSchema;
|
|
40
|
+
/** @deprecated Use the `composes` property inside `cva` instead. */
|
|
41
|
+
declare const compose: Compose<import("clsx").ClassValue>;
|
|
42
|
+
declare const cva: CVA<import("clsx").ClassValue>, cx: CX<import("clsx").ClassValue>;
|
|
133
43
|
//#endregion
|
|
134
|
-
export { CVA, CVAComponent, CVAComponentShape, CVAVariantShape, CX, CXOptions, CXReturn, ClassArray, ClassDictionary, ClassValue, Compose, DefineConfig, DefineConfigOptions, GetSchema, VariantProps, compose, cva, cx, defineConfig, getSchema };
|
|
44
|
+
export { type AnyCX, type CVA, type CVAComponent, type CVAComponentShape, type CVAVariantShape, type CX, type CXInput, type CXOptions, type CXReturn, type ClassArray, type ClassDictionary, type ClassValue, type Compose, DefineConfig, DefineConfigOptions, GetSchema, type VariantProps, compose, cva, cx, defineConfig, getSchema };
|
|
135
45
|
//# sourceMappingURL=index.d.cts.map
|