-
Notifications
You must be signed in to change notification settings - Fork 331
/
Copy pathclassGeneration.ts
204 lines (181 loc) · 6.03 KB
/
classGeneration.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
import type { Elements, ElementState } from '@clerk/types';
import type { FlowMetadata } from '../elements/contexts';
import type { ElementDescriptor, ElementId } from './elementDescriptors';
import { CLASS_PREFIX } from './elementDescriptors';
import type { ParsedElements } from './parseAppearance';
const STATE_PROP_TO_CLASSNAME = Object.freeze({
loading: ` ${CLASS_PREFIX}loading`,
error: ` ${CLASS_PREFIX}error`,
open: ` ${CLASS_PREFIX}open`,
active: ` ${CLASS_PREFIX}active`,
} as const);
const STATE_PROP_TO_SPECIFICITY = Object.freeze({
loading: 2,
error: 2,
open: 2,
active: 2,
} as const);
type PropsWithState = Partial<Record<'isLoading' | 'isDisabled' | 'hasError' | 'isOpen' | 'isActive', any>> &
Record<string, any>;
export const generateClassName = (
parsedElements: ParsedElements,
elemDescriptors: ElementDescriptor[],
elemId: ElementId | undefined,
props: PropsWithState | undefined,
) => {
const state = getElementState(props);
let className = '';
const css: unknown[] = [];
className = addClerkTargettableClassname(className, elemDescriptors);
className = addClerkTargettableIdClassname(className, elemDescriptors, elemId);
className = addClerkTargettableStateClass(className, state);
className = addClerkTargettableRequiredAttribute(className, props as any);
className = addUserProvidedClassnames(className, parsedElements, elemDescriptors, elemId, state);
addUserProvidedStyleRules(css, parsedElements, elemDescriptors, elemId, state);
return { className, css };
};
const addClerkTargettableClassname = (cn: string, elemDescriptors: ElementDescriptor[]) => {
// We want the most specific classname to be displayed first, so we reverse the order
for (let i = elemDescriptors.length - 1; i >= 0; i--) {
cn += elemDescriptors[i].targettableClassname + ' ';
}
return cn.trimEnd();
};
const addClerkTargettableIdClassname = (
cn: string,
elemDescriptors: ElementDescriptor[],
elemId: ElementId | undefined,
) => {
if (!elemId) {
return cn;
}
// We want the most specific classname to be displayed first, so we reverse the order
for (let i = elemDescriptors.length - 1; i >= 0; i--) {
cn = cn + ' ' + elemDescriptors[i].getTargettableIdClassname(elemId);
}
return cn;
};
const addClerkTargettableStateClass = (cn: string, state: ElementState | undefined) => {
return state ? cn + STATE_PROP_TO_CLASSNAME[state] : cn;
};
const addClerkTargettableRequiredAttribute = (cn: string, props: { isRequired?: boolean } | undefined) => {
return props && props.isRequired ? cn + ` ${CLASS_PREFIX}required` : cn;
};
export const appendEmojiSeparator = (generatedCn: string, otherCn?: string) => {
return generatedCn + (otherCn ? ' ' + otherCn : '') + ' 🔒️';
};
const addUserProvidedStyleRules = (
css: unknown[],
parsedElements: ParsedElements,
elemDescriptors: ElementDescriptor[],
elemId: ElementId | undefined,
state: ElementState | undefined,
) => {
for (let j = 0; j < elemDescriptors.length; j++) {
for (let i = 0; i < parsedElements.length; i++) {
addRulesFromElements(css, parsedElements[i], elemDescriptors[j], elemId, state);
}
}
};
const addUserProvidedClassnames = (
cn: string,
parsedElements: ParsedElements,
elemDescriptors: ElementDescriptor[],
elemId: ElementId | undefined,
state: ElementState | undefined,
) => {
for (let j = 0; j < elemDescriptors.length; j++) {
for (let i = 0; i < parsedElements.length; i++) {
cn = addClassnamesFromElements(cn, parsedElements[i], elemDescriptors[j], elemId, state);
}
}
return cn; //.trimEnd();
};
const getElementState = (props: PropsWithState | undefined): ElementState | undefined => {
if (!props) {
return undefined;
}
if (props.isLoading) {
return 'loading';
}
if (props.hasError) {
return 'error';
}
if (props.isOpen) {
return 'open';
}
if (props.isActive) {
return 'active';
}
return undefined;
};
const addStringClassname = (cn: string, val?: unknown) => (typeof val === 'string' ? cn + ' ' + val : cn);
const addStyleRuleObject = (css: unknown[], val: unknown, specificity = 0) => {
if (specificity) {
val && typeof val === 'object' && css.push({ ['&'.repeat(specificity)]: val });
} else {
val && typeof val === 'object' && css.push(val);
}
};
export const generateFlowClassname = (props: Pick<FlowMetadata, 'flow'>) => {
return CLASS_PREFIX + props.flow + '-root';
};
export const generateFlowPartClassname = (props: FlowMetadata) => {
if (!props.part) {
return '';
}
return CLASS_PREFIX + props.flow + '-' + props.part;
};
const addClassnamesFromElements = (
cn: string,
elements: Elements | undefined,
elemDescriptor: ElementDescriptor,
elemId: ElementId | undefined,
state: ElementState | undefined,
) => {
if (!elements) {
return cn;
}
type Key = keyof typeof elements;
cn = addStringClassname(cn, elements[elemDescriptor.objectKey as Key]);
if (elemId) {
cn = addStringClassname(cn, elements[elemDescriptor.getObjectKeyWithId(elemId) as Key]);
}
if (state) {
cn = addStringClassname(cn, elements[elemDescriptor.getObjectKeyWithState(state) as Key]);
}
if (elemId && state) {
cn = addStringClassname(cn, elements[elemDescriptor.getObjectKeyWithIdAndState(elemId, state) as Key]);
}
return cn;
};
const addRulesFromElements = (
css: unknown[],
elements: Elements | undefined,
elemDescriptor: ElementDescriptor,
elemId: ElementId | undefined,
state: ElementState | undefined,
) => {
if (!elements) {
return;
}
type Key = keyof typeof elements;
addStyleRuleObject(css, elements[elemDescriptor.objectKey as Key]);
if (elemId) {
addStyleRuleObject(css, elements[elemDescriptor.getObjectKeyWithId(elemId) as Key]);
}
if (state) {
addStyleRuleObject(
css,
elements[elemDescriptor.getObjectKeyWithState(state) as Key],
STATE_PROP_TO_SPECIFICITY[state],
);
}
if (elemId && state) {
addStyleRuleObject(
css,
elements[elemDescriptor.getObjectKeyWithIdAndState(elemId, state) as Key],
STATE_PROP_TO_SPECIFICITY[state],
);
}
};