-
-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathindex.ts
256 lines (220 loc) · 6.64 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/**
* Copyright 2022 Joe Bell. All rights reserved.
*
* This file is licensed to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
import { clsx } from "clsx";
/* Types
============================================ */
/* clsx
---------------------------------- */
// When compiling with `declaration: true`, many projects experience the dreaded
// TS2742 error. To combat this, we copy clsx's types manually.
// Should this project move to JSDoc, this workaround would no longer be needed.
export type ClassValue =
| ClassArray
| ClassDictionary
| string
| number
| bigint
| null
| boolean
| undefined;
export type ClassDictionary = Record<string, any>;
export type ClassArray = ClassValue[];
/* Utils
---------------------------------- */
type OmitUndefined<T> = T extends undefined ? never : T;
type StringToBoolean<T> = T extends "true" | "false" ? boolean : T;
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (
k: infer I,
) => void
? I
: never;
export type VariantProps<Component extends (...args: any) => any> = Omit<
OmitUndefined<Parameters<Component>[0]>,
"class" | "className"
>;
/* compose
---------------------------------- */
export interface Compose {
<T extends ReturnType<CVA>[]>(
...components: [...T]
): (
props?: (
| UnionToIntersection<
{
[K in keyof T]: VariantProps<T[K]>;
}[number]
>
| undefined
) &
CVAClassProp,
) => string;
}
/* cx
---------------------------------- */
export interface CX {
(...inputs: ClassValue[]): string;
}
export type CXOptions = Parameters<CX>;
export type CXReturn = ReturnType<CX>;
/* cva
============================================ */
type CVAConfigBase = { base?: ClassValue };
type CVAVariantShape = Record<string, Record<string, ClassValue>>;
type CVAVariantSchema<V extends CVAVariantShape> = {
[Variant in keyof V]?: StringToBoolean<keyof V[Variant]> | undefined;
};
type CVAClassProp =
| {
class?: ClassValue;
className?: never;
}
| {
class?: never;
className?: ClassValue;
};
export interface CVA {
<
_ extends "cva's generic parameters are restricted to internal use only.",
V,
>(
config: V extends CVAVariantShape
? CVAConfigBase & {
variants?: V;
compoundVariants?: (V extends CVAVariantShape
? (
| CVAVariantSchema<V>
| {
[Variant in keyof V]?:
| StringToBoolean<keyof V[Variant]>
| StringToBoolean<keyof V[Variant]>[]
| undefined;
}
) &
CVAClassProp
: CVAClassProp)[];
defaultVariants?: CVAVariantSchema<V>;
}
: CVAConfigBase & {
variants?: never;
compoundVariants?: never;
defaultVariants?: never;
},
): (
props?: V extends CVAVariantShape
? CVAVariantSchema<V> & CVAClassProp
: CVAClassProp,
) => string;
}
/* defineConfig
---------------------------------- */
export interface DefineConfigOptions {
hooks?: {
/**
* @deprecated please use `onComplete`
*/
"cx:done"?: (className: string) => string;
/**
* Returns the completed string of concatenated classes/classNames.
*/
onComplete?: (className: string) => string;
};
}
export interface DefineConfig {
(options?: DefineConfigOptions): {
compose: Compose;
cx: CX;
cva: CVA;
};
}
/* Exports
============================================ */
const falsyToString = <T extends unknown>(value: T) =>
typeof value === "boolean" ? `${value}` : value === 0 ? "0" : value;
export const defineConfig: DefineConfig = (options) => {
const cx: CX = (...inputs) => {
if (typeof options?.hooks?.["cx:done"] !== "undefined")
return options?.hooks["cx:done"](clsx(inputs));
if (typeof options?.hooks?.onComplete !== "undefined")
return options?.hooks.onComplete(clsx(inputs));
return clsx(inputs);
};
const cva: CVA = (config) => (props) => {
if (config?.variants == null)
return cx(config?.base, props?.class, props?.className);
const { variants, defaultVariants } = config;
const getVariantClassNames = Object.keys(variants).map(
(variant: keyof typeof variants) => {
const variantProp = props?.[variant as keyof typeof props];
const defaultVariantProp = defaultVariants?.[variant];
const variantKey = (falsyToString(variantProp) ||
falsyToString(
defaultVariantProp,
)) as keyof (typeof variants)[typeof variant];
return variants[variant][variantKey];
},
);
const defaultsAndProps = {
...defaultVariants,
// remove `undefined` props
...(props &&
Object.entries(props).reduce<typeof props>(
(acc, [key, value]) =>
typeof value === "undefined" ? acc : { ...acc, [key]: value },
{} as typeof props,
)),
};
const getCompoundVariantClassNames = config?.compoundVariants?.reduce(
(acc, { class: cvClass, className: cvClassName, ...cvConfig }) =>
Object.entries(cvConfig).every(([cvKey, cvSelector]) => {
const selector =
defaultsAndProps[cvKey as keyof typeof defaultsAndProps];
return Array.isArray(cvSelector)
? cvSelector.includes(selector)
: selector === cvSelector;
})
? [...acc, cvClass, cvClassName]
: acc,
[] as ClassValue[],
);
return cx(
config?.base,
getVariantClassNames,
getCompoundVariantClassNames,
props?.class,
props?.className,
);
};
const compose: Compose =
(...components) =>
(props) => {
const propsWithoutClass = Object.fromEntries(
Object.entries(props || {}).filter(
([key]) => !["class", "className"].includes(key),
),
);
return cx(
components.map((component) => component(propsWithoutClass)),
props?.class,
props?.className,
);
};
return {
compose,
cva,
cx,
};
};
export const { compose, cva, cx } = defineConfig();