react-native-unistyles 1.1.1 → 2.0.0-alpha.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (157) hide show
  1. package/ios/UnistylesModule.h +10 -0
  2. package/ios/UnistylesModule.mm +108 -0
  3. package/ios/UnistylesRuntime.h +30 -0
  4. package/ios/UnistylesRuntime.mm +191 -0
  5. package/lib/commonjs/UnistyleRegistry.js +37 -0
  6. package/lib/commonjs/UnistyleRegistry.js.map +1 -0
  7. package/lib/commonjs/Unistyles.js +36 -0
  8. package/lib/commonjs/Unistyles.js.map +1 -0
  9. package/lib/commonjs/UnistylesEngine.js +22 -0
  10. package/lib/commonjs/UnistylesEngine.js.map +1 -0
  11. package/lib/commonjs/UnistylesModule.js +9 -0
  12. package/lib/commonjs/UnistylesModule.js.map +1 -0
  13. package/lib/commonjs/UnistylesRuntime.js +52 -0
  14. package/lib/commonjs/UnistylesRuntime.js.map +1 -0
  15. package/lib/commonjs/createStyleSheet.js +14 -0
  16. package/lib/commonjs/createStyleSheet.js.map +1 -0
  17. package/lib/commonjs/global.js +2 -0
  18. package/lib/commonjs/global.js.map +1 -0
  19. package/lib/commonjs/index.js +33 -6
  20. package/lib/commonjs/index.js.map +1 -1
  21. package/lib/commonjs/types/cxx.js +23 -0
  22. package/lib/commonjs/types/cxx.js.map +1 -0
  23. package/lib/commonjs/types/index.js +11 -0
  24. package/lib/commonjs/types/index.js.map +1 -1
  25. package/lib/commonjs/useInitialTheme.js +13 -0
  26. package/lib/commonjs/useInitialTheme.js.map +1 -0
  27. package/lib/commonjs/useStyles.js +48 -0
  28. package/lib/commonjs/useStyles.js.map +1 -0
  29. package/lib/commonjs/useUnistyles.js +59 -0
  30. package/lib/commonjs/useUnistyles.js.map +1 -0
  31. package/lib/commonjs/utils/breakpoints.js +4 -2
  32. package/lib/commonjs/utils/breakpoints.js.map +1 -1
  33. package/lib/commonjs/utils/styles.js +6 -6
  34. package/lib/commonjs/utils/styles.js.map +1 -1
  35. package/lib/module/UnistyleRegistry.js +30 -0
  36. package/lib/module/UnistyleRegistry.js.map +1 -0
  37. package/lib/module/Unistyles.js +30 -0
  38. package/lib/module/Unistyles.js.map +1 -0
  39. package/lib/module/UnistylesEngine.js +15 -0
  40. package/lib/module/UnistylesEngine.js.map +1 -0
  41. package/lib/module/UnistylesModule.js +3 -0
  42. package/lib/module/UnistylesModule.js.map +1 -0
  43. package/lib/module/UnistylesRuntime.js +45 -0
  44. package/lib/module/UnistylesRuntime.js.map +1 -0
  45. package/lib/module/createStyleSheet.js +7 -0
  46. package/lib/module/createStyleSheet.js.map +1 -0
  47. package/lib/module/global.js +2 -0
  48. package/lib/module/global.js.map +1 -0
  49. package/lib/module/index.js +18 -2
  50. package/lib/module/index.js.map +1 -1
  51. package/lib/module/types/cxx.js +17 -0
  52. package/lib/module/types/cxx.js.map +1 -0
  53. package/lib/module/types/index.js +1 -0
  54. package/lib/module/types/index.js.map +1 -1
  55. package/lib/module/useInitialTheme.js +6 -0
  56. package/lib/module/useInitialTheme.js.map +1 -0
  57. package/lib/module/useStyles.js +42 -0
  58. package/lib/module/useStyles.js.map +1 -0
  59. package/lib/module/useUnistyles.js +52 -0
  60. package/lib/module/useUnistyles.js.map +1 -0
  61. package/lib/module/utils/breakpoints.js +4 -3
  62. package/lib/module/utils/breakpoints.js.map +1 -1
  63. package/lib/module/utils/styles.js +6 -7
  64. package/lib/module/utils/styles.js.map +1 -1
  65. package/lib/typescript/examples/expo/src/App.d.ts.map +1 -1
  66. package/lib/typescript/examples/expo/src/examples/Cxx.d.ts +3 -0
  67. package/lib/typescript/examples/expo/src/examples/Cxx.d.ts.map +1 -0
  68. package/lib/typescript/examples/expo/src/examples/index.d.ts +1 -7
  69. package/lib/typescript/examples/expo/src/examples/index.d.ts.map +1 -1
  70. package/lib/typescript/examples/expo/src/styles/index.d.ts +15 -45
  71. package/lib/typescript/examples/expo/src/styles/index.d.ts.map +1 -1
  72. package/lib/typescript/examples/expo/src/styles/theme.d.ts +17 -1
  73. package/lib/typescript/examples/expo/src/styles/theme.d.ts.map +1 -1
  74. package/lib/typescript/src/UnistyleRegistry.d.ts +15 -0
  75. package/lib/typescript/src/UnistyleRegistry.d.ts.map +1 -0
  76. package/lib/typescript/src/Unistyles.d.ts +16 -0
  77. package/lib/typescript/src/Unistyles.d.ts.map +1 -0
  78. package/lib/typescript/src/UnistylesEngine.d.ts +8 -0
  79. package/lib/typescript/src/UnistylesEngine.d.ts.map +1 -0
  80. package/lib/typescript/src/UnistylesModule.d.ts +6 -0
  81. package/lib/typescript/src/UnistylesModule.d.ts.map +1 -0
  82. package/lib/typescript/src/UnistylesRuntime.d.ts +56 -0
  83. package/lib/typescript/src/UnistylesRuntime.d.ts.map +1 -0
  84. package/lib/typescript/src/createStyleSheet.d.ts +6 -0
  85. package/lib/typescript/src/createStyleSheet.d.ts.map +1 -0
  86. package/lib/typescript/src/global.d.ts +5 -0
  87. package/lib/typescript/src/global.d.ts.map +1 -0
  88. package/lib/typescript/src/index.d.ts +14 -2
  89. package/lib/typescript/src/index.d.ts.map +1 -1
  90. package/lib/typescript/src/types/breakpoints.d.ts +6 -7
  91. package/lib/typescript/src/types/breakpoints.d.ts.map +1 -1
  92. package/lib/typescript/src/types/core.d.ts +4 -4
  93. package/lib/typescript/src/types/core.d.ts.map +1 -1
  94. package/lib/typescript/src/types/cxx.d.ts +50 -0
  95. package/lib/typescript/src/types/cxx.d.ts.map +1 -0
  96. package/lib/typescript/src/types/index.d.ts +3 -1
  97. package/lib/typescript/src/types/index.d.ts.map +1 -1
  98. package/lib/typescript/src/useInitialTheme.d.ts +3 -0
  99. package/lib/typescript/src/useInitialTheme.d.ts.map +1 -0
  100. package/lib/typescript/src/useStyles.d.ts +46 -0
  101. package/lib/typescript/src/useStyles.d.ts.map +1 -0
  102. package/lib/typescript/src/useUnistyles.d.ts +45 -0
  103. package/lib/typescript/src/useUnistyles.d.ts.map +1 -0
  104. package/lib/typescript/src/utils/breakpoints.d.ts +5 -4
  105. package/lib/typescript/src/utils/breakpoints.d.ts.map +1 -1
  106. package/lib/typescript/src/utils/styles.d.ts +4 -3
  107. package/lib/typescript/src/utils/styles.d.ts.map +1 -1
  108. package/package.json +11 -8
  109. package/react-native-unistyles.podspec +21 -0
  110. package/src/UnistyleRegistry.ts +39 -0
  111. package/src/Unistyles.ts +41 -0
  112. package/src/UnistylesEngine.ts +15 -0
  113. package/src/UnistylesModule.ts +7 -0
  114. package/src/UnistylesRuntime.ts +58 -0
  115. package/src/createStyleSheet.ts +12 -0
  116. package/src/global.ts +2 -0
  117. package/src/index.ts +30 -2
  118. package/src/types/breakpoints.ts +9 -11
  119. package/src/types/core.ts +6 -6
  120. package/src/types/cxx.ts +61 -0
  121. package/src/types/index.ts +3 -3
  122. package/src/useInitialTheme.ts +7 -0
  123. package/src/useStyles.ts +49 -0
  124. package/src/useUnistyles.ts +67 -0
  125. package/src/utils/breakpoints.ts +16 -14
  126. package/src/utils/styles.ts +16 -18
  127. package/lib/commonjs/UnistylesTheme.js +0 -21
  128. package/lib/commonjs/UnistylesTheme.js.map +0 -1
  129. package/lib/commonjs/createUnistyles.js +0 -57
  130. package/lib/commonjs/createUnistyles.js.map +0 -1
  131. package/lib/module/UnistylesTheme.js +0 -12
  132. package/lib/module/UnistylesTheme.js.map +0 -1
  133. package/lib/module/createUnistyles.js +0 -50
  134. package/lib/module/createUnistyles.js.map +0 -1
  135. package/lib/typescript/examples/expo/src/examples/Breakpoints.d.ts +0 -3
  136. package/lib/typescript/examples/expo/src/examples/Breakpoints.d.ts.map +0 -1
  137. package/lib/typescript/examples/expo/src/examples/EmptyStyles.d.ts +0 -3
  138. package/lib/typescript/examples/expo/src/examples/EmptyStyles.d.ts.map +0 -1
  139. package/lib/typescript/examples/expo/src/examples/Extreme.d.ts +0 -7
  140. package/lib/typescript/examples/expo/src/examples/Extreme.d.ts.map +0 -1
  141. package/lib/typescript/examples/expo/src/examples/MediaQueries.d.ts +0 -3
  142. package/lib/typescript/examples/expo/src/examples/MediaQueries.d.ts.map +0 -1
  143. package/lib/typescript/examples/expo/src/examples/Memoization.d.ts +0 -3
  144. package/lib/typescript/examples/expo/src/examples/Memoization.d.ts.map +0 -1
  145. package/lib/typescript/examples/expo/src/examples/Minimal.d.ts +0 -3
  146. package/lib/typescript/examples/expo/src/examples/Minimal.d.ts.map +0 -1
  147. package/lib/typescript/examples/expo/src/examples/MinimalWithCreateStyleSheet.d.ts +0 -3
  148. package/lib/typescript/examples/expo/src/examples/MinimalWithCreateStyleSheet.d.ts.map +0 -1
  149. package/lib/typescript/examples/expo/src/examples/Theme.d.ts +0 -3
  150. package/lib/typescript/examples/expo/src/examples/Theme.d.ts.map +0 -1
  151. package/lib/typescript/src/UnistylesTheme.d.ts +0 -9
  152. package/lib/typescript/src/UnistylesTheme.d.ts.map +0 -1
  153. package/lib/typescript/src/createUnistyles.d.ts +0 -10
  154. package/lib/typescript/src/createUnistyles.d.ts.map +0 -1
  155. package/src/UnistylesTheme.tsx +0 -17
  156. package/src/__tests__/createUnistyles.spec.tsx +0 -192
  157. package/src/createUnistyles.ts +0 -70
@@ -1,192 +0,0 @@
1
- import React from 'react'
2
- import { StyleSheet } from 'react-native'
3
- import type { ViewStyle } from 'react-native'
4
- import { renderHook } from '@testing-library/react-hooks'
5
- import { createUnistyles } from '../createUnistyles'
6
- import { UnistylesTheme } from '../UnistylesTheme'
7
- import type { CustomNamedStyles } from '../types'
8
-
9
- jest.mock('../hooks', () => ({
10
- useDimensions: jest.fn(() => ({
11
- width: 500,
12
- height: 1000
13
- }))
14
- }))
15
-
16
- describe('createUnistyles', () => {
17
- describe('createStyleSheet', () => {
18
- it('should work exactly the same like StyleSheet.create', () => {
19
- const breakpoints = {
20
- xs: 0
21
- }
22
- const { createStyleSheet } = createUnistyles(breakpoints)
23
-
24
- const styles = {
25
- container: {
26
- flex: 1,
27
- justifyContent: 'center',
28
- alignItems: 'center'
29
- },
30
- text: {
31
- fontWeight: 'bold',
32
- fontSize: 32
33
- }
34
- } as const
35
-
36
- expect(createStyleSheet(styles)).toEqual(StyleSheet.create(styles))
37
- })
38
-
39
- it('should inject the theme to the createStyleSheet', () => {
40
- const theme = {
41
- colors: {
42
- barbie: '#ff9ff3',
43
- oak: '#1dd1a1',
44
- sky: '#48dbfb',
45
- fog: '#c8d6e5',
46
- aloes: '#00d2d3'
47
- }
48
- }
49
- const breakpoints = {
50
- xs: 0
51
- }
52
- const { useStyles, createStyleSheet } = createUnistyles<typeof breakpoints, typeof theme>(breakpoints)
53
- const stylesheet = createStyleSheet(theme => ({
54
- container: {
55
- backgroundColor: theme.colors.barbie
56
- }
57
- }))
58
- const { result } = renderHook(() => useStyles(stylesheet), {
59
- // @ts-ignore
60
- wrapper: ({ children }) => (
61
- <UnistylesTheme theme={theme}>
62
- {children}
63
- </UnistylesTheme>
64
- )
65
- })
66
-
67
- expect(result.current.theme).toEqual(theme)
68
- expect(result.current.styles).not.toBe(Function)
69
- expect(result.current.styles.container.backgroundColor).toEqual(theme.colors.barbie)
70
- })
71
- })
72
-
73
- describe('useStyles', () => {
74
- it('should return empty object for optional stylesheet', () => {
75
- const breakpoints = {
76
- xs: 0
77
- }
78
- const { useStyles } = createUnistyles(breakpoints)
79
- const { result } = renderHook(() => useStyles())
80
-
81
- expect(result.current.styles).toEqual({})
82
- expect(result.current.theme).toEqual({})
83
- })
84
-
85
- it ('should choose single value from breakpoints', () => {
86
- const breakpoints = {
87
- xs: 0,
88
- sm: 200,
89
- md: 500,
90
- lg: 1000
91
- }
92
-
93
- const { useStyles, createStyleSheet } = createUnistyles(breakpoints)
94
- const stylesheet = createStyleSheet({
95
- container: {
96
- flex: 1,
97
- justifyContent: 'center',
98
- alignItems: {
99
- xs: 'row',
100
- md: 'column'
101
- }
102
- }
103
- })
104
- const { result } = renderHook(() => useStyles(stylesheet))
105
-
106
- expect(result.current.styles.container.alignItems).toEqual('column')
107
- })
108
-
109
- it ('should choose single value from media queries', () => {
110
- const breakpoints = {
111
- xs: 0
112
- }
113
-
114
- const { useStyles, createStyleSheet } = createUnistyles(breakpoints)
115
- const stylesheet = createStyleSheet({
116
- container: {
117
- flex: 1,
118
- justifyContent: 'center',
119
- alignItems: {
120
- xs: 'row',
121
- ':w[300, 490]': 'column',
122
- ':w[491]': 'row'
123
- }
124
- }
125
- })
126
- const { result } = renderHook(() => useStyles(stylesheet as CustomNamedStyles<typeof stylesheet, typeof breakpoints>))
127
-
128
- expect(result.current.styles.container.alignItems).toEqual('row')
129
- })
130
-
131
- it ('should choose wrap dynamic functions in Proxy', () => {
132
- const breakpoints = {
133
- xs: 0
134
- }
135
-
136
- const { useStyles, createStyleSheet } = createUnistyles<typeof breakpoints, {}>(breakpoints)
137
- const stylesheet = createStyleSheet({
138
- container: (flex: number) => ({
139
- flex,
140
- justifyContent: 'center',
141
- alignItems: {
142
- xs: 'row',
143
- ':w[300, 490]': 'column',
144
- ':w[491]': 'row'
145
- }
146
- })
147
- })
148
- const { result } = renderHook(() => useStyles(stylesheet as CustomNamedStyles<typeof stylesheet, typeof breakpoints>))
149
-
150
- expect(result.current.styles.container).toBeInstanceOf(Function)
151
- expect((result.current.styles.container as (flex: number) => ViewStyle)(2)).toEqual({
152
- flex: 2,
153
- justifyContent: 'center',
154
- alignItems: 'row'
155
- })
156
- })
157
-
158
- it ('should return breakpoint even if no stylesheet has been provided', () => {
159
- const breakpoints = {
160
- xs: 0,
161
- sm: 200,
162
- md: 500,
163
- lg: 1000
164
- }
165
-
166
- const { useStyles } = createUnistyles<typeof breakpoints, {}>(breakpoints)
167
- const { result } = renderHook(() => useStyles())
168
-
169
- expect(result.current.breakpoint).toEqual('md')
170
- })
171
-
172
- it ('should return breakpoint for stylesheet', () => {
173
- const breakpoints = {
174
- xs: 0,
175
- sm: 200,
176
- lg: 1000
177
- }
178
-
179
- const { useStyles, createStyleSheet } = createUnistyles<typeof breakpoints, {}>(breakpoints)
180
- const stylesheet = createStyleSheet({
181
- container: {
182
- flex: 1,
183
- justifyContent: 'center',
184
- alignItems: 'center'
185
- }
186
- })
187
- const { result } = renderHook(() => useStyles(stylesheet as CustomNamedStyles<typeof stylesheet, typeof breakpoints>))
188
-
189
- expect(result.current.breakpoint).toEqual('sm')
190
- })
191
- })
192
- })
@@ -1,70 +0,0 @@
1
- import { useContext, useMemo } from 'react'
2
- import { StyleSheet } from 'react-native'
3
- import type {
4
- Breakpoints,
5
- CreateStylesFactory,
6
- CustomNamedStyles,
7
- ExtractBreakpoints,
8
- RemoveKeysWithPrefix,
9
- SortedBreakpointEntries
10
- } from './types'
11
- import { UnistylesContext } from './UnistylesTheme'
12
- import { useDimensions } from './hooks'
13
- import { getBreakpointFromScreenWidth, proxifyFunction, parseStyle, sortAndValidateBreakpoints } from './utils'
14
-
15
- export const createUnistyles = <B extends Breakpoints, T = {}>(breakpoints: B) => {
16
- const sortedBreakpoints = sortAndValidateBreakpoints(breakpoints)
17
- const sortedBreakpointEntries = Object
18
- .entries(sortedBreakpoints) as SortedBreakpointEntries<B>
19
-
20
- return {
21
- createStyleSheet: <S extends CustomNamedStyles<S, B>, X>(styles: S | CustomNamedStyles<S, B> | X | ((theme: T) => X | CustomNamedStyles<X, B>)): S | X => {
22
- if (typeof styles === 'function') {
23
- return styles as X
24
- }
25
-
26
- return styles as S
27
- },
28
- useStyles: <ST extends CustomNamedStyles<ST, B>>(stylesheet?: ST | CreateStylesFactory<ST, T>) => {
29
- const theme = useContext(UnistylesContext) as T
30
- const screenSize = useDimensions()
31
- const breakpoint = getBreakpointFromScreenWidth<B>(screenSize.width, sortedBreakpointEntries)
32
-
33
- if (!stylesheet) {
34
- return {
35
- theme,
36
- breakpoint,
37
- styles: {} as ExtractBreakpoints<RemoveKeysWithPrefix<ST, B>, B>
38
- }
39
- }
40
-
41
- const parsedStyles = useMemo(() => typeof stylesheet === 'function'
42
- ? stylesheet(theme)
43
- : stylesheet, [theme, stylesheet])
44
-
45
- const dynamicStyleSheet = useMemo(() => Object
46
- .entries(parsedStyles)
47
- .reduce((acc, [key, value]) => {
48
- const style = value as CustomNamedStyles<ST, B>
49
-
50
- if (typeof value === 'function') {
51
- return {
52
- ...acc,
53
- [key]: proxifyFunction<B>(value, breakpoint, screenSize, sortedBreakpointEntries)
54
- }
55
- }
56
-
57
- return StyleSheet.create({
58
- ...acc,
59
- [key]: parseStyle<ST, B>(style, breakpoint, screenSize, sortedBreakpointEntries)
60
- })
61
- }, {} as ST), [breakpoint, screenSize, parsedStyles])
62
-
63
- return {
64
- theme,
65
- breakpoint,
66
- styles: dynamicStyleSheet as ExtractBreakpoints<RemoveKeysWithPrefix<ST, B>, B>
67
- }
68
- }
69
- }
70
- }