@draftbit/theme 50.6.2-531e17.2 → 50.6.2-8430ca.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.
Files changed (37) hide show
  1. package/lib/commonjs/DefaultTheme.js +1 -1
  2. package/lib/commonjs/Provider.js +1 -1
  3. package/lib/commonjs/createTheme.js +1 -1
  4. package/lib/commonjs/createThemeValuesProxy.js +1 -1
  5. package/lib/commonjs/types.js +0 -1
  6. package/lib/commonjs/validators.js +1 -1
  7. package/lib/typescript/src/DefaultTheme.js +38 -38
  8. package/lib/typescript/src/DefaultTheme.js.map +1 -1
  9. package/lib/typescript/src/Provider.js +11 -23
  10. package/lib/typescript/src/Provider.js.map +1 -1
  11. package/lib/typescript/src/createTheme.d.ts +1 -13
  12. package/lib/typescript/src/createTheme.js +6 -17
  13. package/lib/typescript/src/createTheme.js.map +1 -1
  14. package/lib/typescript/src/createThemeValuesProxy.js +22 -34
  15. package/lib/typescript/src/createThemeValuesProxy.js.map +1 -1
  16. package/lib/typescript/src/types.d.ts +2 -30
  17. package/lib/typescript/src/validators.d.ts +1 -3
  18. package/lib/typescript/src/validators.js +45 -10
  19. package/lib/typescript/src/validators.js.map +1 -1
  20. package/lib/typescript/tsconfig.tsbuildinfo +1 -1
  21. package/package.json +3 -2
  22. package/src/DefaultTheme.js +38 -38
  23. package/src/DefaultTheme.js.map +1 -1
  24. package/src/DefaultTheme.ts +38 -38
  25. package/src/Provider.js +11 -23
  26. package/src/Provider.js.map +1 -1
  27. package/src/Provider.tsx +41 -29
  28. package/src/createTheme.js +6 -17
  29. package/src/createTheme.js.map +1 -1
  30. package/src/createTheme.ts +12 -19
  31. package/src/createThemeValuesProxy.js +22 -34
  32. package/src/createThemeValuesProxy.js.map +1 -1
  33. package/src/createThemeValuesProxy.ts +31 -49
  34. package/src/types.ts +2 -33
  35. package/src/validators.js +45 -10
  36. package/src/validators.js.map +1 -1
  37. package/src/validators.ts +49 -12
@@ -1,5 +1,5 @@
1
1
  import { ThemeValues, Breakpoints } from "./types";
2
- import { Platform, TextStyle } from "react-native";
2
+ import { Platform } from "react-native";
3
3
  import { isObject } from "lodash";
4
4
 
5
5
  /**
@@ -23,20 +23,16 @@ export default function createThemeValuesProxy(
23
23
  get: (
24
24
  target: ThemeValues,
25
25
  key: string
26
- ): string | number | ThemeValues | TextStyle | undefined => {
26
+ ): string | number | ThemeValues | undefined => {
27
27
  const currentValue = target[key];
28
-
29
- const valueAsThemeValues = isObject(currentValue)
30
- ? (currentValue as ThemeValues)
31
- : undefined;
32
-
33
- if (valueAsThemeValues) {
28
+ if (!isObject(currentValue)) {
29
+ return currentValue;
30
+ } else {
34
31
  const platformKeys = ["ios", "android", "web", "macos", "windows"];
35
32
  const breakpointKeys = Object.keys(breakpoints);
36
33
  const lightDarkKeys = ["light", "dark"];
37
-
38
34
  const keysType = getKeysType(
39
- valueAsThemeValues,
35
+ currentValue,
40
36
  platformKeys,
41
37
  breakpointKeys,
42
38
  lightDarkKeys
@@ -44,7 +40,7 @@ export default function createThemeValuesProxy(
44
40
 
45
41
  if (keysType === "default") {
46
42
  return createThemeValuesProxy(
47
- valueAsThemeValues,
43
+ currentValue,
48
44
  breakpoints,
49
45
  deviceWidth,
50
46
  devicePlatform,
@@ -52,7 +48,7 @@ export default function createThemeValuesProxy(
52
48
  );
53
49
  } else if (keysType === "platform") {
54
50
  return getPlatformValue(
55
- valueAsThemeValues,
51
+ currentValue,
56
52
  breakpoints,
57
53
  deviceWidth,
58
54
  devicePlatform,
@@ -60,7 +56,7 @@ export default function createThemeValuesProxy(
60
56
  );
61
57
  } else if (keysType === "breakpoint") {
62
58
  return getBreakpointValue(
63
- valueAsThemeValues,
59
+ currentValue,
64
60
  breakpoints,
65
61
  deviceWidth,
66
62
  devicePlatform,
@@ -68,7 +64,7 @@ export default function createThemeValuesProxy(
68
64
  );
69
65
  } else if (keysType === "lightDark") {
70
66
  return getLightDarkValue(
71
- valueAsThemeValues,
67
+ currentValue,
72
68
  breakpoints,
73
69
  deviceWidth,
74
70
  devicePlatform,
@@ -77,8 +73,6 @@ export default function createThemeValuesProxy(
77
73
  } else {
78
74
  return undefined;
79
75
  }
80
- } else {
81
- return currentValue;
82
76
  }
83
77
  },
84
78
  set: () => {
@@ -146,40 +140,36 @@ function allFalse(a: boolean, b: boolean, c: boolean, d: boolean) {
146
140
  }
147
141
 
148
142
  function getPlatformValue(
149
- value: ThemeValues | undefined,
143
+ value: ThemeValues,
150
144
  breakpoints: Breakpoints,
151
145
  deviceWidth: number,
152
146
  devicePlatform: typeof Platform.OS,
153
147
  currentLightDarkSelection: "light" | "dark"
154
148
  ) {
155
- const currentPlatformValue = value?.[devicePlatform] ?? value?.default;
156
- const valueAsThemeValues = isObject(currentPlatformValue)
157
- ? (currentPlatformValue as ThemeValues)
158
- : undefined;
159
-
160
- if (valueAsThemeValues) {
149
+ const currentPlatformValue = value[devicePlatform] ?? value.default;
150
+ if (!isObject(currentPlatformValue)) {
151
+ return currentPlatformValue;
152
+ } else {
161
153
  return createThemeValuesProxy(
162
- valueAsThemeValues,
154
+ currentPlatformValue,
163
155
  breakpoints,
164
156
  deviceWidth,
165
157
  devicePlatform,
166
158
  currentLightDarkSelection
167
159
  );
168
- } else {
169
- return currentPlatformValue;
170
160
  }
171
161
  }
172
162
 
173
163
  function getBreakpointValue(
174
- value: ThemeValues | undefined,
164
+ value: ThemeValues,
175
165
  breakpoints: Breakpoints,
176
166
  deviceWidth: number,
177
167
  devicePlatform: typeof Platform.OS,
178
168
  currentLightDarkSelection: "light" | "dark"
179
169
  ) {
180
- const keysToBreakpointValue: [string, number][] = Object.keys(
181
- value ?? {}
182
- ).map((key) => [key, breakpoints[key]]);
170
+ const keysToBreakpointValue: [string, number][] = Object.keys(value).map(
171
+ (key) => [key, breakpoints[key]]
172
+ );
183
173
  const orderedBreakpoints = keysToBreakpointValue.sort(([_, val]) => val);
184
174
  let currentBreakpointKey = "";
185
175
  for (const [breakpointKey, breakpointValue] of orderedBreakpoints) {
@@ -187,47 +177,39 @@ function getBreakpointValue(
187
177
  currentBreakpointKey = breakpointKey;
188
178
  }
189
179
  }
190
- const currentBreakpointValue =
191
- value?.[currentBreakpointKey] ?? value?.default;
192
- const valueAsThemeValues = isObject(currentBreakpointKey)
193
- ? (currentBreakpointKey as ThemeValues)
194
- : undefined;
195
-
196
- if (valueAsThemeValues) {
180
+ const currentBreakpointValue = value[currentBreakpointKey] ?? value.default;
181
+ if (!isObject(currentBreakpointValue)) {
182
+ return currentBreakpointValue;
183
+ } else {
197
184
  return createThemeValuesProxy(
198
- valueAsThemeValues,
185
+ currentBreakpointValue,
199
186
  breakpoints,
200
187
  deviceWidth,
201
188
  devicePlatform,
202
189
  currentLightDarkSelection
203
190
  );
204
- } else {
205
- return currentBreakpointValue;
206
191
  }
207
192
  }
208
193
 
209
194
  function getLightDarkValue(
210
- value: ThemeValues | undefined,
195
+ value: ThemeValues,
211
196
  breakpoints: Breakpoints,
212
197
  deviceWidth: number,
213
198
  devicePlatform: typeof Platform.OS,
214
199
  currentLightDarkSelection: "light" | "dark"
215
200
  ) {
216
201
  const currentLightDarkValue =
217
- value?.[currentLightDarkSelection] ?? value?.default;
218
- const valueAsThemeValues = isObject(currentLightDarkSelection)
219
- ? (currentLightDarkSelection as ThemeValues)
220
- : undefined;
202
+ value[currentLightDarkSelection] ?? value.default;
221
203
 
222
- if (valueAsThemeValues) {
204
+ if (!isObject(currentLightDarkValue)) {
205
+ return currentLightDarkValue;
206
+ } else {
223
207
  return createThemeValuesProxy(
224
- valueAsThemeValues,
208
+ currentLightDarkValue,
225
209
  breakpoints,
226
210
  deviceWidth,
227
211
  devicePlatform,
228
212
  currentLightDarkSelection
229
213
  );
230
- } else {
231
- return currentLightDarkValue;
232
214
  }
233
215
  }
package/src/types.ts CHANGED
@@ -1,7 +1,5 @@
1
- import { TextStyle } from "react-native";
2
-
3
1
  export type ThemeValues = {
4
- [key: string]: string | number | TextStyle | ThemeValues;
2
+ [key: string]: string | number | ThemeValues;
5
3
  };
6
4
 
7
5
  export type Theme = {
@@ -13,21 +11,7 @@ export type Theme = {
13
11
  foreground?: ThemeValues;
14
12
  border?: ThemeValues;
15
13
  };
16
- typography: {
17
- body1?: ThemeValues | TextStyle;
18
- body2?: ThemeValues | TextStyle;
19
- button?: ThemeValues | TextStyle;
20
- caption?: ThemeValues | TextStyle;
21
- headline1?: ThemeValues | TextStyle;
22
- headline2?: ThemeValues | TextStyle;
23
- headline3?: ThemeValues | TextStyle;
24
- headline4?: ThemeValues | TextStyle;
25
- headline5?: ThemeValues | TextStyle;
26
- headline6?: ThemeValues | TextStyle;
27
- overline?: ThemeValues | TextStyle;
28
- subtitle1?: ThemeValues | TextStyle;
29
- subtitle2?: ThemeValues | TextStyle;
30
- };
14
+ typography: { [key: string]: any }; //TODO: Update theme to support typography. Left as 'any' for legacy support until updated and migrated
31
15
  };
32
16
 
33
17
  export type ValidatedTheme = Theme & {
@@ -42,21 +26,6 @@ export type ReadTheme = Theme & {
42
26
  foreground?: any;
43
27
  border?: any;
44
28
  };
45
- typography: {
46
- body1?: any;
47
- body2?: any;
48
- button?: any;
49
- caption?: any;
50
- headline1?: any;
51
- headline2?: any;
52
- headline3?: any;
53
- headline4?: any;
54
- headline5?: any;
55
- headline6?: any;
56
- overline?: any;
57
- subtitle1?: any;
58
- subtitle2?: any;
59
- };
60
29
  };
61
30
 
62
31
  export type ColorPaletteValues = {
package/src/validators.js CHANGED
@@ -1,17 +1,52 @@
1
1
  export function validatePalettes(palettes) {
2
- palettes;
2
+ for (const key of Object.keys(palettes)) {
3
+ if (typeof key !== "string") {
4
+ throw new Error(`Invalid palettes object, ${key} is not a string`);
5
+ }
6
+ for (const [innerKey, value] of Object.entries(palettes[key])) {
7
+ if (typeof innerKey !== "string") {
8
+ throw new Error(`Invalid palette color key, ${innerKey} is not a string`);
9
+ }
10
+ if (typeof value !== "string") {
11
+ throw new Error(`Invalid palette color value, ${value} is not a string`);
12
+ }
13
+ }
14
+ }
3
15
  }
4
16
  export function validateBreakpoints(breakpoints) {
5
- breakpoints;
17
+ for (const [key, value] of Object.entries(breakpoints)) {
18
+ if (typeof key !== "string") {
19
+ throw new Error(`Invalid breakpoint key, ${key} is not a string`);
20
+ }
21
+ if (typeof value !== "number") {
22
+ throw new Error(`Invalid breakpoint value, ${value} is not a number`);
23
+ }
24
+ }
6
25
  }
7
26
  export function validateTheme(theme) {
8
- theme;
9
- }
10
- export function isTextStyleObject(value) {
11
- value;
12
- return true;
13
- }
14
- export function asThemeValuesObject(value) {
15
- return value;
27
+ function validateThemeValues(themeValue) {
28
+ if (themeValue === undefined || themeValue === null) {
29
+ return;
30
+ }
31
+ for (const [key, value] of Object.entries(themeValue)) {
32
+ if (typeof key !== "string") {
33
+ throw new Error(`Invalid theme key, ${key} is not a string`);
34
+ }
35
+ if (value === undefined || value === null) {
36
+ continue;
37
+ }
38
+ else if (typeof value === "object") {
39
+ validateThemeValues(value);
40
+ }
41
+ else if (typeof value !== "string" && typeof value !== "number") {
42
+ throw new Error(`Invalid theme value, ${value} is not a string, number, or object`);
43
+ }
44
+ }
45
+ }
46
+ validateThemeValues(theme.colors.branding);
47
+ validateThemeValues(theme.colors.text);
48
+ validateThemeValues(theme.colors.foreground);
49
+ validateThemeValues(theme.colors.background);
50
+ validateThemeValues(theme.colors.border);
16
51
  }
17
52
  //# sourceMappingURL=validators.js.map
@@ -1 +1 @@
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,U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D,OAAO;SACR;QACD,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,UAAU,CAAC,EAAE;YACrD,IAAI,OAAO,GAAG,KAAK,QAAQ,EAAE;gBAC3B,MAAM,IAAI,KAAK,CAAC,sBAAsB,GAAG,kBAAkB,CAAC,CAAC;aAC9D;YACD,IAAI,KAAK,KAAK,SAAS,IAAI,KAAK,KAAK,IAAI,EAAE;gBACzC,SAAS;aACV;iBAAM,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE;gBACpC,mBAAmB,CAAC,KAAK,CAAC,CAAC;aAC5B;iBAAM,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE;gBACjE,MAAM,IAAI,KAAK,CACb,wBAAwB,KAAK,qCAAqC,CACnE,CAAC;aACH;SACF;IACH,CAAC;IACD,mBAAmB,CAAC,KAAK,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;IAC3C,mBAAmB,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IACvC,mBAAmB,CAAC,KAAK,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;IAC7C,mBAAmB,CAAC,KAAK,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;IAC7C,mBAAmB,CAAC,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;AAC3C,CAAC"}
package/src/validators.ts CHANGED
@@ -1,22 +1,59 @@
1
1
  import { ColorPalettes, Breakpoints, Theme, ThemeValues } from "./types";
2
2
 
3
3
  export function validatePalettes(palettes: ColorPalettes) {
4
- palettes;
4
+ for (const key of Object.keys(palettes)) {
5
+ if (typeof key !== "string") {
6
+ throw new Error(`Invalid palettes object, ${key} is not a string`);
7
+ }
8
+ for (const [innerKey, value] of Object.entries(palettes[key])) {
9
+ if (typeof innerKey !== "string") {
10
+ throw new Error(
11
+ `Invalid palette color key, ${innerKey} is not a string`
12
+ );
13
+ }
14
+ if (typeof value !== "string") {
15
+ throw new Error(
16
+ `Invalid palette color value, ${value} is not a string`
17
+ );
18
+ }
19
+ }
20
+ }
5
21
  }
6
22
 
7
23
  export function validateBreakpoints(breakpoints: Breakpoints) {
8
- breakpoints;
24
+ for (const [key, value] of Object.entries(breakpoints)) {
25
+ if (typeof key !== "string") {
26
+ throw new Error(`Invalid breakpoint key, ${key} is not a string`);
27
+ }
28
+ if (typeof value !== "number") {
29
+ throw new Error(`Invalid breakpoint value, ${value} is not a number`);
30
+ }
31
+ }
9
32
  }
10
33
 
11
34
  export function validateTheme(theme: Theme) {
12
- theme;
13
- }
14
-
15
- export function isTextStyleObject(value: any): boolean {
16
- value;
17
- return true;
18
- }
19
-
20
- export function asThemeValuesObject(value: any): ThemeValues | null {
21
- return value;
35
+ function validateThemeValues(themeValue?: ThemeValues) {
36
+ if (themeValue === undefined || themeValue === null) {
37
+ return;
38
+ }
39
+ for (const [key, value] of Object.entries(themeValue)) {
40
+ if (typeof key !== "string") {
41
+ throw new Error(`Invalid theme key, ${key} is not a string`);
42
+ }
43
+ if (value === undefined || value === null) {
44
+ continue;
45
+ } else if (typeof value === "object") {
46
+ validateThemeValues(value);
47
+ } else if (typeof value !== "string" && typeof value !== "number") {
48
+ throw new Error(
49
+ `Invalid theme value, ${value} is not a string, number, or object`
50
+ );
51
+ }
52
+ }
53
+ }
54
+ validateThemeValues(theme.colors.branding);
55
+ validateThemeValues(theme.colors.text);
56
+ validateThemeValues(theme.colors.foreground);
57
+ validateThemeValues(theme.colors.background);
58
+ validateThemeValues(theme.colors.border);
22
59
  }