@dryanovski/react-native-components 1.0.0 → 1.0.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 (41) hide show
  1. package/lib/module/components/Card/Card.js +29 -0
  2. package/lib/module/components/Card/Card.js.map +1 -0
  3. package/lib/module/components/Card/styles.js +30 -0
  4. package/lib/module/components/Card/styles.js.map +1 -0
  5. package/lib/module/components/Card/types.js +4 -0
  6. package/lib/module/components/Card/types.js.map +1 -0
  7. package/lib/module/index.js +3 -0
  8. package/lib/module/index.js.map +1 -1
  9. package/lib/module/providers/README.md +22 -0
  10. package/lib/module/providers/ThemeProvider.js +59 -4
  11. package/lib/module/providers/ThemeProvider.js.map +1 -1
  12. package/lib/module/providers/ThemeProvider.test.js +384 -0
  13. package/lib/module/providers/ThemeProvider.test.js.map +1 -0
  14. package/lib/module/utils/Style.js +6 -0
  15. package/lib/module/utils/Style.js.map +1 -0
  16. package/lib/typescript/src/components/Card/Card.d.ts +14 -0
  17. package/lib/typescript/src/components/Card/Card.d.ts.map +1 -0
  18. package/lib/typescript/src/components/Card/styles.d.ts +17 -0
  19. package/lib/typescript/src/components/Card/styles.d.ts.map +1 -0
  20. package/lib/typescript/src/components/Card/types.d.ts +9 -0
  21. package/lib/typescript/src/components/Card/types.d.ts.map +1 -0
  22. package/lib/typescript/src/index.d.ts +1 -0
  23. package/lib/typescript/src/index.d.ts.map +1 -1
  24. package/lib/typescript/src/providers/ThemeProvider.d.ts +23 -1
  25. package/lib/typescript/src/providers/ThemeProvider.d.ts.map +1 -1
  26. package/lib/typescript/src/providers/ThemeProvider.test.d.ts +2 -0
  27. package/lib/typescript/src/providers/ThemeProvider.test.d.ts.map +1 -0
  28. package/lib/typescript/src/providers/types.d.ts +17 -1
  29. package/lib/typescript/src/providers/types.d.ts.map +1 -1
  30. package/lib/typescript/src/utils/Style.d.ts +4 -0
  31. package/lib/typescript/src/utils/Style.d.ts.map +1 -0
  32. package/package.json +13 -9
  33. package/src/components/Card/Card.tsx +25 -0
  34. package/src/components/Card/styles.ts +27 -0
  35. package/src/components/Card/types.ts +9 -0
  36. package/src/index.tsx +3 -0
  37. package/src/providers/README.md +22 -0
  38. package/src/providers/ThemeProvider.test.tsx +340 -0
  39. package/src/providers/ThemeProvider.tsx +73 -4
  40. package/src/providers/types.ts +23 -1
  41. package/src/utils/Style.ts +5 -0
@@ -1,2 +1,3 @@
1
+ export * from './components/Card/Card';
1
2
  export * from './providers/ThemeProvider';
2
3
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.tsx"],"names":[],"mappings":"AACA,cAAc,2BAA2B,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.tsx"],"names":[],"mappings":"AACA,cAAc,wBAAwB,CAAC;AAGvC,cAAc,2BAA2B,CAAC"}
@@ -1,5 +1,27 @@
1
1
  import React from 'react';
2
- import type { ThemeProviderProps } from './types';
2
+ import type { ThemeContextType, ThemeProviderProps } from './types';
3
+ /**
4
+ * Give access to theme context
5
+ * @returns {ThemeContextType} The current theme context
6
+ * @throws Will throw an error if used outside of ThemeProvider
7
+ * @example
8
+ * const { theme, changeTheme } = useTheme();
9
+ * console.log(theme); // 'light' | 'dark' | 'system'
10
+ */
11
+ export declare const useTheme: () => ThemeContextType;
12
+ /**
13
+ * ThemeProvider component to manage and provide theme context
14
+ * @param {React.ReactNode} children - The child components
15
+ * @param {ThemeVariants} theme - The current theme ('light', 'dark', 'system')
16
+ * @param {ThemeVariants} defaultTheme - The default theme if none is provided
17
+ * @param {(theme: ThemeVariants) => void} onThemeChange - Callback when theme changes
18
+ * @returns {JSX.Element} The ThemeProvider component
19
+ *
20
+ * @example
21
+ * <ThemeProvider theme="dark" onThemeChange={(theme) => console.log(theme)}>
22
+ * <App />
23
+ * </ThemeProvider>
24
+ */
3
25
  export declare const ThemeProvider: React.FC<ThemeProviderProps>;
4
26
  export default ThemeProvider;
5
27
  //# sourceMappingURL=ThemeProvider.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"ThemeProvider.d.ts","sourceRoot":"","sources":["../../../../src/providers/ThemeProvider.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAC;AAElD,eAAO,MAAM,aAAa,EAAE,KAAK,CAAC,EAAE,CAAC,kBAAkB,CAEtD,CAAC;AAEF,eAAe,aAAa,CAAC"}
1
+ {"version":3,"file":"ThemeProvider.d.ts","sourceRoot":"","sources":["../../../../src/providers/ThemeProvider.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAuD,MAAM,OAAO,CAAC;AAE5E,OAAO,KAAK,EACV,gBAAgB,EAChB,kBAAkB,EAEnB,MAAM,SAAS,CAAC;AAQjB;;;;;;;GAOG;AACH,eAAO,MAAM,QAAQ,wBAMpB,CAAC;AAEF;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,aAAa,EAAE,KAAK,CAAC,EAAE,CAAC,kBAAkB,CA+BtD,CAAC;AAEF,eAAe,aAAa,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=ThemeProvider.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ThemeProvider.test.d.ts","sourceRoot":"","sources":["../../../../src/providers/ThemeProvider.test.tsx"],"names":[],"mappings":""}
@@ -1,5 +1,21 @@
1
+ /**
2
+ * Theme variants type definition
3
+ */
4
+ export type ThemeVariants = 'light' | 'dark' | 'system';
5
+ /**
6
+ * ThemeProvider component props definition
7
+ */
1
8
  export type ThemeProviderProps = {
2
9
  children: React.ReactNode;
3
- configuration?: {};
10
+ theme?: ThemeVariants;
11
+ defaultTheme?: ThemeVariants;
12
+ onThemeChange?: (theme: ThemeVariants) => void;
13
+ };
14
+ /**
15
+ * Theme context type definition
16
+ */
17
+ export type ThemeContextType = {
18
+ theme: ThemeVariants;
19
+ changeTheme: (theme: ThemeVariants) => void;
4
20
  };
5
21
  //# sourceMappingURL=types.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../../src/providers/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,kBAAkB,GAAG;IAC/B,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;IAC1B,aAAa,CAAC,EAAE,EAAE,CAAC;CACpB,CAAC"}
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../../src/providers/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,MAAM,aAAa,GAAG,OAAO,GAAG,MAAM,GAAG,QAAQ,CAAC;AAExD;;GAEG;AACH,MAAM,MAAM,kBAAkB,GAAG;IAC/B,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;IAC1B,KAAK,CAAC,EAAE,aAAa,CAAC;IACtB,YAAY,CAAC,EAAE,aAAa,CAAC;IAG7B,aAAa,CAAC,EAAE,CAAC,KAAK,EAAE,aAAa,KAAK,IAAI,CAAC;CAChD,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,gBAAgB,GAAG;IAC7B,KAAK,EAAE,aAAa,CAAC;IAGrB,WAAW,EAAE,CAAC,KAAK,EAAE,aAAa,KAAK,IAAI,CAAC;CAC7C,CAAC"}
@@ -0,0 +1,4 @@
1
+ import { StyleSheet } from 'react-native';
2
+ export declare const Style: typeof StyleSheet;
3
+ export default Style;
4
+ //# sourceMappingURL=Style.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Style.d.ts","sourceRoot":"","sources":["../../../../src/utils/Style.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAE1C,eAAO,MAAM,KAAK,mBAAa,CAAC;AAEhC,eAAe,KAAK,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dryanovski/react-native-components",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "Collection of reusable React Native components for building mobile applications.",
5
5
  "main": "./lib/module/index.js",
6
6
  "types": "./lib/typescript/src/index.d.ts",
@@ -36,6 +36,7 @@
36
36
  "@react-native-community/cli": "latest",
37
37
  "@react-native/babel-preset": "0.78.2",
38
38
  "@react-native/eslint-config": "^0.78.0",
39
+ "@testing-library/react-native": "^13.3.3",
39
40
  "@types/jest": "^29.5.5",
40
41
  "@types/react": "^19.0.12",
41
42
  "babel-plugin-module-resolver": "^5.0.2",
@@ -56,6 +57,7 @@
56
57
  "react-native-screens": "~4.16.0",
57
58
  "react-native-svg": "15.12.1",
58
59
  "react-native-worklets": "0.5.1",
60
+ "react-test-renderer": "19.1.0",
59
61
  "typescript": "^5.8.3"
60
62
  },
61
63
  "peerDependencies": {
@@ -67,14 +69,6 @@
67
69
  "react-native-svg": "^15.12.1",
68
70
  "react-native-worklets": "0.5.1"
69
71
  },
70
- "jest": {
71
- "preset": "react-native",
72
- "passWithNoTests": true,
73
- "modulePathIgnorePatterns": [
74
- "<rootDir>/example/node_modules",
75
- "<rootDir>/lib/"
76
- ]
77
- },
78
72
  "prettier": {
79
73
  "quoteProps": "consistent",
80
74
  "singleQuote": true,
@@ -108,5 +102,15 @@
108
102
  "languages": "js",
109
103
  "type": "library",
110
104
  "version": "0.51.1"
105
+ },
106
+ "dependencies": {
107
+ "@react-native/eslint-plugin": "^0.82.1",
108
+ "@typescript-eslint/eslint-plugin": "^8.48.1",
109
+ "eslint-plugin-eslint-comments": "^3.2.0",
110
+ "eslint-plugin-ft-flow": "^3.0.11",
111
+ "eslint-plugin-jest": "^29.2.1",
112
+ "eslint-plugin-react": "^7.37.5",
113
+ "eslint-plugin-react-hooks": "^7.0.1",
114
+ "eslint-plugin-react-native": "^5.0.0"
111
115
  }
112
116
  }
@@ -0,0 +1,25 @@
1
+ import { View } from 'react-native';
2
+ import Styles from './styles';
3
+ import type { CardProps } from './types';
4
+
5
+ /**
6
+ * Card component to display content within a styled container
7
+ * @param {React.ReactNode} children - The content to be displayed inside the card
8
+ * @param {ViewStyle} style - Additional styles for the card container
9
+ * @returns {JSX.Element} The Card component
10
+ * @example
11
+ * <Card style={{ margin: 10 }}>
12
+ * <Text>This is a card</Text>
13
+ * </Card>
14
+ */
15
+ export const Card: React.FC<CardProps> = ({ children, style, ...props }) => {
16
+ return (
17
+ <View style={[Styles.container, style]} {...props}>
18
+ {children}
19
+ </View>
20
+ );
21
+ };
22
+
23
+ Card.displayName = 'Card';
24
+
25
+ export default Card;
@@ -0,0 +1,27 @@
1
+ import Style from '../../utils/Style';
2
+
3
+ const DEFAULT_CARD = {
4
+ card_background_color: '#fff',
5
+ card_border_radius: 8,
6
+ card_padding: 16,
7
+ card_shadow_color: '#000',
8
+ card_shadow_offset: { width: 0, height: 2 },
9
+ card_shadow_opacity: 0.1,
10
+ card_shadow_radius: 4,
11
+ card_elevation: 3,
12
+ };
13
+
14
+ export const Styles = Style.create({
15
+ container: {
16
+ backgroundColor: DEFAULT_CARD.card_background_color,
17
+ borderRadius: DEFAULT_CARD.card_border_radius,
18
+ padding: DEFAULT_CARD.card_padding,
19
+ shadowColor: DEFAULT_CARD.card_shadow_color,
20
+ shadowOffset: DEFAULT_CARD.card_shadow_offset,
21
+ shadowOpacity: DEFAULT_CARD.card_shadow_opacity,
22
+ shadowRadius: DEFAULT_CARD.card_shadow_radius,
23
+ elevation: DEFAULT_CARD.card_elevation,
24
+ },
25
+ });
26
+
27
+ export default Styles;
@@ -0,0 +1,9 @@
1
+ import type { View, ViewStyle } from 'react-native';
2
+
3
+ /**
4
+ * Props for the Card component
5
+ */
6
+ export type CardProps = {
7
+ children?: React.ReactNode;
8
+ style?: ViewStyle;
9
+ } & View;
package/src/index.tsx CHANGED
@@ -1,2 +1,5 @@
1
+ // Export Components
2
+ export * from './components/Card/Card';
3
+
1
4
  // Provider
2
5
  export * from './providers/ThemeProvider';
@@ -0,0 +1,22 @@
1
+ # ThemeProvider
2
+
3
+ Design to manage and provide theming capabilities to your application.
4
+
5
+ ## Usage
6
+
7
+ ```tsx
8
+ import { ThemeProvider } from '@dryanovski/react-native-components';
9
+
10
+ const App = () => {
11
+ return (
12
+ <ThemeProvider theme="dark">
13
+ <YourAppComponents />
14
+ </ThemeProvider>
15
+ );
16
+ };
17
+ ```
18
+
19
+ ## Props
20
+
21
+ - `theme`: A string representing the theme to be applied (e.g., "light", "dark").
22
+ - `children`: The child components that will have access to the theme context.
@@ -0,0 +1,340 @@
1
+ import { act, renderHook, waitFor } from '@testing-library/react-native';
2
+ import React from 'react';
3
+ import { useColorScheme as useDeviceColorScheme } from 'react-native';
4
+ import { ThemeProvider, useTheme } from './ThemeProvider';
5
+
6
+ // Mock react-native's useColorScheme hook
7
+ jest.mock('react-native', () => ({
8
+ useColorScheme: jest.fn(),
9
+ }));
10
+
11
+ const mockedUseDeviceColorScheme = useDeviceColorScheme as jest.MockedFunction<
12
+ typeof useDeviceColorScheme
13
+ >;
14
+
15
+ describe('ThemeProvider', () => {
16
+ beforeEach(() => {
17
+ jest.clearAllMocks();
18
+ mockedUseDeviceColorScheme.mockReturnValue('light');
19
+ });
20
+
21
+ describe('useTheme hook', () => {
22
+ it('should throw error when used outside ThemeProvider', () => {
23
+ // Suppress console.error for this test
24
+ const originalError = console.error;
25
+ console.error = jest.fn();
26
+
27
+ expect(() => {
28
+ renderHook(() => useTheme());
29
+ }).toThrow('useTheme must be used within a ThemeProvider');
30
+
31
+ console.error = originalError;
32
+ });
33
+
34
+ it('should return theme context when used inside ThemeProvider', () => {
35
+ const wrapper = ({ children }: { children: React.ReactNode }) => (
36
+ <ThemeProvider>{children}</ThemeProvider>
37
+ );
38
+
39
+ const { result } = renderHook(() => useTheme(), { wrapper });
40
+
41
+ expect(result.current).toHaveProperty('theme');
42
+ expect(result.current).toHaveProperty('changeTheme');
43
+ });
44
+ });
45
+
46
+ describe('ThemeProvider component', () => {
47
+ it('should use defaultTheme when no theme prop is provided and device is in light mode', () => {
48
+ mockedUseDeviceColorScheme.mockReturnValue('light');
49
+
50
+ const wrapper = ({ children }: { children: React.ReactNode }) => (
51
+ <ThemeProvider defaultTheme="light">{children}</ThemeProvider>
52
+ );
53
+
54
+ const { result } = renderHook(() => useTheme(), { wrapper });
55
+
56
+ expect(result.current.theme).toBe('light');
57
+ });
58
+
59
+ it('should use defaultTheme even when device is in dark mode', () => {
60
+ mockedUseDeviceColorScheme.mockReturnValue('dark');
61
+
62
+ const wrapper = ({ children }: { children: React.ReactNode }) => (
63
+ <ThemeProvider>{children}</ThemeProvider>
64
+ );
65
+
66
+ const { result } = renderHook(() => useTheme(), { wrapper });
67
+
68
+ // defaultTheme is 'light' by default, so it should use 'light' even though device is 'dark'
69
+ expect(result.current.theme).toBe('light');
70
+ });
71
+
72
+ it('should use provided theme prop over device color scheme', () => {
73
+ mockedUseDeviceColorScheme.mockReturnValue('dark');
74
+
75
+ const wrapper = ({ children }: { children: React.ReactNode }) => (
76
+ <ThemeProvider theme="light">{children}</ThemeProvider>
77
+ );
78
+
79
+ const { result } = renderHook(() => useTheme(), { wrapper });
80
+
81
+ expect(result.current.theme).toBe('light');
82
+ });
83
+
84
+ it('should use custom defaultTheme when provided', () => {
85
+ mockedUseDeviceColorScheme.mockReturnValue('light');
86
+
87
+ const wrapper = ({ children }: { children: React.ReactNode }) => (
88
+ <ThemeProvider defaultTheme="dark">{children}</ThemeProvider>
89
+ );
90
+
91
+ const { result } = renderHook(() => useTheme(), { wrapper });
92
+
93
+ expect(result.current.theme).toBe('dark');
94
+ });
95
+
96
+ it('should call onThemeChange when theme prop is provided', async () => {
97
+ const onThemeChange = jest.fn();
98
+
99
+ const wrapper = ({ children }: { children: React.ReactNode }) => (
100
+ <ThemeProvider theme="light" onThemeChange={onThemeChange}>
101
+ {children}
102
+ </ThemeProvider>
103
+ );
104
+
105
+ renderHook(() => useTheme(), { wrapper });
106
+
107
+ await waitFor(() => {
108
+ expect(onThemeChange).toHaveBeenCalledWith('light');
109
+ });
110
+ });
111
+
112
+ it('should not call onThemeChange when no theme prop is provided', () => {
113
+ const onThemeChange = jest.fn();
114
+
115
+ const wrapper = ({ children }: { children: React.ReactNode }) => (
116
+ <ThemeProvider onThemeChange={onThemeChange}>{children}</ThemeProvider>
117
+ );
118
+
119
+ renderHook(() => useTheme(), { wrapper });
120
+
121
+ // onThemeChange should not be called when theme prop is not provided
122
+ expect(onThemeChange).not.toHaveBeenCalled();
123
+ });
124
+ });
125
+
126
+ describe('changeTheme function', () => {
127
+ it('should change theme to dark', () => {
128
+ mockedUseDeviceColorScheme.mockReturnValue('light');
129
+
130
+ const wrapper = ({ children }: { children: React.ReactNode }) => (
131
+ <ThemeProvider defaultTheme="light">{children}</ThemeProvider>
132
+ );
133
+
134
+ const { result } = renderHook(() => useTheme(), { wrapper });
135
+
136
+ expect(result.current.theme).toBe('light');
137
+
138
+ act(() => {
139
+ result.current.changeTheme('dark');
140
+ });
141
+
142
+ expect(result.current.theme).toBe('dark');
143
+ });
144
+
145
+ it('should change theme to light', () => {
146
+ mockedUseDeviceColorScheme.mockReturnValue('dark');
147
+
148
+ const wrapper = ({ children }: { children: React.ReactNode }) => (
149
+ <ThemeProvider defaultTheme="dark">{children}</ThemeProvider>
150
+ );
151
+
152
+ const { result } = renderHook(() => useTheme(), { wrapper });
153
+
154
+ expect(result.current.theme).toBe('dark');
155
+
156
+ act(() => {
157
+ result.current.changeTheme('light');
158
+ });
159
+
160
+ expect(result.current.theme).toBe('light');
161
+ });
162
+
163
+ it('should use device color scheme when theme is set to "system"', () => {
164
+ mockedUseDeviceColorScheme.mockReturnValue('dark');
165
+
166
+ const wrapper = ({ children }: { children: React.ReactNode }) => (
167
+ <ThemeProvider defaultTheme="light">{children}</ThemeProvider>
168
+ );
169
+
170
+ const { result } = renderHook(() => useTheme(), { wrapper });
171
+
172
+ expect(result.current.theme).toBe('light');
173
+
174
+ act(() => {
175
+ result.current.changeTheme('system');
176
+ });
177
+
178
+ expect(result.current.theme).toBe('dark');
179
+ });
180
+
181
+ it('should switch to light theme when system theme is set and device is in light mode', () => {
182
+ mockedUseDeviceColorScheme.mockReturnValue('light');
183
+
184
+ const wrapper = ({ children }: { children: React.ReactNode }) => (
185
+ <ThemeProvider defaultTheme="dark">{children}</ThemeProvider>
186
+ );
187
+
188
+ const { result } = renderHook(() => useTheme(), { wrapper });
189
+
190
+ expect(result.current.theme).toBe('dark');
191
+
192
+ act(() => {
193
+ result.current.changeTheme('system');
194
+ });
195
+
196
+ expect(result.current.theme).toBe('light');
197
+ });
198
+
199
+ it('should handle multiple theme changes', () => {
200
+ mockedUseDeviceColorScheme.mockReturnValue('light');
201
+
202
+ const wrapper = ({ children }: { children: React.ReactNode }) => (
203
+ <ThemeProvider defaultTheme="light">{children}</ThemeProvider>
204
+ );
205
+
206
+ const { result } = renderHook(() => useTheme(), { wrapper });
207
+
208
+ expect(result.current.theme).toBe('light');
209
+
210
+ act(() => {
211
+ result.current.changeTheme('dark');
212
+ });
213
+ expect(result.current.theme).toBe('dark');
214
+
215
+ act(() => {
216
+ result.current.changeTheme('light');
217
+ });
218
+ expect(result.current.theme).toBe('light');
219
+
220
+ act(() => {
221
+ result.current.changeTheme('system');
222
+ });
223
+ expect(result.current.theme).toBe('light');
224
+ });
225
+
226
+ it('should respect device color scheme when changing to system theme', () => {
227
+ // Mock device as dark initially
228
+ mockedUseDeviceColorScheme.mockReturnValue('dark');
229
+
230
+ const wrapper = ({ children }: { children: React.ReactNode }) => (
231
+ <ThemeProvider defaultTheme="light">{children}</ThemeProvider>
232
+ );
233
+
234
+ const { result } = renderHook(() => useTheme(), { wrapper });
235
+
236
+ // Initially should be light (from defaultTheme)
237
+ expect(result.current.theme).toBe('light');
238
+
239
+ // Change to system should use device color scheme (dark)
240
+ act(() => {
241
+ result.current.changeTheme('system');
242
+ });
243
+
244
+ expect(result.current.theme).toBe('dark');
245
+ });
246
+
247
+ it('should update system theme when device color scheme is light', () => {
248
+ // Mock device as light
249
+ mockedUseDeviceColorScheme.mockReturnValue('light');
250
+
251
+ const wrapper = ({ children }: { children: React.ReactNode }) => (
252
+ <ThemeProvider defaultTheme="dark">{children}</ThemeProvider>
253
+ );
254
+
255
+ const { result } = renderHook(() => useTheme(), { wrapper });
256
+
257
+ // Initially should be dark (from defaultTheme)
258
+ expect(result.current.theme).toBe('dark');
259
+
260
+ // Change to system should use device color scheme (light)
261
+ act(() => {
262
+ result.current.changeTheme('system');
263
+ });
264
+
265
+ expect(result.current.theme).toBe('light');
266
+ });
267
+ });
268
+
269
+ describe('edge cases', () => {
270
+ it('should handle undefined device color scheme', () => {
271
+ mockedUseDeviceColorScheme.mockReturnValue(undefined);
272
+
273
+ const wrapper = ({ children }: { children: React.ReactNode }) => (
274
+ <ThemeProvider defaultTheme="light">{children}</ThemeProvider>
275
+ );
276
+
277
+ const { result } = renderHook(() => useTheme(), { wrapper });
278
+
279
+ expect(result.current.theme).toBe('light');
280
+ });
281
+
282
+ it('should handle null device color scheme', () => {
283
+ mockedUseDeviceColorScheme.mockReturnValue(null);
284
+
285
+ const wrapper = ({ children }: { children: React.ReactNode }) => (
286
+ <ThemeProvider defaultTheme="dark">{children}</ThemeProvider>
287
+ );
288
+
289
+ const { result } = renderHook(() => useTheme(), { wrapper });
290
+
291
+ expect(result.current.theme).toBe('dark');
292
+ });
293
+
294
+ it('should maintain theme stability with same changeTheme callback reference', () => {
295
+ const wrapper = ({ children }: { children: React.ReactNode }) => (
296
+ <ThemeProvider defaultTheme="light">{children}</ThemeProvider>
297
+ );
298
+
299
+ const { result, rerender } = renderHook(() => useTheme(), { wrapper });
300
+
301
+ const initialChangeTheme = result.current.changeTheme;
302
+
303
+ rerender({});
304
+
305
+ // changeTheme should maintain reference stability due to useCallback
306
+ expect(result.current.changeTheme).toBe(initialChangeTheme);
307
+ });
308
+ });
309
+
310
+ describe('integration tests', () => {
311
+ it('should work with nested providers', () => {
312
+ const wrapper = ({ children }: { children: React.ReactNode }) => (
313
+ <ThemeProvider defaultTheme="light">
314
+ <ThemeProvider defaultTheme="dark">{children}</ThemeProvider>
315
+ </ThemeProvider>
316
+ );
317
+
318
+ const { result } = renderHook(() => useTheme(), { wrapper });
319
+
320
+ // Should use the innermost provider
321
+ expect(result.current.theme).toBe('dark');
322
+ });
323
+
324
+ it('should maintain theme state across re-renders', () => {
325
+ const wrapper = ({ children }: { children: React.ReactNode }) => (
326
+ <ThemeProvider defaultTheme="light">{children}</ThemeProvider>
327
+ );
328
+
329
+ const { result, rerender } = renderHook(() => useTheme(), { wrapper });
330
+
331
+ act(() => {
332
+ result.current.changeTheme('dark');
333
+ });
334
+ expect(result.current.theme).toBe('dark');
335
+
336
+ rerender({});
337
+ expect(result.current.theme).toBe('dark');
338
+ });
339
+ });
340
+ });
@@ -1,8 +1,77 @@
1
- import React from 'react';
2
- import type { ThemeProviderProps } from './types';
1
+ import React, { useCallback, useContext, useEffect, useState } from 'react';
2
+ import { useColorScheme as useDeviceColorScheme } from 'react-native';
3
+ import type {
4
+ ThemeContextType,
5
+ ThemeProviderProps,
6
+ ThemeVariants,
7
+ } from './types';
3
8
 
4
- export const ThemeProvider: React.FC<ThemeProviderProps> = ({ children }) => {
5
- return <>{children}</>;
9
+ /**
10
+ * Create a ThemeContext with default value null
11
+ * @type {React.Context<ThemeContextType | null>}
12
+ */
13
+ const ThemeContext = React.createContext<ThemeContextType | null>(null);
14
+
15
+ /**
16
+ * Give access to theme context
17
+ * @returns {ThemeContextType} The current theme context
18
+ * @throws Will throw an error if used outside of ThemeProvider
19
+ * @example
20
+ * const { theme, changeTheme } = useTheme();
21
+ * console.log(theme); // 'light' | 'dark' | 'system'
22
+ */
23
+ export const useTheme = () => {
24
+ const context = useContext(ThemeContext);
25
+ if (!context) {
26
+ throw new Error('useTheme must be used within a ThemeProvider');
27
+ }
28
+ return context;
29
+ };
30
+
31
+ /**
32
+ * ThemeProvider component to manage and provide theme context
33
+ * @param {React.ReactNode} children - The child components
34
+ * @param {ThemeVariants} theme - The current theme ('light', 'dark', 'system')
35
+ * @param {ThemeVariants} defaultTheme - The default theme if none is provided
36
+ * @param {(theme: ThemeVariants) => void} onThemeChange - Callback when theme changes
37
+ * @returns {JSX.Element} The ThemeProvider component
38
+ *
39
+ * @example
40
+ * <ThemeProvider theme="dark" onThemeChange={(theme) => console.log(theme)}>
41
+ * <App />
42
+ * </ThemeProvider>
43
+ */
44
+ export const ThemeProvider: React.FC<ThemeProviderProps> = ({
45
+ children,
46
+ theme,
47
+ defaultTheme = 'light',
48
+ onThemeChange = () => undefined,
49
+ }) => {
50
+ const deviceColorscheme = useDeviceColorScheme();
51
+ const [currentTheme, setTheme] = useState<ThemeVariants>(
52
+ theme || defaultTheme
53
+ );
54
+
55
+ useEffect(() => {
56
+ onThemeChange && theme && onThemeChange(theme);
57
+ }, [theme, onThemeChange]);
58
+
59
+ const changeTheme = useCallback(
60
+ (newTheme: ThemeVariants) => {
61
+ if (newTheme === 'system') {
62
+ setTheme(deviceColorscheme === 'dark' ? 'dark' : 'light');
63
+ return;
64
+ }
65
+ setTheme(newTheme);
66
+ },
67
+ [deviceColorscheme]
68
+ );
69
+
70
+ return (
71
+ <ThemeContext.Provider value={{ theme: currentTheme, changeTheme }}>
72
+ {children}
73
+ </ThemeContext.Provider>
74
+ );
6
75
  };
7
76
 
8
77
  export default ThemeProvider;