-
Notifications
You must be signed in to change notification settings - Fork 330
/
Copy pathparseVariables.ts
113 lines (100 loc) · 4.24 KB
/
parseVariables.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
import type { Theme } from '@clerk/types';
import { spaceScaleKeys } from '../foundations/sizes';
import type { fontSizes, fontWeights } from '../foundations/typography';
import {
colorOptionToHslaAlphaScale,
colorOptionToHslaLightnessScale,
colors,
fromEntries,
removeUndefinedProps,
} from '../utils';
export const createColorScales = (theme: Theme) => {
const variables = theme.variables || {};
//values dependent on other values
const colorTextSecondary = toHSLA(variables.colorTextSecondary) || colors.makeTransparent(variables.colorText, 0.15);
const colorTextTertiary = toHSLA(variables.colorTextTertiary) || colors.makeTransparent(colorTextSecondary, 0.4);
const colorTextLabel = toHSLA(variables.colorTextLabel) || colors.makeTransparent(variables.colorText, 0.05);
const primaryScale = colorOptionToHslaLightnessScale(variables.colorPrimary, 'primary');
return removeUndefinedProps({
...primaryScale,
...colorOptionToHslaAlphaScale(variables.colorPrimary, 'primaryAlpha'),
...colorOptionToHslaLightnessScale(variables.colorDanger, 'danger'),
...colorOptionToHslaAlphaScale(variables.colorDanger, 'dangerAlpha'),
...colorOptionToHslaLightnessScale(variables.colorSuccess, 'success'),
...colorOptionToHslaAlphaScale(variables.colorSuccess, 'successAlpha'),
...colorOptionToHslaLightnessScale(variables.colorWarning, 'warning'),
...colorOptionToHslaAlphaScale(variables.colorWarning, 'warningAlpha'),
...colorOptionToHslaAlphaScale(variables.colorNeutral, 'neutralAlpha'),
primaryHover: colors.adjustForLightness(primaryScale?.primary500),
colorTextOnPrimaryBackground: toHSLA(variables.colorTextOnPrimaryBackground),
colorText: toHSLA(variables.colorText),
colorTextSecondary,
colorTextTertiary,
colorTextLabel,
colorInputText: toHSLA(variables.colorInputText),
colorBackground: toHSLA(variables.colorBackground),
colorInputBackground: toHSLA(variables.colorInputBackground),
colorShimmer: toHSLA(variables.colorShimmer),
});
};
export const toHSLA = (str: string | undefined) => {
return str ? colors.toHslaString(str) : undefined;
};
export const createRadiiUnits = (theme: Theme) => {
const { borderRadius } = theme.variables || {};
if (borderRadius === undefined) {
return;
}
const md = borderRadius === 'none' ? '0' : borderRadius;
const { numericValue, unit = 'rem' } = splitCssUnit(md);
return {
sm: (numericValue * 0.66).toString() + unit,
md,
lg: (numericValue * 1.33).toString() + unit,
xl: (numericValue * 2).toString() + unit,
};
};
export const createSpaceScale = (theme: Theme) => {
const { spacingUnit } = theme.variables || {};
if (spacingUnit === undefined) {
return;
}
const { numericValue, unit } = splitCssUnit(spacingUnit);
return fromEntries(
spaceScaleKeys.map(k => {
const num = Number.parseFloat(k.replace('x', '.'));
const percentage = (num / 0.5) * 0.125;
return [k, `${numericValue * percentage}${unit}`];
}),
);
};
// We want to keep the same ratio between the font sizes we have for the default theme
// This is directly related to the fontSizes object in the theme default foundations
// ref: src/ui/foundations/typography.ts
export const createFontSizeScale = (theme: Theme): Record<keyof typeof fontSizes, string> | undefined => {
const { fontSize } = theme.variables || {};
if (fontSize === undefined) {
return;
}
const { numericValue, unit = 'rem' } = splitCssUnit(fontSize);
return {
xs: (numericValue * 0.8).toString() + unit,
sm: (numericValue * 0.9).toString() + unit,
md: fontSize,
lg: (numericValue * 1.3).toString() + unit,
xl: (numericValue * 1.85).toString() + unit,
};
};
export const createFontWeightScale = (theme: Theme): Record<keyof typeof fontWeights, any> => {
const { fontWeight } = theme.variables || {};
return removeUndefinedProps({ ...fontWeight });
};
export const createFonts = (theme: Theme) => {
const { fontFamily, fontFamilyButtons } = theme.variables || {};
return removeUndefinedProps({ main: fontFamily, buttons: fontFamilyButtons });
};
const splitCssUnit = (str: string) => {
const numericValue = Number.parseFloat(str);
const unit = str.replace(numericValue.toString(), '') || undefined;
return { numericValue, unit };
};