@jobber/components-native 0.114.12 → 0.114.13

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jobber/components-native",
3
- "version": "0.114.12",
3
+ "version": "0.114.13",
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": "a9ac41dfb966649048fcaf7cce2db45f2569fc29"
126
+ "gitHead": "0991c3a7ba2e5b5bce8aa8adea0eb60aa9d360f8"
127
127
  }
@@ -62,3 +62,15 @@ surface that visually leaves the normal React subtree.
62
62
  Important: a forced subtree preserves the selected theme semantics from the
63
63
  nearest non-forced provider. Calling `setTheme` inside the forced subtree still
64
64
  updates the upstream selected theme; only the rendered tokens stay forced.
65
+
66
+ ## Themed style cache
67
+
68
+ `buildThemedStyles` returns a module-scope `useStyles` hook. Each generated hook
69
+ owns a `WeakMap` keyed by the `tokens` object from the nearest Atlantis theme
70
+ context, so repeated component instances under the same theme reuse the same
71
+ `StyleSheet.create` result.
72
+
73
+ Call `buildThemedStyles` at module scope, keep the style factory pure over its
74
+ `tokens` argument, and treat the returned styles as immutable. If the active
75
+ theme changes and the context returns a different `tokens` object, the generated
76
+ hook creates and caches a separate style object for that token identity.
@@ -1,5 +1,6 @@
1
1
  import React from "react";
2
- import { renderHook } from "@testing-library/react-native";
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
 
@@ -47,6 +48,67 @@ describe("buildThemedStyles", () => {
47
48
  expect(result.current).toBe(initialResult);
48
49
  });
49
50
 
51
+ it("reuses cached styles across multiple instances of the same generated hook", () => {
52
+ const styleFactory = jest.fn(tokens => ({
53
+ container: {
54
+ backgroundColor: tokens["color-surface"],
55
+ },
56
+ }));
57
+ const useTestStyles = buildThemedStyles(styleFactory);
58
+ const renderedStyles: Array<ReturnType<typeof useTestStyles>> = [];
59
+
60
+ function TestComponent() {
61
+ const styles = useTestStyles();
62
+ renderedStyles.push(styles);
63
+
64
+ return <Text style={styles.container}>Test</Text>;
65
+ }
66
+
67
+ render(
68
+ <AtlantisThemeContextProvider>
69
+ <TestComponent />
70
+ <TestComponent />
71
+ <TestComponent />
72
+ </AtlantisThemeContextProvider>,
73
+ );
74
+
75
+ expect(styleFactory).toHaveBeenCalledTimes(1);
76
+ expect(renderedStyles[1]).toBe(renderedStyles[0]);
77
+ expect(renderedStyles[2]).toBe(renderedStyles[0]);
78
+ });
79
+
80
+ it("creates styles again when the theme tokens change", () => {
81
+ const styleFactory = jest.fn(tokens => ({
82
+ container: {
83
+ backgroundColor: tokens["color-surface"],
84
+ },
85
+ }));
86
+ const useTestStyles = buildThemedStyles(styleFactory);
87
+ const renderedStyles: Array<ReturnType<typeof useTestStyles>> = [];
88
+
89
+ function TestComponent() {
90
+ const styles = useTestStyles();
91
+ renderedStyles.push(styles);
92
+
93
+ return <Text style={styles.container}>Test</Text>;
94
+ }
95
+
96
+ const { rerender } = render(
97
+ <AtlantisThemeContextProvider dangerouslyOverrideTheme="light">
98
+ <TestComponent />
99
+ </AtlantisThemeContextProvider>,
100
+ );
101
+
102
+ rerender(
103
+ <AtlantisThemeContextProvider dangerouslyOverrideTheme="dark">
104
+ <TestComponent />
105
+ </AtlantisThemeContextProvider>,
106
+ );
107
+
108
+ expect(styleFactory).toHaveBeenCalledTimes(2);
109
+ expect(renderedStyles[1]).not.toBe(renderedStyles[0]);
110
+ });
111
+
50
112
  it("handles empty style objects", () => {
51
113
  const useTestStyles = buildThemedStyles(() => ({}));
52
114
 
@@ -1,5 +1,4 @@
1
1
  import { StyleSheet } from "react-native";
2
- import { useMemo } from "react";
3
2
  import type { AtlantisThemeContextValue } from "./types";
4
3
  import { useAtlantisTheme } from "./AtlantisThemeContext";
5
4
 
@@ -35,9 +34,19 @@ import { useAtlantisTheme } from "./AtlantisThemeContext";
35
34
  export function buildThemedStyles<
36
35
  T extends Parameters<typeof StyleSheet.create>[0],
37
36
  >(styleFactory: (tokens: AtlantisThemeContextValue["tokens"]) => T) {
37
+ const stylesByTokens = new WeakMap<AtlantisThemeContextValue["tokens"], T>();
38
+
38
39
  return function useStyles() {
39
40
  const { tokens } = useAtlantisTheme();
41
+ const cached = stylesByTokens.get(tokens);
42
+
43
+ if (cached !== undefined) {
44
+ return cached;
45
+ }
46
+
47
+ const themedStyles = StyleSheet.create(styleFactory(tokens));
48
+ stylesByTokens.set(tokens, themedStyles);
40
49
 
41
- return useMemo(() => StyleSheet.create(styleFactory(tokens)), [tokens]);
50
+ return themedStyles;
42
51
  };
43
52
  }