@jobber/components-native 0.114.12 → 0.114.13-JOB-182796-7b6fda7.0
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/dist/package.json +2 -2
- package/dist/src/AtlantisThemeContext/buildThemedStyles.js +8 -2
- package/dist/src/AtlantisThemeContext/buildThemedStyles.test.js +43 -1
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +2 -2
- package/src/AtlantisThemeContext/AtlantisThemeContext.guide.md +12 -0
- package/src/AtlantisThemeContext/buildThemedStyles.test.tsx +63 -1
- package/src/AtlantisThemeContext/buildThemedStyles.ts +11 -2
package/dist/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jobber/components-native",
|
|
3
|
-
"version": "0.114.
|
|
3
|
+
"version": "0.114.13-JOB-182796-7b6fda7.0+7b6fda7b",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "React Native implementation of Atlantis",
|
|
6
6
|
"repository": {
|
|
@@ -123,5 +123,5 @@
|
|
|
123
123
|
"react-native-screens": ">=4.18.0",
|
|
124
124
|
"react-native-svg": ">=12.0.0"
|
|
125
125
|
},
|
|
126
|
-
"gitHead": "
|
|
126
|
+
"gitHead": "7b6fda7bf9b386a9eb3676dd5f432d2a8b4050ae"
|
|
127
127
|
}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { StyleSheet } from "react-native";
|
|
2
|
-
import { useMemo } from "react";
|
|
3
2
|
import { useAtlantisTheme } from "./AtlantisThemeContext";
|
|
4
3
|
/**
|
|
5
4
|
* Creates a hook that generates themed styles using the current theme tokens.
|
|
@@ -31,8 +30,15 @@ import { useAtlantisTheme } from "./AtlantisThemeContext";
|
|
|
31
30
|
* @see Related functions: {@link useAtlantisTheme}
|
|
32
31
|
*/
|
|
33
32
|
export function buildThemedStyles(styleFactory) {
|
|
33
|
+
const stylesByTokens = new WeakMap();
|
|
34
34
|
return function useStyles() {
|
|
35
35
|
const { tokens } = useAtlantisTheme();
|
|
36
|
-
|
|
36
|
+
const cached = stylesByTokens.get(tokens);
|
|
37
|
+
if (cached !== undefined) {
|
|
38
|
+
return cached;
|
|
39
|
+
}
|
|
40
|
+
const themedStyles = StyleSheet.create(styleFactory(tokens));
|
|
41
|
+
stylesByTokens.set(tokens, themedStyles);
|
|
42
|
+
return themedStyles;
|
|
37
43
|
};
|
|
38
44
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import React from "react";
|
|
2
|
-
import {
|
|
2
|
+
import { Text } from "react-native";
|
|
3
|
+
import { render, renderHook } from "@testing-library/react-native";
|
|
3
4
|
import { buildThemedStyles } from "./buildThemedStyles";
|
|
4
5
|
import { AtlantisThemeContextProvider } from "./AtlantisThemeContext";
|
|
5
6
|
describe("buildThemedStyles", () => {
|
|
@@ -35,6 +36,47 @@ describe("buildThemedStyles", () => {
|
|
|
35
36
|
expect(styleFactory).toHaveBeenCalledTimes(1);
|
|
36
37
|
expect(result.current).toBe(initialResult);
|
|
37
38
|
});
|
|
39
|
+
it("reuses cached styles across multiple instances of the same generated hook", () => {
|
|
40
|
+
const styleFactory = jest.fn(tokens => ({
|
|
41
|
+
container: {
|
|
42
|
+
backgroundColor: tokens["color-surface"],
|
|
43
|
+
},
|
|
44
|
+
}));
|
|
45
|
+
const useTestStyles = buildThemedStyles(styleFactory);
|
|
46
|
+
const renderedStyles = [];
|
|
47
|
+
function TestComponent() {
|
|
48
|
+
const styles = useTestStyles();
|
|
49
|
+
renderedStyles.push(styles);
|
|
50
|
+
return React.createElement(Text, { style: styles.container }, "Test");
|
|
51
|
+
}
|
|
52
|
+
render(React.createElement(AtlantisThemeContextProvider, null,
|
|
53
|
+
React.createElement(TestComponent, null),
|
|
54
|
+
React.createElement(TestComponent, null),
|
|
55
|
+
React.createElement(TestComponent, null)));
|
|
56
|
+
expect(styleFactory).toHaveBeenCalledTimes(1);
|
|
57
|
+
expect(renderedStyles[1]).toBe(renderedStyles[0]);
|
|
58
|
+
expect(renderedStyles[2]).toBe(renderedStyles[0]);
|
|
59
|
+
});
|
|
60
|
+
it("creates styles again when the theme tokens change", () => {
|
|
61
|
+
const styleFactory = jest.fn(tokens => ({
|
|
62
|
+
container: {
|
|
63
|
+
backgroundColor: tokens["color-surface"],
|
|
64
|
+
},
|
|
65
|
+
}));
|
|
66
|
+
const useTestStyles = buildThemedStyles(styleFactory);
|
|
67
|
+
const renderedStyles = [];
|
|
68
|
+
function TestComponent() {
|
|
69
|
+
const styles = useTestStyles();
|
|
70
|
+
renderedStyles.push(styles);
|
|
71
|
+
return React.createElement(Text, { style: styles.container }, "Test");
|
|
72
|
+
}
|
|
73
|
+
const { rerender } = render(React.createElement(AtlantisThemeContextProvider, { dangerouslyOverrideTheme: "light" },
|
|
74
|
+
React.createElement(TestComponent, null)));
|
|
75
|
+
rerender(React.createElement(AtlantisThemeContextProvider, { dangerouslyOverrideTheme: "dark" },
|
|
76
|
+
React.createElement(TestComponent, null)));
|
|
77
|
+
expect(styleFactory).toHaveBeenCalledTimes(2);
|
|
78
|
+
expect(renderedStyles[1]).not.toBe(renderedStyles[0]);
|
|
79
|
+
});
|
|
38
80
|
it("handles empty style objects", () => {
|
|
39
81
|
const useTestStyles = buildThemedStyles(() => ({}));
|
|
40
82
|
const { result } = renderHook(() => useTestStyles(), { wrapper });
|