@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.
@@ -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
- }: CreateThemeValuesProxyInput): any /* return type 'any' to allow arbitrary object references (object.example.another.there) */ {
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(input);
46
+ return createThemeValuesProxy(
47
+ valueAsThemeValues,
48
+ breakpoints,
49
+ deviceWidth,
50
+ devicePlatform,
51
+ currentLightDarkSelection
52
+ );
63
53
  } else if (keysType === "platform") {
64
- return getPlatformValue(input);
54
+ return getPlatformValue(
55
+ valueAsThemeValues,
56
+ breakpoints,
57
+ deviceWidth,
58
+ devicePlatform,
59
+ currentLightDarkSelection
60
+ );
65
61
  } else if (keysType === "breakpoint") {
66
- return getBreakpointValue(input);
62
+ return getBreakpointValue(
63
+ valueAsThemeValues,
64
+ breakpoints,
65
+ deviceWidth,
66
+ devicePlatform,
67
+ currentLightDarkSelection
68
+ );
67
69
  } else if (keysType === "lightDark") {
68
- return getLightDarkValue(input);
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(input: CreateThemeValuesProxyInput) {
141
- const { value, devicePlatform } = input;
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({ ...input, value: valueAsThemeValues });
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(input: CreateThemeValuesProxyInput) {
156
- const { value, breakpoints, deviceWidth } = input;
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({ ...input, value: valueAsThemeValues });
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(input: CreateThemeValuesProxyInput) {
182
- const { value, currentLightDarkSelection } = input;
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({ ...input, value: valueAsThemeValues });
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
- const result = PaletteSchema.safeParse(palettes);
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
- const result = BreakpointSchema.safeParse(breakpoints);
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
- const result = ThemeSchema.safeParse(theme);
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
- return TextStyleSchema.safeParse(value).success;
11
+ value;
12
+ return true;
83
13
  }
84
14
  export function asThemeValuesObject(value) {
85
- // Text style matches the shape of ThemeValues, but we don't want to treat it as a ThemeValues
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
@@ -1 +1 @@
1
- {"version":3,"file":"validators.js","sourceRoot":"","sources":["validators.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,MAAM,aAAa,GAA6B,CAAC,CAAC,MAAM,CACtD,CAAC,CAAC,MAAM,EAAE,EACV,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,CACjC,CAAC;AAEF,MAAM,gBAAgB,GAA2B,CAAC,CAAC,MAAM,CACvD,CAAC,CAAC,MAAM,EAAE,EACV,CAAC,CAAC,MAAM,EAAE,CACX,CAAC;AAEF,MAAM,eAAe,GAAyB,CAAC,CAAC,KAAK,CAAC;IACpD,CAAC,CAAC,MAAM,CAAC;QACP,QAAQ,EAAE,CAAC,CAAC,MAAM,EAAE;KACrB,CAAC;IACF,CAAC,CAAC,MAAM,CAAC;QACP,UAAU,EAAE,CAAC,CAAC,MAAM,EA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AC,KAAY;IACxC,MAAM,MAAM,GAAG,WAAW,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;IAC5C,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE;QACnB,MAAM,IAAI,KAAK,CAAC,wBAAwB,GAAG,MAAM,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;KAClE;AACH,CAAC;AAED,MAAM,UAAU,iBAAiB,CAAC,KAAU;IAC1C,OAAO,eAAe,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC;AAClD,CAAC;AAED,MAAM,UAAU,mBAAmB,CAAC,KAAU;IAC5C,8FAA8F;IAC9F,IAAI,iBAAiB,CAAC,KAAK,CAAC,EAAE;QAC5B,OAAO,IAAI,CAAC;KACb;IAED,OAAO,iBAAiB,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC;AACnE,CAAC"}
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
- const result = PaletteSchema.safeParse(palettes);
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
- const result = BreakpointSchema.safeParse(breakpoints);
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
- const result = ThemeSchema.safeParse(theme);
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
- return TextStyleSchema.safeParse(value).success;
16
+ value;
17
+ return true;
107
18
  }
108
19
 
109
20
  export function asThemeValuesObject(value: any): ThemeValues | null {
110
- // Text style matches the shape of ThemeValues, but we don't want to treat it as a ThemeValues
111
- if (isTextStyleObject(value)) {
112
- return null;
113
- }
114
-
115
- return ThemeValuesSchema.safeParse(value).success ? value : null;
21
+ return value;
116
22
  }