@draftbit/theme 50.6.2-eac111.2 → 50.6.2-fba047.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/createTheme.js +1 -1
- package/lib/commonjs/createThemeValuesProxy.js +1 -1
- package/lib/commonjs/validators.js +1 -1
- package/lib/typescript/src/Provider.js +18 -38
- package/lib/typescript/src/Provider.js.map +1 -1
- package/lib/typescript/src/createTheme.d.ts +13 -1
- package/lib/typescript/src/createTheme.js +7 -18
- package/lib/typescript/src/createTheme.js.map +1 -1
- package/lib/typescript/src/createThemeValuesProxy.d.ts +1 -9
- package/lib/typescript/src/createThemeValuesProxy.js +24 -26
- 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 +4 -5
- package/src/Provider.js +18 -38
- package/src/Provider.js.map +1 -1
- package/src/Provider.tsx +53 -44
- package/src/createTheme.js +7 -18
- package/src/createTheme.js.map +1 -1
- package/src/createTheme.ts +7 -28
- package/src/createThemeValuesProxy.js +24 -26
- package/src/createThemeValuesProxy.js.map +1 -1
- package/src/createThemeValuesProxy.ts +90 -44
- package/src/validators.js +6 -80
- package/src/validators.js.map +1 -1
- package/src/validators.ts +6 -100
package/src/Provider.tsx
CHANGED
|
@@ -7,7 +7,6 @@ import {
|
|
|
7
7
|
Breakpoints,
|
|
8
8
|
ChangeThemeOptions,
|
|
9
9
|
ReadTheme,
|
|
10
|
-
ThemeValues,
|
|
11
10
|
ValidatedTheme,
|
|
12
11
|
} from "./types";
|
|
13
12
|
|
|
@@ -47,18 +46,16 @@ const Provider: React.FC<React.PropsWithChildren<ProviderProps>> = ({
|
|
|
47
46
|
|
|
48
47
|
const changeTheme = React.useCallback(
|
|
49
48
|
(themeName: string, options?: ChangeThemeOptions) => {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
return;
|
|
59
|
-
}
|
|
60
|
-
setCurrentTheme(theme);
|
|
49
|
+
const theme = themes.find((t) => t.name === themeName);
|
|
50
|
+
if (!theme) {
|
|
51
|
+
console.warn(
|
|
52
|
+
"Theme with name",
|
|
53
|
+
themeName,
|
|
54
|
+
"not found. Make sure it's passed into the top level ThemeProvider"
|
|
55
|
+
);
|
|
56
|
+
return;
|
|
61
57
|
}
|
|
58
|
+
setCurrentTheme(theme);
|
|
62
59
|
|
|
63
60
|
if (options?.persistent === true) {
|
|
64
61
|
AsyncStorage.setItem(SAVED_SELECTED_THEME_KEY, themeName).catch((e) => {
|
|
@@ -66,31 +63,53 @@ const Provider: React.FC<React.PropsWithChildren<ProviderProps>> = ({
|
|
|
66
63
|
});
|
|
67
64
|
}
|
|
68
65
|
},
|
|
69
|
-
[themes,
|
|
66
|
+
[themes, setCurrentTheme]
|
|
70
67
|
);
|
|
71
68
|
|
|
72
|
-
const proxiedTheme: ReadTheme = React.useMemo(
|
|
73
|
-
|
|
74
|
-
createThemeValuesProxy({
|
|
75
|
-
value,
|
|
76
|
-
breakpoints,
|
|
77
|
-
deviceWidth,
|
|
78
|
-
devicePlatform: Platform.OS,
|
|
79
|
-
currentLightDarkSelection: lightDarkSelection,
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
return {
|
|
69
|
+
const proxiedTheme: ReadTheme = React.useMemo(
|
|
70
|
+
() => ({
|
|
83
71
|
...currentTheme,
|
|
84
72
|
colors: {
|
|
85
|
-
branding:
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
73
|
+
branding: createThemeValuesProxy(
|
|
74
|
+
currentTheme.colors.branding,
|
|
75
|
+
breakpoints,
|
|
76
|
+
deviceWidth,
|
|
77
|
+
Platform.OS,
|
|
78
|
+
lightDarkSelection
|
|
79
|
+
),
|
|
80
|
+
text: createThemeValuesProxy(
|
|
81
|
+
currentTheme.colors.text,
|
|
82
|
+
breakpoints,
|
|
83
|
+
deviceWidth,
|
|
84
|
+
Platform.OS,
|
|
85
|
+
lightDarkSelection
|
|
86
|
+
),
|
|
87
|
+
background: createThemeValuesProxy(
|
|
88
|
+
currentTheme.colors.background,
|
|
89
|
+
breakpoints,
|
|
90
|
+
deviceWidth,
|
|
91
|
+
Platform.OS,
|
|
92
|
+
lightDarkSelection
|
|
93
|
+
),
|
|
94
|
+
foreground: createThemeValuesProxy(
|
|
95
|
+
currentTheme.colors.foreground,
|
|
96
|
+
breakpoints,
|
|
97
|
+
deviceWidth,
|
|
98
|
+
Platform.OS,
|
|
99
|
+
lightDarkSelection
|
|
100
|
+
),
|
|
101
|
+
border: createThemeValuesProxy(
|
|
102
|
+
currentTheme.colors.border,
|
|
103
|
+
breakpoints,
|
|
104
|
+
deviceWidth,
|
|
105
|
+
Platform.OS,
|
|
106
|
+
lightDarkSelection
|
|
107
|
+
),
|
|
90
108
|
},
|
|
91
|
-
typography:
|
|
92
|
-
}
|
|
93
|
-
|
|
109
|
+
typography: currentTheme.typography,
|
|
110
|
+
}),
|
|
111
|
+
[currentTheme, deviceWidth, breakpoints, lightDarkSelection]
|
|
112
|
+
);
|
|
94
113
|
|
|
95
114
|
React.useEffect(() => {
|
|
96
115
|
const listener = Dimensions.addEventListener("change", ({ window }) =>
|
|
@@ -106,19 +125,9 @@ const Provider: React.FC<React.PropsWithChildren<ProviderProps>> = ({
|
|
|
106
125
|
const savedSelectedThemeName = await AsyncStorage.getItem(
|
|
107
126
|
SAVED_SELECTED_THEME_KEY
|
|
108
127
|
);
|
|
128
|
+
|
|
109
129
|
if (savedSelectedThemeName) {
|
|
110
|
-
|
|
111
|
-
(t) => t.name === savedSelectedThemeName
|
|
112
|
-
);
|
|
113
|
-
console.log("RUNNING", savedSelectedThemeName);
|
|
114
|
-
|
|
115
|
-
if (themeExists) {
|
|
116
|
-
changeTheme(savedSelectedThemeName);
|
|
117
|
-
} else {
|
|
118
|
-
AsyncStorage.removeItem(SAVED_SELECTED_THEME_KEY).catch((e) => {
|
|
119
|
-
console.warn("Failed to reset persisted selected theme", e);
|
|
120
|
-
});
|
|
121
|
-
}
|
|
130
|
+
changeTheme(savedSelectedThemeName);
|
|
122
131
|
}
|
|
123
132
|
};
|
|
124
133
|
run();
|
package/src/createTheme.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { isTextStyleObject, validateBreakpoints, validatePalettes, validateTheme, } from "./validators";
|
|
1
|
+
import merge from "deepmerge";
|
|
3
2
|
/**
|
|
4
3
|
* Custom deepmerge function to skip merging of typography/text style objects.
|
|
5
4
|
*
|
|
@@ -12,24 +11,14 @@ import { isTextStyleObject, validateBreakpoints, validatePalettes, validateTheme
|
|
|
12
11
|
* keys and the keys of the style object which breaks how the proxy is able to
|
|
13
12
|
* return the correct value.
|
|
14
13
|
*/
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
if (isTextStyleObject(firstValue)) {
|
|
20
|
-
return utils.defaultMergeFunctions.mergeRecords(values.slice(1), utils, meta);
|
|
21
|
-
}
|
|
22
|
-
return utils.defaultMergeFunctions.mergeRecords(values, utils, meta);
|
|
23
|
-
},
|
|
24
|
-
});
|
|
25
|
-
export default function createTheme({ breakpoints, palettes, theme, baseTheme, }) {
|
|
26
|
-
validateBreakpoints(breakpoints);
|
|
27
|
-
validatePalettes(palettes);
|
|
28
|
-
validateTheme(theme);
|
|
14
|
+
export default function createTheme({ theme, baseTheme, }) {
|
|
15
|
+
// validateBreakpoints(breakpoints);
|
|
16
|
+
// validatePalettes(palettes);
|
|
17
|
+
// validateTheme(theme);
|
|
29
18
|
let resultTheme = theme;
|
|
30
19
|
if (baseTheme) {
|
|
31
|
-
validateTheme(baseTheme);
|
|
32
|
-
resultTheme =
|
|
20
|
+
//validateTheme(baseTheme);
|
|
21
|
+
resultTheme = merge(baseTheme, theme);
|
|
33
22
|
}
|
|
34
23
|
return {
|
|
35
24
|
...resultTheme,
|
package/src/createTheme.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createTheme.js","sourceRoot":"","sources":["createTheme.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"createTheme.js","sourceRoot":"","sources":["createTheme.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,MAAM,WAAW,CAAC;AAE9B;;;;;;;;;;;GAWG;AAEH,MAAM,CAAC,OAAO,UAAU,WAAW,CAAC,EAClC,KAAK,EACL,SAAS,GAMV;IACC,oCAAoC;IACpC,8BAA8B;IAC9B,wBAAwB;IAExB,IAAI,WAAW,GAAG,KAAK,CAAC;IAExB,IAAI,SAAS,EAAE;QACb,2BAA2B;QAC3B,WAAW,GAAG,KAAK,CAAC,SAAS,EAAE,KAAK,CAAC,CAAC;KACvC;IAED,OAAO;QACL,GAAG,WAAW;QACd,SAAS,EAAE,IAAI;KAChB,CAAC;AACJ,CAAC"}
|
package/src/createTheme.ts
CHANGED
|
@@ -1,16 +1,11 @@
|
|
|
1
|
-
import { deepmergeCustom } from "deepmerge-ts";
|
|
2
1
|
import type {
|
|
3
2
|
Breakpoints,
|
|
4
3
|
Theme,
|
|
5
4
|
ValidatedTheme,
|
|
6
5
|
ColorPalettes,
|
|
7
6
|
} from "./types";
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
validateBreakpoints,
|
|
11
|
-
validatePalettes,
|
|
12
|
-
validateTheme,
|
|
13
|
-
} from "./validators";
|
|
7
|
+
|
|
8
|
+
import merge from "deepmerge";
|
|
14
9
|
|
|
15
10
|
/**
|
|
16
11
|
* Custom deepmerge function to skip merging of typography/text style objects.
|
|
@@ -24,24 +19,8 @@ import {
|
|
|
24
19
|
* keys and the keys of the style object which breaks how the proxy is able to
|
|
25
20
|
* return the correct value.
|
|
26
21
|
*/
|
|
27
|
-
const themeMerge = deepmergeCustom({
|
|
28
|
-
enableImplicitDefaultMerging: true,
|
|
29
|
-
mergeRecords(values, utils, meta) {
|
|
30
|
-
const firstValue = values[0];
|
|
31
|
-
if (isTextStyleObject(firstValue)) {
|
|
32
|
-
return utils.defaultMergeFunctions.mergeRecords(
|
|
33
|
-
values.slice(1),
|
|
34
|
-
utils,
|
|
35
|
-
meta
|
|
36
|
-
);
|
|
37
|
-
}
|
|
38
|
-
return utils.defaultMergeFunctions.mergeRecords(values, utils, meta);
|
|
39
|
-
},
|
|
40
|
-
});
|
|
41
22
|
|
|
42
23
|
export default function createTheme({
|
|
43
|
-
breakpoints,
|
|
44
|
-
palettes,
|
|
45
24
|
theme,
|
|
46
25
|
baseTheme,
|
|
47
26
|
}: {
|
|
@@ -50,15 +29,15 @@ export default function createTheme({
|
|
|
50
29
|
theme: Theme;
|
|
51
30
|
baseTheme?: Theme;
|
|
52
31
|
}): ValidatedTheme {
|
|
53
|
-
validateBreakpoints(breakpoints);
|
|
54
|
-
validatePalettes(palettes);
|
|
55
|
-
validateTheme(theme);
|
|
32
|
+
// validateBreakpoints(breakpoints);
|
|
33
|
+
// validatePalettes(palettes);
|
|
34
|
+
// validateTheme(theme);
|
|
56
35
|
|
|
57
36
|
let resultTheme = theme;
|
|
58
37
|
|
|
59
38
|
if (baseTheme) {
|
|
60
|
-
validateTheme(baseTheme);
|
|
61
|
-
resultTheme =
|
|
39
|
+
//validateTheme(baseTheme);
|
|
40
|
+
resultTheme = merge(baseTheme, theme);
|
|
62
41
|
}
|
|
63
42
|
|
|
64
43
|
return {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { isObject } from "lodash";
|
|
2
2
|
/**
|
|
3
3
|
* Creates a proxy for theme value objects to select a value whenever
|
|
4
4
|
* multiple values are provided for different platforms, breakpoints, and/or light/dark modes
|
|
@@ -6,37 +6,32 @@ import { asThemeValuesObject } from "./validators";
|
|
|
6
6
|
* Ex: {color: {ios: "blue", android: "red"}}
|
|
7
7
|
* -> theme.color returns "blue" when the platform is ios and "red" when android
|
|
8
8
|
*/
|
|
9
|
-
export default function createThemeValuesProxy(
|
|
9
|
+
export default function createThemeValuesProxy(value, breakpoints, deviceWidth, devicePlatform, currentLightDarkSelection) {
|
|
10
10
|
if (value === undefined || value === null) {
|
|
11
11
|
return undefined;
|
|
12
12
|
}
|
|
13
13
|
return new Proxy(value, {
|
|
14
14
|
get: (target, key) => {
|
|
15
15
|
const currentValue = target[key];
|
|
16
|
-
const valueAsThemeValues =
|
|
16
|
+
const valueAsThemeValues = isObject(currentValue)
|
|
17
|
+
? currentValue
|
|
18
|
+
: undefined;
|
|
17
19
|
if (valueAsThemeValues) {
|
|
18
20
|
const platformKeys = ["ios", "android", "web", "macos", "windows"];
|
|
19
21
|
const breakpointKeys = Object.keys(breakpoints);
|
|
20
22
|
const lightDarkKeys = ["light", "dark"];
|
|
21
23
|
const keysType = getKeysType(valueAsThemeValues, platformKeys, breakpointKeys, lightDarkKeys);
|
|
22
|
-
const input = {
|
|
23
|
-
value: valueAsThemeValues,
|
|
24
|
-
breakpoints,
|
|
25
|
-
deviceWidth,
|
|
26
|
-
devicePlatform,
|
|
27
|
-
currentLightDarkSelection,
|
|
28
|
-
};
|
|
29
24
|
if (keysType === "default") {
|
|
30
|
-
return createThemeValuesProxy(
|
|
25
|
+
return createThemeValuesProxy(valueAsThemeValues, breakpoints, deviceWidth, devicePlatform, currentLightDarkSelection);
|
|
31
26
|
}
|
|
32
27
|
else if (keysType === "platform") {
|
|
33
|
-
return getPlatformValue(
|
|
28
|
+
return getPlatformValue(valueAsThemeValues, breakpoints, deviceWidth, devicePlatform, currentLightDarkSelection);
|
|
34
29
|
}
|
|
35
30
|
else if (keysType === "breakpoint") {
|
|
36
|
-
return getBreakpointValue(
|
|
31
|
+
return getBreakpointValue(valueAsThemeValues, breakpoints, deviceWidth, devicePlatform, currentLightDarkSelection);
|
|
37
32
|
}
|
|
38
33
|
else if (keysType === "lightDark") {
|
|
39
|
-
return getLightDarkValue(
|
|
34
|
+
return getLightDarkValue(valueAsThemeValues, breakpoints, deviceWidth, devicePlatform, currentLightDarkSelection);
|
|
40
35
|
}
|
|
41
36
|
else {
|
|
42
37
|
return undefined;
|
|
@@ -83,21 +78,21 @@ function onlyOneTrue(a, b, c, d) {
|
|
|
83
78
|
function allFalse(a, b, c, d) {
|
|
84
79
|
return [a, b, c, d].filter((x) => x).length === 0;
|
|
85
80
|
}
|
|
86
|
-
function getPlatformValue(
|
|
81
|
+
function getPlatformValue(value, breakpoints, deviceWidth, devicePlatform, currentLightDarkSelection) {
|
|
87
82
|
var _a;
|
|
88
|
-
const { value, devicePlatform } = input;
|
|
89
83
|
const currentPlatformValue = (_a = value === null || value === void 0 ? void 0 : value[devicePlatform]) !== null && _a !== void 0 ? _a : value === null || value === void 0 ? void 0 : value.default;
|
|
90
|
-
const valueAsThemeValues =
|
|
84
|
+
const valueAsThemeValues = isObject(currentPlatformValue)
|
|
85
|
+
? currentPlatformValue
|
|
86
|
+
: undefined;
|
|
91
87
|
if (valueAsThemeValues) {
|
|
92
|
-
return createThemeValuesProxy(
|
|
88
|
+
return createThemeValuesProxy(valueAsThemeValues, breakpoints, deviceWidth, devicePlatform, currentLightDarkSelection);
|
|
93
89
|
}
|
|
94
90
|
else {
|
|
95
91
|
return currentPlatformValue;
|
|
96
92
|
}
|
|
97
93
|
}
|
|
98
|
-
function getBreakpointValue(
|
|
94
|
+
function getBreakpointValue(value, breakpoints, deviceWidth, devicePlatform, currentLightDarkSelection) {
|
|
99
95
|
var _a;
|
|
100
|
-
const { value, breakpoints, deviceWidth } = input;
|
|
101
96
|
const keysToBreakpointValue = Object.keys(value !== null && value !== void 0 ? value : {}).map((key) => [key, breakpoints[key]]);
|
|
102
97
|
const orderedBreakpoints = keysToBreakpointValue.sort(([_, val]) => val);
|
|
103
98
|
let currentBreakpointKey = "";
|
|
@@ -107,21 +102,24 @@ function getBreakpointValue(input) {
|
|
|
107
102
|
}
|
|
108
103
|
}
|
|
109
104
|
const currentBreakpointValue = (_a = value === null || value === void 0 ? void 0 : value[currentBreakpointKey]) !== null && _a !== void 0 ? _a : value === null || value === void 0 ? void 0 : value.default;
|
|
110
|
-
const valueAsThemeValues =
|
|
105
|
+
const valueAsThemeValues = isObject(currentBreakpointKey)
|
|
106
|
+
? currentBreakpointKey
|
|
107
|
+
: undefined;
|
|
111
108
|
if (valueAsThemeValues) {
|
|
112
|
-
return createThemeValuesProxy(
|
|
109
|
+
return createThemeValuesProxy(valueAsThemeValues, breakpoints, deviceWidth, devicePlatform, currentLightDarkSelection);
|
|
113
110
|
}
|
|
114
111
|
else {
|
|
115
112
|
return currentBreakpointValue;
|
|
116
113
|
}
|
|
117
114
|
}
|
|
118
|
-
function getLightDarkValue(
|
|
115
|
+
function getLightDarkValue(value, breakpoints, deviceWidth, devicePlatform, currentLightDarkSelection) {
|
|
119
116
|
var _a;
|
|
120
|
-
const { value, currentLightDarkSelection } = input;
|
|
121
117
|
const currentLightDarkValue = (_a = value === null || value === void 0 ? void 0 : value[currentLightDarkSelection]) !== null && _a !== void 0 ? _a : value === null || value === void 0 ? void 0 : value.default;
|
|
122
|
-
const valueAsThemeValues =
|
|
118
|
+
const valueAsThemeValues = isObject(currentLightDarkSelection)
|
|
119
|
+
? currentLightDarkSelection
|
|
120
|
+
: undefined;
|
|
123
121
|
if (valueAsThemeValues) {
|
|
124
|
-
return createThemeValuesProxy(
|
|
122
|
+
return createThemeValuesProxy(valueAsThemeValues, breakpoints, deviceWidth, devicePlatform, currentLightDarkSelection);
|
|
125
123
|
}
|
|
126
124
|
else {
|
|
127
125
|
return currentLightDarkValue;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createThemeValuesProxy.js","sourceRoot":"","sources":["createThemeValuesProxy.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"createThemeValuesProxy.js","sourceRoot":"","sources":["createThemeValuesProxy.ts"],"names":[],"mappings":"AAEA,OAAO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AAC;AAED,SAAS,iBAAiB,CACxB,KAA8B,EAC9B,WAAwB,EACxB,WAAmB,EACnB,cAAkC,EAClC,yBAA2C;;IAE3C,MAAM,qBAAqB,GACzB,MAAA,KAAK,aAAL,KAAK,uBAAL,KAAK,CAAG,yBAAyB,CAAC,mCAAI,KAAK,aAAL,KAAK,uBAAL,KAAK,CAAE,OAAO,CAAC;IACvD,MAAM,kBAAkB,GAAG,QAAQ,CAAC,yBAAyB,CAAC;QAC5D,CAAC,CAAE,yBAAyC;QAC5C,CAAC,CAAC,SAAS,CAAC;IAEd,IAAI,kBAAkB,EAAE;QACtB,OAAO,sBAAsB,CAC3B,kBAAkB,EAClB,WAAW,EACX,WAAW,EACX,cAAc,EACd,yBAAyB,CAC1B,CAAC;KACH;SAAM;QACL,OAAO,qBAAqB,CAAC;KAC9B;AACH,CAAC"}
|
|
@@ -1,14 +1,6 @@
|
|
|
1
1
|
import { ThemeValues, Breakpoints } from "./types";
|
|
2
2
|
import { Platform, TextStyle } from "react-native";
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
interface CreateThemeValuesProxyInput {
|
|
6
|
-
value: ThemeValues | undefined;
|
|
7
|
-
breakpoints: Breakpoints;
|
|
8
|
-
deviceWidth: number;
|
|
9
|
-
devicePlatform: typeof Platform.OS;
|
|
10
|
-
currentLightDarkSelection: "light" | "dark";
|
|
11
|
-
}
|
|
3
|
+
import { isObject } from "lodash";
|
|
12
4
|
|
|
13
5
|
/**
|
|
14
6
|
* Creates a proxy for theme value objects to select a value whenever
|
|
@@ -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
|
}
|
|
@@ -34,7 +26,9 @@ export default function createThemeValuesProxy({
|
|
|
34
26
|
): string | number | ThemeValues | TextStyle | undefined => {
|
|
35
27
|
const currentValue = target[key];
|
|
36
28
|
|
|
37
|
-
const valueAsThemeValues =
|
|
29
|
+
const valueAsThemeValues = isObject(currentValue)
|
|
30
|
+
? (currentValue as ThemeValues)
|
|
31
|
+
: undefined;
|
|
38
32
|
|
|
39
33
|
if (valueAsThemeValues) {
|
|
40
34
|
const platformKeys = ["ios", "android", "web", "macos", "windows"];
|
|
@@ -48,22 +42,38 @@ export default function createThemeValuesProxy({
|
|
|
48
42
|
lightDarkKeys
|
|
49
43
|
);
|
|
50
44
|
|
|
51
|
-
const input: CreateThemeValuesProxyInput = {
|
|
52
|
-
value: valueAsThemeValues,
|
|
53
|
-
breakpoints,
|
|
54
|
-
deviceWidth,
|
|
55
|
-
devicePlatform,
|
|
56
|
-
currentLightDarkSelection,
|
|
57
|
-
};
|
|
58
|
-
|
|
59
45
|
if (keysType === "default") {
|
|
60
|
-
return createThemeValuesProxy(
|
|
46
|
+
return createThemeValuesProxy(
|
|
47
|
+
valueAsThemeValues,
|
|
48
|
+
breakpoints,
|
|
49
|
+
deviceWidth,
|
|
50
|
+
devicePlatform,
|
|
51
|
+
currentLightDarkSelection
|
|
52
|
+
);
|
|
61
53
|
} else if (keysType === "platform") {
|
|
62
|
-
return getPlatformValue(
|
|
54
|
+
return getPlatformValue(
|
|
55
|
+
valueAsThemeValues,
|
|
56
|
+
breakpoints,
|
|
57
|
+
deviceWidth,
|
|
58
|
+
devicePlatform,
|
|
59
|
+
currentLightDarkSelection
|
|
60
|
+
);
|
|
63
61
|
} else if (keysType === "breakpoint") {
|
|
64
|
-
return getBreakpointValue(
|
|
62
|
+
return getBreakpointValue(
|
|
63
|
+
valueAsThemeValues,
|
|
64
|
+
breakpoints,
|
|
65
|
+
deviceWidth,
|
|
66
|
+
devicePlatform,
|
|
67
|
+
currentLightDarkSelection
|
|
68
|
+
);
|
|
65
69
|
} else if (keysType === "lightDark") {
|
|
66
|
-
return getLightDarkValue(
|
|
70
|
+
return getLightDarkValue(
|
|
71
|
+
valueAsThemeValues,
|
|
72
|
+
breakpoints,
|
|
73
|
+
deviceWidth,
|
|
74
|
+
devicePlatform,
|
|
75
|
+
currentLightDarkSelection
|
|
76
|
+
);
|
|
67
77
|
} else {
|
|
68
78
|
return undefined;
|
|
69
79
|
}
|
|
@@ -135,22 +145,38 @@ function allFalse(a: boolean, b: boolean, c: boolean, d: boolean) {
|
|
|
135
145
|
return [a, b, c, d].filter((x) => x).length === 0;
|
|
136
146
|
}
|
|
137
147
|
|
|
138
|
-
function getPlatformValue(
|
|
139
|
-
|
|
140
|
-
|
|
148
|
+
function getPlatformValue(
|
|
149
|
+
value: ThemeValues | undefined,
|
|
150
|
+
breakpoints: Breakpoints,
|
|
151
|
+
deviceWidth: number,
|
|
152
|
+
devicePlatform: typeof Platform.OS,
|
|
153
|
+
currentLightDarkSelection: "light" | "dark"
|
|
154
|
+
) {
|
|
141
155
|
const currentPlatformValue = value?.[devicePlatform] ?? value?.default;
|
|
142
|
-
const valueAsThemeValues =
|
|
156
|
+
const valueAsThemeValues = isObject(currentPlatformValue)
|
|
157
|
+
? (currentPlatformValue as ThemeValues)
|
|
158
|
+
: undefined;
|
|
143
159
|
|
|
144
160
|
if (valueAsThemeValues) {
|
|
145
|
-
return createThemeValuesProxy(
|
|
161
|
+
return createThemeValuesProxy(
|
|
162
|
+
valueAsThemeValues,
|
|
163
|
+
breakpoints,
|
|
164
|
+
deviceWidth,
|
|
165
|
+
devicePlatform,
|
|
166
|
+
currentLightDarkSelection
|
|
167
|
+
);
|
|
146
168
|
} else {
|
|
147
169
|
return currentPlatformValue;
|
|
148
170
|
}
|
|
149
171
|
}
|
|
150
172
|
|
|
151
|
-
function getBreakpointValue(
|
|
152
|
-
|
|
153
|
-
|
|
173
|
+
function getBreakpointValue(
|
|
174
|
+
value: ThemeValues | undefined,
|
|
175
|
+
breakpoints: Breakpoints,
|
|
176
|
+
deviceWidth: number,
|
|
177
|
+
devicePlatform: typeof Platform.OS,
|
|
178
|
+
currentLightDarkSelection: "light" | "dark"
|
|
179
|
+
) {
|
|
154
180
|
const keysToBreakpointValue: [string, number][] = Object.keys(
|
|
155
181
|
value ?? {}
|
|
156
182
|
).map((key) => [key, breakpoints[key]]);
|
|
@@ -163,24 +189,44 @@ function getBreakpointValue(input: CreateThemeValuesProxyInput) {
|
|
|
163
189
|
}
|
|
164
190
|
const currentBreakpointValue =
|
|
165
191
|
value?.[currentBreakpointKey] ?? value?.default;
|
|
166
|
-
const valueAsThemeValues =
|
|
192
|
+
const valueAsThemeValues = isObject(currentBreakpointKey)
|
|
193
|
+
? (currentBreakpointKey as ThemeValues)
|
|
194
|
+
: undefined;
|
|
167
195
|
|
|
168
196
|
if (valueAsThemeValues) {
|
|
169
|
-
return createThemeValuesProxy(
|
|
197
|
+
return createThemeValuesProxy(
|
|
198
|
+
valueAsThemeValues,
|
|
199
|
+
breakpoints,
|
|
200
|
+
deviceWidth,
|
|
201
|
+
devicePlatform,
|
|
202
|
+
currentLightDarkSelection
|
|
203
|
+
);
|
|
170
204
|
} else {
|
|
171
205
|
return currentBreakpointValue;
|
|
172
206
|
}
|
|
173
207
|
}
|
|
174
208
|
|
|
175
|
-
function getLightDarkValue(
|
|
176
|
-
|
|
177
|
-
|
|
209
|
+
function getLightDarkValue(
|
|
210
|
+
value: ThemeValues | undefined,
|
|
211
|
+
breakpoints: Breakpoints,
|
|
212
|
+
deviceWidth: number,
|
|
213
|
+
devicePlatform: typeof Platform.OS,
|
|
214
|
+
currentLightDarkSelection: "light" | "dark"
|
|
215
|
+
) {
|
|
178
216
|
const currentLightDarkValue =
|
|
179
217
|
value?.[currentLightDarkSelection] ?? value?.default;
|
|
180
|
-
const valueAsThemeValues =
|
|
218
|
+
const valueAsThemeValues = isObject(currentLightDarkSelection)
|
|
219
|
+
? (currentLightDarkSelection as ThemeValues)
|
|
220
|
+
: undefined;
|
|
181
221
|
|
|
182
222
|
if (valueAsThemeValues) {
|
|
183
|
-
return createThemeValuesProxy(
|
|
223
|
+
return createThemeValuesProxy(
|
|
224
|
+
valueAsThemeValues,
|
|
225
|
+
breakpoints,
|
|
226
|
+
deviceWidth,
|
|
227
|
+
devicePlatform,
|
|
228
|
+
currentLightDarkSelection
|
|
229
|
+
);
|
|
184
230
|
} else {
|
|
185
231
|
return currentLightDarkValue;
|
|
186
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
|