clava 0.6.4 → 0.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +76 -0
- package/README.md +128 -32
- package/dist/index.d.ts +109 -44
- package/dist/index.js +34 -34
- package/dist/index.js.map +1 -1
- package/package.json +6 -7
- package/src/index.ts +162 -119
- package/src/refine-warning.ts +6 -6
- package/src/types.ts +104 -48
package/src/types.ts
CHANGED
|
@@ -48,7 +48,7 @@ export type StyleProperty =
|
|
|
48
48
|
| string;
|
|
49
49
|
|
|
50
50
|
/**
|
|
51
|
-
* The prop object returned by a
|
|
51
|
+
* The prop object returned by a recipe's `.jsx()` mode.
|
|
52
52
|
*
|
|
53
53
|
* @example
|
|
54
54
|
* ```ts
|
|
@@ -64,7 +64,7 @@ export interface JSXProps {
|
|
|
64
64
|
}
|
|
65
65
|
|
|
66
66
|
/**
|
|
67
|
-
* The prop object returned by a
|
|
67
|
+
* The prop object returned by a recipe's `.html()` mode. The `style` value
|
|
68
68
|
* is serialized as an HTML style string.
|
|
69
69
|
*
|
|
70
70
|
* @example
|
|
@@ -81,7 +81,7 @@ export interface HTMLProps {
|
|
|
81
81
|
}
|
|
82
82
|
|
|
83
83
|
/**
|
|
84
|
-
* The prop object returned by a
|
|
84
|
+
* The prop object returned by a recipe's `.htmlObj()` mode. The `style`
|
|
85
85
|
* value uses hyphenated CSS property names.
|
|
86
86
|
*
|
|
87
87
|
* @example
|
|
@@ -98,7 +98,7 @@ export interface HTMLObjProps {
|
|
|
98
98
|
}
|
|
99
99
|
|
|
100
100
|
/**
|
|
101
|
-
* The default prop object returned by a Clava
|
|
101
|
+
* The default prop object returned by a Clava recipe. It uses `class`
|
|
102
102
|
* rather than `className` and keeps styles as a normalized object.
|
|
103
103
|
*
|
|
104
104
|
* @example
|
|
@@ -120,18 +120,18 @@ export interface StyleProps {
|
|
|
120
120
|
htmlObj: HTMLObjProps;
|
|
121
121
|
}
|
|
122
122
|
|
|
123
|
-
export type
|
|
123
|
+
export type RecipeResult =
|
|
124
124
|
| JSXProps
|
|
125
125
|
| HTMLProps
|
|
126
126
|
| HTMLObjProps
|
|
127
127
|
| StyleClassProps;
|
|
128
128
|
|
|
129
|
-
type
|
|
129
|
+
type AllRecipeResultKeys =
|
|
130
130
|
| keyof JSXProps
|
|
131
131
|
| keyof HTMLProps
|
|
132
132
|
| keyof HTMLObjProps;
|
|
133
133
|
|
|
134
|
-
type
|
|
134
|
+
type RecipeResultValue<K extends AllRecipeResultKeys> = K extends "style"
|
|
135
135
|
? StyleProperty
|
|
136
136
|
: K extends "className"
|
|
137
137
|
? string
|
|
@@ -139,31 +139,31 @@ type ComponentResultValue<K extends AllComponentResultKeys> = K extends "style"
|
|
|
139
139
|
? string
|
|
140
140
|
: never;
|
|
141
141
|
|
|
142
|
-
export type
|
|
143
|
-
[K in
|
|
142
|
+
export type NullableRecipeResult = {
|
|
143
|
+
[K in AllRecipeResultKeys]?: RecipeResultValue<K> | null;
|
|
144
144
|
};
|
|
145
145
|
|
|
146
|
-
export type
|
|
146
|
+
export type RecipeProps<V = {}> = VariantValues<V> & NullableRecipeResult;
|
|
147
147
|
|
|
148
148
|
export type GetVariants<V> = (variants?: VariantValues<V>) => VariantValues<V>;
|
|
149
149
|
|
|
150
|
-
type
|
|
150
|
+
type RecipePropKey<R extends RecipeResult> =
|
|
151
151
|
| keyof R
|
|
152
152
|
| (R extends StyleClassProps ? "className" : never);
|
|
153
153
|
|
|
154
154
|
// Key source types - what can be passed as additional parameters to splitProps
|
|
155
155
|
export type KeySourceArray = readonly string[];
|
|
156
|
-
export interface
|
|
156
|
+
export interface KeySourceRecipe {
|
|
157
157
|
propKeys: readonly string[];
|
|
158
158
|
variantKeys: readonly string[];
|
|
159
159
|
getVariants: () => Record<string, unknown>;
|
|
160
160
|
}
|
|
161
|
-
export type KeySource = KeySourceArray |
|
|
161
|
+
export type KeySource = KeySourceArray | KeySourceRecipe;
|
|
162
162
|
|
|
163
|
-
// Check if source is a
|
|
164
|
-
type
|
|
163
|
+
// Check if source is a recipe (has getVariants)
|
|
164
|
+
type IsRecipe<S> = S extends { getVariants: () => unknown } ? true : false;
|
|
165
165
|
|
|
166
|
-
// Extract keys from a source (includes class/style for
|
|
166
|
+
// Extract keys from a source (includes class/style for recipes)
|
|
167
167
|
type SourceKeys<S> = S extends readonly (infer K)[]
|
|
168
168
|
? K
|
|
169
169
|
: S extends { propKeys: readonly (infer K)[] }
|
|
@@ -177,28 +177,28 @@ type SourceVariantKeys<S> = S extends readonly (infer K)[]
|
|
|
177
177
|
? K
|
|
178
178
|
: never;
|
|
179
179
|
|
|
180
|
-
// Extract defaults from a source (
|
|
180
|
+
// Extract defaults from a source (recipes have defaults, arrays don't)
|
|
181
181
|
type SourceDefaults<S> = S extends { getVariants: () => infer Defaults }
|
|
182
182
|
? Defaults
|
|
183
183
|
: {};
|
|
184
184
|
|
|
185
185
|
// Result type for a source when styling is NOT yet claimed
|
|
186
186
|
// - Arrays: use listed keys (no defaults)
|
|
187
|
-
// -
|
|
187
|
+
// - Recipes: use full keys including class/style (with defaults)
|
|
188
188
|
type SourceResultWithStyling<T, S> = Pick<T, Extract<keyof T, SourceKeys<S>>> &
|
|
189
189
|
Omit<SourceDefaults<S>, keyof T>;
|
|
190
190
|
|
|
191
191
|
// Result type for a source when styling IS already claimed
|
|
192
192
|
// - Arrays: use listed keys (no defaults)
|
|
193
|
-
// -
|
|
193
|
+
// - Recipes: use only variant keys (with defaults)
|
|
194
194
|
type SourceResultWithoutStyling<T, S> =
|
|
195
|
-
|
|
195
|
+
IsRecipe<S> extends true
|
|
196
196
|
? Pick<T, Extract<keyof T, SourceVariantKeys<S>>> &
|
|
197
197
|
Omit<SourceDefaults<S>, keyof T>
|
|
198
198
|
: Pick<T, Extract<keyof T, SourceKeys<S>>>;
|
|
199
199
|
|
|
200
200
|
// Recursive helper to build the result tuple for sources after S1
|
|
201
|
-
// StylingClaimed: whether a
|
|
201
|
+
// StylingClaimed: whether a recipe has already claimed styling
|
|
202
202
|
// UsedKeys: accumulator for all keys used so far (for the rest object)
|
|
203
203
|
type BuildSourceResults<
|
|
204
204
|
T,
|
|
@@ -216,7 +216,7 @@ type BuildSourceResults<
|
|
|
216
216
|
...BuildSourceResults<
|
|
217
217
|
T,
|
|
218
218
|
Rest,
|
|
219
|
-
StylingClaimed extends true ? true :
|
|
219
|
+
StylingClaimed extends true ? true : IsRecipe<Current>,
|
|
220
220
|
UsedKeys | SourceKeys<Current>
|
|
221
221
|
>,
|
|
222
222
|
]
|
|
@@ -234,35 +234,35 @@ export type SplitPropsFunction = <
|
|
|
234
234
|
) => SplitPropsFunctionResult<T, S1, Sources>;
|
|
235
235
|
|
|
236
236
|
// Result type for standalone splitProps function
|
|
237
|
-
// S1: First source - uses SourceResultWithStyling (either array or first
|
|
238
|
-
// Sources: Subsequent sources - use SourceResultWithStyling if no prior
|
|
237
|
+
// S1: First source - uses SourceResultWithStyling (either array or first recipe gets styling)
|
|
238
|
+
// Sources: Subsequent sources - use SourceResultWithStyling if no prior recipe, else SourceResultWithoutStyling
|
|
239
239
|
type SplitPropsFunctionResult<
|
|
240
240
|
T,
|
|
241
241
|
S1 extends KeySource,
|
|
242
242
|
Sources extends readonly KeySource[],
|
|
243
243
|
> = [
|
|
244
244
|
SourceResultWithStyling<T, S1>,
|
|
245
|
-
...BuildSourceResults<T, Sources,
|
|
245
|
+
...BuildSourceResults<T, Sources, IsRecipe<S1>, SourceKeys<S1>>,
|
|
246
246
|
];
|
|
247
247
|
|
|
248
|
-
export interface
|
|
249
|
-
(props?:
|
|
250
|
-
class: (props?:
|
|
251
|
-
style: (props?:
|
|
248
|
+
export interface ModalRecipe<V, R extends RecipeResult> {
|
|
249
|
+
(props?: RecipeProps<V>): R;
|
|
250
|
+
class: (props?: RecipeProps<V>) => string;
|
|
251
|
+
style: (props?: RecipeProps<V>) => R["style"];
|
|
252
252
|
getVariants: GetVariants<V>;
|
|
253
253
|
variantKeys: (keyof V)[];
|
|
254
|
-
propKeys: (keyof V |
|
|
254
|
+
propKeys: (keyof V | RecipePropKey<R>)[];
|
|
255
255
|
}
|
|
256
256
|
|
|
257
257
|
/**
|
|
258
|
-
* A callable Clava
|
|
258
|
+
* A callable Clava recipe returned by `cv()`. It includes the default
|
|
259
259
|
* output mode plus `.jsx()`, `.html()`, and `.htmlObj()` mode helpers.
|
|
260
260
|
*
|
|
261
261
|
* @example
|
|
262
262
|
* ```ts
|
|
263
|
-
* import { type
|
|
263
|
+
* import { type Recipe, cv } from "clava";
|
|
264
264
|
*
|
|
265
|
-
* const button:
|
|
265
|
+
* const button: Recipe<{
|
|
266
266
|
* size: { sm: string; lg: string };
|
|
267
267
|
* }> = cv({
|
|
268
268
|
* variants: {
|
|
@@ -273,29 +273,85 @@ export interface ModalComponent<V, R extends ComponentResult> {
|
|
|
273
273
|
* button.jsx({ size: "lg" });
|
|
274
274
|
* ```
|
|
275
275
|
*/
|
|
276
|
-
export interface
|
|
276
|
+
export interface Recipe<
|
|
277
277
|
V extends Variants = {},
|
|
278
|
-
E extends
|
|
279
|
-
R extends
|
|
280
|
-
> extends
|
|
281
|
-
jsx:
|
|
282
|
-
html:
|
|
283
|
-
htmlObj:
|
|
278
|
+
E extends AnyRecipe[] = [],
|
|
279
|
+
R extends RecipeResult = StyleClassProps,
|
|
280
|
+
> extends ModalRecipe<MergeVariants<V, E>, R> {
|
|
281
|
+
jsx: ModalRecipe<MergeVariants<V, E>, JSXProps>;
|
|
282
|
+
html: ModalRecipe<MergeVariants<V, E>, HTMLProps>;
|
|
283
|
+
htmlObj: ModalRecipe<MergeVariants<V, E>, HTMLObjProps>;
|
|
284
284
|
}
|
|
285
285
|
|
|
286
|
-
export type
|
|
287
|
-
|
|
288
|
-
|
|
286
|
+
export type AnyRecipe = Recipe<any, any, any> | ModalRecipe<any, any>;
|
|
287
|
+
|
|
288
|
+
// Derive keys from the arrays, not getVariants: its base return constraint can
|
|
289
|
+
// hide added keys when a generic recipe is combined with its default recipe.
|
|
290
|
+
type RecipeVariantKeys<T> = T extends {
|
|
291
|
+
variantKeys: (infer Key extends string)[];
|
|
292
|
+
}
|
|
293
|
+
? Key
|
|
294
|
+
: never;
|
|
295
|
+
|
|
296
|
+
// Optional variant props alone cannot require every base key. Check each
|
|
297
|
+
// candidate separately so a union cannot hide a member's missing variants.
|
|
298
|
+
type MissingRecipeVariantKeys<
|
|
299
|
+
Base extends AnyRecipe,
|
|
300
|
+
Candidate,
|
|
301
|
+
> = Candidate extends unknown
|
|
302
|
+
? Exclude<Base["variantKeys"][number], RecipeVariantKeys<Candidate>>
|
|
303
|
+
: never;
|
|
304
|
+
|
|
305
|
+
// Keep keys tied to the candidate while retaining the base's callable contract.
|
|
306
|
+
// Copying the base's key arrays would reject recipes that add variants.
|
|
307
|
+
interface RecipeLikeMode<Base extends AnyRecipe, Candidate> {
|
|
308
|
+
(props?: Parameters<Base>[0]): ReturnType<Base>;
|
|
309
|
+
class: Base["class"];
|
|
310
|
+
style: Base["style"];
|
|
311
|
+
getVariants: (
|
|
312
|
+
variants?: Parameters<Base["getVariants"]>[0],
|
|
313
|
+
) => ReturnType<Base["getVariants"]> &
|
|
314
|
+
Partial<Record<RecipeVariantKeys<Candidate>, unknown>>;
|
|
315
|
+
variantKeys: RecipeVariantKeys<Candidate>[];
|
|
316
|
+
propKeys: (RecipeVariantKeys<Candidate> | RecipePropKey<ReturnType<Base>>)[];
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* Constrains a recipe to one that supplies every base variant and preserves
|
|
321
|
+
* the base's input and output types. Recipes can be defined independently or
|
|
322
|
+
* through extension. Added variants retain their prop and key types in generic
|
|
323
|
+
* code. Extra metadata does not affect compatibility.
|
|
324
|
+
*
|
|
325
|
+
* @example
|
|
326
|
+
* ```ts
|
|
327
|
+
* import { type RecipeLike, type VariantPropsWithRecipe, cv } from "clava";
|
|
328
|
+
*
|
|
329
|
+
* const disclosure = cv({ variants: { $open: "open" } });
|
|
330
|
+
*
|
|
331
|
+
* type DisclosureProps<
|
|
332
|
+
* R extends RecipeLike<typeof disclosure, R> = typeof disclosure,
|
|
333
|
+
* > = VariantPropsWithRecipe<typeof disclosure, R>;
|
|
334
|
+
* ```
|
|
335
|
+
*/
|
|
336
|
+
export type RecipeLike<Base extends Recipe<any, any, any>, Candidate> = [
|
|
337
|
+
MissingRecipeVariantKeys<Base, Candidate>,
|
|
338
|
+
] extends [never]
|
|
339
|
+
? RecipeLikeMode<Base, Candidate> & {
|
|
340
|
+
jsx: RecipeLikeMode<Base["jsx"], Candidate>;
|
|
341
|
+
html: RecipeLikeMode<Base["html"], Candidate>;
|
|
342
|
+
htmlObj: RecipeLikeMode<Base["htmlObj"], Candidate>;
|
|
343
|
+
}
|
|
344
|
+
: never;
|
|
289
345
|
|
|
290
346
|
type MergeExtendedVariants<T> = T extends readonly [infer First, ...infer Rest]
|
|
291
347
|
? ExtractVariants<First> & MergeExtendedVariants<Rest>
|
|
292
348
|
: {};
|
|
293
349
|
|
|
294
|
-
// Returns a
|
|
350
|
+
// Returns a recipe's effective variants so an intermediate recipe's
|
|
295
351
|
// static variant hides a grandparent's function variant from descendants.
|
|
296
|
-
//
|
|
352
|
+
// Recipe and its mode helpers instantiate ModalRecipe with the already
|
|
297
353
|
// merged MergeVariants<V, E>, so inferring V yields that effective chain.
|
|
298
|
-
type ExtractVariants<T> = T extends
|
|
354
|
+
type ExtractVariants<T> = T extends ModalRecipe<infer V, any> ? V : {};
|
|
299
355
|
|
|
300
356
|
// A function value in `variants` (a function variant) replaces any inherited
|
|
301
357
|
// variant for the same key. An object value merges value-by-value with an
|
|
@@ -320,7 +376,7 @@ type MergeVariantMaps<Child, Parent> = Omit<Parent, keyof Child> &
|
|
|
320
376
|
>;
|
|
321
377
|
};
|
|
322
378
|
|
|
323
|
-
export type MergeVariants<V, E extends
|
|
379
|
+
export type MergeVariants<V, E extends AnyRecipe[]> = MergeVariantMaps<
|
|
324
380
|
NoInfer<V>,
|
|
325
381
|
MergeExtendedVariants<E>
|
|
326
382
|
>;
|
|
@@ -425,7 +481,7 @@ type NullablePartial<T> =
|
|
|
425
481
|
|
|
426
482
|
export type ExtendableVariants<
|
|
427
483
|
V extends Variants,
|
|
428
|
-
E extends
|
|
484
|
+
E extends AnyRecipe[],
|
|
429
485
|
> = V & {
|
|
430
486
|
[K in keyof MergeExtendedVariants<E>]?:
|
|
431
487
|
| NullablePartial<MergeExtendedVariants<E>[K]>
|