-
Notifications
You must be signed in to change notification settings - Fork 462
Expand file tree
/
Copy pathparseAppearance.ts
More file actions
171 lines (150 loc) · 5.34 KB
/
Copy pathparseAppearance.ts
File metadata and controls
171 lines (150 loc) · 5.34 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
import type { DeepPartial } from '@clerk/shared/types';
import { fastDeepMergeAndReplace } from '@clerk/shared/utils';
import { baseTheme, getBaseTheme } from '../baseTheme';
import { createInternalTheme, defaultInternalTheme } from '../foundations';
import type { Appearance, CaptchaAppearanceOptions, Elements, Options, Theme } from '../internal/appearance';
import type { InternalTheme } from '../styledSystem';
import {
createColorScales,
createFonts,
createFontSizeScale,
createFontWeightScale,
createRadiiUnits,
createShadowsUnits,
createSpaceScale,
} from './parseVariables';
export type ParsedElements = Elements[];
export type ParsedInternalTheme = InternalTheme;
export type ParsedOptions = Required<Options>;
export type ParsedCaptcha = Required<CaptchaAppearanceOptions>;
type PublicAppearanceTopLevelKey = Exclude<
keyof Appearance,
keyof Theme | 'captcha' | 'cssLayerName' | 'elements' | 'layout' | 'theme' | 'variables'
>;
export type AppearanceCascade = {
globalAppearance?: Appearance;
appearance?: Appearance;
appearanceKey: PublicAppearanceTopLevelKey | 'impersonationFab' | 'enableOrganizationsPrompt';
};
export type ParsedAppearance = {
parsedElements: ParsedElements;
parsedInternalTheme: ParsedInternalTheme;
parsedOptions: ParsedOptions;
parsedCaptcha: ParsedCaptcha;
};
const defaultOptions: ParsedOptions = {
logoPlacement: 'inside',
socialButtonsPlacement: 'top',
socialButtonsVariant: 'auto',
logoImageUrl: '',
logoLinkUrl: '',
showOptionalFields: false,
helpPageUrl: '',
privacyPageUrl: '',
termsPageUrl: '',
shimmer: true,
animations: true,
unsafe_disableDevelopmentModeWarnings: false,
autoFocus: true,
};
const defaultCaptchaOptions: ParsedCaptcha = {
theme: 'auto',
size: 'normal',
language: '',
};
/**
* Parses the public appearance object.
* It splits the resulting styles into 2 objects: parsedElements, parsedInternalTheme
* parsedElements is used by the makeCustomizables HOC to handle per-element styling
* parsedInternalTheme is used by FlowCard/InternalThemeProvider for generic theming
* Both are injected by the AppearanceContext
*/
export const parseAppearance = (cascade: AppearanceCascade): ParsedAppearance => {
const { globalAppearance, appearance: componentAppearance, appearanceKey } = cascade;
const appearanceList: Appearance[] = [];
[globalAppearance, globalAppearance?.[appearanceKey as PublicAppearanceTopLevelKey], componentAppearance].forEach(a =>
expand(a, appearanceList),
);
const parsedInternalTheme = parseVariables(appearanceList);
const parsedOptions = parseOptions(appearanceList);
const parsedCaptcha = parseCaptcha(appearanceList);
if (
!appearanceList.find(a => {
//@ts-expect-error not public api
return !!a.simpleStyles;
})
) {
appearanceList.unshift(baseTheme);
}
const parsedElements = parseElements(
appearanceList.map(appearance => {
if (!appearance.elements || typeof appearance.elements !== 'function') {
return appearance;
}
const res = { ...appearance };
res.elements = appearance.elements({ theme: parsedInternalTheme });
return res;
}),
);
return { parsedElements, parsedInternalTheme, parsedOptions, parsedCaptcha };
};
const expand = (theme: Theme | undefined, cascade: any[]) => {
if (!theme) {
return;
}
const themeProperty = theme.theme;
if (themeProperty !== undefined) {
(Array.isArray(themeProperty) ? themeProperty : [themeProperty]).forEach(t => {
if (typeof t === 'string') {
expand(getBaseTheme(t), cascade);
} else {
expand(t as Theme, cascade);
}
});
}
cascade.push(theme);
};
const parseElements = (appearances: Appearance[]) => {
return appearances.map(appearance => ({ ...appearance?.elements }));
};
const parseOptions = (appearanceList: Appearance[]) => {
return { ...defaultOptions, ...appearanceList.reduce((acc, appearance) => ({ ...acc, ...appearance.options }), {}) };
};
const parseCaptcha = (appearanceList: Appearance[]) => {
return {
...defaultCaptchaOptions,
...appearanceList.reduce((acc, appearance) => {
if (appearance.captcha) {
const { theme: captchaTheme, size, language } = appearance.captcha;
return {
...acc,
...(captchaTheme && { theme: captchaTheme }),
...(size && { size }),
...(language && { language }),
};
}
return acc;
}, {} as Partial<CaptchaAppearanceOptions>),
};
};
const parseVariables = (appearances: Appearance[]) => {
const res = {} as DeepPartial<InternalTheme>;
fastDeepMergeAndReplace({ ...defaultInternalTheme }, res);
appearances.forEach(appearance => {
fastDeepMergeAndReplace(createInternalThemeFromVariables(appearance), res);
});
return res as ParsedInternalTheme;
};
const createInternalThemeFromVariables = (theme: Theme | undefined): DeepPartial<InternalTheme> => {
if (!theme) {
return {};
}
const colors = { ...createColorScales(theme) };
const radii = { ...createRadiiUnits(theme) };
const space = { ...createSpaceScale(theme) };
const fontSizes = { ...createFontSizeScale(theme) };
const fontWeights = { ...createFontWeightScale(theme) };
const fonts = { ...createFonts(theme) };
const shadows = { ...createShadowsUnits(theme) };
return createInternalTheme({ colors, radii, space, fontSizes, fontWeights, fonts, shadows } as any);
};