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
@@ -0,0 +1,99 @@
1
+ // based on react-native-web normalizer
2
+ // https://github.com/necolas/react-native-web
3
+ import normalizeColors from '@react-native/normalize-colors'
4
+ import type { TextShadow, Transforms, BoxShadow } from '../types'
5
+
6
+ type Preprocessor = {
7
+ createTextShadowValue(style: TextShadow): string,
8
+ createBoxShadowValue(style: Required<BoxShadow>): string,
9
+ createTransformValue(transforms: Required<Transforms>): string,
10
+ }
11
+
12
+ export const normalizeColor = (color: string, opacity: number = 1) => {
13
+ // If the opacity is 1 there's no need to normalize the color
14
+ if (opacity === 1) {
15
+ return color
16
+ }
17
+
18
+ const integer = normalizeColors(color) as number | null
19
+
20
+ // If the colour is an unknown format, the return value is null
21
+ if (integer === null) {
22
+ return color
23
+ }
24
+
25
+ const hex = integer.toString(16).padStart(8, '0')
26
+
27
+ if (hex.length === 8) {
28
+ const [r = 0, g = 0, b = 0, a = 1] = hex
29
+ .split(/(?=(?:..)*$)/)
30
+ .map(x => parseInt(x, 16))
31
+ .filter(num => !isNaN(num))
32
+
33
+ return `rgba(${r},${g},${b},${((a as number) / 255) * opacity})`
34
+ }
35
+
36
+ return color
37
+ }
38
+
39
+ export const normalizeNumericValue = (value: number) => value ? `${value}px` : value
40
+ const normalizeTransform = (key: string, value: number | string) => {
41
+ if (key.includes('scale')) {
42
+ return value
43
+ }
44
+
45
+ if (typeof value === 'number') {
46
+ return normalizeNumericValue(value)
47
+ }
48
+
49
+ return value
50
+ }
51
+
52
+ const createTextShadowValue = (style: TextShadow) => {
53
+ // at this point every prop is present
54
+ const { textShadowColor, textShadowOffset, textShadowRadius } = style
55
+ const offsetX = normalizeNumericValue(textShadowOffset.width)
56
+ const offsetY = normalizeNumericValue(textShadowOffset.height)
57
+ const radius = normalizeNumericValue(textShadowRadius)
58
+ const color = normalizeColor(textShadowColor as string)
59
+
60
+ return `${offsetX} ${offsetY} ${radius} ${color}`
61
+ }
62
+
63
+ const createBoxShadowValue = (style: BoxShadow) => {
64
+ // at this point every prop is present
65
+ const { shadowColor, shadowOffset, shadowOpacity, shadowRadius } = style
66
+ const offsetX = normalizeNumericValue(shadowOffset.width)
67
+ const offsetY = normalizeNumericValue(shadowOffset.height)
68
+ const radius = normalizeNumericValue(shadowRadius)
69
+ const color = normalizeColor(shadowColor as string, shadowOpacity as number)
70
+
71
+ return `${offsetX} ${offsetY} ${radius} ${color}`
72
+ }
73
+
74
+ const createTransformValue = (transforms: Transforms) => transforms
75
+ .map(transform => {
76
+ const [key] = Object.keys(transform)
77
+
78
+ if (!key) {
79
+ return undefined
80
+ }
81
+
82
+ const value = transform[key as keyof typeof transform]
83
+
84
+ switch(key) {
85
+ case 'matrix':
86
+ case 'matrix3d':
87
+ return `${key}(${(value as Array<number>).join(',')})`
88
+ default:
89
+ return `${key}(${normalizeTransform(key, value)})`
90
+ }
91
+ })
92
+ .filter(Boolean)
93
+ .join(' ')
94
+
95
+ export const preprocessor: Preprocessor = {
96
+ createTextShadowValue,
97
+ createBoxShadowValue,
98
+ createTransformValue
99
+ }
@@ -1,73 +1,54 @@
1
- import type { Breakpoints, CustomNamedStyles, ScreenSize, SortedBreakpointEntries } from '../types'
1
+ import type { CustomNamedStyles, ScreenSize, NestedStyle } from '../types'
2
2
  import { getValueForBreakpoint } from './breakpoints'
3
3
  import { normalizeStyles } from './normalizeStyles'
4
- import { isWeb } from './common'
4
+ import type { UnistylesBreakpoints } from '../global'
5
+ import { isAndroid, isIOS, isWeb } from './common'
6
+ import { getKeyForVariant } from './variants'
5
7
 
6
- /**
7
- * Proxies a function to parse its return value for custom media queries or breakpoints.
8
- *
9
- * @template B - An object type where keys represent breakpoint names and values represent breakpoint values.
10
- *
11
- * @param {Function} fn - The function to be proxified.
12
- * @param {keyof B & string} breakpoint - The breakpoint name to check against.
13
- * @param {ScreenSize} screenSize - An object representing the screen size to be checked against the media queries.
14
- * @param breakpointPairs - sorted pairs of breakpoints
15
- *
16
- * @returns {Function} Returns the proxified function
17
- *
18
- * @example
19
- *
20
- * const myFunction = () => ({ ':w[200]': 'value1', sm: 'value2' })
21
- * const screenSize = { width: 250, height: 400 }
22
- * const breakpoints = { sm: 300, md: 600 }
23
- *
24
- * const proxifiedFunction = proxifyFunction(myFunction, 'sm', screenSize, breakpoints)
25
- * proxifiedFunction() // parsed style based on screenSize and breakpoints
26
- */
27
- export const proxifyFunction = <B extends Breakpoints>(
28
- fn: Function, breakpoint: keyof B & string,
8
+ export const proxifyFunction = (
9
+ fn: Function, breakpoint: keyof UnistylesBreakpoints & string,
29
10
  screenSize: ScreenSize,
30
- breakpointPairs: SortedBreakpointEntries<B>
11
+ variant?: string
31
12
  ): Function => new Proxy(fn, {
32
13
  apply: (target, thisArg, argumentsList) =>
33
- parseStyle(target.apply(thisArg, argumentsList), breakpoint, screenSize, breakpointPairs)
14
+ parseStyle(target.apply(thisArg, argumentsList), breakpoint, screenSize, variant)
34
15
  })
35
16
 
36
- /**
37
- * Parses a style object to resolve custom media queries or breakpoints based on the provided screen size and breakpoints.
38
- *
39
- * The function processes each key-value pair in the style object. If the value is a function or a valid style (not an object or a 'transform' key),
40
- * it is returned as-is. Otherwise, the function attempts to resolve the value based on the provided breakpoint, screen size, and defined breakpoints.
41
- *
42
- * @template T - The type of the style object.
43
- * @template B - An object type where keys represent breakpoint names and values represent breakpoint values.
44
- *
45
- * @param {CustomNamedStyles<T, B>} style - The style object to be parsed.
46
- * @param {keyof B & string} breakpoint - The breakpoint name to check against.
47
- * @param {ScreenSize} screenSize - An object representing the screen size to be checked against the media queries.
48
- * @param breakpointPairs - sorted pairs of breakpoints
49
- *
50
- * @returns {Record<string, string | number | Function>} Returns the parsed style object with resolved custom media queries or breakpoints.
51
- *
52
- * @example
53
- *
54
- * const style = { fontSize: { sm: '12px', md: '16px' } }
55
- * const screenSize = { width: 300, height: 400 }
56
- * const breakpoints = { xs: 0, sm: 300, md: 600 }
57
- *
58
- * const parsedStyle = parseStyle(style, 'sm', screenSize, breakpoints)
59
- * // { fontSize: '12px' }
60
- */
61
- export const parseStyle = <T, B extends Breakpoints>(
62
- style: CustomNamedStyles<T, B>,
63
- breakpoint: keyof B & string,
17
+ export const isPlatformColor = <T extends {}>(value: T): boolean => {
18
+ if (isIOS) {
19
+ return 'semantic' in value && typeof value.semantic === 'object'
20
+ }
21
+
22
+ return isAndroid && 'resource_paths' in value && typeof value.resource_paths === 'object'
23
+ }
24
+
25
+ export const parseStyle = <T>(
26
+ style: CustomNamedStyles<T>,
27
+ breakpoint: keyof UnistylesBreakpoints & string,
64
28
  screenSize: ScreenSize,
65
- breakpointPairs: SortedBreakpointEntries<B>
29
+ variant?: string
66
30
  ): T => {
67
- const entries = Object.entries(style) as [[
68
- keyof T,
69
- CustomNamedStyles<T, B> | Record<keyof B & string, string | number | undefined>]
70
- ]
31
+ const entries = (Object
32
+ .entries(style || {}) as Array<[keyof T, CustomNamedStyles<T> | NestedStyle]>)
33
+ .map(([key, value]) => {
34
+ if (key !== 'variants') {
35
+ return [key, value]
36
+ }
37
+
38
+ const variantKey = getKeyForVariant(
39
+ value as NestedStyle,
40
+ variant
41
+ )
42
+
43
+ if (!variantKey) {
44
+ return undefined
45
+ }
46
+
47
+ return Object
48
+ .entries(value[variantKey as keyof typeof value] as NestedStyle)
49
+ .flat()
50
+ })
51
+ .filter(Boolean) as Array<[keyof T, CustomNamedStyles<T> | NestedStyle]>
71
52
 
72
53
  const parsedStyles = Object
73
54
  .fromEntries(entries
@@ -77,7 +58,7 @@ export const parseStyle = <T, B extends Breakpoints>(
77
58
  if (hasNestedProperties) {
78
59
  return [
79
60
  key,
80
- parseStyle(value as CustomNamedStyles<T, B>, breakpoint, screenSize, breakpointPairs)
61
+ parseStyle(value as CustomNamedStyles<T>, breakpoint, screenSize, variant)
81
62
  ]
82
63
  }
83
64
 
@@ -86,12 +67,12 @@ export const parseStyle = <T, B extends Breakpoints>(
86
67
  if (isTransform && Array.isArray(value)) {
87
68
  return [
88
69
  key,
89
- value.map(value => parseStyle(value, breakpoint, screenSize, breakpointPairs))
70
+ value.map(value => parseStyle(value, breakpoint, screenSize, variant))
90
71
  ]
91
72
  }
92
73
 
93
74
  const isDynamicFunction = typeof value === 'function'
94
- const isValidStyle = typeof value !== 'object'
75
+ const isValidStyle = typeof value !== 'object' || isPlatformColor(value)
95
76
 
96
77
  if (isDynamicFunction || isValidStyle) {
97
78
  return [key, value]
@@ -99,17 +80,12 @@ export const parseStyle = <T, B extends Breakpoints>(
99
80
 
100
81
  return [
101
82
  key,
102
- getValueForBreakpoint<B>(
103
- value as Record<keyof B & string, string | number | undefined>,
104
- breakpoint,
105
- screenSize,
106
- breakpointPairs
107
- )
83
+ getValueForBreakpoint(value as NestedStyle)
108
84
  ]
109
85
  })
110
86
  )
111
87
 
112
- return isWeb()
88
+ return isWeb
113
89
  ? normalizeStyles(parsedStyles)
114
90
  : parsedStyles
115
91
  }
@@ -0,0 +1,13 @@
1
+ import type { Optional, NestedStyle } from '../types'
2
+
3
+ export const getKeyForVariant = (value: NestedStyle, variant?: string): Optional<string> => {
4
+ if (variant && variant in value) {
5
+ return variant
6
+ }
7
+
8
+ if ('default' in value) {
9
+ return 'default'
10
+ }
11
+
12
+ return undefined
13
+ }
@@ -1,21 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.UnistylesTheme = exports.UnistylesContext = void 0;
7
- var _react = _interopRequireWildcard(require("react"));
8
- function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
9
- function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
10
- const UnistylesContext = exports.UnistylesContext = /*#__PURE__*/(0, _react.createContext)({});
11
- const UnistylesTheme = _ref => {
12
- let {
13
- theme,
14
- children
15
- } = _ref;
16
- return /*#__PURE__*/_react.default.createElement(UnistylesContext.Provider, {
17
- value: theme
18
- }, children);
19
- };
20
- exports.UnistylesTheme = UnistylesTheme;
21
- //# sourceMappingURL=UnistylesTheme.js.map
@@ -1 +0,0 @@
1
- {"version":3,"names":["_react","_interopRequireWildcard","require","_getRequireWildcardCache","e","WeakMap","r","t","__esModule","default","has","get","n","__proto__","a","Object","defineProperty","getOwnPropertyDescriptor","u","prototype","hasOwnProperty","call","i","set","UnistylesContext","exports","createContext","UnistylesTheme","_ref","theme","children","createElement","Provider","value"],"sourceRoot":"../../src","sources":["UnistylesTheme.tsx"],"mappings":";;;;;;AAAA,IAAAA,MAAA,GAAAC,uBAAA,CAAAC,OAAA;AAA4C,SAAAC,yBAAAC,CAAA,6BAAAC,OAAA,mBAAAC,CAAA,OAAAD,OAAA,IAAAE,CAAA,OAAAF,OAAA,YAAAF,wBAAA,YAAAA,CAAAC,CAAA,WAAAA,CAAA,GAAAG,CAAA,GAAAD,CAAA,KAAAF,CAAA;AAAA,SAAAH,wBAAAG,CAAA,EAAAE,CAAA,SAAAA,CAAA,IAAAF,CAAA,IAAAA,CAAA,CAAAI,UAAA,SAAAJ,CAAA,eAAAA,CAAA,uBAAAA,CAAA,yBAAAA,CAAA,WAAAK,OAAA,EAAAL,CAAA,QAAAG,CAAA,GAAAJ,wBAAA,CAAAG,CAAA,OAAAC,CAAA,IAAAA,CAAA,CAAAG,GAAA,CAAAN,CAAA,UAAAG,CAAA,CAAAI,GAAA,CAAAP,CAAA,OAAAQ,CAAA,KAAAC,SAAA,UAAAC,CAAA,GAAAC,MAAA,CAAAC,cAAA,IAAAD,MAAA,CAAAE,wBAAA,WAAAC,CAAA,IAAAd,CAAA,oBAAAc,CAAA,IAAAH,MAAA,CAAAI,SAAA,CAAAC,cAAA,CAAAC,IAAA,CAAAjB,CAAA,EAAAc,CAAA,SAAAI,CAAA,GAAAR,CAAA,GAAAC,MAAA,CAAAE,wBAAA,CAAAb,CAAA,EAAAc,CAAA,UAAAI,CAAA,KAAAA,CAAA,CAAAX,GAAA,IAAAW,CAAA,CAAAC,GAAA,IAAAR,MAAA,CAAAC,cAAA,CAAAJ,CAAA,EAAAM,CAAA,EAAAI,CAAA,IAAAV,CAAA,CAAAM,CAAA,IAAAd,CAAA,CAAAc,CAAA,YAAAN,CAAA,CAAAH,OAAA,GAAAL,CAAA,EAAAG,CAAA,IAAAA,CAAA,CAAAgB,GAAA,CAAAnB,CAAA,EAAAQ,CAAA,GAAAA,CAAA;AAOrC,MAAMY,gBAAgB,GAAAC,OAAA,CAAAD,gBAAA,gBAAG,IAAAE,oBAAa,EAAC,CAAC,CAAC,CAAC;AAE1C,MAAMC,cAA4D,GAAGC,IAAA;EAAA,IAAC;IACzEC,KAAK;IACLC;EACJ,CAAC,GAAAF,IAAA;EAAA,oBACG5B,MAAA,CAAAS,OAAA,CAAAsB,aAAA,CAACP,gBAAgB,CAACQ,QAAQ;IAACC,KAAK,EAAEJ;EAAM,GACnCC,QACsB,CAAC;AAAA,CAC/B;AAAAL,OAAA,CAAAE,cAAA,GAAAA,cAAA"}
@@ -1,57 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.createUnistyles = void 0;
7
- var _react = require("react");
8
- var _reactNative = require("react-native");
9
- var _UnistylesTheme = require("./UnistylesTheme");
10
- var _hooks = require("./hooks");
11
- var _utils = require("./utils");
12
- const createUnistyles = breakpoints => {
13
- const sortedBreakpoints = (0, _utils.sortAndValidateBreakpoints)(breakpoints);
14
- const sortedBreakpointEntries = Object.entries(sortedBreakpoints);
15
- return {
16
- createStyleSheet: styles => {
17
- if (typeof styles === 'function') {
18
- return styles;
19
- }
20
- return styles;
21
- },
22
- useStyles: stylesheet => {
23
- const theme = (0, _react.useContext)(_UnistylesTheme.UnistylesContext);
24
- const screenSize = (0, _hooks.useDimensions)();
25
- const breakpoint = (0, _utils.getBreakpointFromScreenWidth)(screenSize.width, sortedBreakpointEntries);
26
- if (!stylesheet) {
27
- return {
28
- theme,
29
- breakpoint,
30
- styles: {}
31
- };
32
- }
33
- const parsedStyles = (0, _react.useMemo)(() => typeof stylesheet === 'function' ? stylesheet(theme) : stylesheet, [theme, stylesheet]);
34
- const dynamicStyleSheet = (0, _react.useMemo)(() => Object.entries(parsedStyles).reduce((acc, _ref) => {
35
- let [key, value] = _ref;
36
- const style = value;
37
- if (typeof value === 'function') {
38
- return {
39
- ...acc,
40
- [key]: (0, _utils.proxifyFunction)(value, breakpoint, screenSize, sortedBreakpointEntries)
41
- };
42
- }
43
- return _reactNative.StyleSheet.create({
44
- ...acc,
45
- [key]: (0, _utils.parseStyle)(style, breakpoint, screenSize, sortedBreakpointEntries)
46
- });
47
- }, {}), [breakpoint, screenSize, parsedStyles]);
48
- return {
49
- theme,
50
- breakpoint,
51
- styles: dynamicStyleSheet
52
- };
53
- }
54
- };
55
- };
56
- exports.createUnistyles = createUnistyles;
57
- //# sourceMappingURL=createUnistyles.js.map
@@ -1 +0,0 @@
1
- {"version":3,"names":["_react","require","_reactNative","_UnistylesTheme","_hooks","_utils","createUnistyles","breakpoints","sortedBreakpoints","sortAndValidateBreakpoints","sortedBreakpointEntries","Object","entries","createStyleSheet","styles","useStyles","stylesheet","theme","useContext","UnistylesContext","screenSize","useDimensions","breakpoint","getBreakpointFromScreenWidth","width","parsedStyles","useMemo","dynamicStyleSheet","reduce","acc","_ref","key","value","style","proxifyFunction","StyleSheet","create","parseStyle","exports"],"sourceRoot":"../../src","sources":["createUnistyles.ts"],"mappings":";;;;;;AAAA,IAAAA,MAAA,GAAAC,OAAA;AACA,IAAAC,YAAA,GAAAD,OAAA;AASA,IAAAE,eAAA,GAAAF,OAAA;AACA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sBAAe,EAAIF,KAAK,EAAEV,UAAU,EAAEF,UAAU,EAAEV,uBAAuB;UACpF,CAAC;QACL;QAEA,OAAOyB,uBAAU,CAACC,MAAM,CAAC;UACrB,GAAGP,GAAG;UACN,CAACE,GAAG,GAAG,IAAAM,iBAAU,EAAQJ,KAAK,EAAEX,UAAU,EAAEF,UAAU,EAAEV,uBAAuB;QACnF,CAAC,CAAC;MACN,CAAC,EAAE,CAAC,CAAO,CAAC,EAAE,CAACY,UAAU,EAAEF,UAAU,EAAEK,YAAY,CAAC,CAAC;MAEzD,OAAO;QACHR,KAAK;QACLK,UAAU;QACVR,MAAM,EAAEa;MACZ,CAAC;IACL;EACJ,CAAC;AACL,CAAC;AAAAW,OAAA,CAAAhC,eAAA,GAAAA,eAAA"}
@@ -1,10 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.useDimensions = void 0;
7
- var _reactNative = require("react-native");
8
- const useDimensions = () => (0, _reactNative.useWindowDimensions)();
9
- exports.useDimensions = useDimensions;
10
- //# sourceMappingURL=useDimensions.js.map
@@ -1 +0,0 @@
1
- {"version":3,"names":["_reactNative","require","useDimensions","useWindowDimensions","exports"],"sourceRoot":"../../../src","sources":["hooks/useDimensions.ts"],"mappings":";;;;;;AAAA,IAAAA,YAAA,GAAAC,OAAA;AAGO,MAAMC,aAAa,GAAGA,CAAA,KAAkB,IAAAC,gCAAmB,EAAC,CAAC;AAAAC,OAAA,CAAAF,aAAA,GAAAA,aAAA"}
@@ -1,31 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.useDimensions = void 0;
7
- var _react = require("react");
8
- const useDimensions = () => {
9
- const timerRef = (0, _react.useRef)();
10
- const [screenSize, setScreenSize] = (0, _react.useState)({
11
- width: window.innerWidth,
12
- height: window.innerHeight
13
- });
14
- (0, _react.useEffect)(() => {
15
- const handleResize = () => {
16
- clearTimeout(timerRef.current);
17
- timerRef.current = setTimeout(() => setScreenSize({
18
- width: window.innerWidth,
19
- height: window.innerHeight
20
- }), 100);
21
- };
22
- window.addEventListener('resize', handleResize);
23
- return () => {
24
- window.removeEventListener('resize', handleResize);
25
- clearTimeout(timerRef.current);
26
- };
27
- }, []);
28
- return screenSize;
29
- };
30
- exports.useDimensions = useDimensions;
31
- //# sourceMappingURL=useDimensions.web.js.map
@@ -1 +0,0 @@
1
- {"version":3,"names":["_react","require","useDimensions","timerRef","useRef","screenSize","setScreenSize","useState","width","window","innerWidth","height","innerHeight","useEffect","handleResize","clearTimeout","current","setTimeout","addEventListener","removeEventListener","exports"],"sourceRoot":"../../../src","sources":["hooks/useDimensions.web.ts"],"mappings":";;;;;;AAAA,IAAAA,MAAA,GAAAC,OAAA;AAGO,MAAMC,aAAa,GAAGA,CAAA,KAAkB;EAC3C,MAAMC,QAAQ,GAAG,IAAAC,aAAM,EAAgC,CAAC;EACxD,MAAM,CAACC,UAAU,EAAEC,aAAa,CAAC,GAAG,IAAAC,eAAQ,EAAa;IACrDC,KAAK,EAAEC,MAAM,CAACC,UAAU;IACxBC,MAAM,EAAEF,MAAM,CAACG;EACnB,CAAC,CAAC;EAEF,IAAAC,gBAAS,EAAC,MAAM;IACZ,MAAMC,YAAY,GAAGA,CAAA,KAAM;MACvBC,YAAY,CAACZ,QAAQ,CAACa,OAAO,CAAC;MAE9Bb,QAAQ,CAACa,OAAO,GAAGC,UAAU,CAAC,MAAMX,aAAa,CAAC;QAC9CE,KAAK,EAAEC,MAAM,CAACC,UAAU;QACxBC,MAAM,EAAEF,MAAM,CAACG;MACnB,CAAC,CAAC,EAAE,GAAG,CAAC;IACZ,CAAC;IAEDH,MAAM,CAACS,gBAAgB,CAAC,QAAQ,EAAEJ,YAAY,CAAC;IAE/C,OAAO,MAAM;MACTL,MAAM,CAACU,mBAAmB,CAAC,QAAQ,EAAEL,YAAY,CAAC;MAClDC,YAAY,CAACZ,QAAQ,CAACa,OAAO,CAAC;IAClC,CAAC;EACL,CAAC,EAAE,EAAE,CAAC;EAEN,OAAOX,UAAU;AACrB,CAAC;AAAAe,OAAA,CAAAlB,aAAA,GAAAA,aAAA"}
@@ -1,2 +0,0 @@
1
- "use strict";
2
- //# sourceMappingURL=mediaQueries.js.map
@@ -1,189 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.isWithinTheWidthAndHeight = exports.isWithinTheWidth = exports.isWithinTheHeight = exports.isWithinBreakpoint = exports.isMediaQuery = exports.getKeyForCustomMediaQuery = exports.extractValues = void 0;
7
- /**
8
- * Extracts numeric values from a coded string.
9
- *
10
- * The function is designed to process strings that have a format like "w[100,200]" or "h[300]".
11
- * It removes characters 'w', 'h', '[', and ']' from the input string and then extracts the numbers.
12
- *
13
- * @param {string} codedValue - The input string to extract values from.
14
- * @returns {Array<number>} An array of extracted numbers. Can contain one or two numbers based on the input format.
15
- *
16
- * @example
17
- * extractValues("w[100,200]") // returns [100, 200]
18
- * extractValues("h[300]") // returns [300]
19
- * extractValues("h[,300]") // returns [0,300]
20
- * extractValues("h[100,]") // returns [100]
21
- */
22
- const extractValues = codedValue => {
23
- const [lh, rh] = codedValue.replace(/[wh[\]]/g, '').split(',');
24
- return rh ? [Number(lh), Number(rh)] : [Number(lh)];
25
- };
26
-
27
- /**
28
- * Determines if the given screen size matches the specified breakpoint query.
29
- *
30
- * The function checks if the screen size (width and/or height) falls within the range
31
- * specified by the breakpoint query. The query can specify width (using 'w'), height (using 'h'),
32
- * or both.
33
- *
34
- * @param {string} query - The breakpoint query string. Examples: 'w[100,200]', 'h[300]', 'w[100,200]h[300,400]'.
35
- * @param {ScreenSize} screenSize - The screen size to check against the breakpoint query.
36
- * @returns {boolean} True if the screen size matches the breakpoint query, false otherwise.
37
- *
38
- * @example
39
- * const screenSize = { width: 150, height: 350 }
40
- * isWithinBreakpoint('w[100,200]', screenSize) // returns true
41
- * isWithinBreakpoint('h[400]', screenSize) // returns false
42
- */
43
- exports.extractValues = extractValues;
44
- const isWithinBreakpoint = (query, screenSize) => {
45
- if (query.includes('w') && query.includes('h')) {
46
- return isWithinTheWidthAndHeight(query, screenSize);
47
- }
48
- if (query.charAt(0) === 'w') {
49
- return isWithinTheWidth(query, screenSize.width);
50
- }
51
- if (query.charAt(0) === 'h') {
52
- return isWithinTheHeight(query, screenSize.height);
53
- }
54
- return false;
55
- };
56
-
57
- /**
58
- * Determines if the given width matches the specified width range in the query.
59
- *
60
- * The function checks if the provided width falls within the range specified by the query.
61
- * The query specifies a width range using a format like 'w[100,200]'. If only one value is provided,
62
- * it's treated as a minimum width.
63
- *
64
- * @param {string} query - The width query string. Examples: 'w[100,200]' or 'w[100]'.
65
- * @param {number} width - The width to check against the query.
66
- * @returns {boolean} True if the width matches the query range, false otherwise.
67
- *
68
- * @example
69
- * isWithinTheWidth('w[100,200]', 150) // returns true
70
- * isWithinTheWidth('w[100]', 50) // returns false
71
- * isWithinTheWidth('w[100]', 150) // returns true
72
- */
73
- exports.isWithinBreakpoint = isWithinBreakpoint;
74
- const isWithinTheWidth = (query, width) => {
75
- const [minWidth, maxWidth] = extractValues(query);
76
- if (maxWidth && width >= minWidth && width <= maxWidth) {
77
- return true;
78
- }
79
- return !maxWidth && width >= minWidth;
80
- };
81
-
82
- /**
83
- * Determines if the given height matches the specified height range in the query.
84
- *
85
- * The function checks if the provided height falls within the range specified by the query.
86
- * The query specifies a height range using a format like 'h[100,200]'. If only one value is provided,
87
- * it's treated as a minimum height.
88
- *
89
- * @param {string} query - The height query string. Examples: 'h[100,200]' or 'h[100]'.
90
- * @param {number} height - The height to check against the query.
91
- * @returns {boolean} True if the height matches the query range, false otherwise.
92
- *
93
- * @example
94
- * isWithinTheHeight('h[100,200]', 150) // returns true
95
- * isWithinTheHeight('h[100]', 50) // returns false
96
- * isWithinTheHeight('h[100]', 150) // returns true
97
- */
98
- exports.isWithinTheWidth = isWithinTheWidth;
99
- const isWithinTheHeight = (query, height) => {
100
- const [minHeight, maxHeight] = extractValues(query);
101
- if (maxHeight && height >= minHeight && height <= maxHeight) {
102
- return true;
103
- }
104
- return !maxHeight && height >= minHeight;
105
- };
106
-
107
- /**
108
- * Determines if the given screen size matches both the specified width and height ranges in the query.
109
- *
110
- * The function checks if the provided screen size (both width and height) falls within the ranges
111
- * specified by the query. The query can specify both width and height using a format like 'w[100,200]:h[300,400]'.
112
- *
113
- * @param {string} query - The combined width and height query string. Example: 'w[100,200]:h[300,400]'.
114
- * @param {ScreenSize} screenSize - The screen size to check against the query.
115
- * @returns {boolean} True if the screen size matches both the width and height ranges in the query, false otherwise.
116
- *
117
- * @example
118
- * const screenSize = { width: 150, height: 350 }
119
- * isWithinTheWidthAndHeight('w[100,200]:h[300,400]', screenSize) // returns true
120
- * isWithinTheWidthAndHeight('w[100,200]:h[400,500]', screenSize) // returns false
121
- */
122
- exports.isWithinTheHeight = isWithinTheHeight;
123
- const isWithinTheWidthAndHeight = (query, screenSize) => {
124
- const result = query.split(':').filter(Boolean).map(q => isWithinBreakpoint(q, screenSize)).filter(Boolean);
125
- return result.length === 2;
126
- };
127
-
128
- /**
129
- * Checks if the given query string is a valid custom media query.
130
- *
131
- * The valid custom media query formats include:
132
- * - :w[200]
133
- * - :w[0, 200]
134
- * - :w[, 300]
135
- * - :h[200]
136
- * - :h[0, 500]
137
- * - :h[,200]
138
- * - :w[100, 300]:h[200,500]
139
- * - :h[200,500]:w[100, 300]
140
- *
141
- * @param {string} query - The query string to be checked.
142
- * @returns {boolean} Returns `true` if the query is a valid custom media query, otherwise `false`.
143
- * @example
144
- *
145
- * isMediaQuery(':w[200]') // true
146
- * isMediaQuery(':w100]') // false
147
- */
148
- exports.isWithinTheWidthAndHeight = isWithinTheWidthAndHeight;
149
- const isMediaQuery = query => {
150
- const regex = /^(?:(:w\[\d*(?:,\s?\d+)?])?(:h\[\d*(?:,\s?\d+)?])?|(:h\[\d*(?:,\s?\d+)?])?(:w\[\d*(?:,\s?\d+)?])?)$/;
151
- return query.length > 0 && regex.test(query);
152
- };
153
-
154
- /**
155
- * Retrieves the first matching custom media query key based on the provided screen size.
156
- *
157
- * The function processes an array of media queries and returns the first query that matches
158
- * the given screen size. The media queries can be in formats like:
159
- * - w[200]
160
- * - w[0, 200]
161
- * - w[, 300]
162
- * - h[200]
163
- * - h[0, 500]
164
- * - h[,200]
165
- * - w[100, 300]:h[200,500]
166
- * - h[200,500]:w[100, 300]
167
- *
168
- * @param {Array<[string, string | number]>} mediaQueries - An array of tuples containing media query keys and associated values.
169
- * @param {ScreenSize} screenSize - An object representing the screen size to be checked against the media queries.
170
- * @returns {string | undefined} Returns the first matching media query key or `undefined` if no match is found.
171
- * @example
172
- *
173
- * const queries = [[':w[200]', 'value1'], [':h[300,500]', 'value2']]
174
- * const size = { width: 250, height: 400 }
175
- * getKeyForCustomMediaQuery(queries, size) // ':w[200]
176
- */
177
- exports.isMediaQuery = isMediaQuery;
178
- const getKeyForCustomMediaQuery = (mediaQueries, screenSize) => {
179
- const [matchedQuery] = mediaQueries.flatMap(_ref => {
180
- let [key] = _ref;
181
- if (key.includes('w') && key.includes('h')) {
182
- return isWithinBreakpoint(key, screenSize) ? key : undefined;
183
- }
184
- return key.split(':').filter(Boolean).map(query => isWithinBreakpoint(query, screenSize) ? key : undefined);
185
- }).filter(Boolean);
186
- return matchedQuery;
187
- };
188
- exports.getKeyForCustomMediaQuery = getKeyForCustomMediaQuery;
189
- //# sourceMappingURL=mediaQueries.js.map
@@ -1 +0,0 @@
1
- {"version":3,"names":["extractValues","codedValue","lh","rh","replace","split","Number","exports","isWithinBreakpoint","query","screenSize","includes","isWithinTheWidthAndHeight","charAt","isWithinTheWidth","width","isWithinTheHeight","height","minWidth","maxWidth","minHeight","maxHeight","result","filter","Boolean","map","q","length","isMediaQuery","regex","test","getKeyForCustomMediaQuery","mediaQueries","matchedQuery","flatMap","_ref","key","undefined"],"sourceRoot":"../../../src","sources":["utils/mediaQueries.ts"],"mappings":";;;;;;AAEA;AACA;AACA;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AAA;AAoBO,MAAMgB,YAAY,GAAInB,KAAa,IAAc;EACpD,MAAMoB,KAAK,GAAG,qGAAqG;EAEnH,OAAOpB,KAAK,CAACkB,MAAM,GAAG,CAAC,IAAIE,KAAK,CAACC,IAAI,CAACrB,KAAK,CAAC;AAChD,CAAC;;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAtBAF,OAAA,CAAAqB,YAAA,GAAAA,YAAA;AAuBO,MAAMG,yBAAyB,GAAGA,CAACC,YAA0D,EAAEtB,UAAsB,KAAyB;EACjJ,MAAM,CAACuB,YAAY,CAAC,GAAGD,YAAY,CAC9BE,OAAO,CAACC,IAAA,IAAW;IAAA,IAAV,CAACC,GAAG,CAAC,GAAAD,IAAA;IACX,IAAIC,GAAG,CAACzB,QAAQ,CAAC,GAAG,CAAC,IAAIyB,GAAG,CAACzB,QAAQ,CAAC,GAAG,CAAC,EAAE;MACxC,OAAOH,kBAAkB,CAAC4B,GAAG,EAAE1B,UAAU,CAAC,GAAG0B,GAAG,GAAGC,SAAS;IAChE;IAEA,OAAOD,GAAG,CACL/B,KAAK,CAAC,GAAG,CAAC,CACVkB,MAAM,CAACC,OAAO,CAAC,CACfC,GAAG,CAAChB,KAAK,IAAID,kBAAkB,CAACC,KAAK,EAAEC,UAAU,CAAC,GAAG0B,GAAG,GAAGC,SAAS,CAAC;EAC9E,CAAC,CAAC,CACDd,MAAM,CAACC,OAAO,CAAC;EAEpB,OAAOS,YAAY;AACvB,CAAC;AAAA1B,OAAA,CAAAwB,yBAAA,GAAAA,yBAAA"}
@@ -1,12 +0,0 @@
1
- import React, { createContext } from 'react';
2
- export const UnistylesContext = /*#__PURE__*/createContext({});
3
- export const UnistylesTheme = _ref => {
4
- let {
5
- theme,
6
- children
7
- } = _ref;
8
- return /*#__PURE__*/React.createElement(UnistylesContext.Provider, {
9
- value: theme
10
- }, children);
11
- };
12
- //# sourceMappingURL=UnistylesTheme.js.map
@@ -1 +0,0 @@
1
- {"version":3,"names":["React","createContext","UnistylesContext","UnistylesTheme","_ref","theme","children","createElement","Provider","value"],"sourceRoot":"../../src","sources":["UnistylesTheme.tsx"],"mappings":"AAAA,OAAOA,KAAK,IAAIC,aAAa,QAAQ,OAAO;AAO5C,OAAO,MAAMC,gBAAgB,gBAAGD,aAAa,CAAC,CAAC,CAAC,CAAC;AAEjD,OAAO,MAAME,cAA4D,GAAGC,IAAA;EAAA,IAAC;IACzEC,KAAK;IACLC;EACJ,CAAC,GAAAF,IAAA;EAAA,oBACGJ,KAAA,CAAAO,aAAA,CAACL,gBAAgB,CAACM,QAAQ;IAACC,KAAK,EAAEJ;EAAM,GACnCC,QACsB,CAAC;AAAA,CAC/B"}