@draftbit/theme 50.6.2-18a5f8.2 → 50.6.2-531e17.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/commonjs/Provider.js +1 -1
- package/lib/commonjs/createThemeValuesProxy.js +1 -1
- package/lib/commonjs/validators.js +1 -1
- package/lib/typescript/src/Provider.js +1 -7
- package/lib/typescript/src/Provider.js.map +1 -1
- package/lib/typescript/src/createThemeValuesProxy.d.ts +1 -9
- package/lib/typescript/src/createThemeValuesProxy.js +11 -21
- package/lib/typescript/src/createThemeValuesProxy.js.map +1 -1
- package/lib/typescript/src/validators.js +6 -80
- package/lib/typescript/src/validators.js.map +1 -1
- package/lib/typescript/tsconfig.tsbuildinfo +1 -1
- package/package.json +3 -4
- package/src/Provider.js +1 -7
- package/src/Provider.js.map +1 -1
- package/src/Provider.tsx +4 -4
- package/src/createThemeValuesProxy.js +11 -21
- package/src/createThemeValuesProxy.js.map +1 -1
- package/src/createThemeValuesProxy.ts +77 -39
- package/src/validators.js +6 -80
- package/src/validators.js.map +1 -1
- package/src/validators.ts +6 -100
|
@@ -2,14 +2,6 @@ import { ThemeValues, Breakpoints } from "./types";
|
|
|
2
2
|
import { Platform, TextStyle } from "react-native";
|
|
3
3
|
import { isObject } from "lodash";
|
|
4
4
|
|
|
5
|
-
interface CreateThemeValuesProxyInput {
|
|
6
|
-
value: ThemeValues | undefined;
|
|
7
|
-
breakpoints: Breakpoints;
|
|
8
|
-
deviceWidth: number;
|
|
9
|
-
devicePlatform: typeof Platform.OS;
|
|
10
|
-
currentLightDarkSelection: "light" | "dark";
|
|
11
|
-
}
|
|
12
|
-
|
|
13
5
|
/**
|
|
14
6
|
* Creates a proxy for theme value objects to select a value whenever
|
|
15
7
|
* multiple values are provided for different platforms, breakpoints, and/or light/dark modes
|
|
@@ -17,13 +9,13 @@ interface CreateThemeValuesProxyInput {
|
|
|
17
9
|
* Ex: {color: {ios: "blue", android: "red"}}
|
|
18
10
|
* -> theme.color returns "blue" when the platform is ios and "red" when android
|
|
19
11
|
*/
|
|
20
|
-
export default function createThemeValuesProxy(
|
|
21
|
-
value,
|
|
22
|
-
breakpoints,
|
|
23
|
-
deviceWidth,
|
|
24
|
-
devicePlatform,
|
|
25
|
-
currentLightDarkSelection
|
|
26
|
-
|
|
12
|
+
export default function createThemeValuesProxy(
|
|
13
|
+
value: ThemeValues | undefined,
|
|
14
|
+
breakpoints: Breakpoints,
|
|
15
|
+
deviceWidth: number,
|
|
16
|
+
devicePlatform: typeof Platform.OS,
|
|
17
|
+
currentLightDarkSelection: "light" | "dark"
|
|
18
|
+
): any /* return type 'any' to allow arbitrary object references (object.example.another.there) */ {
|
|
27
19
|
if (value === undefined || value === null) {
|
|
28
20
|
return undefined;
|
|
29
21
|
}
|
|
@@ -50,22 +42,38 @@ export default function createThemeValuesProxy({
|
|
|
50
42
|
lightDarkKeys
|
|
51
43
|
);
|
|
52
44
|
|
|
53
|
-
const input: CreateThemeValuesProxyInput = {
|
|
54
|
-
value: valueAsThemeValues,
|
|
55
|
-
breakpoints,
|
|
56
|
-
deviceWidth,
|
|
57
|
-
devicePlatform,
|
|
58
|
-
currentLightDarkSelection,
|
|
59
|
-
};
|
|
60
|
-
|
|
61
45
|
if (keysType === "default") {
|
|
62
|
-
return createThemeValuesProxy(
|
|
46
|
+
return createThemeValuesProxy(
|
|
47
|
+
valueAsThemeValues,
|
|
48
|
+
breakpoints,
|
|
49
|
+
deviceWidth,
|
|
50
|
+
devicePlatform,
|
|
51
|
+
currentLightDarkSelection
|
|
52
|
+
);
|
|
63
53
|
} else if (keysType === "platform") {
|
|
64
|
-
return getPlatformValue(
|
|
54
|
+
return getPlatformValue(
|
|
55
|
+
valueAsThemeValues,
|
|
56
|
+
breakpoints,
|
|
57
|
+
deviceWidth,
|
|
58
|
+
devicePlatform,
|
|
59
|
+
currentLightDarkSelection
|
|
60
|
+
);
|
|
65
61
|
} else if (keysType === "breakpoint") {
|
|
66
|
-
return getBreakpointValue(
|
|
62
|
+
return getBreakpointValue(
|
|
63
|
+
valueAsThemeValues,
|
|
64
|
+
breakpoints,
|
|
65
|
+
deviceWidth,
|
|
66
|
+
devicePlatform,
|
|
67
|
+
currentLightDarkSelection
|
|
68
|
+
);
|
|
67
69
|
} else if (keysType === "lightDark") {
|
|
68
|
-
return getLightDarkValue(
|
|
70
|
+
return getLightDarkValue(
|
|
71
|
+
valueAsThemeValues,
|
|
72
|
+
breakpoints,
|
|
73
|
+
deviceWidth,
|
|
74
|
+
devicePlatform,
|
|
75
|
+
currentLightDarkSelection
|
|
76
|
+
);
|
|
69
77
|
} else {
|
|
70
78
|
return undefined;
|
|
71
79
|
}
|
|
@@ -137,24 +145,38 @@ function allFalse(a: boolean, b: boolean, c: boolean, d: boolean) {
|
|
|
137
145
|
return [a, b, c, d].filter((x) => x).length === 0;
|
|
138
146
|
}
|
|
139
147
|
|
|
140
|
-
function getPlatformValue(
|
|
141
|
-
|
|
142
|
-
|
|
148
|
+
function getPlatformValue(
|
|
149
|
+
value: ThemeValues | undefined,
|
|
150
|
+
breakpoints: Breakpoints,
|
|
151
|
+
deviceWidth: number,
|
|
152
|
+
devicePlatform: typeof Platform.OS,
|
|
153
|
+
currentLightDarkSelection: "light" | "dark"
|
|
154
|
+
) {
|
|
143
155
|
const currentPlatformValue = value?.[devicePlatform] ?? value?.default;
|
|
144
156
|
const valueAsThemeValues = isObject(currentPlatformValue)
|
|
145
157
|
? (currentPlatformValue as ThemeValues)
|
|
146
158
|
: undefined;
|
|
147
159
|
|
|
148
160
|
if (valueAsThemeValues) {
|
|
149
|
-
return createThemeValuesProxy(
|
|
161
|
+
return createThemeValuesProxy(
|
|
162
|
+
valueAsThemeValues,
|
|
163
|
+
breakpoints,
|
|
164
|
+
deviceWidth,
|
|
165
|
+
devicePlatform,
|
|
166
|
+
currentLightDarkSelection
|
|
167
|
+
);
|
|
150
168
|
} else {
|
|
151
169
|
return currentPlatformValue;
|
|
152
170
|
}
|
|
153
171
|
}
|
|
154
172
|
|
|
155
|
-
function getBreakpointValue(
|
|
156
|
-
|
|
157
|
-
|
|
173
|
+
function getBreakpointValue(
|
|
174
|
+
value: ThemeValues | undefined,
|
|
175
|
+
breakpoints: Breakpoints,
|
|
176
|
+
deviceWidth: number,
|
|
177
|
+
devicePlatform: typeof Platform.OS,
|
|
178
|
+
currentLightDarkSelection: "light" | "dark"
|
|
179
|
+
) {
|
|
158
180
|
const keysToBreakpointValue: [string, number][] = Object.keys(
|
|
159
181
|
value ?? {}
|
|
160
182
|
).map((key) => [key, breakpoints[key]]);
|
|
@@ -172,15 +194,25 @@ function getBreakpointValue(input: CreateThemeValuesProxyInput) {
|
|
|
172
194
|
: undefined;
|
|
173
195
|
|
|
174
196
|
if (valueAsThemeValues) {
|
|
175
|
-
return createThemeValuesProxy(
|
|
197
|
+
return createThemeValuesProxy(
|
|
198
|
+
valueAsThemeValues,
|
|
199
|
+
breakpoints,
|
|
200
|
+
deviceWidth,
|
|
201
|
+
devicePlatform,
|
|
202
|
+
currentLightDarkSelection
|
|
203
|
+
);
|
|
176
204
|
} else {
|
|
177
205
|
return currentBreakpointValue;
|
|
178
206
|
}
|
|
179
207
|
}
|
|
180
208
|
|
|
181
|
-
function getLightDarkValue(
|
|
182
|
-
|
|
183
|
-
|
|
209
|
+
function getLightDarkValue(
|
|
210
|
+
value: ThemeValues | undefined,
|
|
211
|
+
breakpoints: Breakpoints,
|
|
212
|
+
deviceWidth: number,
|
|
213
|
+
devicePlatform: typeof Platform.OS,
|
|
214
|
+
currentLightDarkSelection: "light" | "dark"
|
|
215
|
+
) {
|
|
184
216
|
const currentLightDarkValue =
|
|
185
217
|
value?.[currentLightDarkSelection] ?? value?.default;
|
|
186
218
|
const valueAsThemeValues = isObject(currentLightDarkSelection)
|
|
@@ -188,7 +220,13 @@ function getLightDarkValue(input: CreateThemeValuesProxyInput) {
|
|
|
188
220
|
: undefined;
|
|
189
221
|
|
|
190
222
|
if (valueAsThemeValues) {
|
|
191
|
-
return createThemeValuesProxy(
|
|
223
|
+
return createThemeValuesProxy(
|
|
224
|
+
valueAsThemeValues,
|
|
225
|
+
breakpoints,
|
|
226
|
+
deviceWidth,
|
|
227
|
+
devicePlatform,
|
|
228
|
+
currentLightDarkSelection
|
|
229
|
+
);
|
|
192
230
|
} else {
|
|
193
231
|
return currentLightDarkValue;
|
|
194
232
|
}
|
package/src/validators.js
CHANGED
|
@@ -1,91 +1,17 @@
|
|
|
1
|
-
import { z } from "zod";
|
|
2
|
-
const PaletteSchema = z.record(z.string(), z.record(z.string(), z.string()));
|
|
3
|
-
const BreakpointSchema = z.record(z.string(), z.number());
|
|
4
|
-
const TextStyleSchema = z.union([
|
|
5
|
-
z.object({
|
|
6
|
-
fontSize: z.number(),
|
|
7
|
-
}),
|
|
8
|
-
z.object({
|
|
9
|
-
fontFamily: z.string(),
|
|
10
|
-
}),
|
|
11
|
-
z.object({
|
|
12
|
-
fontWeight: z.enum([
|
|
13
|
-
"normal",
|
|
14
|
-
"bold",
|
|
15
|
-
"100",
|
|
16
|
-
"200",
|
|
17
|
-
"300",
|
|
18
|
-
"400",
|
|
19
|
-
"500",
|
|
20
|
-
"600",
|
|
21
|
-
"700",
|
|
22
|
-
"800",
|
|
23
|
-
"900",
|
|
24
|
-
]),
|
|
25
|
-
}),
|
|
26
|
-
z.object({
|
|
27
|
-
fontStyle: z.enum(["normal", "italic"]),
|
|
28
|
-
}),
|
|
29
|
-
z.object({
|
|
30
|
-
letterSpacing: z.number(),
|
|
31
|
-
}),
|
|
32
|
-
z.object({
|
|
33
|
-
lineHeight: z.number(),
|
|
34
|
-
}),
|
|
35
|
-
]);
|
|
36
|
-
const ThemeValuesSchema = z.record(z.string(), z.lazy(() => z.union([z.string(), z.number(), TextStyleSchema, ThemeValuesSchema])));
|
|
37
|
-
const TextStyleOrThemeValuesSchema = z.union([TextStyleSchema, ThemeValuesSchema]);
|
|
38
|
-
const ThemeSchema = z.object({
|
|
39
|
-
name: z.string(),
|
|
40
|
-
colors: z.object({
|
|
41
|
-
branding: ThemeValuesSchema.optional(),
|
|
42
|
-
text: ThemeValuesSchema.optional(),
|
|
43
|
-
background: ThemeValuesSchema.optional(),
|
|
44
|
-
foreground: ThemeValuesSchema.optional(),
|
|
45
|
-
border: ThemeValuesSchema.optional(),
|
|
46
|
-
}),
|
|
47
|
-
typography: z.object({
|
|
48
|
-
body1: TextStyleOrThemeValuesSchema.optional(),
|
|
49
|
-
body2: TextStyleOrThemeValuesSchema.optional(),
|
|
50
|
-
button: TextStyleOrThemeValuesSchema.optional(),
|
|
51
|
-
caption: TextStyleOrThemeValuesSchema.optional(),
|
|
52
|
-
headline1: TextStyleOrThemeValuesSchema.optional(),
|
|
53
|
-
headline2: TextStyleOrThemeValuesSchema.optional(),
|
|
54
|
-
headline3: TextStyleOrThemeValuesSchema.optional(),
|
|
55
|
-
headline4: TextStyleOrThemeValuesSchema.optional(),
|
|
56
|
-
headline5: TextStyleOrThemeValuesSchema.optional(),
|
|
57
|
-
headline6: TextStyleOrThemeValuesSchema.optional(),
|
|
58
|
-
overline: TextStyleOrThemeValuesSchema.optional(),
|
|
59
|
-
subtitle1: TextStyleOrThemeValuesSchema.optional(),
|
|
60
|
-
subtitle2: TextStyleOrThemeValuesSchema.optional(),
|
|
61
|
-
}),
|
|
62
|
-
});
|
|
63
1
|
export function validatePalettes(palettes) {
|
|
64
|
-
|
|
65
|
-
if (!result.success) {
|
|
66
|
-
throw new Error("Invalid palettes object: " + result.error.message);
|
|
67
|
-
}
|
|
2
|
+
palettes;
|
|
68
3
|
}
|
|
69
4
|
export function validateBreakpoints(breakpoints) {
|
|
70
|
-
|
|
71
|
-
if (!result.success) {
|
|
72
|
-
throw new Error("Invalid breakpoints object: " + result.error.message);
|
|
73
|
-
}
|
|
5
|
+
breakpoints;
|
|
74
6
|
}
|
|
75
7
|
export function validateTheme(theme) {
|
|
76
|
-
|
|
77
|
-
if (!result.success) {
|
|
78
|
-
throw new Error("Invalid theme object: " + result.error.message);
|
|
79
|
-
}
|
|
8
|
+
theme;
|
|
80
9
|
}
|
|
81
10
|
export function isTextStyleObject(value) {
|
|
82
|
-
|
|
11
|
+
value;
|
|
12
|
+
return true;
|
|
83
13
|
}
|
|
84
14
|
export function asThemeValuesObject(value) {
|
|
85
|
-
|
|
86
|
-
if (isTextStyleObject(value)) {
|
|
87
|
-
return null;
|
|
88
|
-
}
|
|
89
|
-
return ThemeValuesSchema.safeParse(value).success ? value : null;
|
|
15
|
+
return value;
|
|
90
16
|
}
|
|
91
17
|
//# sourceMappingURL=validators.js.map
|
package/src/validators.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validators.js","sourceRoot":"","sources":["validators.ts"],"names":[],"mappings":"AAEA,
|
|
1
|
+
{"version":3,"file":"validators.js","sourceRoot":"","sources":["validators.ts"],"names":[],"mappings":"AAEA,MAAM,UAAU,gBAAgB,CAAC,QAAuB;IACtD,QAAQ,CAAC;AACX,CAAC;AAED,MAAM,UAAU,mBAAmB,CAAC,WAAwB;IAC1D,WAAW,CAAC;AACd,CAAC;AAED,MAAM,UAAU,aAAa,CAAC,KAAY;IACxC,KAAK,CAAC;AACR,CAAC;AAED,MAAM,UAAU,iBAAiB,CAAC,KAAU;IAC1C,KAAK,CAAC;IACN,OAAO,IAAI,CAAC;AACd,CAAC;AAED,MAAM,UAAU,mBAAmB,CAAC,KAAU;IAC5C,OAAO,KAAK,CAAC;AACf,CAAC"}
|
package/src/validators.ts
CHANGED
|
@@ -1,116 +1,22 @@
|
|
|
1
1
|
import { ColorPalettes, Breakpoints, Theme, ThemeValues } from "./types";
|
|
2
|
-
import { TextStyle } from "react-native";
|
|
3
|
-
import { z } from "zod";
|
|
4
|
-
|
|
5
|
-
const PaletteSchema: z.ZodType<ColorPalettes> = z.record(
|
|
6
|
-
z.string(),
|
|
7
|
-
z.record(z.string(), z.string())
|
|
8
|
-
);
|
|
9
|
-
|
|
10
|
-
const BreakpointSchema: z.ZodType<Breakpoints> = z.record(
|
|
11
|
-
z.string(),
|
|
12
|
-
z.number()
|
|
13
|
-
);
|
|
14
|
-
|
|
15
|
-
const TextStyleSchema: z.ZodType<TextStyle> = z.union([
|
|
16
|
-
z.object({
|
|
17
|
-
fontSize: z.number(),
|
|
18
|
-
}),
|
|
19
|
-
z.object({
|
|
20
|
-
fontFamily: z.string(),
|
|
21
|
-
}),
|
|
22
|
-
z.object({
|
|
23
|
-
fontWeight: z.enum([
|
|
24
|
-
"normal",
|
|
25
|
-
"bold",
|
|
26
|
-
"100",
|
|
27
|
-
"200",
|
|
28
|
-
"300",
|
|
29
|
-
"400",
|
|
30
|
-
"500",
|
|
31
|
-
"600",
|
|
32
|
-
"700",
|
|
33
|
-
"800",
|
|
34
|
-
"900",
|
|
35
|
-
]),
|
|
36
|
-
}),
|
|
37
|
-
z.object({
|
|
38
|
-
fontStyle: z.enum(["normal", "italic"]),
|
|
39
|
-
}),
|
|
40
|
-
z.object({
|
|
41
|
-
letterSpacing: z.number(),
|
|
42
|
-
}),
|
|
43
|
-
z.object({
|
|
44
|
-
lineHeight: z.number(),
|
|
45
|
-
}),
|
|
46
|
-
]);
|
|
47
|
-
|
|
48
|
-
const ThemeValuesSchema: z.ZodType<ThemeValues> = z.record(
|
|
49
|
-
z.string(),
|
|
50
|
-
z.lazy(() =>
|
|
51
|
-
z.union([z.string(), z.number(), TextStyleSchema, ThemeValuesSchema])
|
|
52
|
-
)
|
|
53
|
-
);
|
|
54
|
-
|
|
55
|
-
const TextStyleOrThemeValuesSchema: z.ZodType<ThemeValues | TextStyle> =
|
|
56
|
-
z.union([TextStyleSchema, ThemeValuesSchema]);
|
|
57
|
-
|
|
58
|
-
const ThemeSchema: z.ZodType<Theme> = z.object({
|
|
59
|
-
name: z.string(),
|
|
60
|
-
colors: z.object({
|
|
61
|
-
branding: ThemeValuesSchema.optional(),
|
|
62
|
-
text: ThemeValuesSchema.optional(),
|
|
63
|
-
background: ThemeValuesSchema.optional(),
|
|
64
|
-
foreground: ThemeValuesSchema.optional(),
|
|
65
|
-
border: ThemeValuesSchema.optional(),
|
|
66
|
-
}),
|
|
67
|
-
typography: z.object({
|
|
68
|
-
body1: TextStyleOrThemeValuesSchema.optional(),
|
|
69
|
-
body2: TextStyleOrThemeValuesSchema.optional(),
|
|
70
|
-
button: TextStyleOrThemeValuesSchema.optional(),
|
|
71
|
-
caption: TextStyleOrThemeValuesSchema.optional(),
|
|
72
|
-
headline1: TextStyleOrThemeValuesSchema.optional(),
|
|
73
|
-
headline2: TextStyleOrThemeValuesSchema.optional(),
|
|
74
|
-
headline3: TextStyleOrThemeValuesSchema.optional(),
|
|
75
|
-
headline4: TextStyleOrThemeValuesSchema.optional(),
|
|
76
|
-
headline5: TextStyleOrThemeValuesSchema.optional(),
|
|
77
|
-
headline6: TextStyleOrThemeValuesSchema.optional(),
|
|
78
|
-
overline: TextStyleOrThemeValuesSchema.optional(),
|
|
79
|
-
subtitle1: TextStyleOrThemeValuesSchema.optional(),
|
|
80
|
-
subtitle2: TextStyleOrThemeValuesSchema.optional(),
|
|
81
|
-
}),
|
|
82
|
-
});
|
|
83
2
|
|
|
84
3
|
export function validatePalettes(palettes: ColorPalettes) {
|
|
85
|
-
|
|
86
|
-
if (!result.success) {
|
|
87
|
-
throw new Error("Invalid palettes object: " + result.error.message);
|
|
88
|
-
}
|
|
4
|
+
palettes;
|
|
89
5
|
}
|
|
90
6
|
|
|
91
7
|
export function validateBreakpoints(breakpoints: Breakpoints) {
|
|
92
|
-
|
|
93
|
-
if (!result.success) {
|
|
94
|
-
throw new Error("Invalid breakpoints object: " + result.error.message);
|
|
95
|
-
}
|
|
8
|
+
breakpoints;
|
|
96
9
|
}
|
|
97
10
|
|
|
98
11
|
export function validateTheme(theme: Theme) {
|
|
99
|
-
|
|
100
|
-
if (!result.success) {
|
|
101
|
-
throw new Error("Invalid theme object: " + result.error.message);
|
|
102
|
-
}
|
|
12
|
+
theme;
|
|
103
13
|
}
|
|
104
14
|
|
|
105
15
|
export function isTextStyleObject(value: any): boolean {
|
|
106
|
-
|
|
16
|
+
value;
|
|
17
|
+
return true;
|
|
107
18
|
}
|
|
108
19
|
|
|
109
20
|
export function asThemeValuesObject(value: any): ThemeValues | null {
|
|
110
|
-
|
|
111
|
-
if (isTextStyleObject(value)) {
|
|
112
|
-
return null;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
return ThemeValuesSchema.safeParse(value).success ? value : null;
|
|
21
|
+
return value;
|
|
116
22
|
}
|