forked from clerk/javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdva.ts
More file actions
230 lines (190 loc) · 6.9 KB
/
Copy pathdva.ts
File metadata and controls
230 lines (190 loc) · 6.9 KB
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
/**
* 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';
import { type DescriptorIdentifier, mergeDescriptors, type ParsedElements } from '~/contexts/AppearanceContext';
/* 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.
type ClassValue = ClassArray | ClassDictionary | DescriptorIdentifier | number | null | boolean | undefined;
type ClassDictionary = Record<DescriptorIdentifier, any>;
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<DVA>[]>(
...components: [...T]
): (
props?: (
| UnionToIntersection<
{
[K in keyof T]: VariantProps<T[K]>;
}[number]
>
| undefined
) &
DVAClassProp,
) => string;
}
/* cx
---------------------------------- */
export interface CX {
(...inputs: ClassValue[]): string;
}
export type CXOptions = Parameters<CX>;
export type CXReturn = ReturnType<CX>;
/* dva
============================================ */
type DVAConfigBase = { base?: ClassValue };
type DVAVariantShape = Record<string, Record<string, ClassValue>>;
type DVAVariantSchema<V extends DVAVariantShape> = {
[Variant in keyof V]?: StringToBoolean<keyof V[Variant]> | undefined;
};
type DVAClassProp = { descriptor?: ClassValue };
export interface DVA {
<_ extends "dva's generic parameters are restricted to internal use only.", V>(
config: V extends DVAVariantShape
? DVAConfigBase & {
variants?: V;
compoundVariants?: (V extends DVAVariantShape
? (
| DVAVariantSchema<V>
| {
[Variant in keyof V]?:
| StringToBoolean<keyof V[Variant]>
| StringToBoolean<keyof V[Variant]>[]
| undefined;
}
) &
DVAClassProp
: DVAClassProp)[];
defaultVariants?: DVAVariantSchema<V>;
}
: DVAConfigBase & {
variants?: never;
compoundVariants?: never;
defaultVariants?: never;
},
): (props?: V extends DVAVariantShape ? DVAVariantSchema<V> & DVAClassProp : DVAClassProp) => 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;
dva: DVA;
};
}
/* Exports
============================================ */
const falsyToString = <T, _>(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 dva: DVA = config => props => {
if (config?.variants == null) {
return cx(config?.base, props?.descriptor);
}
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, { descriptor, ...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, descriptor]
: acc,
[] as ClassValue[],
);
return cx(config?.base, getVariantClassNames, getCompoundVariantClassNames, props?.descriptor);
};
const compose: Compose =
(...components) =>
props => {
const propsWithoutDescriptor = Object.fromEntries(
Object.entries(props || {}).filter(([key]) => !['descriptor'].includes(key)),
);
return cx(components.map(component => component(propsWithoutDescriptor)) as ClassValue[], props?.descriptor);
};
return {
compose,
dva,
cx,
};
};
export const { compose, dva, cx } = defineConfig();
export function getDescriptors(elements: ParsedElements, descriptorList: string) {
const descriptors = descriptorList.split(' ') as (keyof ParsedElements)[];
return descriptors.map(d => {
const value = elements[d];
if (!value) {
console.warn(`Clerk: unable to access style configuration for descriptor: ${d}`);
}
return value;
});
}
export function applyDescriptors(elements: ParsedElements, descriptorList: string) {
const descriptors = getDescriptors(elements, descriptorList);
return mergeDescriptors(...descriptors);
}