clava 0.0.1 → 0.0.2

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/src/types.ts ADDED
@@ -0,0 +1,567 @@
1
+ import type * as CSS from "csstype";
2
+
3
+ export type ClassValue =
4
+ | string
5
+ | number
6
+ | boolean
7
+ | null
8
+ | undefined
9
+ | void
10
+ | ClassValue[];
11
+
12
+ export type JSXCSSProperties = CSS.Properties<string | number>;
13
+
14
+ export type HTMLCSSProperties = CSS.PropertiesHyphen<string | number>;
15
+
16
+ export type CSSProperties = CSS.Properties;
17
+
18
+ export type StyleProperty = JSXCSSProperties | HTMLCSSProperties | string;
19
+
20
+ export interface JSXProps {
21
+ className: string;
22
+ style: JSXCSSProperties;
23
+ }
24
+
25
+ export interface HTMLProps {
26
+ class: string;
27
+ style: string;
28
+ }
29
+
30
+ export interface HTMLObjProps {
31
+ class: string;
32
+ style: HTMLCSSProperties;
33
+ }
34
+
35
+ export interface StyleProps {
36
+ jsx: JSXProps;
37
+ html: HTMLProps;
38
+ htmlObj: HTMLObjProps;
39
+ }
40
+
41
+ export type ComponentResult = JSXProps | HTMLProps | HTMLObjProps;
42
+
43
+ export type ComponentProps<V = {}> = VariantValues<V> &
44
+ Partial<ComponentResult>;
45
+
46
+ export type GetVariants<V> = (variants?: VariantValues<V>) => VariantValues<V>;
47
+
48
+ // Key source types - what can be passed as additional parameters to splitProps
49
+ export type KeySourceArray = readonly string[];
50
+ export type KeySourceComponent = {
51
+ keys: readonly (string | number | symbol)[];
52
+ getVariants: () => Record<string, unknown>;
53
+ };
54
+ export type KeySource = KeySourceArray | KeySourceComponent;
55
+
56
+ // Extract keys from a source
57
+ type SourceKeys<S> = S extends readonly (infer K)[]
58
+ ? K
59
+ : S extends { keys: readonly (infer K)[] }
60
+ ? K
61
+ : never;
62
+
63
+ // Extract defaults from a source (components have defaults, arrays don't)
64
+ type SourceDefaults<S> = S extends { getVariants: () => infer Defaults }
65
+ ? Defaults
66
+ : {};
67
+
68
+ // Result type for one source - pick keys from T and add defaults
69
+ type SourceResult<T, S> = Pick<T, Extract<keyof T, SourceKeys<S>>> &
70
+ Omit<SourceDefaults<S>, keyof T>;
71
+
72
+ // Self result with defaults
73
+ type SelfResult<T, Keys, Defaults> = Pick<T, Extract<keyof T, Keys>> &
74
+ Omit<Defaults, keyof T>;
75
+
76
+ // All possible keys from ComponentResult (class, className, style)
77
+ type ComponentResultKeys =
78
+ | keyof JSXProps
79
+ | keyof HTMLProps
80
+ | keyof HTMLObjProps;
81
+
82
+ // Build result tuple based on number of sources
83
+ type SplitPropsResult<
84
+ T,
85
+ V,
86
+ D,
87
+ Sources extends readonly KeySource[],
88
+ > = Sources extends readonly []
89
+ ? [
90
+ SelfResult<T, keyof V | ComponentResultKeys, D>,
91
+ Omit<T, keyof V | ComponentResultKeys>,
92
+ ]
93
+ : Sources extends readonly [infer S1 extends KeySource]
94
+ ? [
95
+ SelfResult<T, keyof V | ComponentResultKeys, D>,
96
+ SourceResult<T, S1>,
97
+ Omit<T, keyof V | ComponentResultKeys | SourceKeys<S1>>,
98
+ ]
99
+ : Sources extends readonly [
100
+ infer S1 extends KeySource,
101
+ infer S2 extends KeySource,
102
+ ]
103
+ ? [
104
+ SelfResult<T, keyof V | ComponentResultKeys, D>,
105
+ SourceResult<T, S1>,
106
+ SourceResult<T, S2>,
107
+ Omit<
108
+ T,
109
+ keyof V | ComponentResultKeys | SourceKeys<S1> | SourceKeys<S2>
110
+ >,
111
+ ]
112
+ : Sources extends readonly [
113
+ infer S1 extends KeySource,
114
+ infer S2 extends KeySource,
115
+ infer S3 extends KeySource,
116
+ ]
117
+ ? [
118
+ SelfResult<T, keyof V | ComponentResultKeys, D>,
119
+ SourceResult<T, S1>,
120
+ SourceResult<T, S2>,
121
+ SourceResult<T, S3>,
122
+ Omit<
123
+ T,
124
+ | keyof V
125
+ | ComponentResultKeys
126
+ | SourceKeys<S1>
127
+ | SourceKeys<S2>
128
+ | SourceKeys<S3>
129
+ >,
130
+ ]
131
+ : Sources extends readonly [
132
+ infer S1 extends KeySource,
133
+ infer S2 extends KeySource,
134
+ infer S3 extends KeySource,
135
+ infer S4 extends KeySource,
136
+ ]
137
+ ? [
138
+ SelfResult<T, keyof V | ComponentResultKeys, D>,
139
+ SourceResult<T, S1>,
140
+ SourceResult<T, S2>,
141
+ SourceResult<T, S3>,
142
+ SourceResult<T, S4>,
143
+ Omit<
144
+ T,
145
+ | keyof V
146
+ | ComponentResultKeys
147
+ | SourceKeys<S1>
148
+ | SourceKeys<S2>
149
+ | SourceKeys<S3>
150
+ | SourceKeys<S4>
151
+ >,
152
+ ]
153
+ : Sources extends readonly [
154
+ infer S1 extends KeySource,
155
+ infer S2 extends KeySource,
156
+ infer S3 extends KeySource,
157
+ infer S4 extends KeySource,
158
+ infer S5 extends KeySource,
159
+ ]
160
+ ? [
161
+ SelfResult<T, keyof V | ComponentResultKeys, D>,
162
+ SourceResult<T, S1>,
163
+ SourceResult<T, S2>,
164
+ SourceResult<T, S3>,
165
+ SourceResult<T, S4>,
166
+ SourceResult<T, S5>,
167
+ Omit<
168
+ T,
169
+ | keyof V
170
+ | ComponentResultKeys
171
+ | SourceKeys<S1>
172
+ | SourceKeys<S2>
173
+ | SourceKeys<S3>
174
+ | SourceKeys<S4>
175
+ | SourceKeys<S5>
176
+ >,
177
+ ]
178
+ : Sources extends readonly [
179
+ infer S1 extends KeySource,
180
+ infer S2 extends KeySource,
181
+ infer S3 extends KeySource,
182
+ infer S4 extends KeySource,
183
+ infer S5 extends KeySource,
184
+ infer S6 extends KeySource,
185
+ ]
186
+ ? [
187
+ SelfResult<T, keyof V | ComponentResultKeys, D>,
188
+ SourceResult<T, S1>,
189
+ SourceResult<T, S2>,
190
+ SourceResult<T, S3>,
191
+ SourceResult<T, S4>,
192
+ SourceResult<T, S5>,
193
+ SourceResult<T, S6>,
194
+ Omit<
195
+ T,
196
+ | keyof V
197
+ | ComponentResultKeys
198
+ | SourceKeys<S1>
199
+ | SourceKeys<S2>
200
+ | SourceKeys<S3>
201
+ | SourceKeys<S4>
202
+ | SourceKeys<S5>
203
+ | SourceKeys<S6>
204
+ >,
205
+ ]
206
+ : Sources extends readonly [
207
+ infer S1 extends KeySource,
208
+ infer S2 extends KeySource,
209
+ infer S3 extends KeySource,
210
+ infer S4 extends KeySource,
211
+ infer S5 extends KeySource,
212
+ infer S6 extends KeySource,
213
+ infer S7 extends KeySource,
214
+ ]
215
+ ? [
216
+ SelfResult<T, keyof V | ComponentResultKeys, D>,
217
+ SourceResult<T, S1>,
218
+ SourceResult<T, S2>,
219
+ SourceResult<T, S3>,
220
+ SourceResult<T, S4>,
221
+ SourceResult<T, S5>,
222
+ SourceResult<T, S6>,
223
+ SourceResult<T, S7>,
224
+ Omit<
225
+ T,
226
+ | keyof V
227
+ | ComponentResultKeys
228
+ | SourceKeys<S1>
229
+ | SourceKeys<S2>
230
+ | SourceKeys<S3>
231
+ | SourceKeys<S4>
232
+ | SourceKeys<S5>
233
+ | SourceKeys<S6>
234
+ | SourceKeys<S7>
235
+ >,
236
+ ]
237
+ : Sources extends readonly [
238
+ infer S1 extends KeySource,
239
+ infer S2 extends KeySource,
240
+ infer S3 extends KeySource,
241
+ infer S4 extends KeySource,
242
+ infer S5 extends KeySource,
243
+ infer S6 extends KeySource,
244
+ infer S7 extends KeySource,
245
+ infer S8 extends KeySource,
246
+ ]
247
+ ? [
248
+ SelfResult<T, keyof V | ComponentResultKeys, D>,
249
+ SourceResult<T, S1>,
250
+ SourceResult<T, S2>,
251
+ SourceResult<T, S3>,
252
+ SourceResult<T, S4>,
253
+ SourceResult<T, S5>,
254
+ SourceResult<T, S6>,
255
+ SourceResult<T, S7>,
256
+ SourceResult<T, S8>,
257
+ Omit<
258
+ T,
259
+ | keyof V
260
+ | ComponentResultKeys
261
+ | SourceKeys<S1>
262
+ | SourceKeys<S2>
263
+ | SourceKeys<S3>
264
+ | SourceKeys<S4>
265
+ | SourceKeys<S5>
266
+ | SourceKeys<S6>
267
+ | SourceKeys<S7>
268
+ | SourceKeys<S8>
269
+ >,
270
+ ]
271
+ : unknown[];
272
+
273
+ // SplitProps as a single generic function type
274
+ export type SplitProps<V, D, _R extends ComponentResult> = <
275
+ const T extends Record<string, unknown>,
276
+ const Sources extends readonly KeySource[],
277
+ >(
278
+ props: T,
279
+ ...sources: Sources
280
+ ) => SplitPropsResult<T, V, D, Sources>;
281
+
282
+ // Build result tuple for OnlyVariants based on number of sources
283
+ type OnlyVariantsSplitPropsResult<
284
+ T,
285
+ V,
286
+ D,
287
+ Sources extends readonly KeySource[],
288
+ > = Sources extends readonly []
289
+ ? [SelfResult<T, keyof V, D>, Omit<T, keyof V>]
290
+ : Sources extends readonly [infer S1 extends KeySource]
291
+ ? [
292
+ SelfResult<T, keyof V, D>,
293
+ SourceResult<T, S1>,
294
+ Omit<T, keyof V | SourceKeys<S1>>,
295
+ ]
296
+ : Sources extends readonly [
297
+ infer S1 extends KeySource,
298
+ infer S2 extends KeySource,
299
+ ]
300
+ ? [
301
+ SelfResult<T, keyof V, D>,
302
+ SourceResult<T, S1>,
303
+ SourceResult<T, S2>,
304
+ Omit<T, keyof V | SourceKeys<S1> | SourceKeys<S2>>,
305
+ ]
306
+ : Sources extends readonly [
307
+ infer S1 extends KeySource,
308
+ infer S2 extends KeySource,
309
+ infer S3 extends KeySource,
310
+ ]
311
+ ? [
312
+ SelfResult<T, keyof V, D>,
313
+ SourceResult<T, S1>,
314
+ SourceResult<T, S2>,
315
+ SourceResult<T, S3>,
316
+ Omit<T, keyof V | SourceKeys<S1> | SourceKeys<S2> | SourceKeys<S3>>,
317
+ ]
318
+ : Sources extends readonly [
319
+ infer S1 extends KeySource,
320
+ infer S2 extends KeySource,
321
+ infer S3 extends KeySource,
322
+ infer S4 extends KeySource,
323
+ ]
324
+ ? [
325
+ SelfResult<T, keyof V, D>,
326
+ SourceResult<T, S1>,
327
+ SourceResult<T, S2>,
328
+ SourceResult<T, S3>,
329
+ SourceResult<T, S4>,
330
+ Omit<
331
+ T,
332
+ | keyof V
333
+ | SourceKeys<S1>
334
+ | SourceKeys<S2>
335
+ | SourceKeys<S3>
336
+ | SourceKeys<S4>
337
+ >,
338
+ ]
339
+ : Sources extends readonly [
340
+ infer S1 extends KeySource,
341
+ infer S2 extends KeySource,
342
+ infer S3 extends KeySource,
343
+ infer S4 extends KeySource,
344
+ infer S5 extends KeySource,
345
+ ]
346
+ ? [
347
+ SelfResult<T, keyof V, D>,
348
+ SourceResult<T, S1>,
349
+ SourceResult<T, S2>,
350
+ SourceResult<T, S3>,
351
+ SourceResult<T, S4>,
352
+ SourceResult<T, S5>,
353
+ Omit<
354
+ T,
355
+ | keyof V
356
+ | SourceKeys<S1>
357
+ | SourceKeys<S2>
358
+ | SourceKeys<S3>
359
+ | SourceKeys<S4>
360
+ | SourceKeys<S5>
361
+ >,
362
+ ]
363
+ : Sources extends readonly [
364
+ infer S1 extends KeySource,
365
+ infer S2 extends KeySource,
366
+ infer S3 extends KeySource,
367
+ infer S4 extends KeySource,
368
+ infer S5 extends KeySource,
369
+ infer S6 extends KeySource,
370
+ ]
371
+ ? [
372
+ SelfResult<T, keyof V, D>,
373
+ SourceResult<T, S1>,
374
+ SourceResult<T, S2>,
375
+ SourceResult<T, S3>,
376
+ SourceResult<T, S4>,
377
+ SourceResult<T, S5>,
378
+ SourceResult<T, S6>,
379
+ Omit<
380
+ T,
381
+ | keyof V
382
+ | SourceKeys<S1>
383
+ | SourceKeys<S2>
384
+ | SourceKeys<S3>
385
+ | SourceKeys<S4>
386
+ | SourceKeys<S5>
387
+ | SourceKeys<S6>
388
+ >,
389
+ ]
390
+ : Sources extends readonly [
391
+ infer S1 extends KeySource,
392
+ infer S2 extends KeySource,
393
+ infer S3 extends KeySource,
394
+ infer S4 extends KeySource,
395
+ infer S5 extends KeySource,
396
+ infer S6 extends KeySource,
397
+ infer S7 extends KeySource,
398
+ ]
399
+ ? [
400
+ SelfResult<T, keyof V, D>,
401
+ SourceResult<T, S1>,
402
+ SourceResult<T, S2>,
403
+ SourceResult<T, S3>,
404
+ SourceResult<T, S4>,
405
+ SourceResult<T, S5>,
406
+ SourceResult<T, S6>,
407
+ SourceResult<T, S7>,
408
+ Omit<
409
+ T,
410
+ | keyof V
411
+ | SourceKeys<S1>
412
+ | SourceKeys<S2>
413
+ | SourceKeys<S3>
414
+ | SourceKeys<S4>
415
+ | SourceKeys<S5>
416
+ | SourceKeys<S6>
417
+ | SourceKeys<S7>
418
+ >,
419
+ ]
420
+ : Sources extends readonly [
421
+ infer S1 extends KeySource,
422
+ infer S2 extends KeySource,
423
+ infer S3 extends KeySource,
424
+ infer S4 extends KeySource,
425
+ infer S5 extends KeySource,
426
+ infer S6 extends KeySource,
427
+ infer S7 extends KeySource,
428
+ infer S8 extends KeySource,
429
+ ]
430
+ ? [
431
+ SelfResult<T, keyof V, D>,
432
+ SourceResult<T, S1>,
433
+ SourceResult<T, S2>,
434
+ SourceResult<T, S3>,
435
+ SourceResult<T, S4>,
436
+ SourceResult<T, S5>,
437
+ SourceResult<T, S6>,
438
+ SourceResult<T, S7>,
439
+ SourceResult<T, S8>,
440
+ Omit<
441
+ T,
442
+ | keyof V
443
+ | SourceKeys<S1>
444
+ | SourceKeys<S2>
445
+ | SourceKeys<S3>
446
+ | SourceKeys<S4>
447
+ | SourceKeys<S5>
448
+ | SourceKeys<S6>
449
+ | SourceKeys<S7>
450
+ | SourceKeys<S8>
451
+ >,
452
+ ]
453
+ : unknown[];
454
+
455
+ // OnlyVariantsSplitProps as a single generic function type
456
+ export type OnlyVariantsSplitProps<V, D> = <
457
+ const T extends Record<string, unknown>,
458
+ const Sources extends readonly KeySource[],
459
+ >(
460
+ props: T,
461
+ ...sources: Sources
462
+ ) => OnlyVariantsSplitPropsResult<T, V, D, Sources>;
463
+
464
+ export interface OnlyVariantsComponent<V, D> {
465
+ splitProps: OnlyVariantsSplitProps<V, D>;
466
+ getVariants: GetVariants<V>;
467
+ keys: (keyof V)[];
468
+ }
469
+
470
+ export interface ModalComponent<V, D, R extends ComponentResult> {
471
+ (props?: ComponentProps<V>): R;
472
+ class: (props?: ComponentProps<V>) => string;
473
+ style: (props?: ComponentProps<V>) => R["style"];
474
+ splitProps: SplitProps<V, D, R>;
475
+ getVariants: GetVariants<V>;
476
+ keys: (keyof V | keyof R)[];
477
+ onlyVariants: OnlyVariantsComponent<V, D>;
478
+ /** @internal Base class without variants */
479
+ _baseClass: string;
480
+ }
481
+
482
+ export interface Component<
483
+ V extends Variants = {},
484
+ CV extends ComputedVariants = {},
485
+ E extends AnyComponent[] = [],
486
+ R extends ComponentResult = ComponentResult,
487
+ D = VariantValues<MergeVariants<V, CV, E>>,
488
+ > extends ModalComponent<MergeVariants<V, CV, E>, D, R> {
489
+ jsx: ModalComponent<MergeVariants<V, CV, E>, D, JSXProps>;
490
+ html: ModalComponent<MergeVariants<V, CV, E>, D, HTMLProps>;
491
+ htmlObj: ModalComponent<MergeVariants<V, CV, E>, D, HTMLObjProps>;
492
+ }
493
+
494
+ export type AnyComponent =
495
+ | Component<any, any, any, any, any>
496
+ | ModalComponent<any, any, any>;
497
+
498
+ type MergeExtendedVariants<T> = T extends readonly [infer First, ...infer Rest]
499
+ ? ExtractVariants<First> & MergeExtendedVariants<Rest>
500
+ : {};
501
+
502
+ type MergeExtendedComputedVariants<T> = T extends readonly [
503
+ infer First,
504
+ ...infer Rest,
505
+ ]
506
+ ? ExtractComputedVariants<First> & MergeExtendedComputedVariants<Rest>
507
+ : {};
508
+
509
+ type ExtractVariants<T> =
510
+ T extends Component<infer V, any, infer E, any>
511
+ ? V & MergeExtendedVariants<E>
512
+ : {};
513
+
514
+ type ExtractComputedVariants<T> =
515
+ T extends Component<any, infer CV, infer E, any>
516
+ ? CV & Omit<MergeExtendedComputedVariants<E>, keyof CV>
517
+ : {};
518
+
519
+ export type MergeVariants<V, CV, E extends AnyComponent[]> = NoInfer<CV> &
520
+ Omit<NoInfer<V>, keyof CV> &
521
+ Omit<MergeExtendedVariants<E>, keyof CV> &
522
+ Omit<MergeExtendedComputedVariants<E>, keyof CV>;
523
+
524
+ type StringToBoolean<T> = T extends "true" | "false" ? boolean : T;
525
+
526
+ type VariantValue = ClassValue | StyleClassValue;
527
+
528
+ type ExtractVariantValue<T> = T extends (value: infer V) => any
529
+ ? V
530
+ : T extends ClassValue
531
+ ? boolean
532
+ : T extends Record<infer K, any>
533
+ ? StringToBoolean<K>
534
+ : never;
535
+
536
+ export type VariantValues<V> = {
537
+ [K in keyof V]?: ExtractVariantValue<V[K]>;
538
+ };
539
+
540
+ export type StyleValue = CSS.Properties & {
541
+ [key: `--${string}`]: string;
542
+ };
543
+
544
+ export type StyleClassValue = StyleValue & { class?: ClassValue };
545
+
546
+ export interface ComputedContext<V> {
547
+ variants: VariantValues<V>;
548
+ setVariants: (variants: VariantValues<V>) => void;
549
+ setDefaultVariants: (variants: VariantValues<V>) => void;
550
+ }
551
+
552
+ export type Computed<V> = (context: ComputedContext<V>) => VariantValue;
553
+
554
+ export type ComputedVariant = (value: any) => VariantValue;
555
+ export type ComputedVariants = Record<string, ComputedVariant>;
556
+ export type Variant = ClassValue | Record<string, VariantValue>;
557
+ export type Variants = Record<string, Variant>;
558
+
559
+ type ExtendedVariants<E extends AnyComponent[]> = MergeExtendedVariants<E> &
560
+ MergeExtendedComputedVariants<E>;
561
+
562
+ export type ExtendableVariants<
563
+ V extends Variants,
564
+ E extends AnyComponent[],
565
+ > = V & {
566
+ [K in keyof ExtendedVariants<E>]?: Partial<ExtendedVariants<E>[K]> | Variant;
567
+ };