responsive-class-variants 1.3.1 → 1.4.0
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/README.md +46 -27
- package/dist/helpers.d.ts +38 -0
- package/dist/helpers.js +139 -0
- package/dist/helpers.js.map +1 -0
- package/dist/index.d.ts +3 -117
- package/dist/index.js +2 -207
- package/dist/index.js.map +1 -1
- package/dist/rcv.d.ts +67 -0
- package/dist/rcv.js +75 -0
- package/dist/rcv.js.map +1 -0
- package/dist/types.d.ts +50 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +8 -7
package/README.md
CHANGED
|
@@ -8,7 +8,7 @@ rcv helps you create responsive class variants. It handles the logic of generati
|
|
|
8
8
|
- You just need to provide the base classes, the variants and optionally compound variants.
|
|
9
9
|
- **Slots support**: Create multiple class-generating functions for different parts of a component.
|
|
10
10
|
- **Framework agnostic**: Supports both `className` (React) and `class` (Svelte, Vue, SolidJS) props.
|
|
11
|
-
- You can use the default breakpoints (sm
|
|
11
|
+
- You can use the default breakpoints (`sm`, `md`, `lg`, `xl`) or teach TypeScript your own names with an optional **`breakpoints`** tuple on the config (type-only) or with **`createRcv`**.
|
|
12
12
|
- You can pass an optional onComplete callback to the createRcv function. This callback will be called with the generated classes. Helpful if you want to pass your classes to a library like twMerge.
|
|
13
13
|
|
|
14
14
|
## Installation
|
|
@@ -23,10 +23,12 @@ rcv is a function that takes a config object and returns a function that takes a
|
|
|
23
23
|
|
|
24
24
|
The config object has the following properties:
|
|
25
25
|
|
|
26
|
-
- base: The base classes that are always applied.
|
|
27
|
-
-
|
|
28
|
-
-
|
|
29
|
-
-
|
|
26
|
+
- **base** (non-slots): The base classes that are always applied.
|
|
27
|
+
- **slots** (slots API): An object mapping slot names to their base classes.
|
|
28
|
+
- **variants**: An object with the keys of the variants and the values are the values of the variants.
|
|
29
|
+
- **compoundVariants**: An array of compound variants that apply additional classes when multiple variants have specific values.
|
|
30
|
+
- **breakpoints** (optional): A `const` tuple of breakpoint names used **only for TypeScript** so responsive props are typed with your names (e.g. `mobile`, `tablet`). Not read at runtime. Omit to use the defaults `sm`, `md`, `lg`, `xl`.
|
|
31
|
+
- **onComplete**: An optional callback function that receives the generated classes and returns the final classes.
|
|
30
32
|
|
|
31
33
|
rcv works very well with tailwindcss but it can be used with any CSS solution.
|
|
32
34
|
|
|
@@ -282,13 +284,18 @@ const SIZES = {
|
|
|
282
284
|
|
|
283
285
|
The structure doesn't really matter, the classes just need to be in the compiled javascript to be picked up by the JIT compiler.
|
|
284
286
|
|
|
285
|
-
## Custom breakpoints
|
|
287
|
+
## Custom breakpoints
|
|
286
288
|
|
|
287
|
-
|
|
288
|
-
|
|
289
|
+
Runtime behavior is unchanged: class names are still prefixed with whatever breakpoint key you use (e.g. `mobile:`, `md:`). You only need to tell TypeScript which names are valid.
|
|
290
|
+
|
|
291
|
+
### Option A: `breakpoints` on `rcv` (recommended)
|
|
289
292
|
|
|
293
|
+
Pass a `const` tuple so responsive props are inferred for `initial` plus those keys only:
|
|
294
|
+
|
|
295
|
+
```ts
|
|
290
296
|
const getButtonVariants = rcv({
|
|
291
297
|
base: "px-4 py-2 rounded",
|
|
298
|
+
breakpoints: ["mobile", "tablet", "desktop"] as const,
|
|
292
299
|
variants: {
|
|
293
300
|
intent: {
|
|
294
301
|
primary: "bg-blue-500 text-white",
|
|
@@ -297,24 +304,37 @@ const getButtonVariants = rcv({
|
|
|
297
304
|
}
|
|
298
305
|
});
|
|
299
306
|
|
|
300
|
-
|
|
301
|
-
|
|
307
|
+
getButtonVariants({
|
|
308
|
+
intent: { initial: "primary", mobile: "secondary", desktop: "primary" }
|
|
309
|
+
});
|
|
310
|
+
```
|
|
302
311
|
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
312
|
+
The `breakpoints` field is **not** read at runtime; it exists so the compiler can infer the breakpoint union.
|
|
313
|
+
|
|
314
|
+
### Option B: `createRcv`
|
|
315
|
+
|
|
316
|
+
Same typing effect by passing the tuple to `createRcv` once (also type-only for the first argument):
|
|
317
|
+
|
|
318
|
+
```ts
|
|
319
|
+
const rcv = createRcv(["mobile", "tablet", "desktop"]);
|
|
320
|
+
|
|
321
|
+
const getButtonVariants = rcv({
|
|
322
|
+
base: "px-4 py-2 rounded",
|
|
309
323
|
variants: {
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
324
|
+
intent: {
|
|
325
|
+
primary: "bg-blue-500 text-white",
|
|
326
|
+
secondary: "bg-gray-200 text-gray-800"
|
|
313
327
|
}
|
|
314
328
|
}
|
|
315
329
|
});
|
|
330
|
+
|
|
331
|
+
getButtonVariants({
|
|
332
|
+
intent: { initial: "primary", mobile: "secondary", desktop: "primary" }
|
|
333
|
+
});
|
|
316
334
|
```
|
|
317
335
|
|
|
336
|
+
Custom breakpoints work with the slots API as well—add `breakpoints: [...] as const` next to `slots` / `variants`, or use `createRcv` as above.
|
|
337
|
+
|
|
318
338
|
## onComplete callback (via createRcv)
|
|
319
339
|
|
|
320
340
|
You can pass an optional onComplete callback to the createRcv function. This callback will be called with the generated classes. Helpful if you want to pass your classes to a library like tailwind Merge.
|
|
@@ -363,7 +383,7 @@ compoundVariants: [
|
|
|
363
383
|
|
|
364
384
|
rcv provides a helper type to make it easier to type your component props.
|
|
365
385
|
|
|
366
|
-
If you use the default breakpoints (sm
|
|
386
|
+
If you use the default breakpoints (`sm`, `md`, `lg`, `xl`), you can use the `ResponsiveValue` type to make existing props responsive.
|
|
367
387
|
|
|
368
388
|
```ts
|
|
369
389
|
type ButtonProps = {
|
|
@@ -374,16 +394,15 @@ type ButtonProps = {
|
|
|
374
394
|
};
|
|
375
395
|
```
|
|
376
396
|
|
|
377
|
-
If you use custom
|
|
397
|
+
If you use **custom** breakpoint names, pass the same union as the second generic parameter, or derive it once from your `const` tuple:
|
|
378
398
|
|
|
379
399
|
```ts
|
|
380
|
-
import {
|
|
400
|
+
import { type ResponsiveValue as RcvResponsiveValue } from "responsive-class-variants";
|
|
381
401
|
|
|
382
402
|
const breakpoints = ["tablet", "desktop", "wide"] as const;
|
|
403
|
+
type CustomBreakpoint = (typeof breakpoints)[number];
|
|
383
404
|
|
|
384
|
-
export
|
|
385
|
-
|
|
386
|
-
type Breakpoints = (typeof breakpoints)[number];
|
|
387
|
-
|
|
388
|
-
export type ResponsiveValue<T> = RcvResponsiveValue<T, Breakpoints>;
|
|
405
|
+
export type ResponsiveValue<T> = RcvResponsiveValue<T, CustomBreakpoint>;
|
|
389
406
|
```
|
|
407
|
+
|
|
408
|
+
When you define variants with `rcv({ breakpoints: [...] as const, ... })` or `createRcv(breakpoints)`, the returned function’s props are already inferred; you only need the snippet above for **separate** prop types (e.g. wrapping a design-system component).
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { CompoundVariantWithSlots, DefaultBreakpoints, ResponsiveClassesConfig, ResponsiveClassesConfigSlots, ResponsiveValue, SlotConfig, VariantConfig, VariantProps } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Maps a ResponsiveValue to a new ResponsiveValue using the provided mapper function. Singular values are passed through as is.
|
|
4
|
+
*
|
|
5
|
+
* @template V The type of the original value
|
|
6
|
+
* @template T The type of the mapped value
|
|
7
|
+
* @template B The type of breakpoints
|
|
8
|
+
* @param {ResponsiveValue<V, B>} value - The original ResponsiveValue to be mapped
|
|
9
|
+
* @param {function(V): T} mapper - A function that maps a ResponsiveValue to a new ResponsiveValue
|
|
10
|
+
* @returns {ResponsiveValue<T, B>} A new ResponsiveValue with the mapped values
|
|
11
|
+
*
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* const sizes = {
|
|
15
|
+
* initial: 'md',
|
|
16
|
+
* sm: 'lg',
|
|
17
|
+
* }
|
|
18
|
+
*
|
|
19
|
+
* const output = mapResponsiveValue(sizes, size => {
|
|
20
|
+
* switch (size) {
|
|
21
|
+
* case 'initial':
|
|
22
|
+
* return 'sm';
|
|
23
|
+
* case 'sm':
|
|
24
|
+
* return 'md';
|
|
25
|
+
* }
|
|
26
|
+
* });
|
|
27
|
+
*
|
|
28
|
+
* // console.log(output)
|
|
29
|
+
* {
|
|
30
|
+
* initial: 'sm',
|
|
31
|
+
* sm: 'md',
|
|
32
|
+
* }
|
|
33
|
+
*/
|
|
34
|
+
export declare const mapResponsiveValue: <V, T, B extends string = DefaultBreakpoints>(value: ResponsiveValue<V, B>, mapper: (value: V) => T) => ResponsiveValue<T, B>;
|
|
35
|
+
export declare const isSlotsConfig: <T extends VariantConfig, B extends string>(config: ResponsiveClassesConfig<T, B>) => config is ResponsiveClassesConfigSlots<T, Record<string, SlotConfig>, B>;
|
|
36
|
+
export declare const processVariantProps: <T extends VariantConfig, B extends string>(props: Omit<VariantProps<T, B>, "className" | "class">, variants: T | undefined, slotName?: string) => (string | (string | undefined)[] | undefined)[];
|
|
37
|
+
export declare const matchesCompoundVariant: <T extends VariantConfig, B extends string>(compound: Omit<CompoundVariantWithSlots<T, string, B>, "className" | "class">, props: Omit<VariantProps<T, B>, "className" | "class">) => boolean;
|
|
38
|
+
export declare const createSlotFunction: <T extends VariantConfig, B extends string>(slotConfig: SlotConfig, variants: T | undefined, compoundVariants: CompoundVariantWithSlots<T, string, B>[] | undefined, onComplete: ((classes: string) => string) | undefined, slotName: string) => ({ className, class: classFromProps, ...props }?: VariantProps<T, B>) => string;
|
package/dist/helpers.js
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { clsx } from "clsx";
|
|
2
|
+
const isSingularValue = (value) => !isBreakpointsMap(value);
|
|
3
|
+
const isBreakpointsMap = (value) => typeof value === "object" && value != null && !Array.isArray(value);
|
|
4
|
+
/**
|
|
5
|
+
* Maps a ResponsiveValue to a new ResponsiveValue using the provided mapper function. Singular values are passed through as is.
|
|
6
|
+
*
|
|
7
|
+
* @template V The type of the original value
|
|
8
|
+
* @template T The type of the mapped value
|
|
9
|
+
* @template B The type of breakpoints
|
|
10
|
+
* @param {ResponsiveValue<V, B>} value - The original ResponsiveValue to be mapped
|
|
11
|
+
* @param {function(V): T} mapper - A function that maps a ResponsiveValue to a new ResponsiveValue
|
|
12
|
+
* @returns {ResponsiveValue<T, B>} A new ResponsiveValue with the mapped values
|
|
13
|
+
*
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* const sizes = {
|
|
17
|
+
* initial: 'md',
|
|
18
|
+
* sm: 'lg',
|
|
19
|
+
* }
|
|
20
|
+
*
|
|
21
|
+
* const output = mapResponsiveValue(sizes, size => {
|
|
22
|
+
* switch (size) {
|
|
23
|
+
* case 'initial':
|
|
24
|
+
* return 'sm';
|
|
25
|
+
* case 'sm':
|
|
26
|
+
* return 'md';
|
|
27
|
+
* }
|
|
28
|
+
* });
|
|
29
|
+
*
|
|
30
|
+
* // console.log(output)
|
|
31
|
+
* {
|
|
32
|
+
* initial: 'sm',
|
|
33
|
+
* sm: 'md',
|
|
34
|
+
* }
|
|
35
|
+
*/
|
|
36
|
+
export const mapResponsiveValue = (value, mapper) => {
|
|
37
|
+
if (isSingularValue(value)) {
|
|
38
|
+
return mapper(value);
|
|
39
|
+
}
|
|
40
|
+
const result = {};
|
|
41
|
+
for (const key of Object.keys(value)) {
|
|
42
|
+
result[key] = mapper(value[key]);
|
|
43
|
+
}
|
|
44
|
+
return result;
|
|
45
|
+
};
|
|
46
|
+
// Helper functions for slots
|
|
47
|
+
export const isSlotsConfig = (config) => {
|
|
48
|
+
return "slots" in config;
|
|
49
|
+
};
|
|
50
|
+
const normalizeClassValue = (value) => {
|
|
51
|
+
if (Array.isArray(value)) {
|
|
52
|
+
return value.join(" ");
|
|
53
|
+
}
|
|
54
|
+
if (typeof value === "string") {
|
|
55
|
+
return value;
|
|
56
|
+
}
|
|
57
|
+
return undefined;
|
|
58
|
+
};
|
|
59
|
+
const prefixClasses = (classes, prefix) => classes.replace(/(\S+)/g, `${prefix}:$1`);
|
|
60
|
+
// Helper function to get variant value for a specific slot or base
|
|
61
|
+
const getVariantValue = (variants, key, value, slotName) => {
|
|
62
|
+
const variantValue = variants?.[key]?.[value];
|
|
63
|
+
// Early return if no variant value found
|
|
64
|
+
if (variantValue == null)
|
|
65
|
+
return undefined;
|
|
66
|
+
// Handle string or array values directly
|
|
67
|
+
if (typeof variantValue === "string" || Array.isArray(variantValue)) {
|
|
68
|
+
return normalizeClassValue(variantValue);
|
|
69
|
+
}
|
|
70
|
+
// Handle slot-specific values (object with slot keys)
|
|
71
|
+
if (slotName && slotName in variantValue) {
|
|
72
|
+
return normalizeClassValue(variantValue[slotName]);
|
|
73
|
+
}
|
|
74
|
+
return undefined;
|
|
75
|
+
};
|
|
76
|
+
// Helper function to process responsive values
|
|
77
|
+
const processResponsiveValue = (variants, key, value, slotName) => {
|
|
78
|
+
return Object.entries(value).map(([breakpoint, breakpointValue]) => {
|
|
79
|
+
const variantValue = getVariantValue(variants, key, breakpointValue, slotName);
|
|
80
|
+
if (!variantValue)
|
|
81
|
+
return undefined;
|
|
82
|
+
// If the breakpoint is initial, return without prefix
|
|
83
|
+
if (breakpoint === "initial") {
|
|
84
|
+
return variantValue;
|
|
85
|
+
}
|
|
86
|
+
// Otherwise, return with breakpoint prefix
|
|
87
|
+
return prefixClasses(variantValue, breakpoint);
|
|
88
|
+
});
|
|
89
|
+
};
|
|
90
|
+
// Helper function to process variant props into classes
|
|
91
|
+
export const processVariantProps = (props, variants, slotName) => {
|
|
92
|
+
return Object.entries(props).map(([key, propValue]) => {
|
|
93
|
+
const value = typeof propValue === "boolean" ? String(propValue) : propValue;
|
|
94
|
+
// Handle undefined values
|
|
95
|
+
if (!value)
|
|
96
|
+
return undefined;
|
|
97
|
+
// Handle singular values
|
|
98
|
+
if (typeof value === "string") {
|
|
99
|
+
return getVariantValue(variants, key, value, slotName);
|
|
100
|
+
}
|
|
101
|
+
// Handle responsive values
|
|
102
|
+
return processResponsiveValue(variants, key, value, slotName);
|
|
103
|
+
});
|
|
104
|
+
};
|
|
105
|
+
// Helper function to match compound variants
|
|
106
|
+
export const matchesCompoundVariant = (compound, props) => {
|
|
107
|
+
return Object.entries(compound).every(([key, value]) => {
|
|
108
|
+
const propValue = props[key];
|
|
109
|
+
// Direct comparison first, then try string conversion for boolean handling
|
|
110
|
+
return propValue === value || propValue === String(value);
|
|
111
|
+
});
|
|
112
|
+
};
|
|
113
|
+
// Helper function to extract class value from compound variant class prop
|
|
114
|
+
const getCompoundVariantSlotClass = (classValue, slotName) => {
|
|
115
|
+
if (!classValue)
|
|
116
|
+
return undefined;
|
|
117
|
+
if (typeof classValue === "object" && classValue[slotName]) {
|
|
118
|
+
return normalizeClassValue(classValue[slotName]);
|
|
119
|
+
}
|
|
120
|
+
if (typeof classValue === "string") {
|
|
121
|
+
return classValue;
|
|
122
|
+
}
|
|
123
|
+
return undefined;
|
|
124
|
+
};
|
|
125
|
+
export const createSlotFunction = (slotConfig, variants, compoundVariants, onComplete, slotName) => ({ className, class: classFromProps, ...props } = {}) => {
|
|
126
|
+
const responsiveClasses = processVariantProps(props, variants, slotName);
|
|
127
|
+
const compoundClasses = compoundVariants?.map(({ class: classFromCompound, className: classNameFromCompound, ...compound }) => {
|
|
128
|
+
if (matchesCompoundVariant(compound, props)) {
|
|
129
|
+
return [
|
|
130
|
+
getCompoundVariantSlotClass(classFromCompound, slotName),
|
|
131
|
+
getCompoundVariantSlotClass(classNameFromCompound, slotName),
|
|
132
|
+
];
|
|
133
|
+
}
|
|
134
|
+
return undefined;
|
|
135
|
+
});
|
|
136
|
+
const classes = clsx(slotConfig, responsiveClasses, compoundClasses, className, classFromProps);
|
|
137
|
+
return onComplete ? onComplete(classes) : classes;
|
|
138
|
+
};
|
|
139
|
+
//# sourceMappingURL=helpers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"helpers.js","sourceRoot":"/","sources":["helpers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAe5B,MAAM,eAAe,GAAG,CACvB,KAA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ots,\n\tDefaultBreakpoints,\n\tResponsiveClassesConfig,\n\tResponsiveClassesConfigSlots,\n\tResponsiveValue,\n\tSlotConfig,\n\tVariantConfig,\n\tVariantProps,\n\tVariantPropValue,\n} from \"./types\";\n\nconst isSingularValue = <A, B extends string>(\n\tvalue: ResponsiveValue<A, B>,\n): value is A => !isBreakpointsMap(value);\n\nconst isBreakpointsMap = <A, B extends string>(\n\tvalue: ResponsiveValue<A, B>,\n): value is BreakpointsMap<A, B> =>\n\ttypeof value === \"object\" && value != null && !Array.isArray(value);\n\n/**\n * Maps a ResponsiveValue to a new ResponsiveValue using the provided mapper function. Singular values are passed through as is.\n *\n * @template V The type of the original value\n * @template T The type of the mapped value\n * @template B The type of breakpoints\n * @param {ResponsiveValue<V, B>} value - The original ResponsiveValue to be mapped\n * @param {function(V): T} mapper - A function that maps a ResponsiveValue to a new ResponsiveValue\n * @returns {ResponsiveValue<T, B>} A new ResponsiveValue with the mapped values\n *\n *\n * @example\n * const sizes = {\n * initial: 'md',\n * sm: 'lg',\n * }\n *\n * const output = mapResponsiveValue(sizes, size => {\n *\tswitch (size) {\n *\t\tcase 'initial':\n *\t\treturn 'sm';\n *\t\tcase 'sm':\n *\t\t\treturn 'md';\n *\t\t}\n *\t});\n *\n * // console.log(output)\n * {\n *\tinitial: 'sm',\n *\tsm: 'md',\n * }\n */\nexport const mapResponsiveValue = <V, T, B extends string = DefaultBreakpoints>(\n\tvalue: ResponsiveValue<V, B>,\n\tmapper: (value: V) => T,\n): ResponsiveValue<T, B> => {\n\tif (isSingularValue(value)) {\n\t\treturn mapper(value);\n\t}\n\n\tconst result: Record<string, T> = {};\n\tfor (const key of Object.keys(value)) {\n\t\tresult[key] = mapper(value[key as keyof typeof value] as V);\n\t}\n\treturn result as BreakpointsMap<T, B>;\n};\n\n// Helper functions for slots\nexport const isSlotsConfig = <T extends VariantConfig, B extends string>(\n\tconfig: ResponsiveClassesConfig<T, B>,\n): config is ResponsiveClassesConfigSlots<T, Record<string, SlotConfig>, B> => {\n\treturn \"slots\" in config;\n};\n\nconst normalizeClassValue = (\n\tvalue: string | ReadonlyArray<string> | undefined,\n) => {\n\tif (Array.isArray(value)) {\n\t\treturn value.join(\" \");\n\t}\n\tif (typeof value === \"string\") {\n\t\treturn value;\n\t}\n\treturn undefined;\n};\n\nconst prefixClasses = (classes: string, prefix: string) =>\n\tclasses.replace(/(\\S+)/g, `${prefix}:$1`);\n\n// Helper function to get variant value for a specific slot or base\nconst getVariantValue = <T extends VariantConfig>(\n\tvariants: T | undefined,\n\tkey: keyof T,\n\tvalue: string,\n\tslotName?: string,\n): string | undefined => {\n\tconst variantValue = variants?.[key]?.[value];\n\n\t// Early return if no variant value found\n\tif (variantValue == null) return undefined;\n\n\t// Handle string or array values directly\n\tif (typeof variantValue === \"string\" || Array.isArray(variantValue)) {\n\t\treturn normalizeClassValue(variantValue);\n\t}\n\n\t// Handle slot-specific values (object with slot keys)\n\tif (slotName && slotName in variantValue) {\n\t\treturn normalizeClassValue(\n\t\t\t(variantValue as Record<string, string | ReadonlyArray<string>>)[\n\t\t\t\tslotName\n\t\t\t],\n\t\t);\n\t}\n\n\treturn undefined;\n};\n\n// Helper function to process responsive values\nconst processResponsiveValue = <T extends VariantConfig, B extends string>(\n\tvariants: T | undefined,\n\tkey: keyof T,\n\tvalue: Partial<BreakpointsMap<T, B>>,\n\tslotName?: string,\n) => {\n\treturn Object.entries(value).map(([breakpoint, breakpointValue]) => {\n\t\tconst variantValue = getVariantValue(\n\t\t\tvariants,\n\t\t\tkey,\n\t\t\tbreakpointValue as string,\n\t\t\tslotName,\n\t\t);\n\n\t\tif (!variantValue) return undefined;\n\n\t\t// If the breakpoint is initial, return without prefix\n\t\tif (breakpoint === \"initial\") {\n\t\t\treturn variantValue;\n\t\t}\n\n\t\t// Otherwise, return with breakpoint prefix\n\t\treturn prefixClasses(variantValue, breakpoint);\n\t});\n};\n\n// Helper function to process variant props into classes\nexport const processVariantProps = <T extends VariantConfig, B extends string>(\n\tprops: Omit<VariantProps<T, B>, \"className\" | \"class\">,\n\tvariants: T | undefined,\n\tslotName?: string,\n) => {\n\treturn Object.entries(props).map(\n\t\t([key, propValue]: [keyof T, VariantPropValue<T[keyof T], B>]) => {\n\t\t\tconst value =\n\t\t\t\ttypeof propValue === \"boolean\" ? String(propValue) : propValue;\n\n\t\t\t// Handle undefined values\n\t\t\tif (!value) return undefined;\n\n\t\t\t// Handle singular values\n\t\t\tif (typeof value === \"string\") {\n\t\t\t\treturn getVariantValue(variants, key, value, slotName);\n\t\t\t}\n\n\t\t\t// Handle responsive values\n\t\t\treturn processResponsiveValue(\n\t\t\t\tvariants,\n\t\t\t\tkey,\n\t\t\t\tvalue as Partial<BreakpointsMap<T, B>>,\n\t\t\t\tslotName,\n\t\t\t);\n\t\t},\n\t);\n};\n\n// Helper function to match compound variants\nexport const matchesCompoundVariant = <\n\tT extends VariantConfig,\n\tB extends string,\n>(\n\tcompound: Omit<CompoundVariantWithSlots<T, string, B>, \"className\" | \"class\">,\n\tprops: Omit<VariantProps<T, B>, \"className\" | \"class\">,\n) => {\n\treturn Object.entries(compound).every(([key, value]) => {\n\t\tconst propValue = props[key as keyof typeof props];\n\t\t// Direct comparison first, then try string conversion for boolean handling\n\t\treturn propValue === value || propValue === String(value);\n\t});\n};\n\n// Helper function to extract class value from compound variant class prop\nconst getCompoundVariantSlotClass = <S extends string>(\n\tclassValue: CompoundVariantClassValue<S> | undefined,\n\tslotName: string,\n): string | undefined => {\n\tif (!classValue) return undefined;\n\n\tif (typeof classValue === \"object\" && classValue[slotName as S]) {\n\t\treturn normalizeClassValue(classValue[slotName as S]);\n\t}\n\n\tif (typeof classValue === \"string\") {\n\t\treturn classValue;\n\t}\n\n\treturn undefined;\n};\n\nexport const createSlotFunction =\n\t<T extends VariantConfig, B extends string>(\n\t\tslotConfig: SlotConfig,\n\t\tvariants: T | undefined,\n\t\tcompoundVariants: CompoundVariantWithSlots<T, string, B>[] | undefined,\n\t\tonComplete: ((classes: string) => string) | undefined,\n\t\tslotName: string,\n\t) =>\n\t(\n\t\t{\n\t\t\tclassName,\n\t\t\tclass: classFromProps,\n\t\t\t...props\n\t\t}: VariantProps<T, B> = {} as VariantProps<T, B>,\n\t) => {\n\t\tconst responsiveClasses = processVariantProps(props, variants, slotName);\n\n\t\tconst compoundClasses = compoundVariants?.map(\n\t\t\t({\n\t\t\t\tclass: classFromCompound,\n\t\t\t\tclassName: classNameFromCompound,\n\t\t\t\t...compound\n\t\t\t}) => {\n\t\t\t\tif (matchesCompoundVariant(compound, props)) {\n\t\t\t\t\treturn [\n\t\t\t\t\t\tgetCompoundVariantSlotClass(classFromCompound, slotName),\n\t\t\t\t\t\tgetCompoundVariantSlotClass(classNameFromCompound, slotName),\n\t\t\t\t\t];\n\t\t\t\t}\n\t\t\t\treturn undefined;\n\t\t\t},\n\t\t);\n\n\t\tconst classes = clsx(\n\t\t\tslotConfig,\n\t\t\tresponsiveClasses,\n\t\t\tcompoundClasses,\n\t\t\tclassName,\n\t\t\tclassFromProps,\n\t\t);\n\t\treturn onComplete ? onComplete(classes) : classes;\n\t};\n"]}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,117 +1,3 @@
|
|
|
1
|
-
export
|
|
2
|
-
export
|
|
3
|
-
export type BreakpointsMap
|
|
4
|
-
initial: V;
|
|
5
|
-
} & Partial<{
|
|
6
|
-
[breakpoint in B]: V;
|
|
7
|
-
}>;
|
|
8
|
-
export type ResponsiveValue<T, B extends string = DefaultBreakpoints> = T | BreakpointsMap<T, B>;
|
|
9
|
-
/**
|
|
10
|
-
* Maps a ResponsiveValue to a new ResponsiveValue using the provided mapper function. Singular values are passed through as is.
|
|
11
|
-
*
|
|
12
|
-
* @template V The type of the original value
|
|
13
|
-
* @template T The type of the mapped value
|
|
14
|
-
* @template B The type of breakpoints
|
|
15
|
-
* @param {ResponsiveValue<V, B>} value - The original ResponsiveValue to be mapped
|
|
16
|
-
* @param {function(V): T} mapper - A function that maps a ResponsiveValue to a new ResponsiveValue
|
|
17
|
-
* @returns {ResponsiveValue<T, B>} A new ResponsiveValue with the mapped values
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
* @example
|
|
21
|
-
* const sizes = {
|
|
22
|
-
* initial: 'md',
|
|
23
|
-
* sm: 'lg',
|
|
24
|
-
* }
|
|
25
|
-
*
|
|
26
|
-
* const output = mapResponsiveValue(sizes, size => {
|
|
27
|
-
* switch (size) {
|
|
28
|
-
* case 'initial':
|
|
29
|
-
* return 'sm';
|
|
30
|
-
* case 'sm':
|
|
31
|
-
* return 'md';
|
|
32
|
-
* }
|
|
33
|
-
* });
|
|
34
|
-
*
|
|
35
|
-
* // console.log(output)
|
|
36
|
-
* {
|
|
37
|
-
* initial: 'sm',
|
|
38
|
-
* sm: 'md',
|
|
39
|
-
* }
|
|
40
|
-
*/
|
|
41
|
-
export declare const mapResponsiveValue: <V, T, B extends string = DefaultBreakpoints>(value: ResponsiveValue<V, B>, mapper: (value: V) => T) => ResponsiveValue<T, B>;
|
|
42
|
-
/**
|
|
43
|
-
* Start of rcv and types
|
|
44
|
-
*/
|
|
45
|
-
type ClassValue = string | ReadonlyArray<string>;
|
|
46
|
-
type ValueType = ClassValue | Record<string, ClassValue>;
|
|
47
|
-
type VariantValue = Record<string, ValueType>;
|
|
48
|
-
type VariantConfig = Record<string, VariantValue>;
|
|
49
|
-
type StringBoolean = "true" | "false";
|
|
50
|
-
type BooleanVariant = Partial<Record<StringBoolean, ClassValue | Record<string, ClassValue>>>;
|
|
51
|
-
type VariantPropValue<T, B extends string> = T extends BooleanVariant ? ResponsiveValue<boolean, B> | undefined : T extends Record<string, unknown> ? ResponsiveValue<keyof T, B> : never;
|
|
52
|
-
type VariantPropsBase<T extends VariantConfig, B extends string> = {
|
|
53
|
-
[K in keyof T]?: VariantPropValue<T[K], B>;
|
|
54
|
-
};
|
|
55
|
-
type VariantProps<T extends VariantConfig, B extends string> = VariantPropsBase<T, B> & {
|
|
56
|
-
className?: string;
|
|
57
|
-
class?: string;
|
|
58
|
-
};
|
|
59
|
-
type SlotConfig = ClassValue;
|
|
60
|
-
type CompoundVariantClassValue<S extends string> = string | Partial<Record<S, ClassValue>>;
|
|
61
|
-
type CompoundVariantWithSlots<T extends VariantConfig, S extends string, B extends string> = Partial<Omit<VariantProps<T, B>, "class" | "className">> & {
|
|
62
|
-
class?: CompoundVariantClassValue<S>;
|
|
63
|
-
className?: CompoundVariantClassValue<S>;
|
|
64
|
-
};
|
|
65
|
-
export declare function rcv<T extends VariantConfig = Record<never, VariantValue>, S extends Record<string, SlotConfig> = Record<string, SlotConfig>, B extends string = DefaultBreakpoints>(config: {
|
|
66
|
-
slots: S;
|
|
67
|
-
variants?: T;
|
|
68
|
-
compoundVariants?: CompoundVariantWithSlots<T, keyof S & string, B>[];
|
|
69
|
-
onComplete?: (classes: string) => string;
|
|
70
|
-
}): () => {
|
|
71
|
-
[K in keyof S]: (props?: VariantProps<T, B>) => string;
|
|
72
|
-
};
|
|
73
|
-
export declare function rcv<T extends VariantConfig = Record<never, VariantValue>, B extends string = DefaultBreakpoints>(config: {
|
|
74
|
-
base: string;
|
|
75
|
-
variants?: T;
|
|
76
|
-
compoundVariants?: Partial<VariantProps<T, B>>[];
|
|
77
|
-
onComplete?: (classes: string) => string;
|
|
78
|
-
}): (props?: VariantProps<T, B>) => string;
|
|
79
|
-
/**
|
|
80
|
-
* Creates a custom rcv function with custom breakpoints and an optional onComplete callback
|
|
81
|
-
*
|
|
82
|
-
* @template B - The custom breakpoints type
|
|
83
|
-
* @param breakpoints - Optional array of custom breakpoint names
|
|
84
|
-
* @param onComplete - Optional callback function that receives the generated classes and returns the final classes
|
|
85
|
-
* @returns A function that creates rcv with custom breakpoints
|
|
86
|
-
*
|
|
87
|
-
* @example
|
|
88
|
-
* const customRcv = createRcv(['mobile', 'tablet', 'desktop']);
|
|
89
|
-
*
|
|
90
|
-
* const getButtonVariants = customRcv({
|
|
91
|
-
* base: "px-4 py-2 rounded",
|
|
92
|
-
* variants: {
|
|
93
|
-
* intent: {
|
|
94
|
-
* primary: "bg-blue-500 text-white",
|
|
95
|
-
* secondary: "bg-gray-200 text-gray-800"
|
|
96
|
-
* }
|
|
97
|
-
* }
|
|
98
|
-
* });
|
|
99
|
-
*
|
|
100
|
-
* // Usage with custom breakpoints:
|
|
101
|
-
* getButtonVariants({ intent: { initial: "primary", mobile: "secondary", desktop: "primary" } })
|
|
102
|
-
*/
|
|
103
|
-
export declare const createRcv: <B extends string>(_breakpoints?: readonly B[], onComplete?: (classes: string) => string) => {
|
|
104
|
-
<T extends VariantConfig = Record<never, VariantValue>, S extends Record<string, SlotConfig> = Record<string, ClassValue>>(config: {
|
|
105
|
-
slots: S;
|
|
106
|
-
variants?: T;
|
|
107
|
-
compoundVariants?: CompoundVariantWithSlots<T, keyof S & string, B>[];
|
|
108
|
-
onComplete?: (classes: string) => string;
|
|
109
|
-
}): () => { [K in keyof S]: (props?: VariantProps<T, B>) => string; };
|
|
110
|
-
<T extends VariantConfig = Record<never, VariantValue>>(config: {
|
|
111
|
-
base: string;
|
|
112
|
-
variants?: T;
|
|
113
|
-
compoundVariants?: Partial<VariantProps<T, B>>[];
|
|
114
|
-
onComplete?: (classes: string) => string;
|
|
115
|
-
}): (props?: VariantProps<T, B>) => string;
|
|
116
|
-
};
|
|
117
|
-
export {};
|
|
1
|
+
export { mapResponsiveValue } from "./helpers.js";
|
|
2
|
+
export { createRcv, rcv } from "./rcv.js";
|
|
3
|
+
export type { Breakpoints, BreakpointsMap, DefaultBreakpoints, ResponsiveValue, } from "./types.js";
|
package/dist/index.js
CHANGED
|
@@ -1,208 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const isBreakpointsMap = (value) => typeof value === "object" && value != null && !Array.isArray(value);
|
|
4
|
-
/**
|
|
5
|
-
* Maps a ResponsiveValue to a new ResponsiveValue using the provided mapper function. Singular values are passed through as is.
|
|
6
|
-
*
|
|
7
|
-
* @template V The type of the original value
|
|
8
|
-
* @template T The type of the mapped value
|
|
9
|
-
* @template B The type of breakpoints
|
|
10
|
-
* @param {ResponsiveValue<V, B>} value - The original ResponsiveValue to be mapped
|
|
11
|
-
* @param {function(V): T} mapper - A function that maps a ResponsiveValue to a new ResponsiveValue
|
|
12
|
-
* @returns {ResponsiveValue<T, B>} A new ResponsiveValue with the mapped values
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
* @example
|
|
16
|
-
* const sizes = {
|
|
17
|
-
* initial: 'md',
|
|
18
|
-
* sm: 'lg',
|
|
19
|
-
* }
|
|
20
|
-
*
|
|
21
|
-
* const output = mapResponsiveValue(sizes, size => {
|
|
22
|
-
* switch (size) {
|
|
23
|
-
* case 'initial':
|
|
24
|
-
* return 'sm';
|
|
25
|
-
* case 'sm':
|
|
26
|
-
* return 'md';
|
|
27
|
-
* }
|
|
28
|
-
* });
|
|
29
|
-
*
|
|
30
|
-
* // console.log(output)
|
|
31
|
-
* {
|
|
32
|
-
* initial: 'sm',
|
|
33
|
-
* sm: 'md',
|
|
34
|
-
* }
|
|
35
|
-
*/
|
|
36
|
-
export const mapResponsiveValue = (value, mapper) => {
|
|
37
|
-
if (isSingularValue(value)) {
|
|
38
|
-
return mapper(value);
|
|
39
|
-
}
|
|
40
|
-
const result = {};
|
|
41
|
-
for (const key of Object.keys(value)) {
|
|
42
|
-
result[key] = mapper(value[key]);
|
|
43
|
-
}
|
|
44
|
-
return result;
|
|
45
|
-
};
|
|
46
|
-
// Helper functions for slots
|
|
47
|
-
const isSlotsConfig = (config) => {
|
|
48
|
-
return "slots" in config;
|
|
49
|
-
};
|
|
50
|
-
const normalizeClassValue = (value) => {
|
|
51
|
-
if (Array.isArray(value)) {
|
|
52
|
-
return value.join(" ");
|
|
53
|
-
}
|
|
54
|
-
if (typeof value === "string") {
|
|
55
|
-
return value;
|
|
56
|
-
}
|
|
57
|
-
return undefined;
|
|
58
|
-
};
|
|
59
|
-
const prefixClasses = (classes, prefix) => classes.replace(/(\S+)/g, `${prefix}:$1`);
|
|
60
|
-
// Helper function to get variant value for a specific slot or base
|
|
61
|
-
const getVariantValue = (variants, key, value, slotName) => {
|
|
62
|
-
const variantValue = variants?.[key]?.[value];
|
|
63
|
-
// Early return if no variant value found
|
|
64
|
-
if (variantValue == null)
|
|
65
|
-
return undefined;
|
|
66
|
-
// Handle string or array values directly
|
|
67
|
-
if (typeof variantValue === "string" || Array.isArray(variantValue)) {
|
|
68
|
-
return normalizeClassValue(variantValue);
|
|
69
|
-
}
|
|
70
|
-
// Handle slot-specific values (object with slot keys)
|
|
71
|
-
if (slotName && slotName in variantValue) {
|
|
72
|
-
return normalizeClassValue(variantValue[slotName]);
|
|
73
|
-
}
|
|
74
|
-
return undefined;
|
|
75
|
-
};
|
|
76
|
-
// Helper function to process responsive values
|
|
77
|
-
const processResponsiveValue = (variants, key, value, slotName) => {
|
|
78
|
-
return Object.entries(value).map(([breakpoint, breakpointValue]) => {
|
|
79
|
-
const variantValue = getVariantValue(variants, key, breakpointValue, slotName);
|
|
80
|
-
if (!variantValue)
|
|
81
|
-
return undefined;
|
|
82
|
-
// If the breakpoint is initial, return without prefix
|
|
83
|
-
if (breakpoint === "initial") {
|
|
84
|
-
return variantValue;
|
|
85
|
-
}
|
|
86
|
-
// Otherwise, return with breakpoint prefix
|
|
87
|
-
return prefixClasses(variantValue, breakpoint);
|
|
88
|
-
});
|
|
89
|
-
};
|
|
90
|
-
// Helper function to process variant props into classes
|
|
91
|
-
const processVariantProps = (props, variants, slotName) => {
|
|
92
|
-
return Object.entries(props).map(([key, propValue]) => {
|
|
93
|
-
const value = typeof propValue === "boolean" ? String(propValue) : propValue;
|
|
94
|
-
// Handle undefined values
|
|
95
|
-
if (!value)
|
|
96
|
-
return undefined;
|
|
97
|
-
// Handle singular values
|
|
98
|
-
if (typeof value === "string") {
|
|
99
|
-
return getVariantValue(variants, key, value, slotName);
|
|
100
|
-
}
|
|
101
|
-
// Handle responsive values
|
|
102
|
-
return processResponsiveValue(variants, key, value, slotName);
|
|
103
|
-
});
|
|
104
|
-
};
|
|
105
|
-
// Helper function to match compound variants
|
|
106
|
-
const matchesCompoundVariant = (compound, props) => {
|
|
107
|
-
return Object.entries(compound).every(([key, value]) => {
|
|
108
|
-
const propValue = props[key];
|
|
109
|
-
// Direct comparison first, then try string conversion for boolean handling
|
|
110
|
-
return propValue === value || propValue === String(value);
|
|
111
|
-
});
|
|
112
|
-
};
|
|
113
|
-
// Helper function to extract class value from compound variant class prop
|
|
114
|
-
const getCompoundVariantSlotClass = (classValue, slotName) => {
|
|
115
|
-
if (!classValue)
|
|
116
|
-
return undefined;
|
|
117
|
-
if (typeof classValue === "object" && classValue[slotName]) {
|
|
118
|
-
return normalizeClassValue(classValue[slotName]);
|
|
119
|
-
}
|
|
120
|
-
if (typeof classValue === "string") {
|
|
121
|
-
return classValue;
|
|
122
|
-
}
|
|
123
|
-
return undefined;
|
|
124
|
-
};
|
|
125
|
-
const createSlotFunction = (slotConfig, variants, compoundVariants, onComplete, slotName) => ({ className, class: classFromProps, ...props } = {}) => {
|
|
126
|
-
const responsiveClasses = processVariantProps(props, variants, slotName);
|
|
127
|
-
const compoundClasses = compoundVariants?.map(({ class: classFromCompound, className: classNameFromCompound, ...compound }) => {
|
|
128
|
-
if (matchesCompoundVariant(compound, props)) {
|
|
129
|
-
return [
|
|
130
|
-
getCompoundVariantSlotClass(classFromCompound, slotName),
|
|
131
|
-
getCompoundVariantSlotClass(classNameFromCompound, slotName),
|
|
132
|
-
];
|
|
133
|
-
}
|
|
134
|
-
return undefined;
|
|
135
|
-
});
|
|
136
|
-
const classes = clsx(slotConfig, responsiveClasses, compoundClasses, className, classFromProps);
|
|
137
|
-
return onComplete ? onComplete(classes) : classes;
|
|
138
|
-
};
|
|
139
|
-
export function rcv(config) {
|
|
140
|
-
// Check if config is a slots config
|
|
141
|
-
if (isSlotsConfig(config)) {
|
|
142
|
-
const { slots, variants, compoundVariants, onComplete } = config;
|
|
143
|
-
return () => {
|
|
144
|
-
const slotFunctions = {};
|
|
145
|
-
// Create slot functions for each slot - ensure all slots are always present
|
|
146
|
-
for (const [slotName, slotConfig] of Object.entries(slots)) {
|
|
147
|
-
const slotFunction = createSlotFunction(slotConfig, variants, compoundVariants, onComplete, slotName);
|
|
148
|
-
slotFunctions[slotName] = slotFunction;
|
|
149
|
-
}
|
|
150
|
-
return slotFunctions;
|
|
151
|
-
};
|
|
152
|
-
}
|
|
153
|
-
// If config is not a slots config, create a base function
|
|
154
|
-
const { base, variants, compoundVariants, onComplete } = config;
|
|
155
|
-
return ({ className, class: classFromProps, ...props } = {}) => {
|
|
156
|
-
const responsiveClasses = processVariantProps(props, variants);
|
|
157
|
-
const compoundClasses = compoundVariants?.map(({ className: compoundClassName, ...compound }) => {
|
|
158
|
-
if (matchesCompoundVariant(compound, props)) {
|
|
159
|
-
return compoundClassName;
|
|
160
|
-
}
|
|
161
|
-
return undefined;
|
|
162
|
-
});
|
|
163
|
-
const classes = clsx(base, responsiveClasses, compoundClasses, className, classFromProps);
|
|
164
|
-
return onComplete ? onComplete(classes) : classes;
|
|
165
|
-
};
|
|
166
|
-
}
|
|
167
|
-
/**
|
|
168
|
-
* Creates a custom rcv function with custom breakpoints and an optional onComplete callback
|
|
169
|
-
*
|
|
170
|
-
* @template B - The custom breakpoints type
|
|
171
|
-
* @param breakpoints - Optional array of custom breakpoint names
|
|
172
|
-
* @param onComplete - Optional callback function that receives the generated classes and returns the final classes
|
|
173
|
-
* @returns A function that creates rcv with custom breakpoints
|
|
174
|
-
*
|
|
175
|
-
* @example
|
|
176
|
-
* const customRcv = createRcv(['mobile', 'tablet', 'desktop']);
|
|
177
|
-
*
|
|
178
|
-
* const getButtonVariants = customRcv({
|
|
179
|
-
* base: "px-4 py-2 rounded",
|
|
180
|
-
* variants: {
|
|
181
|
-
* intent: {
|
|
182
|
-
* primary: "bg-blue-500 text-white",
|
|
183
|
-
* secondary: "bg-gray-200 text-gray-800"
|
|
184
|
-
* }
|
|
185
|
-
* }
|
|
186
|
-
* });
|
|
187
|
-
*
|
|
188
|
-
* // Usage with custom breakpoints:
|
|
189
|
-
* getButtonVariants({ intent: { initial: "primary", mobile: "secondary", desktop: "primary" } })
|
|
190
|
-
*/
|
|
191
|
-
export const createRcv = (_breakpoints, onComplete) => {
|
|
192
|
-
function customRcv(config) {
|
|
193
|
-
if (isSlotsConfig(config)) {
|
|
194
|
-
return rcv({
|
|
195
|
-
...config,
|
|
196
|
-
onComplete: onComplete || config.onComplete,
|
|
197
|
-
});
|
|
198
|
-
}
|
|
199
|
-
else {
|
|
200
|
-
return rcv({
|
|
201
|
-
...config,
|
|
202
|
-
onComplete: onComplete || config.onComplete,
|
|
203
|
-
});
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
return customRcv;
|
|
207
|
-
};
|
|
1
|
+
export { mapResponsiveValue } from "./helpers.js";
|
|
2
|
+
export { createRcv, rcv } from "./rcv.js";
|
|
208
3
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"/","sources":["index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAe5B,MAAM,eAAe,GAAG,CACvB,KAA4B,EACf,EAAE,CAAC,CAAC,gBAAgB,CAAC,KAAK,CAAC,CAAC;AAE1C,MAAM,gBAAgB,GAAG,CACxB,KAA4B,EACI,EAAE,CAClC,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,IAAI,IAAI,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AAErE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,CACjC,KAA4B,EAC5B,MAAuB,EACC,EAAE;IAC1B,IAAI,eAAe,CAAC,KAAK,CAAC,EAAE,CAAC;QAC5B,OAAO,MAAM,CAAC,KAAK,CAAC,CAAC;IACtB,CAAC;IAED,MAAM,MAA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;AAClB,CAAC,CAAC","sourcesContent":["import { clsx } from \"clsx\";\n\nexport type DefaultBreakpoints = \"sm\" | \"md\" | \"lg\" | \"xl\";\nexport type Breakpoints = DefaultBreakpoints;\n\nexport type BreakpointsMap<V, B extends string = DefaultBreakpoints> = {\n\tinitial: V;\n} & Partial<{\n\t[breakpoint in B]: V;\n}>;\n\nexport type ResponsiveValue<T, B extends string = DefaultBreakpoints> =\n\t| T\n\t| BreakpointsMap<T, B>;\n\nconst isSingularValue = <A, B extends string>(\n\tvalue: ResponsiveValue<A, B>,\n): value is A => !isBreakpointsMap(value);\n\nconst isBreakpointsMap = <A, B extends string>(\n\tvalue: ResponsiveValue<A, B>,\n): value is BreakpointsMap<A, B> =>\n\ttypeof value === \"object\" && value != null && !Array.isArray(value);\n\n/**\n * Maps a ResponsiveValue to a new ResponsiveValue using the provided mapper function. Singular values are passed through as is.\n *\n * @template V The type of the original value\n * @template T The type of the mapped value\n * @template B The type of breakpoints\n * @param {ResponsiveValue<V, B>} value - The original ResponsiveValue to be mapped\n * @param {function(V): T} mapper - A function that maps a ResponsiveValue to a new ResponsiveValue\n * @returns {ResponsiveValue<T, B>} A new ResponsiveValue with the mapped values\n *\n *\n * @example\n * const sizes = {\n * initial: 'md',\n * sm: 'lg',\n * }\n *\n * const output = mapResponsiveValue(sizes, size => {\n *\tswitch (size) {\n *\t\tcase 'initial':\n *\t\treturn 'sm';\n *\t\tcase 'sm':\n *\t\t\treturn 'md';\n *\t\t}\n *\t});\n *\n * // console.log(output)\n * {\n *\tinitial: 'sm',\n *\tsm: 'md',\n * }\n */\nexport const mapResponsiveValue = <V, T, B extends string = DefaultBreakpoints>(\n\tvalue: ResponsiveValue<V, B>,\n\tmapper: (value: V) => T,\n): ResponsiveValue<T, B> => {\n\tif (isSingularValue(value)) {\n\t\treturn mapper(value);\n\t}\n\n\tconst result: Record<string, T> = {};\n\tfor (const key of Object.keys(value)) {\n\t\tresult[key] = mapper(value[key as keyof typeof value] as V);\n\t}\n\treturn result as BreakpointsMap<T, B>;\n};\n\n/**\n * Start of rcv and types\n */\n\ntype ClassValue = string | ReadonlyArray<string>;\n\ntype ValueType = ClassValue | Record<string, ClassValue>;\n\ntype VariantValue = Record<string, ValueType>;\ntype VariantConfig = Record<string, VariantValue>;\n\ntype StringBoolean = \"true\" | \"false\";\ntype BooleanVariant = Partial<\n\tRecord<StringBoolean, ClassValue | Record<string, ClassValue>>\n>;\n\ntype VariantPropValue<T, B extends string> = T extends BooleanVariant\n\t? ResponsiveValue<boolean, B> | undefined\n\t: T extends Record<string, unknown>\n\t\t? ResponsiveValue<keyof T, B>\n\t\t: never;\n\ntype VariantPropsBase<T extends VariantConfig, B extends string> = {\n\t[K in keyof T]?: VariantPropValue<T[K], B>;\n};\n\ntype VariantProps<T extends VariantConfig, B extends string> = VariantPropsBase<\n\tT,\n\tB\n> & {\n\tclassName?: string;\n\tclass?: string;\n};\n\n// Slot configuration types\ntype SlotConfig = ClassValue;\n\ntype SlotsConfig<S extends Record<string, SlotConfig>> = S;\n\ntype CompoundVariantClassValue<S extends string> =\n\t| string\n\t| Partial<Record<S, ClassValue>>;\n\ntype CompoundVariantWithSlots<\n\tT extends VariantConfig,\n\tS extends string,\n\tB extends string,\n> = Partial<Omit<VariantProps<T, B>, \"class\" | \"className\">> & {\n\tclass?: CompoundVariantClassValue<S>;\n\tclassName?: CompoundVariantClassValue<S>;\n};\n\ntype ResponsiveClassesConfigBase<T extends VariantConfig, B extends string> = {\n\tbase: string;\n\tvariants?: T;\n\tcompoundVariants?: Partial<VariantProps<T, B>>[];\n\tonComplete?: (classes: string) => string;\n};\n\ntype ResponsiveClassesConfigSlots<\n\tT extends VariantConfig,\n\tS extends Record<string, SlotConfig>,\n\tB extends string,\n> = {\n\tslots: SlotsConfig<S>;\n\tvariants?: T;\n\tcompoundVariants?: CompoundVariantWithSlots<T, keyof S & string, B>[];\n\tonComplete?: (classes: string) => string;\n};\n\ntype ResponsiveClassesConfig<T extends VariantConfig, B extends string> =\n\t| ResponsiveClassesConfigBase<T, B>\n\t| ResponsiveClassesConfigSlots<T, Record<string, SlotConfig>, B>;\n\n// Helper functions for slots\nconst isSlotsConfig = <T extends VariantConfig, B extends string>(\n\tconfig: ResponsiveClassesConfig<T, B>,\n): config is ResponsiveClassesConfigSlots<T, Record<string, SlotConfig>, B> => {\n\treturn \"slots\" in config;\n};\n\nconst normalizeClassValue = (value: ClassValue | undefined) => {\n\tif (Array.isArray(value)) {\n\t\treturn value.join(\" \");\n\t}\n\tif (typeof value === \"string\") {\n\t\treturn value;\n\t}\n\treturn undefined;\n};\n\nconst prefixClasses = (classes: string, prefix: string) =>\n\tclasses.replace(/(\\S+)/g, `${prefix}:$1`);\n\n// Helper function to get variant value for a specific slot or base\nconst getVariantValue = <T extends VariantConfig>(\n\tvariants: T | undefined,\n\tkey: keyof T,\n\tvalue: string,\n\tslotName?: string,\n): string | undefined => {\n\tconst variantValue = variants?.[key]?.[value];\n\n\t// Early return if no variant value found\n\tif (variantValue == null) return undefined;\n\n\t// Handle string or array values directly\n\tif (typeof variantValue === \"string\" || Array.isArray(variantValue)) {\n\t\treturn normalizeClassValue(variantValue);\n\t}\n\n\t// Handle slot-specific values (object with slot keys)\n\tif (slotName && slotName in variantValue) {\n\t\treturn normalizeClassValue(\n\t\t\t(variantValue as Record<string, ClassValue>)[slotName],\n\t\t);\n\t}\n\n\treturn undefined;\n};\n\n// Helper function to process responsive values\nconst processResponsiveValue = <T extends VariantConfig, B extends string>(\n\tvariants: T | undefined,\n\tkey: keyof T,\n\tvalue: Partial<BreakpointsMap<T, B>>,\n\tslotName?: string,\n) => {\n\treturn Object.entries(value).map(([breakpoint, breakpointValue]) => {\n\t\tconst variantValue = getVariantValue(\n\t\t\tvariants,\n\t\t\tkey,\n\t\t\tbreakpointValue as string,\n\t\t\tslotName,\n\t\t);\n\n\t\tif (!variantValue) return undefined;\n\n\t\t// If the breakpoint is initial, return without prefix\n\t\tif (breakpoint === \"initial\") {\n\t\t\treturn variantValue;\n\t\t}\n\n\t\t// Otherwise, return with breakpoint prefix\n\t\treturn prefixClasses(variantValue, breakpoint);\n\t});\n};\n\n// Helper function to process variant props into classes\nconst processVariantProps = <T extends VariantConfig, B extends string>(\n\tprops: Omit<VariantProps<T, B>, \"className\" | \"class\">,\n\tvariants: T | undefined,\n\tslotName?: string,\n) => {\n\treturn Object.entries(props).map(\n\t\t([key, propValue]: [keyof T, VariantPropValue<T[keyof T], B>]) => {\n\t\t\tconst value =\n\t\t\t\ttypeof propValue === \"boolean\" ? String(propValue) : propValue;\n\n\t\t\t// Handle undefined values\n\t\t\tif (!value) return undefined;\n\n\t\t\t// Handle singular values\n\t\t\tif (typeof value === \"string\") {\n\t\t\t\treturn getVariantValue(variants, key, value, slotName);\n\t\t\t}\n\n\t\t\t// Handle responsive values\n\t\t\treturn processResponsiveValue(\n\t\t\t\tvariants,\n\t\t\t\tkey,\n\t\t\t\tvalue as Partial<BreakpointsMap<T, B>>,\n\t\t\t\tslotName,\n\t\t\t);\n\t\t},\n\t);\n};\n\n// Helper function to match compound variants\nconst matchesCompoundVariant = <T extends VariantConfig, B extends string>(\n\tcompound: Omit<CompoundVariantWithSlots<T, string, B>, \"className\" | \"class\">,\n\tprops: Omit<VariantProps<T, B>, \"className\" | \"class\">,\n) => {\n\treturn Object.entries(compound).every(([key, value]) => {\n\t\tconst propValue = props[key as keyof typeof props];\n\t\t// Direct comparison first, then try string conversion for boolean handling\n\t\treturn propValue === value || propValue === String(value);\n\t});\n};\n\n// Helper function to extract class value from compound variant class prop\nconst getCompoundVariantSlotClass = <S extends string>(\n\tclassValue: CompoundVariantClassValue<S> | undefined,\n\tslotName: string,\n): string | undefined => {\n\tif (!classValue) return undefined;\n\n\tif (typeof classValue === \"object\" && classValue[slotName as S]) {\n\t\treturn normalizeClassValue(classValue[slotName as S]);\n\t}\n\n\tif (typeof classValue === \"string\") {\n\t\treturn classValue;\n\t}\n\n\treturn undefined;\n};\n\nconst createSlotFunction =\n\t<T extends VariantConfig, B extends string>(\n\t\tslotConfig: SlotConfig,\n\t\tvariants: T | undefined,\n\t\tcompoundVariants: CompoundVariantWithSlots<T, string, B>[] | undefined,\n\t\tonComplete: ((classes: string) => string) | undefined,\n\t\tslotName: string,\n\t) =>\n\t(\n\t\t{\n\t\t\tclassName,\n\t\t\tclass: classFromProps,\n\t\t\t...props\n\t\t}: VariantProps<T, B> = {} as VariantProps<T, B>,\n\t) => {\n\t\tconst responsiveClasses = processVariantProps(props, variants, slotName);\n\n\t\tconst compoundClasses = compoundVariants?.map(\n\t\t\t({\n\t\t\t\tclass: classFromCompound,\n\t\t\t\tclassName: classNameFromCompound,\n\t\t\t\t...compound\n\t\t\t}) => {\n\t\t\t\tif (matchesCompoundVariant(compound, props)) {\n\t\t\t\t\treturn [\n\t\t\t\t\t\tgetCompoundVariantSlotClass(classFromCompound, slotName),\n\t\t\t\t\t\tgetCompoundVariantSlotClass(classNameFromCompound, slotName),\n\t\t\t\t\t];\n\t\t\t\t}\n\t\t\t\treturn undefined;\n\t\t\t},\n\t\t);\n\n\t\tconst classes = clsx(\n\t\t\tslotConfig,\n\t\t\tresponsiveClasses,\n\t\t\tcompoundClasses,\n\t\t\tclassName,\n\t\t\tclassFromProps,\n\t\t);\n\t\treturn onComplete ? onComplete(classes) : classes;\n\t};\n\n// Function overloads for rcv\nexport function rcv<\n\tT extends VariantConfig = Record<never, VariantValue>,\n\tS extends Record<string, SlotConfig> = Record<string, SlotConfig>,\n\tB extends string = DefaultBreakpoints,\n>(config: {\n\tslots: S;\n\tvariants?: T;\n\tcompoundVariants?: CompoundVariantWithSlots<T, keyof S & string, B>[];\n\tonComplete?: (classes: string) => string;\n}): () => {\n\t[K in keyof S]: (props?: VariantProps<T, B>) => string;\n};\n\nexport function rcv<\n\tT extends VariantConfig = Record<never, VariantValue>,\n\tB extends string = DefaultBreakpoints,\n>(config: {\n\tbase: string;\n\tvariants?: T;\n\tcompoundVariants?: Partial<VariantProps<T, B>>[];\n\tonComplete?: (classes: string) => string;\n}): (props?: VariantProps<T, B>) => string;\n\nexport function rcv<\n\tT extends VariantConfig = Record<never, VariantValue>,\n\tS extends Record<string, SlotConfig> = Record<string, SlotConfig>,\n\tB extends string = DefaultBreakpoints,\n>(\n\tconfig:\n\t\t| ResponsiveClassesConfig<T, B>\n\t\t| {\n\t\t\t\tslots: S;\n\t\t\t\tvariants?: T;\n\t\t\t\tcompoundVariants?: CompoundVariantWithSlots<T, keyof S & string, B>[];\n\t\t\t\tonComplete?: (classes: string) => string;\n\t\t },\n) {\n\t// Check if config is a slots config\n\tif (isSlotsConfig(config)) {\n\t\tconst { slots, variants, compoundVariants, onComplete } = config;\n\t\treturn () => {\n\t\t\tconst slotFunctions = {} as {\n\t\t\t\t[K in keyof S]: (props?: VariantProps<T, B>) => string;\n\t\t\t};\n\n\t\t\t// Create slot functions for each slot - ensure all slots are always present\n\t\t\tfor (const [slotName, slotConfig] of Object.entries(slots)) {\n\t\t\t\tconst slotFunction = createSlotFunction<T, B>(\n\t\t\t\t\tslotConfig,\n\t\t\t\t\tvariants,\n\t\t\t\t\tcompoundVariants,\n\t\t\t\t\tonComplete,\n\t\t\t\t\tslotName,\n\t\t\t\t);\n\n\t\t\t\tslotFunctions[slotName as keyof S] = slotFunction;\n\t\t\t}\n\n\t\t\treturn slotFunctions;\n\t\t};\n\t}\n\n\t// If config is not a slots config, create a base function\n\tconst { base, variants, compoundVariants, onComplete } = config;\n\treturn (\n\t\t{\n\t\t\tclassName,\n\t\t\tclass: classFromProps,\n\t\t\t...props\n\t\t}: VariantProps<T, B> = {} as VariantProps<T, B>,\n\t) => {\n\t\tconst responsiveClasses = processVariantProps(props, variants);\n\n\t\tconst compoundClasses = compoundVariants?.map(\n\t\t\t({ className: compoundClassName, ...compound }) => {\n\t\t\t\tif (\n\t\t\t\t\tmatchesCompoundVariant(\n\t\t\t\t\t\tcompound as Omit<\n\t\t\t\t\t\t\tCompoundVariantWithSlots<T, string, B>,\n\t\t\t\t\t\t\t\"className\" | \"class\"\n\t\t\t\t\t\t>,\n\t\t\t\t\t\tprops,\n\t\t\t\t\t)\n\t\t\t\t) {\n\t\t\t\t\treturn compoundClassName;\n\t\t\t\t}\n\t\t\t\treturn undefined;\n\t\t\t},\n\t\t);\n\n\t\tconst classes = clsx(\n\t\t\tbase,\n\t\t\tresponsiveClasses,\n\t\t\tcompoundClasses,\n\t\t\tclassName,\n\t\t\tclassFromProps,\n\t\t);\n\t\treturn onComplete ? onComplete(classes) : classes;\n\t};\n}\n\n/**\n * Creates a custom rcv function with custom breakpoints and an optional onComplete callback\n *\n * @template B - The custom breakpoints type\n * @param breakpoints - Optional array of custom breakpoint names\n * @param onComplete - Optional callback function that receives the generated classes and returns the final classes\n * @returns A function that creates rcv with custom breakpoints\n *\n * @example\n * const customRcv = createRcv(['mobile', 'tablet', 'desktop']);\n *\n * const getButtonVariants = customRcv({\n * base: \"px-4 py-2 rounded\",\n * variants: {\n * intent: {\n * primary: \"bg-blue-500 text-white\",\n * secondary: \"bg-gray-200 text-gray-800\"\n * }\n * }\n * });\n *\n * // Usage with custom breakpoints:\n * getButtonVariants({ intent: { initial: \"primary\", mobile: \"secondary\", desktop: \"primary\" } })\n */\n\nexport const createRcv = <B extends string>(\n\t_breakpoints?: readonly B[],\n\tonComplete?: (classes: string) => string,\n) => {\n\tfunction customRcv<\n\t\tT extends VariantConfig = Record<never, VariantValue>,\n\t\tS extends Record<string, SlotConfig> = Record<string, SlotConfig>,\n\t>(config: {\n\t\tslots: S;\n\t\tvariants?: T;\n\t\tcompoundVariants?: CompoundVariantWithSlots<T, keyof S & string, B>[];\n\t\tonComplete?: (classes: string) => string;\n\t}): () => {\n\t\t[K in keyof S]: (props?: VariantProps<T, B>) => string;\n\t};\n\n\tfunction customRcv<\n\t\tT extends VariantConfig = Record<never, VariantValue>,\n\t>(config: {\n\t\tbase: string;\n\t\tvariants?: T;\n\t\tcompoundVariants?: Partial<VariantProps<T, B>>[];\n\t\tonComplete?: (classes: string) => string;\n\t}): (props?: VariantProps<T, B>) => string;\n\n\tfunction customRcv<\n\t\tT extends VariantConfig = Record<never, VariantValue>,\n\t\tS extends Record<string, SlotConfig> = Record<string, SlotConfig>,\n\t>(\n\t\tconfig:\n\t\t\t| ResponsiveClassesConfig<T, B>\n\t\t\t| {\n\t\t\t\t\tslots: S;\n\t\t\t\t\tvariants?: T;\n\t\t\t\t\tcompoundVariants?: CompoundVariantWithSlots<T, keyof S & string, B>[];\n\t\t\t\t\tonComplete?: (classes: string) => string;\n\t\t\t },\n\t) {\n\t\tif (isSlotsConfig(config)) {\n\t\t\treturn rcv<T, S, B>({\n\t\t\t\t...config,\n\t\t\t\tonComplete: onComplete || config.onComplete,\n\t\t\t} as {\n\t\t\t\tslots: S;\n\t\t\t\tvariants?: T;\n\t\t\t\tcompoundVariants?: CompoundVariantWithSlots<T, keyof S & string, B>[];\n\t\t\t\tonComplete?: (classes: string) => string;\n\t\t\t});\n\t\t} else {\n\t\t\treturn rcv<T, B>({\n\t\t\t\t...config,\n\t\t\t\tonComplete: onComplete || config.onComplete,\n\t\t\t});\n\t\t}\n\t}\n\n\treturn customRcv;\n};\n"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"/","sources":["index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,WAAW,CAAC;AAC/C,OAAO,EAAE,SAAS,EAAE,GAAG,EAAE,MAAM,OAAO,CAAC","sourcesContent":["export { mapResponsiveValue } from \"./helpers\";\nexport { createRcv, rcv } from \"./rcv\";\nexport type {\n\tBreakpoints,\n\tBreakpointsMap,\n\tDefaultBreakpoints,\n\tResponsiveValue,\n} from \"./types\";\n"]}
|
package/dist/rcv.d.ts
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import type { CompoundVariantWithSlots, DefaultBreakpoints, SlotConfig, VariantConfig, VariantProps, VariantValue } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Builds responsive class strings from a config object.
|
|
4
|
+
*
|
|
5
|
+
* Pass **`breakpoints`** as a `const` tuple (e.g. `['mobile', 'tablet'] as const`) only when you want
|
|
6
|
+
* TypeScript to infer custom breakpoint names for responsive props. It is not read at runtime.
|
|
7
|
+
* Omit it to use the default `sm` / `md` / `lg` / `xl` breakpoints.
|
|
8
|
+
*/
|
|
9
|
+
export declare function rcv<T extends VariantConfig = Record<never, VariantValue>, S extends Record<string, SlotConfig> = Record<string, SlotConfig>, B extends string = DefaultBreakpoints>(config: {
|
|
10
|
+
slots: S;
|
|
11
|
+
variants?: T;
|
|
12
|
+
compoundVariants?: CompoundVariantWithSlots<T, keyof S & string, B>[];
|
|
13
|
+
breakpoints?: readonly B[];
|
|
14
|
+
onComplete?: (classes: string) => string;
|
|
15
|
+
}): () => {
|
|
16
|
+
[K in keyof S]: (props?: VariantProps<T, B>) => string;
|
|
17
|
+
};
|
|
18
|
+
export declare function rcv<T extends VariantConfig = Record<never, VariantValue>, B extends string = DefaultBreakpoints>(config: {
|
|
19
|
+
base: string;
|
|
20
|
+
variants?: T;
|
|
21
|
+
compoundVariants?: Partial<VariantProps<T, B>>[];
|
|
22
|
+
breakpoints?: readonly B[];
|
|
23
|
+
onComplete?: (classes: string) => string;
|
|
24
|
+
}): (props?: VariantProps<T, B>) => string;
|
|
25
|
+
/**
|
|
26
|
+
* Returns an `rcv` that uses custom breakpoint names for typing.
|
|
27
|
+
*
|
|
28
|
+
* The first argument is **type-only** (not read at runtime): it lets TypeScript infer `B`.
|
|
29
|
+
* You can get the same effect with {@link rcv} by passing `breakpoints: ['mobile', 'tablet'] as const` on the config object.
|
|
30
|
+
*
|
|
31
|
+
* @template B - The custom breakpoints type
|
|
32
|
+
* @param breakpoints - Optional tuple of custom breakpoint names (for inference only)
|
|
33
|
+
* @param onComplete - Optional callback applied to the merged class string
|
|
34
|
+
* @returns An `rcv` function whose props use `B` for responsive keys
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* const customRcv = createRcv(['mobile', 'tablet', 'desktop']);
|
|
38
|
+
*
|
|
39
|
+
* const getButtonVariants = customRcv({
|
|
40
|
+
* base: "px-4 py-2 rounded",
|
|
41
|
+
* variants: {
|
|
42
|
+
* intent: {
|
|
43
|
+
* primary: "bg-blue-500 text-white",
|
|
44
|
+
* secondary: "bg-gray-200 text-gray-800"
|
|
45
|
+
* }
|
|
46
|
+
* }
|
|
47
|
+
* });
|
|
48
|
+
*
|
|
49
|
+
* // Usage with custom breakpoints:
|
|
50
|
+
* getButtonVariants({ intent: { initial: "primary", mobile: "secondary", desktop: "primary" } })
|
|
51
|
+
*/
|
|
52
|
+
export declare const createRcv: <B extends string>(_breakpoints?: readonly B[], onComplete?: (classes: string) => string) => {
|
|
53
|
+
<T extends VariantConfig = Record<never, VariantValue>, S extends Record<string, SlotConfig> = Record<string, import("./types.js").ClassValue>>(config: {
|
|
54
|
+
slots: S;
|
|
55
|
+
variants?: T;
|
|
56
|
+
compoundVariants?: CompoundVariantWithSlots<T, keyof S & string, B>[];
|
|
57
|
+
breakpoints?: readonly B[];
|
|
58
|
+
onComplete?: (classes: string) => string;
|
|
59
|
+
}): () => { [K in keyof S]: (props?: VariantProps<T, B>) => string; };
|
|
60
|
+
<T extends VariantConfig = Record<never, VariantValue>>(config: {
|
|
61
|
+
base: string;
|
|
62
|
+
variants?: T;
|
|
63
|
+
compoundVariants?: Partial<VariantProps<T, B>>[];
|
|
64
|
+
breakpoints?: readonly B[];
|
|
65
|
+
onComplete?: (classes: string) => string;
|
|
66
|
+
}): (props?: VariantProps<T, B>) => string;
|
|
67
|
+
};
|
package/dist/rcv.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { clsx } from "clsx";
|
|
2
|
+
import { createSlotFunction, isSlotsConfig, matchesCompoundVariant, processVariantProps, } from "./helpers.js";
|
|
3
|
+
export function rcv(config) {
|
|
4
|
+
// Check if config is a slots config
|
|
5
|
+
if (isSlotsConfig(config)) {
|
|
6
|
+
const { slots, variants, compoundVariants, onComplete } = config;
|
|
7
|
+
return () => {
|
|
8
|
+
const slotFunctions = {};
|
|
9
|
+
// Create slot functions for each slot - ensure all slots are always present
|
|
10
|
+
for (const [slotName, slotConfig] of Object.entries(slots)) {
|
|
11
|
+
const slotFunction = createSlotFunction(slotConfig, variants, compoundVariants, onComplete, slotName);
|
|
12
|
+
slotFunctions[slotName] = slotFunction;
|
|
13
|
+
}
|
|
14
|
+
return slotFunctions;
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
// If config is not a slots config, create a base function
|
|
18
|
+
const { base, variants, compoundVariants, onComplete } = config;
|
|
19
|
+
return ({ className, class: classFromProps, ...props } = {}) => {
|
|
20
|
+
const responsiveClasses = processVariantProps(props, variants);
|
|
21
|
+
const compoundClasses = compoundVariants?.map(({ className: compoundClassName, ...compound }) => {
|
|
22
|
+
if (matchesCompoundVariant(compound, props)) {
|
|
23
|
+
return compoundClassName;
|
|
24
|
+
}
|
|
25
|
+
return undefined;
|
|
26
|
+
});
|
|
27
|
+
const classes = clsx(base, responsiveClasses, compoundClasses, className, classFromProps);
|
|
28
|
+
return onComplete ? onComplete(classes) : classes;
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Returns an `rcv` that uses custom breakpoint names for typing.
|
|
33
|
+
*
|
|
34
|
+
* The first argument is **type-only** (not read at runtime): it lets TypeScript infer `B`.
|
|
35
|
+
* You can get the same effect with {@link rcv} by passing `breakpoints: ['mobile', 'tablet'] as const` on the config object.
|
|
36
|
+
*
|
|
37
|
+
* @template B - The custom breakpoints type
|
|
38
|
+
* @param breakpoints - Optional tuple of custom breakpoint names (for inference only)
|
|
39
|
+
* @param onComplete - Optional callback applied to the merged class string
|
|
40
|
+
* @returns An `rcv` function whose props use `B` for responsive keys
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* const customRcv = createRcv(['mobile', 'tablet', 'desktop']);
|
|
44
|
+
*
|
|
45
|
+
* const getButtonVariants = customRcv({
|
|
46
|
+
* base: "px-4 py-2 rounded",
|
|
47
|
+
* variants: {
|
|
48
|
+
* intent: {
|
|
49
|
+
* primary: "bg-blue-500 text-white",
|
|
50
|
+
* secondary: "bg-gray-200 text-gray-800"
|
|
51
|
+
* }
|
|
52
|
+
* }
|
|
53
|
+
* });
|
|
54
|
+
*
|
|
55
|
+
* // Usage with custom breakpoints:
|
|
56
|
+
* getButtonVariants({ intent: { initial: "primary", mobile: "secondary", desktop: "primary" } })
|
|
57
|
+
*/
|
|
58
|
+
export const createRcv = (_breakpoints, onComplete) => {
|
|
59
|
+
function customRcv(config) {
|
|
60
|
+
if (isSlotsConfig(config)) {
|
|
61
|
+
return rcv({
|
|
62
|
+
...config,
|
|
63
|
+
onComplete: onComplete || config.onComplete,
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
return rcv({
|
|
68
|
+
...config,
|
|
69
|
+
onComplete: onComplete || config.onComplete,
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return customRcv;
|
|
74
|
+
};
|
|
75
|
+
//# sourceMappingURL=rcv.js.map
|
package/dist/rcv.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rcv.js","sourceRoot":"/","sources":["rcv.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAC5B,OAAO,EACN,kBAAkB,EAClB,aAAa,EACb,sBAAsB,EACtB,mBAAmB,GACnB,MAAM,WAAW,CAAC;AA4CnB,MAAM,UAAU,GAAG,CAKlB,MAQI;IAEJ,oCAAoC;IACpC,IAAI,aAAa,CAAC,MAAM,CAAC,EAAE,CAAC;QAC3B,MAAM,EAAE,KAAK,EAAE,QAAQ,EAAE,gBAAgB,EAAE,UAAU,EAAE,GAAG,MAAM,CAAC;QACjE,OAAO,GAAG,EAAE;YACX,MAAM,aAAa,GAAG,EAErB,CAAC;YAEF,4EAA4E;YAC5E,KAAK,MAAM,CAAC,Q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,CAAC,EAAE,CAAC;YAC3B,OAAO,GAAG,CAAU;gBACnB,GAAG,MAAM;gBACT,UAAU,EAAE,UAAU,IAAI,MAAM,CAAC,UAAU;aAM3C,CAAC,CAAC;QACJ,CAAC;aAAM,CAAC;YACP,OAAO,GAAG,CAAO;gBAChB,GAAG,MAAM;gBACT,UAAU,EAAE,UAAU,IAAI,MAAM,CAAC,UAAU;aAC3C,CAAC,CAAC;QACJ,CAAC;IACF,CAAC;IAED,OAAO,SAAS,CAAC;AAClB,CAAC,CAAC","sourcesContent":["import { clsx } from \"clsx\";\nimport {\n\tcreateSlotFunction,\n\tisSlotsConfig,\n\tmatchesCompoundVariant,\n\tprocessVariantProps,\n} from \"./helpers\";\nimport type {\n\tCompoundVariantWithSlots,\n\tDefaultBreakpoints,\n\tResponsiveClassesConfig,\n\tSlotConfig,\n\tVariantConfig,\n\tVariantProps,\n\tVariantValue,\n} from \"./types\";\n\n/**\n * Builds responsive class strings from a config object.\n *\n * Pass **`breakpoints`** as a `const` tuple (e.g. `['mobile', 'tablet'] as const`) only when you want\n * TypeScript to infer custom breakpoint names for responsive props. It is not read at runtime.\n * Omit it to use the default `sm` / `md` / `lg` / `xl` breakpoints.\n */\n// Function overloads for rcv\nexport function rcv<\n\tT extends VariantConfig = Record<never, VariantValue>,\n\tS extends Record<string, SlotConfig> = Record<string, SlotConfig>,\n\tB extends string = DefaultBreakpoints,\n>(config: {\n\tslots: S;\n\tvariants?: T;\n\tcompoundVariants?: CompoundVariantWithSlots<T, keyof S & string, B>[];\n\tbreakpoints?: readonly B[];\n\tonComplete?: (classes: string) => string;\n}): () => {\n\t[K in keyof S]: (props?: VariantProps<T, B>) => string;\n};\n\nexport function rcv<\n\tT extends VariantConfig = Record<never, VariantValue>,\n\tB extends string = DefaultBreakpoints,\n>(config: {\n\tbase: string;\n\tvariants?: T;\n\tcompoundVariants?: Partial<VariantProps<T, B>>[];\n\tbreakpoints?: readonly B[];\n\tonComplete?: (classes: string) => string;\n}): (props?: VariantProps<T, B>) => string;\n\nexport function rcv<\n\tT extends VariantConfig = Record<never, VariantValue>,\n\tS extends Record<string, SlotConfig> = Record<string, SlotConfig>,\n\tB extends string = DefaultBreakpoints,\n>(\n\tconfig:\n\t\t| ResponsiveClassesConfig<T, B>\n\t\t| {\n\t\t\t\tslots: S;\n\t\t\t\tvariants?: T;\n\t\t\t\tcompoundVariants?: CompoundVariantWithSlots<T, keyof S & string, B>[];\n\t\t\t\tbreakpoints?: readonly B[];\n\t\t\t\tonComplete?: (classes: string) => string;\n\t\t },\n) {\n\t// Check if config is a slots config\n\tif (isSlotsConfig(config)) {\n\t\tconst { slots, variants, compoundVariants, onComplete } = config;\n\t\treturn () => {\n\t\t\tconst slotFunctions = {} as {\n\t\t\t\t[K in keyof S]: (props?: VariantProps<T, B>) => string;\n\t\t\t};\n\n\t\t\t// Create slot functions for each slot - ensure all slots are always present\n\t\t\tfor (const [slotName, slotConfig] of Object.entries(slots)) {\n\t\t\t\tconst slotFunction = createSlotFunction<T, B>(\n\t\t\t\t\tslotConfig,\n\t\t\t\t\tvariants,\n\t\t\t\t\tcompoundVariants,\n\t\t\t\t\tonComplete,\n\t\t\t\t\tslotName,\n\t\t\t\t);\n\n\t\t\t\tslotFunctions[slotName as keyof S] = slotFunction;\n\t\t\t}\n\n\t\t\treturn slotFunctions;\n\t\t};\n\t}\n\n\t// If config is not a slots config, create a base function\n\tconst { base, variants, compoundVariants, onComplete } = config;\n\treturn (\n\t\t{\n\t\t\tclassName,\n\t\t\tclass: classFromProps,\n\t\t\t...props\n\t\t}: VariantProps<T, B> = {} as VariantProps<T, B>,\n\t) => {\n\t\tconst responsiveClasses = processVariantProps(props, variants);\n\n\t\tconst compoundClasses = compoundVariants?.map(\n\t\t\t({ className: compoundClassName, ...compound }) => {\n\t\t\t\tif (\n\t\t\t\t\tmatchesCompoundVariant(\n\t\t\t\t\t\tcompound as Omit<\n\t\t\t\t\t\t\tCompoundVariantWithSlots<T, string, B>,\n\t\t\t\t\t\t\t\"className\" | \"class\"\n\t\t\t\t\t\t>,\n\t\t\t\t\t\tprops,\n\t\t\t\t\t)\n\t\t\t\t) {\n\t\t\t\t\treturn compoundClassName;\n\t\t\t\t}\n\t\t\t\treturn undefined;\n\t\t\t},\n\t\t);\n\n\t\tconst classes = clsx(\n\t\t\tbase,\n\t\t\tresponsiveClasses,\n\t\t\tcompoundClasses,\n\t\t\tclassName,\n\t\t\tclassFromProps,\n\t\t);\n\t\treturn onComplete ? onComplete(classes) : classes;\n\t};\n}\n\n/**\n * Returns an `rcv` that uses custom breakpoint names for typing.\n *\n * The first argument is **type-only** (not read at runtime): it lets TypeScript infer `B`.\n * You can get the same effect with {@link rcv} by passing `breakpoints: ['mobile', 'tablet'] as const` on the config object.\n *\n * @template B - The custom breakpoints type\n * @param breakpoints - Optional tuple of custom breakpoint names (for inference only)\n * @param onComplete - Optional callback applied to the merged class string\n * @returns An `rcv` function whose props use `B` for responsive keys\n *\n * @example\n * const customRcv = createRcv(['mobile', 'tablet', 'desktop']);\n *\n * const getButtonVariants = customRcv({\n * base: \"px-4 py-2 rounded\",\n * variants: {\n * intent: {\n * primary: \"bg-blue-500 text-white\",\n * secondary: \"bg-gray-200 text-gray-800\"\n * }\n * }\n * });\n *\n * // Usage with custom breakpoints:\n * getButtonVariants({ intent: { initial: \"primary\", mobile: \"secondary\", desktop: \"primary\" } })\n */\n\nexport const createRcv = <B extends string>(\n\t_breakpoints?: readonly B[],\n\tonComplete?: (classes: string) => string,\n) => {\n\tfunction customRcv<\n\t\tT extends VariantConfig = Record<never, VariantValue>,\n\t\tS extends Record<string, SlotConfig> = Record<string, SlotConfig>,\n\t>(config: {\n\t\tslots: S;\n\t\tvariants?: T;\n\t\tcompoundVariants?: CompoundVariantWithSlots<T, keyof S & string, B>[];\n\t\tbreakpoints?: readonly B[];\n\t\tonComplete?: (classes: string) => string;\n\t}): () => {\n\t\t[K in keyof S]: (props?: VariantProps<T, B>) => string;\n\t};\n\n\tfunction customRcv<\n\t\tT extends VariantConfig = Record<never, VariantValue>,\n\t>(config: {\n\t\tbase: string;\n\t\tvariants?: T;\n\t\tcompoundVariants?: Partial<VariantProps<T, B>>[];\n\t\tbreakpoints?: readonly B[];\n\t\tonComplete?: (classes: string) => string;\n\t}): (props?: VariantProps<T, B>) => string;\n\n\tfunction customRcv<\n\t\tT extends VariantConfig = Record<never, VariantValue>,\n\t\tS extends Record<string, SlotConfig> = Record<string, SlotConfig>,\n\t>(\n\t\tconfig:\n\t\t\t| ResponsiveClassesConfig<T, B>\n\t\t\t| {\n\t\t\t\t\tslots: S;\n\t\t\t\t\tvariants?: T;\n\t\t\t\t\tcompoundVariants?: CompoundVariantWithSlots<T, keyof S & string, B>[];\n\t\t\t\t\tbreakpoints?: readonly B[];\n\t\t\t\t\tonComplete?: (classes: string) => string;\n\t\t\t },\n\t) {\n\t\tif (isSlotsConfig(config)) {\n\t\t\treturn rcv<T, S, B>({\n\t\t\t\t...config,\n\t\t\t\tonComplete: onComplete || config.onComplete,\n\t\t\t} as {\n\t\t\t\tslots: S;\n\t\t\t\tvariants?: T;\n\t\t\t\tcompoundVariants?: CompoundVariantWithSlots<T, keyof S & string, B>[];\n\t\t\t\tonComplete?: (classes: string) => string;\n\t\t\t});\n\t\t} else {\n\t\t\treturn rcv<T, B>({\n\t\t\t\t...config,\n\t\t\t\tonComplete: onComplete || config.onComplete,\n\t\t\t});\n\t\t}\n\t}\n\n\treturn customRcv;\n};\n"]}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
export type DefaultBreakpoints = "sm" | "md" | "lg" | "xl";
|
|
2
|
+
export type Breakpoints = DefaultBreakpoints;
|
|
3
|
+
export type BreakpointsMap<V, B extends string = DefaultBreakpoints> = {
|
|
4
|
+
initial: V;
|
|
5
|
+
} & Partial<{
|
|
6
|
+
[breakpoint in B]: V;
|
|
7
|
+
}>;
|
|
8
|
+
export type ResponsiveValue<T, B extends string = DefaultBreakpoints> = T | BreakpointsMap<T, B>;
|
|
9
|
+
type ClassValue = string | ReadonlyArray<string>;
|
|
10
|
+
type ValueType = ClassValue | Record<string, ClassValue>;
|
|
11
|
+
type VariantValue = Record<string, ValueType>;
|
|
12
|
+
type VariantConfig = Record<string, VariantValue>;
|
|
13
|
+
type StringBoolean = "true" | "false";
|
|
14
|
+
type BooleanVariant = Partial<Record<StringBoolean, ClassValue | Record<string, ClassValue>>>;
|
|
15
|
+
type VariantPropValue<T, B extends string> = T extends BooleanVariant ? ResponsiveValue<boolean, B> | undefined : T extends Record<string, unknown> ? ResponsiveValue<keyof T, B> : never;
|
|
16
|
+
type VariantPropsBase<T extends VariantConfig, B extends string> = {
|
|
17
|
+
[K in keyof T]?: VariantPropValue<T[K], B>;
|
|
18
|
+
};
|
|
19
|
+
type VariantProps<T extends VariantConfig, B extends string> = VariantPropsBase<T, B> & {
|
|
20
|
+
className?: string;
|
|
21
|
+
class?: string;
|
|
22
|
+
};
|
|
23
|
+
type SlotConfig = ClassValue;
|
|
24
|
+
type SlotsConfig<S extends Record<string, SlotConfig>> = S;
|
|
25
|
+
type CompoundVariantClassValue<S extends string> = string | Partial<Record<S, ClassValue>>;
|
|
26
|
+
type CompoundVariantWithSlots<T extends VariantConfig, S extends string, B extends string> = Partial<Omit<VariantProps<T, B>, "class" | "className">> & {
|
|
27
|
+
class?: CompoundVariantClassValue<S>;
|
|
28
|
+
className?: CompoundVariantClassValue<S>;
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Optional tuple of breakpoint names, used only so TypeScript can infer `B`.
|
|
32
|
+
* It is not read at runtime; omit it to use {@link DefaultBreakpoints}.
|
|
33
|
+
*/
|
|
34
|
+
type BreakpointsInference<B extends string> = readonly B[];
|
|
35
|
+
type ResponsiveClassesConfigBase<T extends VariantConfig, B extends string> = {
|
|
36
|
+
base: string;
|
|
37
|
+
variants?: T;
|
|
38
|
+
compoundVariants?: Partial<VariantProps<T, B>>[];
|
|
39
|
+
breakpoints?: BreakpointsInference<B>;
|
|
40
|
+
onComplete?: (classes: string) => string;
|
|
41
|
+
};
|
|
42
|
+
type ResponsiveClassesConfigSlots<T extends VariantConfig, S extends Record<string, SlotConfig>, B extends string> = {
|
|
43
|
+
slots: SlotsConfig<S>;
|
|
44
|
+
variants?: T;
|
|
45
|
+
compoundVariants?: CompoundVariantWithSlots<T, keyof S & string, B>[];
|
|
46
|
+
breakpoints?: BreakpointsInference<B>;
|
|
47
|
+
onComplete?: (classes: string) => string;
|
|
48
|
+
};
|
|
49
|
+
type ResponsiveClassesConfig<T extends VariantConfig, B extends string> = ResponsiveClassesConfigBase<T, B> | ResponsiveClassesConfigSlots<T, Record<string, SlotConfig>, B>;
|
|
50
|
+
export type { ClassValue, CompoundVariantClassValue, CompoundVariantWithSlots, ResponsiveClassesConfig, ResponsiveClassesConfigSlots, SlotConfig, SlotsConfig, VariantConfig, VariantProps, VariantPropValue, VariantValue, };
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"/","sources":["types.ts"],"names":[],"mappings":"","sourcesContent":["export type DefaultBreakpoints = \"sm\" | \"md\" | \"lg\" | \"xl\";\nexport type Breakpoints = DefaultBreakpoints;\n\nexport type BreakpointsMap<V, B extends string = DefaultBreakpoints> = {\n\tinitial: V;\n} & Partial<{\n\t[breakpoint in B]: V;\n}>;\n\nexport type ResponsiveValue<T, B extends string = DefaultBreakpoints> =\n\t| T\n\t| BreakpointsMap<T, B>;\n\ntype ClassValue = string | ReadonlyArray<string>;\n\ntype ValueType = ClassValue | Record<string, ClassValue>;\n\ntype VariantValue = Record<string, ValueType>;\ntype VariantConfig = Record<string, VariantValue>;\n\ntype StringBoolean = \"true\" | \"false\";\ntype BooleanVariant = Partial<\n\tRecord<StringBoolean, ClassValue | Record<string, ClassValue>>\n>;\n\ntype VariantPropValue<T, B extends string> = T extends BooleanVariant\n\t? ResponsiveValue<boolean, B> | undefined\n\t: T extends Record<string, unknown>\n\t\t? ResponsiveValue<keyof T, B>\n\t\t: never;\n\ntype VariantPropsBase<T extends VariantConfig, B extends string> = {\n\t[K in keyof T]?: VariantPropValue<T[K], B>;\n};\n\ntype VariantProps<T extends VariantConfig, B extends string> = VariantPropsBase<\n\tT,\n\tB\n> & {\n\tclassName?: string;\n\tclass?: string;\n};\n\n// Slot configuration types\ntype SlotConfig = ClassValue;\n\ntype SlotsConfig<S extends Record<string, SlotConfig>> = S;\n\ntype CompoundVariantClassValue<S extends string> =\n\t| string\n\t| Partial<Record<S, ClassValue>>;\n\ntype CompoundVariantWithSlots<\n\tT extends VariantConfig,\n\tS extends string,\n\tB extends string,\n> = Partial<Omit<VariantProps<T, B>, \"class\" | \"className\">> & {\n\tclass?: CompoundVariantClassValue<S>;\n\tclassName?: CompoundVariantClassValue<S>;\n};\n\n/**\n * Optional tuple of breakpoint names, used only so TypeScript can infer `B`.\n * It is not read at runtime; omit it to use {@link DefaultBreakpoints}.\n */\ntype BreakpointsInference<B extends string> = readonly B[];\n\ntype ResponsiveClassesConfigBase<T extends VariantConfig, B extends string> = {\n\tbase: string;\n\tvariants?: T;\n\tcompoundVariants?: Partial<VariantProps<T, B>>[];\n\tbreakpoints?: BreakpointsInference<B>;\n\tonComplete?: (classes: string) => string;\n};\n\ntype ResponsiveClassesConfigSlots<\n\tT extends VariantConfig,\n\tS extends Record<string, SlotConfig>,\n\tB extends string,\n> = {\n\tslots: SlotsConfig<S>;\n\tvariants?: T;\n\tcompoundVariants?: CompoundVariantWithSlots<T, keyof S & string, B>[];\n\tbreakpoints?: BreakpointsInference<B>;\n\tonComplete?: (classes: string) => string;\n};\n\ntype ResponsiveClassesConfig<T extends VariantConfig, B extends string> =\n\t| ResponsiveClassesConfigBase<T, B>\n\t| ResponsiveClassesConfigSlots<T, Record<string, SlotConfig>, B>;\n\nexport type {\n\tClassValue,\n\tCompoundVariantClassValue,\n\tCompoundVariantWithSlots,\n\tResponsiveClassesConfig,\n\tResponsiveClassesConfigSlots,\n\tSlotConfig,\n\tSlotsConfig,\n\tVariantConfig,\n\tVariantProps,\n\tVariantPropValue,\n\tVariantValue,\n};\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "responsive-class-variants",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0",
|
|
4
4
|
"description": "rcv helps you create responsive class variants",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"tailwindcss"
|
|
21
21
|
],
|
|
22
22
|
"scripts": {
|
|
23
|
-
"build": "tsc",
|
|
23
|
+
"build": "tsc && tsc-alias -f",
|
|
24
24
|
"test": "vitest",
|
|
25
25
|
"test:ci": "vitest run",
|
|
26
26
|
"lint": "biome check .",
|
|
@@ -28,14 +28,15 @@
|
|
|
28
28
|
},
|
|
29
29
|
"author": "Benedikt Sperl",
|
|
30
30
|
"license": "ISC",
|
|
31
|
-
"packageManager": "pnpm@
|
|
31
|
+
"packageManager": "pnpm@10.30.1",
|
|
32
32
|
"dependencies": {
|
|
33
33
|
"clsx": "^2.0.0"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
|
-
"@biomejs/biome": "2.
|
|
37
|
-
"lefthook": "^1.
|
|
38
|
-
"
|
|
39
|
-
"
|
|
36
|
+
"@biomejs/biome": "2.4.15",
|
|
37
|
+
"lefthook": "^2.1.8",
|
|
38
|
+
"tsc-alias": "^1.8.17",
|
|
39
|
+
"typescript": "^6.0.3",
|
|
40
|
+
"vitest": "^4.1.7"
|
|
40
41
|
}
|
|
41
42
|
}
|