@react-native-styled-system/core 1.4.4 → 2.1.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 (127) hide show
  1. package/lib/commonjs/@types/Responsive.js +8 -0
  2. package/lib/commonjs/@types/Responsive.js.map +1 -0
  3. package/lib/commonjs/@types/SxProps.js +9 -0
  4. package/lib/commonjs/@types/SxProps.js.map +1 -1
  5. package/lib/commonjs/@types/ThemedDict.js +396 -2
  6. package/lib/commonjs/@types/ThemedDict.js.map +1 -1
  7. package/lib/commonjs/__testUtils__/testTheme.js +51 -0
  8. package/lib/commonjs/__testUtils__/testTheme.js.map +1 -0
  9. package/lib/commonjs/hook/useSx.js +13 -4
  10. package/lib/commonjs/hook/useSx.js.map +1 -1
  11. package/lib/commonjs/hook/useSxStyle.js +10 -3
  12. package/lib/commonjs/hook/useSxStyle.js.map +1 -1
  13. package/lib/commonjs/index.js +22 -0
  14. package/lib/commonjs/index.js.map +1 -1
  15. package/lib/commonjs/internal/util/resolveResponsiveValue.js +52 -0
  16. package/lib/commonjs/internal/util/resolveResponsiveValue.js.map +1 -0
  17. package/lib/commonjs/provider/StyledSystemProvider.js +17 -11
  18. package/lib/commonjs/provider/StyledSystemProvider.js.map +1 -1
  19. package/lib/commonjs/util/createSxComponent.js +5 -7
  20. package/lib/commonjs/util/createSxComponent.js.map +1 -1
  21. package/lib/commonjs/util/createTheme.js +43 -0
  22. package/lib/commonjs/util/createTheme.js.map +1 -0
  23. package/lib/commonjs/util/propsToThemedStyle.js +11 -3
  24. package/lib/commonjs/util/propsToThemedStyle.js.map +1 -1
  25. package/lib/module/@types/AnyStyle.js +0 -2
  26. package/lib/module/@types/Responsive.js +2 -0
  27. package/lib/module/@types/Responsive.js.map +1 -0
  28. package/lib/module/@types/SxProps.js +9 -2
  29. package/lib/module/@types/SxProps.js.map +1 -1
  30. package/lib/module/@types/ThemedDict.js +394 -3
  31. package/lib/module/@types/ThemedDict.js.map +1 -1
  32. package/lib/module/@types/ThemedTypings.js +0 -2
  33. package/lib/module/@types/Token.js +0 -2
  34. package/lib/module/__testUtils__/testTheme.js +45 -0
  35. package/lib/module/__testUtils__/testTheme.js.map +1 -0
  36. package/lib/module/hook/useSx.js +13 -6
  37. package/lib/module/hook/useSx.js.map +1 -1
  38. package/lib/module/hook/useSxStyle.js +10 -5
  39. package/lib/module/hook/useSxStyle.js.map +1 -1
  40. package/lib/module/hook/useSxTokens.js +0 -2
  41. package/lib/module/hook/useSxTokens.js.map +1 -1
  42. package/lib/module/index.js +2 -2
  43. package/lib/module/index.js.map +1 -1
  44. package/lib/module/internal/TokenParser/ColorsParser.js +0 -2
  45. package/lib/module/internal/TokenParser/ColorsParser.js.map +1 -1
  46. package/lib/module/internal/TokenParser/RadiiParser.js +0 -2
  47. package/lib/module/internal/TokenParser/RadiiParser.js.map +1 -1
  48. package/lib/module/internal/TokenParser/SizesParser.js +0 -2
  49. package/lib/module/internal/TokenParser/SizesParser.js.map +1 -1
  50. package/lib/module/internal/TokenParser/SpaceParser.js +0 -2
  51. package/lib/module/internal/TokenParser/SpaceParser.js.map +1 -1
  52. package/lib/module/internal/TokenParser/TokenParser.js +0 -2
  53. package/lib/module/internal/TokenParser/TokenParser.js.map +1 -1
  54. package/lib/module/internal/TokenParser/TypographyParser.js +0 -2
  55. package/lib/module/internal/TokenParser/TypographyParser.js.map +1 -1
  56. package/lib/module/internal/useStableCallback.js +0 -2
  57. package/lib/module/internal/useStableCallback.js.map +1 -1
  58. package/lib/module/internal/util/StyleHash.js +0 -2
  59. package/lib/module/internal/util/StyleHash.js.map +1 -1
  60. package/lib/module/internal/util/fillStyleIfNotNullish.js +0 -2
  61. package/lib/module/internal/util/fillStyleIfNotNullish.js.map +1 -1
  62. package/lib/module/internal/util/mutateShortcutPropToOriginalKeys.js +0 -2
  63. package/lib/module/internal/util/mutateShortcutPropToOriginalKeys.js.map +1 -1
  64. package/lib/module/internal/util/parsePxSuffixNumber.js +0 -2
  65. package/lib/module/internal/util/parsePxSuffixNumber.js.map +1 -1
  66. package/lib/module/internal/util/printWarning.js +0 -2
  67. package/lib/module/internal/util/printWarning.js.map +1 -1
  68. package/lib/module/internal/util/resolveResponsiveValue.js +44 -0
  69. package/lib/module/internal/util/resolveResponsiveValue.js.map +1 -0
  70. package/lib/module/provider/StyledSystemProvider.js +17 -12
  71. package/lib/module/provider/StyledSystemProvider.js.map +1 -1
  72. package/lib/module/util/createSxComponent.js +5 -9
  73. package/lib/module/util/createSxComponent.js.map +1 -1
  74. package/lib/module/util/createTheme.js +37 -0
  75. package/lib/module/util/createTheme.js.map +1 -0
  76. package/lib/module/util/propsToThemedStyle.js +11 -5
  77. package/lib/module/util/propsToThemedStyle.js.map +1 -1
  78. package/lib/typescript/src/@types/Responsive.d.ts +4 -0
  79. package/lib/typescript/src/@types/Responsive.d.ts.map +1 -0
  80. package/lib/typescript/src/@types/SxProps.d.ts +15 -3
  81. package/lib/typescript/src/@types/SxProps.d.ts.map +1 -1
  82. package/lib/typescript/src/@types/ThemedDict.d.ts +26 -0
  83. package/lib/typescript/src/@types/ThemedDict.d.ts.map +1 -1
  84. package/lib/typescript/src/__testUtils__/testTheme.d.ts +12 -0
  85. package/lib/typescript/src/__testUtils__/testTheme.d.ts.map +1 -0
  86. package/lib/typescript/src/hook/useSx.d.ts +2 -1
  87. package/lib/typescript/src/hook/useSx.d.ts.map +1 -1
  88. package/lib/typescript/src/hook/useSxStyle.d.ts +2 -1
  89. package/lib/typescript/src/hook/useSxStyle.d.ts.map +1 -1
  90. package/lib/typescript/src/index.d.ts +2 -0
  91. package/lib/typescript/src/index.d.ts.map +1 -1
  92. package/lib/typescript/src/internal/util/resolveResponsiveValue.d.ts +12 -0
  93. package/lib/typescript/src/internal/util/resolveResponsiveValue.d.ts.map +1 -0
  94. package/lib/typescript/src/provider/StyledSystemProvider.d.ts +3 -1
  95. package/lib/typescript/src/provider/StyledSystemProvider.d.ts.map +1 -1
  96. package/lib/typescript/src/util/createSxComponent.d.ts +4 -4
  97. package/lib/typescript/src/util/createSxComponent.d.ts.map +1 -1
  98. package/lib/typescript/src/util/createTheme.d.ts +4 -0
  99. package/lib/typescript/src/util/createTheme.d.ts.map +1 -0
  100. package/lib/typescript/src/util/propsToThemedStyle.d.ts +3 -1
  101. package/lib/typescript/src/util/propsToThemedStyle.d.ts.map +1 -1
  102. package/package.json +9 -8
  103. package/src/@types/Responsive.ts +7 -0
  104. package/src/@types/SxProps.ts +36 -7
  105. package/src/@types/ThemedDict.ts +412 -0
  106. package/src/__testUtils__/testTheme.ts +43 -0
  107. package/src/hook/useSx.test.ts +117 -38
  108. package/src/hook/useSx.ts +11 -2
  109. package/src/hook/useSxStyle.test.ts +102 -33
  110. package/src/hook/useSxStyle.ts +12 -1
  111. package/src/hook/useSxTokens.test.ts +65 -32
  112. package/src/index.ts +2 -0
  113. package/src/internal/util/resolveResponsiveValue.test.ts +171 -0
  114. package/src/internal/util/resolveResponsiveValue.ts +58 -0
  115. package/src/provider/StyledSystemProvider.tsx +14 -4
  116. package/src/util/createSxComponent.tsx +8 -4
  117. package/src/util/createTheme.test.ts +126 -0
  118. package/src/util/createTheme.ts +29 -0
  119. package/src/util/propsToThemedStyle.ts +10 -3
  120. package/lib/commonjs/internal/util/fillNullishThemeKey.js +0 -18
  121. package/lib/commonjs/internal/util/fillNullishThemeKey.js.map +0 -1
  122. package/lib/commonjs/package.json +0 -1
  123. package/lib/module/internal/util/fillNullishThemeKey.js +0 -13
  124. package/lib/module/internal/util/fillNullishThemeKey.js.map +0 -1
  125. package/lib/typescript/src/internal/util/fillNullishThemeKey.d.ts +0 -3
  126. package/lib/typescript/src/internal/util/fillNullishThemeKey.d.ts.map +0 -1
  127. package/src/internal/util/fillNullishThemeKey.ts +0 -12
@@ -0,0 +1,126 @@
1
+ import type { ThemedDict } from '../@types/ThemedDict';
2
+ import { defaultTheme } from '../@types/ThemedDict';
3
+
4
+ import { createTheme } from './createTheme';
5
+
6
+ describe('createTheme', () => {
7
+ describe('create mode (single argument)', () => {
8
+ it('returns all empty defaults when called without arguments', () => {
9
+ const theme = createTheme();
10
+ expect(theme).toEqual({
11
+ colors: {},
12
+ space: {},
13
+ sizes: {},
14
+ radii: {},
15
+ typography: {},
16
+ breakpoints: [],
17
+ });
18
+ });
19
+
20
+ it('returns all empty defaults when called with empty object', () => {
21
+ const theme = createTheme({});
22
+ expect(theme).toEqual({
23
+ colors: {},
24
+ space: {},
25
+ sizes: {},
26
+ radii: {},
27
+ typography: {},
28
+ breakpoints: [],
29
+ });
30
+ });
31
+
32
+ it('keeps provided groups and fills missing ones with empty defaults', () => {
33
+ const theme = createTheme({
34
+ colors: { primary: '#000' },
35
+ space: { '1': 4 },
36
+ });
37
+ expect(theme.colors).toEqual({ primary: '#000' });
38
+ expect(theme.space).toEqual({ '1': 4 });
39
+ expect(theme.sizes).toEqual({});
40
+ expect(theme.radii).toEqual({});
41
+ expect(theme.typography).toEqual({});
42
+ expect(theme.breakpoints).toEqual([]);
43
+ });
44
+
45
+ it('keeps provided breakpoints', () => {
46
+ const theme = createTheme({ breakpoints: [480, 768] });
47
+ expect(theme.breakpoints).toEqual([480, 768]);
48
+ });
49
+ });
50
+
51
+ describe('extend mode (two arguments)', () => {
52
+ const base: ThemedDict = {
53
+ colors: { red: '#FF0000', blue: '#0000FF' },
54
+ space: { '1': 4, '2': 8 },
55
+ sizes: { sm: 16 },
56
+ radii: { md: 8 },
57
+ typography: { body: { fontSize: 16, lineHeight: 24 } },
58
+ breakpoints: [480, 768],
59
+ };
60
+
61
+ it('returns base as-is when overrides is empty', () => {
62
+ const theme = createTheme(base, {});
63
+ expect(theme).toEqual(base);
64
+ });
65
+
66
+ it('merges colors (keeps existing + adds new)', () => {
67
+ const theme = createTheme(base, { colors: { brand: '#FF6600' } });
68
+ expect(theme.colors).toEqual({ red: '#FF0000', blue: '#0000FF', brand: '#FF6600' });
69
+ });
70
+
71
+ it('overrides existing token values', () => {
72
+ const theme = createTheme(base, { colors: { red: '#CC0000' } });
73
+ expect(theme.colors.red).toBe('#CC0000');
74
+ expect(theme.colors.blue).toBe('#0000FF');
75
+ });
76
+
77
+ it('merges each token group independently', () => {
78
+ const theme = createTheme(base, {
79
+ space: { '3': 12 },
80
+ sizes: { lg: 32 },
81
+ radii: { lg: 16 },
82
+ typography: { h1: { fontSize: 32, fontWeight: 'bold', lineHeight: 40 } },
83
+ });
84
+ expect(theme.space).toEqual({ '1': 4, '2': 8, '3': 12 });
85
+ expect(theme.sizes).toEqual({ sm: 16, lg: 32 });
86
+ expect(theme.radii).toEqual({ md: 8, lg: 16 });
87
+ expect(theme.typography).toEqual({
88
+ body: { fontSize: 16, lineHeight: 24 },
89
+ h1: { fontSize: 32, fontWeight: 'bold', lineHeight: 40 },
90
+ });
91
+ });
92
+
93
+ it('replaces breakpoints when provided in overrides', () => {
94
+ const theme = createTheme(base, { breakpoints: [500, 1024, 1440] });
95
+ expect(theme.breakpoints).toEqual([500, 1024, 1440]);
96
+ });
97
+
98
+ it('falls back to base breakpoints when overrides omits breakpoints', () => {
99
+ const theme = createTheme(base, { colors: { brand: '#000' } });
100
+ expect(theme.breakpoints).toEqual([480, 768]);
101
+ });
102
+
103
+ it('does not mutate base or overrides', () => {
104
+ const baseCopy = JSON.parse(JSON.stringify(base));
105
+ const overrides = { colors: { brand: '#FF6600' } };
106
+ const overridesCopy = JSON.parse(JSON.stringify(overrides));
107
+
108
+ createTheme(base, overrides);
109
+
110
+ expect(base).toEqual(baseCopy);
111
+ expect(overrides).toEqual(overridesCopy);
112
+ });
113
+
114
+ it('works with defaultTheme as base', () => {
115
+ const theme = createTheme(defaultTheme, {
116
+ colors: { brand: '#FF6600' },
117
+ space: { huge: 200 },
118
+ });
119
+ expect(theme.colors.brand).toBe('#FF6600');
120
+ expect(theme.colors.white).toBe('#FFFFFF');
121
+ expect(theme.space.huge).toBe(200);
122
+ expect(theme.space['1']).toBe(4);
123
+ expect(theme.breakpoints).toEqual(defaultTheme.breakpoints);
124
+ });
125
+ });
126
+ });
@@ -0,0 +1,29 @@
1
+ import type { ThemedDict } from '../@types/ThemedDict';
2
+
3
+ export function createTheme(config?: Partial<ThemedDict>): ThemedDict;
4
+ export function createTheme(base: ThemedDict, overrides: Partial<ThemedDict>): ThemedDict;
5
+ export function createTheme(
6
+ baseOrConfig: Partial<ThemedDict> = {},
7
+ overrides?: Partial<ThemedDict>,
8
+ ): ThemedDict {
9
+ if (overrides !== undefined) {
10
+ return {
11
+ colors: { ...baseOrConfig.colors, ...overrides.colors },
12
+ space: { ...baseOrConfig.space, ...overrides.space },
13
+ sizes: { ...baseOrConfig.sizes, ...overrides.sizes },
14
+ radii: { ...baseOrConfig.radii, ...overrides.radii },
15
+ typography: { ...baseOrConfig.typography, ...overrides.typography },
16
+ breakpoints: overrides.breakpoints ?? baseOrConfig.breakpoints ?? [],
17
+ };
18
+ }
19
+
20
+ return {
21
+ colors: {},
22
+ space: {},
23
+ sizes: {},
24
+ radii: {},
25
+ typography: {},
26
+ breakpoints: [],
27
+ ...baseOrConfig,
28
+ };
29
+ }
@@ -1,5 +1,5 @@
1
1
  import type { AnyStyle } from '../@types/AnyStyle';
2
- import type { TextSxProps } from '../@types/SxProps';
2
+ import type { ResolvedTextSxProps, TextSxProps } from '../@types/SxProps';
3
3
  import type { ThemedDict } from '../@types/ThemedDict';
4
4
  import { createTokenParsers } from '../internal/TokenParser/TokenParser';
5
5
  import {
@@ -7,16 +7,21 @@ import {
7
7
  fillViewStyleIfNotNullish,
8
8
  } from '../internal/util/fillStyleIfNotNullish';
9
9
  import { printWarning } from '../internal/util/printWarning';
10
+ import { resolveResponsiveSx } from '../internal/util/resolveResponsiveValue';
10
11
 
11
12
  export type ThemedStyleType = 'ViewStyle' | 'TextStyle';
12
13
  export const propsToThemedStyle = ({
13
14
  theme,
14
- sx,
15
+ sx: rawSx,
15
16
  styleType = 'ViewStyle',
17
+ breakpoints = [],
18
+ screenWidth = 0,
16
19
  }: {
17
20
  theme?: ThemedDict;
18
21
  sx?: TextSxProps;
19
22
  styleType?: ThemedStyleType;
23
+ breakpoints?: number[];
24
+ screenWidth?: number;
20
25
  }): AnyStyle | undefined => {
21
26
  const ret: AnyStyle = {};
22
27
 
@@ -26,10 +31,12 @@ export const propsToThemedStyle = ({
26
31
  return;
27
32
  }
28
33
 
29
- if (!sx) {
34
+ if (!rawSx) {
30
35
  return;
31
36
  }
32
37
 
38
+ const sx: ResolvedTextSxProps = resolveResponsiveSx({ sx: rawSx, breakpoints, screenWidth });
39
+
33
40
  const { colors, radii, sizes, space, spaceAsNumberOnly, typography } = createTokenParsers(theme);
34
41
 
35
42
  // region colors
@@ -1,18 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.fillNullishThemeKey = void 0;
7
- const fillNullishThemeKey = theme => {
8
- return {
9
- colors: {},
10
- space: {},
11
- sizes: {},
12
- radii: {},
13
- typography: {},
14
- ...theme
15
- };
16
- };
17
- exports.fillNullishThemeKey = fillNullishThemeKey;
18
- //# sourceMappingURL=fillNullishThemeKey.js.map
@@ -1 +0,0 @@
1
- {"version":3,"names":["fillNullishThemeKey","theme","colors","space","sizes","radii","typography","exports"],"sourceRoot":"../../../../src","sources":["internal/util/fillNullishThemeKey.ts"],"mappings":";;;;;;AAEO,MAAMA,mBAAmB,GAAIC,KAA0B,IAAiB;EAC7E,OAAO;IACLC,MAAM,EAAE,CAAC,CAAC;IACVC,KAAK,EAAE,CAAC,CAAC;IACTC,KAAK,EAAE,CAAC,CAAC;IACTC,KAAK,EAAE,CAAC,CAAC;IACTC,UAAU,EAAE,CAAC,CAAC;IACd,GAAGL;EACL,CAAC;AACH,CAAC;AAACM,OAAA,CAAAP,mBAAA,GAAAA,mBAAA","ignoreList":[]}
@@ -1 +0,0 @@
1
- {"type":"commonjs"}
@@ -1,13 +0,0 @@
1
- "use strict";
2
-
3
- export const fillNullishThemeKey = theme => {
4
- return {
5
- colors: {},
6
- space: {},
7
- sizes: {},
8
- radii: {},
9
- typography: {},
10
- ...theme
11
- };
12
- };
13
- //# sourceMappingURL=fillNullishThemeKey.js.map
@@ -1 +0,0 @@
1
- {"version":3,"names":["fillNullishThemeKey","theme","colors","space","sizes","radii","typography"],"sourceRoot":"../../../../src","sources":["internal/util/fillNullishThemeKey.ts"],"mappings":";;AAEA,OAAO,MAAMA,mBAAmB,GAAIC,KAA0B,IAAiB;EAC7E,OAAO;IACLC,MAAM,EAAE,CAAC,CAAC;IACVC,KAAK,EAAE,CAAC,CAAC;IACTC,KAAK,EAAE,CAAC,CAAC;IACTC,KAAK,EAAE,CAAC,CAAC;IACTC,UAAU,EAAE,CAAC,CAAC;IACd,GAAGL;EACL,CAAC;AACH,CAAC","ignoreList":[]}
@@ -1,3 +0,0 @@
1
- import type { ThemedDict } from '../../@types/ThemedDict';
2
- export declare const fillNullishThemeKey: (theme: Partial<ThemedDict>) => ThemedDict;
3
- //# sourceMappingURL=fillNullishThemeKey.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"fillNullishThemeKey.d.ts","sourceRoot":"","sources":["../../../../../src/internal/util/fillNullishThemeKey.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AAE1D,eAAO,MAAM,mBAAmB,GAAI,OAAO,OAAO,CAAC,UAAU,CAAC,KAAG,UAShE,CAAC"}
@@ -1,12 +0,0 @@
1
- import type { ThemedDict } from '../../@types/ThemedDict';
2
-
3
- export const fillNullishThemeKey = (theme: Partial<ThemedDict>): ThemedDict => {
4
- return {
5
- colors: {},
6
- space: {},
7
- sizes: {},
8
- radii: {},
9
- typography: {},
10
- ...theme,
11
- };
12
- };