react-native-unistyles 2.0.0-alpha.1 → 2.0.0-alpha.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (257) hide show
  1. package/README.md +4 -4
  2. package/cxx/UnistylesRuntime.cpp +255 -0
  3. package/cxx/UnistylesRuntime.h +60 -0
  4. package/ios/UnistylesHelpers.h +4 -0
  5. package/ios/UnistylesHelpers.mm +20 -0
  6. package/ios/UnistylesModule.h +10 -0
  7. package/ios/UnistylesModule.mm +158 -0
  8. package/lib/commonjs/common.js +26 -0
  9. package/lib/commonjs/common.js.map +1 -0
  10. package/lib/commonjs/core/UnistyleRegistry.js +38 -0
  11. package/lib/commonjs/core/UnistyleRegistry.js.map +1 -0
  12. package/lib/commonjs/core/Unistyles.js +31 -0
  13. package/lib/commonjs/core/Unistyles.js.map +1 -0
  14. package/lib/commonjs/core/UnistylesModule.js +9 -0
  15. package/lib/commonjs/core/UnistylesModule.js.map +1 -0
  16. package/lib/commonjs/core/UnistylesRuntime.js +66 -0
  17. package/lib/commonjs/core/UnistylesRuntime.js.map +1 -0
  18. package/lib/commonjs/core/index.js +13 -0
  19. package/lib/commonjs/core/index.js.map +1 -0
  20. package/lib/commonjs/createStyleSheet.js +14 -0
  21. package/lib/commonjs/createStyleSheet.js.map +1 -0
  22. package/lib/commonjs/global.js +2 -0
  23. package/lib/commonjs/global.js.map +1 -0
  24. package/lib/commonjs/hooks/index.js +10 -3
  25. package/lib/commonjs/hooks/index.js.map +1 -1
  26. package/lib/commonjs/hooks/useInitialTheme.js +17 -0
  27. package/lib/commonjs/hooks/useInitialTheme.js.map +1 -0
  28. package/lib/commonjs/hooks/useUnistyles.js +54 -0
  29. package/lib/commonjs/hooks/useUnistyles.js.map +1 -0
  30. package/lib/commonjs/index.js +35 -6
  31. package/lib/commonjs/index.js.map +1 -1
  32. package/lib/commonjs/types/common.js +2 -0
  33. package/lib/commonjs/types/{mediaQueries.js.map → common.js.map} +1 -1
  34. package/lib/commonjs/types/index.js +26 -0
  35. package/lib/commonjs/types/index.js.map +1 -1
  36. package/lib/commonjs/types/mq.js +6 -0
  37. package/lib/{module/types/mediaQueries.js.map → commonjs/types/mq.js.map} +1 -1
  38. package/lib/commonjs/types/normalizer.js +6 -0
  39. package/lib/commonjs/types/normalizer.js.map +1 -0
  40. package/lib/commonjs/types/unistyles.js +6 -0
  41. package/lib/commonjs/types/unistyles.js.map +1 -0
  42. package/lib/commonjs/types/variants.js +2 -0
  43. package/lib/commonjs/types/variants.js.map +1 -0
  44. package/lib/commonjs/useStyles.js +49 -0
  45. package/lib/commonjs/useStyles.js.map +1 -0
  46. package/lib/commonjs/utils/breakpoints.js +33 -80
  47. package/lib/commonjs/utils/breakpoints.js.map +1 -1
  48. package/lib/commonjs/utils/common.js +10 -3
  49. package/lib/commonjs/utils/common.js.map +1 -1
  50. package/lib/commonjs/utils/index.js +46 -17
  51. package/lib/commonjs/utils/index.js.map +1 -1
  52. package/lib/commonjs/utils/module.d.js +2 -0
  53. package/lib/commonjs/utils/module.d.js.map +1 -0
  54. package/lib/commonjs/utils/mq.js +89 -0
  55. package/lib/commonjs/utils/mq.js.map +1 -0
  56. package/lib/commonjs/utils/mqParser.js +86 -0
  57. package/lib/commonjs/utils/mqParser.js.map +1 -0
  58. package/lib/commonjs/utils/normalizeStyles.web.js +13 -13
  59. package/lib/commonjs/utils/normalizeStyles.web.js.map +1 -1
  60. package/lib/commonjs/utils/normalizer.js +89 -0
  61. package/lib/commonjs/utils/normalizer.js.map +1 -0
  62. package/lib/commonjs/utils/styles.js +29 -58
  63. package/lib/commonjs/utils/styles.js.map +1 -1
  64. package/lib/commonjs/utils/variants.js +17 -0
  65. package/lib/commonjs/utils/variants.js.map +1 -0
  66. package/lib/module/common.js +22 -0
  67. package/lib/module/common.js.map +1 -0
  68. package/lib/module/core/UnistyleRegistry.js +31 -0
  69. package/lib/module/core/UnistyleRegistry.js.map +1 -0
  70. package/lib/module/core/Unistyles.js +25 -0
  71. package/lib/module/core/Unistyles.js.map +1 -0
  72. package/lib/module/core/UnistylesModule.js +3 -0
  73. package/lib/module/core/UnistylesModule.js.map +1 -0
  74. package/lib/module/core/UnistylesRuntime.js +59 -0
  75. package/lib/module/core/UnistylesRuntime.js.map +1 -0
  76. package/lib/module/core/index.js +2 -0
  77. package/lib/module/core/index.js.map +1 -0
  78. package/lib/module/createStyleSheet.js +7 -0
  79. package/lib/module/createStyleSheet.js.map +1 -0
  80. package/lib/module/global.js +2 -0
  81. package/lib/module/global.js.map +1 -0
  82. package/lib/module/hooks/index.js +2 -1
  83. package/lib/module/hooks/index.js.map +1 -1
  84. package/lib/module/hooks/useInitialTheme.js +10 -0
  85. package/lib/module/hooks/useInitialTheme.js.map +1 -0
  86. package/lib/module/hooks/useUnistyles.js +47 -0
  87. package/lib/module/hooks/useUnistyles.js.map +1 -0
  88. package/lib/module/index.js +13 -2
  89. package/lib/module/index.js.map +1 -1
  90. package/lib/module/types/common.js +2 -0
  91. package/lib/module/types/common.js.map +1 -0
  92. package/lib/module/types/index.js +2 -1
  93. package/lib/module/types/index.js.map +1 -1
  94. package/lib/module/types/mq.js +2 -0
  95. package/lib/module/types/mq.js.map +1 -0
  96. package/lib/module/types/normalizer.js +2 -0
  97. package/lib/module/types/normalizer.js.map +1 -0
  98. package/lib/module/types/unistyles.js +2 -0
  99. package/lib/module/types/unistyles.js.map +1 -0
  100. package/lib/module/types/variants.js +2 -0
  101. package/lib/module/types/variants.js.map +1 -0
  102. package/lib/module/useStyles.js +42 -0
  103. package/lib/module/useStyles.js.map +1 -0
  104. package/lib/module/utils/breakpoints.js +34 -82
  105. package/lib/module/utils/breakpoints.js.map +1 -1
  106. package/lib/module/utils/common.js +9 -1
  107. package/lib/module/utils/common.js.map +1 -1
  108. package/lib/module/utils/index.js +5 -1
  109. package/lib/module/utils/index.js.map +1 -1
  110. package/lib/module/utils/module.d.js +2 -0
  111. package/lib/module/utils/module.d.js.map +1 -0
  112. package/lib/module/utils/mq.js +83 -0
  113. package/lib/module/utils/mq.js.map +1 -0
  114. package/lib/module/utils/mqParser.js +79 -0
  115. package/lib/module/utils/mqParser.js.map +1 -0
  116. package/lib/module/utils/normalizeStyles.web.js +13 -13
  117. package/lib/module/utils/normalizeStyles.web.js.map +1 -1
  118. package/lib/module/utils/normalizer.js +79 -0
  119. package/lib/module/utils/normalizer.js.map +1 -0
  120. package/lib/module/utils/styles.js +28 -59
  121. package/lib/module/utils/styles.js.map +1 -1
  122. package/lib/module/utils/variants.js +10 -0
  123. package/lib/module/utils/variants.js.map +1 -0
  124. package/lib/typescript/src/common.d.ts +17 -0
  125. package/lib/typescript/src/common.d.ts.map +1 -0
  126. package/lib/typescript/src/core/UnistyleRegistry.d.ts +15 -0
  127. package/lib/typescript/src/core/UnistyleRegistry.d.ts.map +1 -0
  128. package/lib/typescript/src/core/Unistyles.d.ts +13 -0
  129. package/lib/typescript/src/core/Unistyles.d.ts.map +1 -0
  130. package/lib/typescript/src/core/UnistylesModule.d.ts +6 -0
  131. package/lib/typescript/src/core/UnistylesModule.d.ts.map +1 -0
  132. package/lib/typescript/src/core/UnistylesRuntime.d.ts +24 -0
  133. package/lib/typescript/src/core/UnistylesRuntime.d.ts.map +1 -0
  134. package/lib/typescript/src/core/index.d.ts +2 -0
  135. package/lib/typescript/src/core/index.d.ts.map +1 -0
  136. package/lib/typescript/src/createStyleSheet.d.ts +3 -0
  137. package/lib/typescript/src/createStyleSheet.d.ts.map +1 -0
  138. package/lib/typescript/src/global.d.ts +7 -0
  139. package/lib/typescript/src/global.d.ts.map +1 -0
  140. package/lib/typescript/src/hooks/index.d.ts +2 -1
  141. package/lib/typescript/src/hooks/index.d.ts.map +1 -1
  142. package/lib/typescript/src/hooks/useInitialTheme.d.ts +3 -0
  143. package/lib/typescript/src/hooks/useInitialTheme.d.ts.map +1 -0
  144. package/lib/typescript/src/hooks/useUnistyles.d.ts +12 -0
  145. package/lib/typescript/src/hooks/useUnistyles.d.ts.map +1 -0
  146. package/lib/typescript/src/index.d.ts +14 -2
  147. package/lib/typescript/src/index.d.ts.map +1 -1
  148. package/lib/typescript/src/types/breakpoints.d.ts +12 -13
  149. package/lib/typescript/src/types/breakpoints.d.ts.map +1 -1
  150. package/lib/typescript/src/types/common.d.ts +3 -0
  151. package/lib/typescript/src/types/common.d.ts.map +1 -0
  152. package/lib/typescript/src/types/core.d.ts +28 -18
  153. package/lib/typescript/src/types/core.d.ts.map +1 -1
  154. package/lib/typescript/src/types/index.d.ts +7 -2
  155. package/lib/typescript/src/types/index.d.ts.map +1 -1
  156. package/lib/typescript/src/types/mq.d.ts +3 -0
  157. package/lib/typescript/src/types/mq.d.ts.map +1 -0
  158. package/lib/typescript/src/types/normalizer.d.ts +20 -0
  159. package/lib/typescript/src/types/normalizer.d.ts.map +1 -0
  160. package/lib/typescript/src/types/unistyles.d.ts +37 -0
  161. package/lib/typescript/src/types/unistyles.d.ts.map +1 -0
  162. package/lib/typescript/src/types/variants.d.ts +8 -0
  163. package/lib/typescript/src/types/variants.d.ts.map +1 -0
  164. package/lib/typescript/src/useStyles.d.ts +10 -0
  165. package/lib/typescript/src/useStyles.d.ts.map +1 -0
  166. package/lib/typescript/src/utils/breakpoints.d.ts +5 -62
  167. package/lib/typescript/src/utils/breakpoints.d.ts.map +1 -1
  168. package/lib/typescript/src/utils/common.d.ts +9 -1
  169. package/lib/typescript/src/utils/common.d.ts.map +1 -1
  170. package/lib/typescript/src/utils/index.d.ts +5 -1
  171. package/lib/typescript/src/utils/index.d.ts.map +1 -1
  172. package/lib/typescript/src/utils/mq.d.ts +21 -0
  173. package/lib/typescript/src/utils/mq.d.ts.map +1 -0
  174. package/lib/typescript/src/utils/mqParser.d.ts +3 -0
  175. package/lib/typescript/src/utils/mqParser.d.ts.map +1 -0
  176. package/lib/typescript/src/utils/normalizeStyles.web.d.ts +4 -1
  177. package/lib/typescript/src/utils/normalizeStyles.web.d.ts.map +1 -1
  178. package/lib/typescript/src/utils/normalizer.d.ts +11 -0
  179. package/lib/typescript/src/utils/normalizer.d.ts.map +1 -0
  180. package/lib/typescript/src/utils/styles.d.ts +5 -49
  181. package/lib/typescript/src/utils/styles.d.ts.map +1 -1
  182. package/lib/typescript/src/utils/variants.d.ts +3 -0
  183. package/lib/typescript/src/utils/variants.d.ts.map +1 -0
  184. package/package.json +33 -15
  185. package/react-native-unistyles.podspec +22 -0
  186. package/src/common.ts +19 -0
  187. package/src/core/UnistyleRegistry.ts +41 -0
  188. package/src/core/Unistyles.ts +34 -0
  189. package/src/core/UnistylesModule.ts +7 -0
  190. package/src/core/UnistylesRuntime.ts +73 -0
  191. package/src/core/index.ts +1 -0
  192. package/src/createStyleSheet.ts +9 -0
  193. package/src/global.ts +5 -0
  194. package/src/hooks/index.ts +2 -1
  195. package/src/hooks/useInitialTheme.ts +11 -0
  196. package/src/hooks/useUnistyles.ts +55 -0
  197. package/src/index.ts +30 -2
  198. package/src/types/breakpoints.ts +21 -20
  199. package/src/types/common.ts +2 -0
  200. package/src/types/core.ts +36 -24
  201. package/src/types/index.ts +7 -9
  202. package/src/types/mq.ts +3 -0
  203. package/src/types/normalizer.ts +29 -0
  204. package/src/types/unistyles.ts +45 -0
  205. package/src/types/variants.ts +11 -0
  206. package/src/useStyles.ts +62 -0
  207. package/src/utils/breakpoints.ts +46 -86
  208. package/src/utils/common.ts +10 -1
  209. package/src/utils/index.ts +5 -8
  210. package/src/utils/module.d.ts +3 -0
  211. package/src/utils/mq.ts +106 -0
  212. package/src/utils/mqParser.ts +99 -0
  213. package/src/utils/normalizeStyles.web.ts +21 -42
  214. package/src/utils/normalizer.ts +99 -0
  215. package/src/utils/styles.ts +46 -70
  216. package/src/utils/variants.ts +13 -0
  217. package/lib/commonjs/UnistylesTheme.js +0 -21
  218. package/lib/commonjs/UnistylesTheme.js.map +0 -1
  219. package/lib/commonjs/createUnistyles.js +0 -57
  220. package/lib/commonjs/createUnistyles.js.map +0 -1
  221. package/lib/commonjs/hooks/useDimensions.js +0 -10
  222. package/lib/commonjs/hooks/useDimensions.js.map +0 -1
  223. package/lib/commonjs/hooks/useDimensions.web.js +0 -31
  224. package/lib/commonjs/hooks/useDimensions.web.js.map +0 -1
  225. package/lib/commonjs/types/mediaQueries.js +0 -2
  226. package/lib/commonjs/utils/mediaQueries.js +0 -189
  227. package/lib/commonjs/utils/mediaQueries.js.map +0 -1
  228. package/lib/module/UnistylesTheme.js +0 -12
  229. package/lib/module/UnistylesTheme.js.map +0 -1
  230. package/lib/module/createUnistyles.js +0 -50
  231. package/lib/module/createUnistyles.js.map +0 -1
  232. package/lib/module/hooks/useDimensions.js +0 -3
  233. package/lib/module/hooks/useDimensions.js.map +0 -1
  234. package/lib/module/hooks/useDimensions.web.js +0 -24
  235. package/lib/module/hooks/useDimensions.web.js.map +0 -1
  236. package/lib/module/types/mediaQueries.js +0 -2
  237. package/lib/module/utils/mediaQueries.js +0 -176
  238. package/lib/module/utils/mediaQueries.js.map +0 -1
  239. package/lib/typescript/src/UnistylesTheme.d.ts +0 -9
  240. package/lib/typescript/src/UnistylesTheme.d.ts.map +0 -1
  241. package/lib/typescript/src/createUnistyles.d.ts +0 -10
  242. package/lib/typescript/src/createUnistyles.d.ts.map +0 -1
  243. package/lib/typescript/src/hooks/useDimensions.d.ts +0 -3
  244. package/lib/typescript/src/hooks/useDimensions.d.ts.map +0 -1
  245. package/lib/typescript/src/hooks/useDimensions.web.d.ts +0 -3
  246. package/lib/typescript/src/hooks/useDimensions.web.d.ts.map +0 -1
  247. package/lib/typescript/src/types/mediaQueries.d.ts +0 -8
  248. package/lib/typescript/src/types/mediaQueries.d.ts.map +0 -1
  249. package/lib/typescript/src/utils/mediaQueries.d.ts +0 -130
  250. package/lib/typescript/src/utils/mediaQueries.d.ts.map +0 -1
  251. package/src/UnistylesTheme.tsx +0 -17
  252. package/src/__tests__/createUnistyles.spec.tsx +0 -192
  253. package/src/createUnistyles.ts +0 -70
  254. package/src/hooks/useDimensions.ts +0 -4
  255. package/src/hooks/useDimensions.web.ts +0 -30
  256. package/src/types/mediaQueries.ts +0 -10
  257. package/src/utils/mediaQueries.ts +0 -201
@@ -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
- }
@@ -1,4 +0,0 @@
1
- import { useWindowDimensions } from 'react-native'
2
- import type { ScreenSize } from '../types'
3
-
4
- export const useDimensions = (): ScreenSize => useWindowDimensions()
@@ -1,30 +0,0 @@
1
- import { useEffect, useRef, useState } from 'react'
2
- import type { ScreenSize } from '../types'
3
-
4
- export const useDimensions = (): ScreenSize => {
5
- const timerRef = useRef<ReturnType<typeof setTimeout>>()
6
- const [screenSize, setScreenSize] = useState<ScreenSize>({
7
- width: window.innerWidth,
8
- height: window.innerHeight
9
- })
10
-
11
- useEffect(() => {
12
- const handleResize = () => {
13
- clearTimeout(timerRef.current)
14
-
15
- timerRef.current = setTimeout(() => setScreenSize({
16
- width: window.innerWidth,
17
- height: window.innerHeight
18
- }), 100)
19
- }
20
-
21
- window.addEventListener('resize', handleResize)
22
-
23
- return () => {
24
- window.removeEventListener('resize', handleResize)
25
- clearTimeout(timerRef.current)
26
- }
27
- }, [])
28
-
29
- return screenSize
30
- }
@@ -1,10 +0,0 @@
1
- type Optional<TOptional extends string> = TOptional | ''
2
- type Separator = ',' | ', '
3
- // this is super weird, but number passes empty string and bigint does not
4
- type MediaQuery = `[${Separator}${bigint}]` | `[${bigint}${Optional<`${Separator}${bigint}`>}]`
5
- type WidthMediaQuery = `:w${MediaQuery}`
6
- type HeightMediaQuery = `:h${MediaQuery}`
7
-
8
- export type MediaQueries =
9
- | `${WidthMediaQuery}${Optional<HeightMediaQuery>}`
10
- | `${HeightMediaQuery}${Optional<WidthMediaQuery>}`
@@ -1,201 +0,0 @@
1
- import type { ScreenSize } from '../types'
2
-
3
- /**
4
- * Extracts numeric values from a coded string.
5
- *
6
- * The function is designed to process strings that have a format like "w[100,200]" or "h[300]".
7
- * It removes characters 'w', 'h', '[', and ']' from the input string and then extracts the numbers.
8
- *
9
- * @param {string} codedValue - The input string to extract values from.
10
- * @returns {Array<number>} An array of extracted numbers. Can contain one or two numbers based on the input format.
11
- *
12
- * @example
13
- * extractValues("w[100,200]") // returns [100, 200]
14
- * extractValues("h[300]") // returns [300]
15
- * extractValues("h[,300]") // returns [0,300]
16
- * extractValues("h[100,]") // returns [100]
17
- */
18
- export const extractValues = (codedValue: string): Array<number> => {
19
- const [lh, rh] = codedValue
20
- .replace(/[wh[\]]/g, '')
21
- .split(',')
22
-
23
- return rh
24
- ? [Number(lh), Number(rh)]
25
- : [Number(lh)]
26
- }
27
-
28
- /**
29
- * Determines if the given screen size matches the specified breakpoint query.
30
- *
31
- * The function checks if the screen size (width and/or height) falls within the range
32
- * specified by the breakpoint query. The query can specify width (using 'w'), height (using 'h'),
33
- * or both.
34
- *
35
- * @param {string} query - The breakpoint query string. Examples: 'w[100,200]', 'h[300]', 'w[100,200]h[300,400]'.
36
- * @param {ScreenSize} screenSize - The screen size to check against the breakpoint query.
37
- * @returns {boolean} True if the screen size matches the breakpoint query, false otherwise.
38
- *
39
- * @example
40
- * const screenSize = { width: 150, height: 350 }
41
- * isWithinBreakpoint('w[100,200]', screenSize) // returns true
42
- * isWithinBreakpoint('h[400]', screenSize) // returns false
43
- */
44
- export const isWithinBreakpoint = (query: string, screenSize: ScreenSize): boolean => {
45
- if (query.includes('w') && query.includes('h')) {
46
- return isWithinTheWidthAndHeight(query, screenSize)
47
- }
48
-
49
- if (query.charAt(0) === 'w') {
50
- return isWithinTheWidth(query, screenSize.width)
51
- }
52
-
53
- if (query.charAt(0) === 'h') {
54
- return isWithinTheHeight(query, screenSize.height)
55
- }
56
-
57
- return false
58
- }
59
-
60
- /**
61
- * Determines if the given width matches the specified width range in the query.
62
- *
63
- * The function checks if the provided width falls within the range specified by the query.
64
- * The query specifies a width range using a format like 'w[100,200]'. If only one value is provided,
65
- * it's treated as a minimum width.
66
- *
67
- * @param {string} query - The width query string. Examples: 'w[100,200]' or 'w[100]'.
68
- * @param {number} width - The width to check against the query.
69
- * @returns {boolean} True if the width matches the query range, false otherwise.
70
- *
71
- * @example
72
- * isWithinTheWidth('w[100,200]', 150) // returns true
73
- * isWithinTheWidth('w[100]', 50) // returns false
74
- * isWithinTheWidth('w[100]', 150) // returns true
75
- */
76
- export const isWithinTheWidth = (query: string, width: number): boolean => {
77
- const [minWidth, maxWidth] = extractValues(query) as [number, number | undefined]
78
-
79
- if (maxWidth && width >= minWidth && width <= maxWidth) {
80
- return true
81
- }
82
-
83
- return !maxWidth && width >= minWidth
84
- }
85
-
86
- /**
87
- * Determines if the given height matches the specified height range in the query.
88
- *
89
- * The function checks if the provided height falls within the range specified by the query.
90
- * The query specifies a height range using a format like 'h[100,200]'. If only one value is provided,
91
- * it's treated as a minimum height.
92
- *
93
- * @param {string} query - The height query string. Examples: 'h[100,200]' or 'h[100]'.
94
- * @param {number} height - The height to check against the query.
95
- * @returns {boolean} True if the height matches the query range, false otherwise.
96
- *
97
- * @example
98
- * isWithinTheHeight('h[100,200]', 150) // returns true
99
- * isWithinTheHeight('h[100]', 50) // returns false
100
- * isWithinTheHeight('h[100]', 150) // returns true
101
- */
102
- export const isWithinTheHeight = (query: string, height: number): boolean => {
103
- const [minHeight, maxHeight] = extractValues(query) as [number, number | undefined]
104
-
105
- if (maxHeight && height >= minHeight && height <= maxHeight) {
106
- return true
107
- }
108
-
109
- return !maxHeight && height >= minHeight
110
- }
111
-
112
- /**
113
- * Determines if the given screen size matches both the specified width and height ranges in the query.
114
- *
115
- * The function checks if the provided screen size (both width and height) falls within the ranges
116
- * specified by the query. The query can specify both width and height using a format like 'w[100,200]:h[300,400]'.
117
- *
118
- * @param {string} query - The combined width and height query string. Example: 'w[100,200]:h[300,400]'.
119
- * @param {ScreenSize} screenSize - The screen size to check against the query.
120
- * @returns {boolean} True if the screen size matches both the width and height ranges in the query, false otherwise.
121
- *
122
- * @example
123
- * const screenSize = { width: 150, height: 350 }
124
- * isWithinTheWidthAndHeight('w[100,200]:h[300,400]', screenSize) // returns true
125
- * isWithinTheWidthAndHeight('w[100,200]:h[400,500]', screenSize) // returns false
126
- */
127
- export const isWithinTheWidthAndHeight = (query: string, screenSize: ScreenSize): boolean => {
128
- const result = query
129
- .split(':')
130
- .filter(Boolean)
131
- .map(q => isWithinBreakpoint(q, screenSize))
132
- .filter(Boolean)
133
-
134
- return result.length === 2
135
- }
136
-
137
- /**
138
- * Checks if the given query string is a valid custom media query.
139
- *
140
- * The valid custom media query formats include:
141
- * - :w[200]
142
- * - :w[0, 200]
143
- * - :w[, 300]
144
- * - :h[200]
145
- * - :h[0, 500]
146
- * - :h[,200]
147
- * - :w[100, 300]:h[200,500]
148
- * - :h[200,500]:w[100, 300]
149
- *
150
- * @param {string} query - The query string to be checked.
151
- * @returns {boolean} Returns `true` if the query is a valid custom media query, otherwise `false`.
152
- * @example
153
- *
154
- * isMediaQuery(':w[200]') // true
155
- * isMediaQuery(':w100]') // false
156
- */
157
- export const isMediaQuery = (query: string): boolean => {
158
- const regex = /^(?:(:w\[\d*(?:,\s?\d+)?])?(:h\[\d*(?:,\s?\d+)?])?|(:h\[\d*(?:,\s?\d+)?])?(:w\[\d*(?:,\s?\d+)?])?)$/
159
-
160
- return query.length > 0 && regex.test(query)
161
- }
162
-
163
- /**
164
- * Retrieves the first matching custom media query key based on the provided screen size.
165
- *
166
- * The function processes an array of media queries and returns the first query that matches
167
- * the given screen size. The media queries can be in formats like:
168
- * - w[200]
169
- * - w[0, 200]
170
- * - w[, 300]
171
- * - h[200]
172
- * - h[0, 500]
173
- * - h[,200]
174
- * - w[100, 300]:h[200,500]
175
- * - h[200,500]:w[100, 300]
176
- *
177
- * @param {Array<[string, string | number]>} mediaQueries - An array of tuples containing media query keys and associated values.
178
- * @param {ScreenSize} screenSize - An object representing the screen size to be checked against the media queries.
179
- * @returns {string | undefined} Returns the first matching media query key or `undefined` if no match is found.
180
- * @example
181
- *
182
- * const queries = [[':w[200]', 'value1'], [':h[300,500]', 'value2']]
183
- * const size = { width: 250, height: 400 }
184
- * getKeyForCustomMediaQuery(queries, size) // ':w[200]
185
- */
186
- export const getKeyForCustomMediaQuery = (mediaQueries: Array<[string, string | number | undefined]>, screenSize: ScreenSize): string | undefined => {
187
- const [matchedQuery] = mediaQueries
188
- .flatMap(([key]) => {
189
- if (key.includes('w') && key.includes('h')) {
190
- return isWithinBreakpoint(key, screenSize) ? key : undefined
191
- }
192
-
193
- return key
194
- .split(':')
195
- .filter(Boolean)
196
- .map(query => isWithinBreakpoint(query, screenSize) ? key : undefined)
197
- })
198
- .filter(Boolean)
199
-
200
- return matchedQuery
201
- }