Skip to content

Commit db56f89

Browse files
authored
Fix error when using computed outside (#22)
1 parent 460471d commit db56f89

5 files changed

Lines changed: 45 additions & 19 deletions

File tree

demo/styles/global.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import { GlobalStyles } from "react-native-use-styles";
22

33
GlobalStyles({
44
constants: {
5-
midnight: '#2c3e50',
6-
clouds: '#ecf0f1',
7-
disabled: '#7f7f7f',
5+
midnight: "#2c3e50",
6+
clouds: "#ecf0f1",
7+
disabled: "#7f7f7f",
88
title: 40
99
},
10-
container: "bg:color:$midnight fx:1 jf:content:center",
10+
container: "bg:color:$midnight fx:1 jf:content:center"
1111
});

demo/styles/namespaced.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import { Styles } from "react-native-use-styles";
22

33
export default Styles({
44
computed: {
5-
disabled: ([isDisabled]) => ({ color: isDisabled ? '$disabled' : '$clouds' })
5+
disabled: ([isDisabled]) => ({
6+
color: isDisabled ? "$disabled" : "$clouds"
7+
})
68
},
79
centered: "txt:align:center"
810
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-use-styles",
3-
"version": "1.2.2",
3+
"version": "1.2.3",
44
"description": "React Native useStyles",
55
"main": "lib/index.js",
66
"module": "src/index.js",

src/core/manager.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
getNamespace,
1212
hasPath
1313
} from "../utils";
14+
import { CONSTANTS_KEY, COMPUTED_KEY } from "../constants";
1415

1516
export const getFromStorage = (
1617
pKey,
@@ -44,7 +45,7 @@ const computePath = (path, namespace, dependencies) => {
4445
if (!fn) {
4546
if (process.env.NODE_ENV !== "production") {
4647
console.warn(
47-
`Non-Existent-Computed: Computed ${path} not found in cache.`
48+
`Non-Existent-Computed: Computed ${path} not found in cache. You are seeing this warning because you are in development mode. In a production build there will be no warning.`
4849
);
4950
}
5051
return;
@@ -86,9 +87,9 @@ export const GlobalUse = (rawStyles, namespace) => {
8687
return dependencies => {
8788
let styles = rawStyles;
8889

89-
if (typeof styles !== "object") {
90+
if (typeof styles === "string") {
9091
styles = processStyles(styles, namespace, dependencies);
91-
} else {
92+
} else if (typeof styles === "object") {
9293
constantsMutation(styles, namespace);
9394
}
9495

@@ -100,9 +101,19 @@ export const GlobalStyles = (definition, namespace) => {
100101
for (let [key, rawStyles] of Object.entries(definition)) {
101102
let styles = rawStyles;
102103

103-
if (typeof styles !== "object") {
104+
if (process.env.NODE_ENV !== "production" && typeof styles === "function") {
105+
console.warn(
106+
`Invalid-Style-Type: The following style is invalid: "${key}", computed styles are placed inside the computed section. You are seeing this warning because you are in development mode. In a production build there will be no warning and these styles will be ignored.`
107+
);
108+
}
109+
110+
if (typeof styles === "string") {
104111
definition[key] = processStyles(styles, namespace, null, definition);
105-
} else {
112+
} else if (
113+
typeof styles === "object" &&
114+
key !== CONSTANTS_KEY &&
115+
key !== COMPUTED_KEY
116+
) {
106117
constantsMutation(styles, namespace, definition);
107118
}
108119
}

tests/core/manager.spec.js

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,6 @@ describe("utils", () => {
2424
expect(getFromCache("local", "namespace")).toMatchObject({ flex: 1 });
2525
});
2626

27-
it("Development mode only: GlobalUse produces a console.warn when providing a non-existent namespace", () => {
28-
GlobalUse("color:@not-a-namespace$blue")();
29-
expect(console.warn).toBeCalledTimes(1);
30-
expect(console.warn).toHaveBeenLastCalledWith(
31-
'Non-Existent-Namespace: The following namespace does not exist or has not been imported: "not-a-namespace". You are seeing this warning because you are in development mode. In a production build there will be no warning and these styles will be ignored.'
32-
);
33-
});
34-
3527
it("GlobalStyles sets constants cache properly", () => {
3628
GlobalStyles({
3729
constants: {
@@ -194,4 +186,25 @@ describe("utils", () => {
194186
it("GlobalUse with only falsey value", () => {
195187
expect(GlobalUse("undefined")()).toMatchObject({});
196188
});
189+
190+
it("Development mode only: GlobalUse produces a console.warn when providing a non-existent namespace", () => {
191+
GlobalUse("color:@not-a-namespace$blue")();
192+
expect(console.warn).toBeCalledTimes(1);
193+
expect(console.warn).toHaveBeenLastCalledWith(
194+
'Non-Existent-Namespace: The following namespace does not exist or has not been imported: "not-a-namespace". You are seeing this warning because you are in development mode. In a production build there will be no warning and these styles will be ignored.'
195+
);
196+
});
197+
198+
it("Development mode only: GlobalStyles produces a console.warn when providing an Invalid-Style-Type", () => {
199+
GlobalStyles({
200+
computed: {
201+
insideComputed: ([isColored]) => ({ color: isColored ? "red" : "blue" })
202+
},
203+
outsideComputed: ([isColored]) => ({ color: isColored ? "red" : "blue" })
204+
});
205+
expect(console.warn).toBeCalledTimes(1);
206+
expect(console.warn).toHaveBeenLastCalledWith(
207+
'Invalid-Style-Type: The following style is invalid: "outsideComputed", computed styles are placed inside the computed section. You are seeing this warning because you are in development mode. In a production build there will be no warning and these styles will be ignored.'
208+
);
209+
});
197210
});

0 commit comments

Comments
 (0)