react-native-unistyles 2.0.0-alpha.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 (193) hide show
  1. package/README.md +4 -4
  2. package/ios/UnistylesModule.h +10 -0
  3. package/ios/UnistylesModule.mm +108 -0
  4. package/ios/UnistylesRuntime.h +30 -0
  5. package/ios/UnistylesRuntime.mm +191 -0
  6. package/lib/commonjs/UnistyleRegistry.js +37 -0
  7. package/lib/commonjs/UnistyleRegistry.js.map +1 -0
  8. package/lib/commonjs/Unistyles.js +36 -0
  9. package/lib/commonjs/Unistyles.js.map +1 -0
  10. package/lib/commonjs/UnistylesEngine.js +22 -0
  11. package/lib/commonjs/UnistylesEngine.js.map +1 -0
  12. package/lib/commonjs/UnistylesModule.js +9 -0
  13. package/lib/commonjs/UnistylesModule.js.map +1 -0
  14. package/lib/commonjs/UnistylesRuntime.js +52 -0
  15. package/lib/commonjs/UnistylesRuntime.js.map +1 -0
  16. package/lib/commonjs/createStyleSheet.js +14 -0
  17. package/lib/commonjs/createStyleSheet.js.map +1 -0
  18. package/lib/commonjs/global.js +2 -0
  19. package/lib/commonjs/global.js.map +1 -0
  20. package/lib/commonjs/hooks/useDimensions.web.js +3 -2
  21. package/lib/commonjs/hooks/useDimensions.web.js.map +1 -1
  22. package/lib/commonjs/index.js +33 -6
  23. package/lib/commonjs/index.js.map +1 -1
  24. package/lib/commonjs/types/cxx.js +23 -0
  25. package/lib/commonjs/types/cxx.js.map +1 -0
  26. package/lib/commonjs/types/index.js +26 -0
  27. package/lib/commonjs/types/index.js.map +1 -1
  28. package/lib/commonjs/types/normalizer.js +6 -0
  29. package/lib/commonjs/types/normalizer.js.map +1 -0
  30. package/lib/commonjs/useInitialTheme.js +13 -0
  31. package/lib/commonjs/useInitialTheme.js.map +1 -0
  32. package/lib/commonjs/useStyles.js +48 -0
  33. package/lib/commonjs/useStyles.js.map +1 -0
  34. package/lib/commonjs/useUnistyles.js +59 -0
  35. package/lib/commonjs/useUnistyles.js.map +1 -0
  36. package/lib/commonjs/utils/breakpoints.js +4 -2
  37. package/lib/commonjs/utils/breakpoints.js.map +1 -1
  38. package/lib/commonjs/utils/common.js +3 -3
  39. package/lib/commonjs/utils/common.js.map +1 -1
  40. package/lib/commonjs/utils/index.js +34 -0
  41. package/lib/commonjs/utils/index.js.map +1 -1
  42. package/lib/commonjs/utils/module.d.js +2 -0
  43. package/lib/commonjs/utils/module.d.js.map +1 -0
  44. package/lib/commonjs/utils/normalizeStyles.web.js +13 -13
  45. package/lib/commonjs/utils/normalizeStyles.web.js.map +1 -1
  46. package/lib/commonjs/utils/normalizer.js +89 -0
  47. package/lib/commonjs/utils/normalizer.js.map +1 -0
  48. package/lib/commonjs/utils/styles.js +7 -7
  49. package/lib/commonjs/utils/styles.js.map +1 -1
  50. package/lib/module/UnistyleRegistry.js +30 -0
  51. package/lib/module/UnistyleRegistry.js.map +1 -0
  52. package/lib/module/Unistyles.js +30 -0
  53. package/lib/module/Unistyles.js.map +1 -0
  54. package/lib/module/UnistylesEngine.js +15 -0
  55. package/lib/module/UnistylesEngine.js.map +1 -0
  56. package/lib/module/UnistylesModule.js +3 -0
  57. package/lib/module/UnistylesModule.js.map +1 -0
  58. package/lib/module/UnistylesRuntime.js +45 -0
  59. package/lib/module/UnistylesRuntime.js.map +1 -0
  60. package/lib/module/createStyleSheet.js +7 -0
  61. package/lib/module/createStyleSheet.js.map +1 -0
  62. package/lib/module/global.js +2 -0
  63. package/lib/module/global.js.map +1 -0
  64. package/lib/module/hooks/useDimensions.web.js +3 -2
  65. package/lib/module/hooks/useDimensions.web.js.map +1 -1
  66. package/lib/module/index.js +18 -2
  67. package/lib/module/index.js.map +1 -1
  68. package/lib/module/types/cxx.js +17 -0
  69. package/lib/module/types/cxx.js.map +1 -0
  70. package/lib/module/types/index.js +2 -1
  71. package/lib/module/types/index.js.map +1 -1
  72. package/lib/module/types/normalizer.js +2 -0
  73. package/lib/module/types/normalizer.js.map +1 -0
  74. package/lib/module/useInitialTheme.js +6 -0
  75. package/lib/module/useInitialTheme.js.map +1 -0
  76. package/lib/module/useStyles.js +42 -0
  77. package/lib/module/useStyles.js.map +1 -0
  78. package/lib/module/useUnistyles.js +52 -0
  79. package/lib/module/useUnistyles.js.map +1 -0
  80. package/lib/module/utils/breakpoints.js +4 -3
  81. package/lib/module/utils/breakpoints.js.map +1 -1
  82. package/lib/module/utils/common.js +2 -1
  83. package/lib/module/utils/common.js.map +1 -1
  84. package/lib/module/utils/index.js +2 -0
  85. package/lib/module/utils/index.js.map +1 -1
  86. package/lib/module/utils/module.d.js +2 -0
  87. package/lib/module/utils/module.d.js.map +1 -0
  88. package/lib/module/utils/normalizeStyles.web.js +13 -13
  89. package/lib/module/utils/normalizeStyles.web.js.map +1 -1
  90. package/lib/module/utils/normalizer.js +79 -0
  91. package/lib/module/utils/normalizer.js.map +1 -0
  92. package/lib/module/utils/styles.js +7 -8
  93. package/lib/module/utils/styles.js.map +1 -1
  94. package/lib/typescript/examples/expo/src/App.d.ts +3 -0
  95. package/lib/typescript/examples/expo/src/App.d.ts.map +1 -0
  96. package/lib/typescript/examples/expo/src/examples/Cxx.d.ts +3 -0
  97. package/lib/typescript/examples/expo/src/examples/Cxx.d.ts.map +1 -0
  98. package/lib/typescript/examples/expo/src/examples/index.d.ts +2 -0
  99. package/lib/typescript/examples/expo/src/examples/index.d.ts.map +1 -0
  100. package/lib/typescript/examples/expo/src/index.d.ts +2 -0
  101. package/lib/typescript/examples/expo/src/index.d.ts.map +1 -0
  102. package/lib/typescript/examples/expo/src/styles/breakpoints.d.ts +8 -0
  103. package/lib/typescript/examples/expo/src/styles/breakpoints.d.ts.map +1 -0
  104. package/lib/typescript/examples/expo/src/styles/index.d.ts +16 -0
  105. package/lib/typescript/examples/expo/src/styles/index.d.ts.map +1 -0
  106. package/lib/typescript/examples/expo/src/styles/theme.d.ts +40 -0
  107. package/lib/typescript/examples/expo/src/styles/theme.d.ts.map +1 -0
  108. package/lib/typescript/src/UnistyleRegistry.d.ts +15 -0
  109. package/lib/typescript/src/UnistyleRegistry.d.ts.map +1 -0
  110. package/lib/typescript/src/Unistyles.d.ts +16 -0
  111. package/lib/typescript/src/Unistyles.d.ts.map +1 -0
  112. package/lib/typescript/src/UnistylesEngine.d.ts +8 -0
  113. package/lib/typescript/src/UnistylesEngine.d.ts.map +1 -0
  114. package/lib/typescript/src/UnistylesModule.d.ts +6 -0
  115. package/lib/typescript/src/UnistylesModule.d.ts.map +1 -0
  116. package/lib/typescript/src/UnistylesRuntime.d.ts +56 -0
  117. package/lib/typescript/src/UnistylesRuntime.d.ts.map +1 -0
  118. package/lib/typescript/src/createStyleSheet.d.ts +6 -0
  119. package/lib/typescript/src/createStyleSheet.d.ts.map +1 -0
  120. package/lib/typescript/src/global.d.ts +5 -0
  121. package/lib/typescript/src/global.d.ts.map +1 -0
  122. package/lib/typescript/src/hooks/useDimensions.web.d.ts.map +1 -1
  123. package/lib/typescript/src/index.d.ts +14 -2
  124. package/lib/typescript/src/index.d.ts.map +1 -1
  125. package/lib/typescript/src/types/breakpoints.d.ts +6 -7
  126. package/lib/typescript/src/types/breakpoints.d.ts.map +1 -1
  127. package/lib/typescript/src/types/core.d.ts +4 -4
  128. package/lib/typescript/src/types/core.d.ts.map +1 -1
  129. package/lib/typescript/src/types/cxx.d.ts +50 -0
  130. package/lib/typescript/src/types/cxx.d.ts.map +1 -0
  131. package/lib/typescript/src/types/index.d.ts +4 -1
  132. package/lib/typescript/src/types/index.d.ts.map +1 -1
  133. package/lib/typescript/src/types/normalizer.d.ts +20 -0
  134. package/lib/typescript/src/types/normalizer.d.ts.map +1 -0
  135. package/lib/typescript/src/useInitialTheme.d.ts +3 -0
  136. package/lib/typescript/src/useInitialTheme.d.ts.map +1 -0
  137. package/lib/typescript/src/useStyles.d.ts +46 -0
  138. package/lib/typescript/src/useStyles.d.ts.map +1 -0
  139. package/lib/typescript/src/useUnistyles.d.ts +45 -0
  140. package/lib/typescript/src/useUnistyles.d.ts.map +1 -0
  141. package/lib/typescript/src/utils/breakpoints.d.ts +5 -4
  142. package/lib/typescript/src/utils/breakpoints.d.ts.map +1 -1
  143. package/lib/typescript/src/utils/common.d.ts +2 -1
  144. package/lib/typescript/src/utils/common.d.ts.map +1 -1
  145. package/lib/typescript/src/utils/index.d.ts +2 -0
  146. package/lib/typescript/src/utils/index.d.ts.map +1 -1
  147. package/lib/typescript/src/utils/normalizeStyles.web.d.ts +4 -1
  148. package/lib/typescript/src/utils/normalizeStyles.web.d.ts.map +1 -1
  149. package/lib/typescript/src/utils/normalizer.d.ts +11 -0
  150. package/lib/typescript/src/utils/normalizer.d.ts.map +1 -0
  151. package/lib/typescript/src/utils/styles.d.ts +4 -3
  152. package/lib/typescript/src/utils/styles.d.ts.map +1 -1
  153. package/package.json +13 -3
  154. package/react-native-unistyles.podspec +21 -0
  155. package/src/UnistyleRegistry.ts +39 -0
  156. package/src/Unistyles.ts +41 -0
  157. package/src/UnistylesEngine.ts +15 -0
  158. package/src/UnistylesModule.ts +7 -0
  159. package/src/UnistylesRuntime.ts +58 -0
  160. package/src/createStyleSheet.ts +12 -0
  161. package/src/global.ts +2 -0
  162. package/src/hooks/useDimensions.web.ts +3 -2
  163. package/src/index.ts +30 -2
  164. package/src/types/breakpoints.ts +9 -11
  165. package/src/types/core.ts +6 -6
  166. package/src/types/cxx.ts +61 -0
  167. package/src/types/index.ts +4 -3
  168. package/src/types/normalizer.ts +29 -0
  169. package/src/useInitialTheme.ts +7 -0
  170. package/src/useStyles.ts +49 -0
  171. package/src/useUnistyles.ts +67 -0
  172. package/src/utils/breakpoints.ts +16 -14
  173. package/src/utils/common.ts +2 -1
  174. package/src/utils/index.ts +2 -0
  175. package/src/utils/module.d.ts +3 -0
  176. package/src/utils/normalizeStyles.web.ts +21 -42
  177. package/src/utils/normalizer.ts +99 -0
  178. package/src/utils/styles.ts +17 -19
  179. package/lib/commonjs/UnistylesTheme.js +0 -21
  180. package/lib/commonjs/UnistylesTheme.js.map +0 -1
  181. package/lib/commonjs/createUnistyles.js +0 -57
  182. package/lib/commonjs/createUnistyles.js.map +0 -1
  183. package/lib/module/UnistylesTheme.js +0 -12
  184. package/lib/module/UnistylesTheme.js.map +0 -1
  185. package/lib/module/createUnistyles.js +0 -50
  186. package/lib/module/createUnistyles.js.map +0 -1
  187. package/lib/typescript/src/UnistylesTheme.d.ts +0 -9
  188. package/lib/typescript/src/UnistylesTheme.d.ts.map +0 -1
  189. package/lib/typescript/src/createUnistyles.d.ts +0 -10
  190. package/lib/typescript/src/createUnistyles.d.ts.map +0 -1
  191. package/src/UnistylesTheme.tsx +0 -17
  192. package/src/__tests__/createUnistyles.spec.tsx +0 -192
  193. package/src/createUnistyles.ts +0 -70
@@ -0,0 +1,61 @@
1
+ import type { UnistylesThemes, UnistylesBreakpoints } from '../global'
2
+
3
+ export type Nullable<T> = T | null
4
+ export enum UnistylesColorScheme {
5
+ System = 'system',
6
+ Manual = 'manual'
7
+ }
8
+
9
+ export type UnistylesConfig = {
10
+ colorScheme?: UnistylesColorScheme,
11
+ featureFlags?: Array<string>
12
+ }
13
+
14
+ export type UnistylesBridge = {
15
+ // getters
16
+ theme: keyof UnistylesThemes
17
+ breakpoint: keyof UnistylesBreakpoints,
18
+ colorScheme: UnistylesColorScheme,
19
+ sortedBreakpointPairs: Array<[keyof UnistylesBreakpoints, UnistylesBreakpoints[keyof UnistylesBreakpoints]]>
20
+
21
+ // setters
22
+ useBreakpoints(breakpoints: UnistylesBreakpoints): void,
23
+ useTheme(name: keyof UnistylesThemes): void,
24
+ useColorScheme(scheme: UnistylesColorScheme): void,
25
+ useFeatureFlags(flags: Array<string>): void
26
+ }
27
+
28
+ export enum CxxUnistylesEventTypes {
29
+ Theme = 'theme',
30
+ Size = 'size',
31
+ Breakpoint = 'breakpoint'
32
+ }
33
+
34
+ export type CxxUnistylesThemeEvent = {
35
+ type: CxxUnistylesEventTypes.Theme,
36
+ payload: {
37
+ currentTheme: keyof UnistylesThemes
38
+ }
39
+ }
40
+
41
+ export type CxxUnistylesSizeEvent = {
42
+ type: CxxUnistylesEventTypes.Size,
43
+ payload: {
44
+ width: number,
45
+ height: number
46
+ }
47
+ }
48
+
49
+ export type CxxUnistylesBreakpointEvent = {
50
+ type: CxxUnistylesEventTypes.Breakpoint,
51
+ payload: {
52
+ currentBreakpoint: keyof UnistylesBreakpoints
53
+ }
54
+ }
55
+
56
+ export type UnistylesEvents = CxxUnistylesThemeEvent | CxxUnistylesSizeEvent | CxxUnistylesBreakpointEvent
57
+
58
+ export enum UnistylesError {
59
+ RuntimeUnavailable = 'UNISTYLES_ERROR_RUNTIME_UNAVAILABLE',
60
+ ThemeNotFound = 'UNISTYLES_ERROR_THEME_NOT_FOUND'
61
+ }
@@ -1,9 +1,10 @@
1
+ export * from './normalizer'
2
+ export * from './cxx'
1
3
  export type { CustomNamedStyles } from './core'
4
+ export type { MediaQueries } from './mediaQueries'
2
5
  export type {
3
6
  ScreenSize,
4
- Breakpoints,
5
7
  CreateStylesFactory,
6
8
  ExtractBreakpoints,
7
- RemoveKeysWithPrefix,
8
- SortedBreakpointEntries
9
+ RemoveKeysWithPrefix
9
10
  } from './breakpoints'
@@ -0,0 +1,29 @@
1
+ import type { ShadowStyleIOS, TextStyle, TransformsStyle } from 'react-native'
2
+
3
+ type TransformArrayElement<T> = T extends Array<infer U> ? U : never
4
+ type BoxShadow = Required<ShadowStyleIOS>
5
+ type TextShadow = Required<Pick<TextStyle, 'textShadowColor' | 'textShadowOffset' | 'textShadowRadius'>>
6
+ type Transforms = Array<TransformArrayElement<TransformsStyle['transform']>>
7
+
8
+ type NormalizedBoxShadow = {
9
+ shadowColor: undefined,
10
+ shadowOffset: undefined,
11
+ shadowOpacity: undefined,
12
+ shadowRadius: undefined,
13
+ boxShadow?: string
14
+ }
15
+
16
+ type NormalizedTextShadow = {
17
+ textShadowColor: undefined
18
+ textShadowOffset: undefined
19
+ textShadowRadius: undefined,
20
+ textShadow?: string
21
+ }
22
+
23
+ export type {
24
+ BoxShadow,
25
+ TextShadow,
26
+ Transforms,
27
+ NormalizedBoxShadow,
28
+ NormalizedTextShadow
29
+ }
@@ -0,0 +1,7 @@
1
+ import { useRef } from 'react'
2
+ import { unistyles } from './Unistyles'
3
+ import type { UnistylesThemes } from './global'
4
+
5
+ export const useInitialTheme = (forName: keyof UnistylesThemes) => {
6
+ useRef(unistyles.runtime.setTheme(forName))
7
+ }
@@ -0,0 +1,49 @@
1
+ import { useMemo } from 'react'
2
+ import { StyleSheet } from 'react-native'
3
+ import { parseStyle, proxifyFunction } from './utils'
4
+ import type { CreateStylesFactory, CustomNamedStyles, ExtractBreakpoints, RemoveKeysWithPrefix } from './types'
5
+ import { useUnistyles } from './useUnistyles'
6
+ import type { UnistylesThemes } from './global'
7
+
8
+ // todo types
9
+ type T = UnistylesThemes[keyof UnistylesThemes]
10
+
11
+ export const useStyles = <ST extends CustomNamedStyles<ST>>(stylesheet?: ST | CreateStylesFactory<ST, T>) => {
12
+ const { theme, breakpoint, screenSize } = useUnistyles()
13
+
14
+ if (!stylesheet) {
15
+ return {
16
+ theme,
17
+ breakpoint,
18
+ styles: {} as ExtractBreakpoints<RemoveKeysWithPrefix<ST>>
19
+ }
20
+ }
21
+
22
+ const parsedStyles = useMemo(() => typeof stylesheet === 'function'
23
+ ? stylesheet(theme!)
24
+ : stylesheet, [theme, stylesheet])
25
+
26
+ const dynamicStyleSheet = useMemo(() => Object
27
+ .entries(parsedStyles)
28
+ .reduce((acc, [key, value]) => {
29
+ const style = value as CustomNamedStyles<ST>
30
+
31
+ if (typeof value === 'function') {
32
+ return {
33
+ ...acc,
34
+ [key]: proxifyFunction(value, breakpoint!, screenSize)
35
+ }
36
+ }
37
+
38
+ return StyleSheet.create({
39
+ ...acc,
40
+ [key]: parseStyle<ST>(style, breakpoint!, screenSize)
41
+ })
42
+ }, {} as ST), [breakpoint, screenSize, parsedStyles])
43
+
44
+ return {
45
+ theme,
46
+ breakpoint,
47
+ styles: dynamicStyleSheet as ExtractBreakpoints<RemoveKeysWithPrefix<ST>>
48
+ }
49
+ }
@@ -0,0 +1,67 @@
1
+ import { NativeEventEmitter, NativeModules } from 'react-native'
2
+ import { useEffect, useState } from 'react'
3
+ import type {
4
+ CxxUnistylesBreakpointEvent,
5
+ CxxUnistylesSizeEvent,
6
+ CxxUnistylesThemeEvent,
7
+ UnistylesEvents
8
+ } from './types'
9
+ import { CxxUnistylesEventTypes } from './types'
10
+ import { unistyles } from './Unistyles'
11
+
12
+ const unistylesEvents = new NativeEventEmitter(NativeModules.Unistyles)
13
+
14
+ export const useUnistyles = () => {
15
+ const [theme, setTheme] = useState(unistyles.runtime.getTheme(unistyles.runtime.theme))
16
+ const [breakpoint, setBreakpoint] = useState(unistyles.runtime.currentBreakpoint)
17
+ const [screenSize, setScreenSize] = useState({
18
+ width: 0,
19
+ height: 0
20
+ })
21
+
22
+ useEffect(() => {
23
+ const subscription = unistylesEvents.addListener(
24
+ 'onChange',
25
+ (event: UnistylesEvents) => {
26
+ switch (event.type) {
27
+ case CxxUnistylesEventTypes.Theme: {
28
+ const themeEvent = event as CxxUnistylesThemeEvent
29
+
30
+ setTheme(unistyles.runtime.getTheme(themeEvent.payload.currentTheme))
31
+
32
+ return
33
+ }
34
+ case CxxUnistylesEventTypes.Size: {
35
+ const sizeEvent = event as CxxUnistylesSizeEvent
36
+
37
+ setScreenSize({
38
+ width: sizeEvent.payload.width,
39
+ height: sizeEvent.payload.height
40
+ })
41
+ // todo
42
+ // setBreakpoint(unistyles.runtime.currentBreakpoint)
43
+
44
+ return
45
+ }
46
+ case CxxUnistylesEventTypes.Breakpoint: {
47
+ const breakpointEvent = event as CxxUnistylesBreakpointEvent
48
+
49
+ setBreakpoint(breakpointEvent.payload.currentBreakpoint)
50
+
51
+ return
52
+ }
53
+ default:
54
+ return
55
+ }
56
+ }
57
+ )
58
+
59
+ return subscription.remove
60
+ }, [])
61
+
62
+ return {
63
+ theme,
64
+ breakpoint,
65
+ screenSize
66
+ }
67
+ }
@@ -1,6 +1,8 @@
1
+ import { unistyles } from '../Unistyles'
1
2
  import { throwError } from './common'
2
- import type { Breakpoints, ScreenSize, SortedBreakpointEntries } from '../types'
3
+ import type { ScreenSize, MediaQueries } from '../types'
3
4
  import { getKeyForCustomMediaQuery, isMediaQuery } from './mediaQueries'
5
+ import type { UnistylesBreakpoints } from '../global'
4
6
 
5
7
  /**
6
8
  * Sorts the breakpoints object based on its numeric values in ascending order and validates them.
@@ -21,17 +23,17 @@ import { getKeyForCustomMediaQuery, isMediaQuery } from './mediaQueries'
21
23
  * const input = { md: 768, lg: 1024, sm: 0 }
22
24
  * sortAndValidateBreakpoints(input) // returns { sm: 0, md: 768, lg: 1024 }
23
25
  */
24
- export const sortAndValidateBreakpoints = <B extends Breakpoints>(breakpoints: B): B => {
26
+ export const sortAndValidateBreakpoints = (breakpoints: UnistylesBreakpoints): UnistylesBreakpoints => {
25
27
  const sortedPairs = Object
26
28
  .entries(breakpoints)
27
29
  .sort((breakpoint1, breakpoint2) => {
28
30
  const [, value1] = breakpoint1
29
31
  const [, value2] = breakpoint2
30
32
 
31
- return value1 - value2
33
+ return (value1 as number) - (value2 as number)
32
34
  })
33
35
 
34
- const sortedBreakpoints = Object.freeze(Object.fromEntries(sortedPairs)) as B
36
+ const sortedBreakpoints = Object.freeze(Object.fromEntries(sortedPairs)) as UnistylesBreakpoints
35
37
  const breakpointValues = Object.values(sortedBreakpoints)
36
38
  const [firstBreakpoint] = breakpointValues
37
39
 
@@ -61,7 +63,7 @@ export const sortAndValidateBreakpoints = <B extends Breakpoints>(breakpoints: B
61
63
  * const breakpoints = { sm: 0, md: 768, lg: 1024 }
62
64
  * getBreakpointFromScreenWidth(800, breakpoints) // returns 'md'
63
65
  */
64
- export const getBreakpointFromScreenWidth = <B extends Breakpoints>(width: number, breakpointEntries: SortedBreakpointEntries<B>): keyof B & string => {
66
+ export const getBreakpointFromScreenWidth = (width: number, breakpointEntries: Array<[keyof UnistylesBreakpoints, UnistylesBreakpoints[keyof UnistylesBreakpoints]]>): keyof UnistylesBreakpoints & string => {
65
67
  const [key] = breakpointEntries
66
68
  .find(([, value], index, otherBreakpoints) => {
67
69
  const minVal = value
@@ -72,7 +74,7 @@ export const getBreakpointFromScreenWidth = <B extends Breakpoints>(width: numbe
72
74
  }
73
75
 
74
76
  return width >= minVal && width < maxVal
75
- }) as [keyof B & string, number]
77
+ }) as [keyof UnistylesBreakpoints & string, number]
76
78
 
77
79
  return key
78
80
  }
@@ -101,24 +103,23 @@ export const getBreakpointFromScreenWidth = <B extends Breakpoints>(width: numbe
101
103
  *
102
104
  * getValueForBreakpoint(values, 'sm', screenSize, breakpoints); // 'value1'
103
105
  */
104
- export const getValueForBreakpoint = <B extends Breakpoints>(
105
- value: Record<keyof B & string, string | number | undefined>,
106
- breakpoint: keyof B & string,
107
- screenSize: ScreenSize,
108
- breakpointPairs: SortedBreakpointEntries<B>
106
+ export const getValueForBreakpoint = (
107
+ value: Record<keyof UnistylesBreakpoints | MediaQueries, string | number | undefined>,
108
+ breakpoint: keyof UnistylesBreakpoints,
109
+ screenSize: ScreenSize
109
110
  ): string | number | undefined => {
110
111
  // the highest priority is for custom media queries
111
112
  const customMediaQueries = Object
112
113
  .entries(value)
113
114
  .filter(([key]) => isMediaQuery(key))
114
- const customMediaQueryKey = getKeyForCustomMediaQuery(customMediaQueries, screenSize)
115
+ const customMediaQueryKey = getKeyForCustomMediaQuery(customMediaQueries, screenSize) as keyof typeof value
115
116
 
116
117
  if (customMediaQueryKey && customMediaQueryKey in value) {
117
118
  return value[customMediaQueryKey]
118
119
  }
119
120
 
120
121
  // if no custom media query, or didn't match, proceed with defined breakpoints
121
- const unifiedKey = breakpoint.toLowerCase()
122
+ const unifiedKey = breakpoint?.toLowerCase() as keyof typeof value
122
123
  const directBreakpoint = value[unifiedKey]
123
124
 
124
125
  // if there is a direct key like 'sm' or 'md', or value for this key exists but its undefined
@@ -127,6 +128,7 @@ export const getValueForBreakpoint = <B extends Breakpoints>(
127
128
  }
128
129
 
129
130
  // there is no direct hit for breakpoint nor media-query, so let's simulate CSS cascading
131
+ const breakpointPairs = unistyles.runtime.sortedBreakpoints
130
132
  const currentBreakpoint = breakpointPairs
131
133
  .findIndex(([key]) => key === unifiedKey)
132
134
 
@@ -135,6 +137,6 @@ export const getValueForBreakpoint = <B extends Breakpoints>(
135
137
  .map(([key]) => key)
136
138
 
137
139
  return breakpointPairs.length > 0
138
- ? value[availableBreakpoints[availableBreakpoints.length - 1] as keyof B & string]
140
+ ? value[availableBreakpoints[availableBreakpoints.length - 1] as keyof UnistylesBreakpoints & string]
139
141
  : undefined
140
142
  }
@@ -8,4 +8,5 @@ export const warn = (message: string) => {
8
8
  console.warn(`🦄 [react-native-unistyles]: ${message}`)
9
9
  }
10
10
 
11
- export const isWeb = () => Platform.OS === 'web'
11
+ export const isWeb = Platform.OS === 'web'
12
+ export const isServer = typeof window === 'undefined'
@@ -1,6 +1,8 @@
1
1
  export { normalizeStyles } from './normalizeStyles'
2
+ export * from './normalizer'
2
3
  export { getBreakpointFromScreenWidth, sortAndValidateBreakpoints, getValueForBreakpoint } from './breakpoints'
3
4
  export { proxifyFunction, parseStyle } from './styles'
5
+ export { isServer } from './common'
4
6
  export {
5
7
  extractValues,
6
8
  getKeyForCustomMediaQuery,
@@ -0,0 +1,3 @@
1
+ declare module '@react-native/normalize-colors' {
2
+ export default function normalizeColor(color: string): number | null
3
+ }
@@ -1,29 +1,8 @@
1
1
  import { warn } from './common'
2
+ import { preprocessor } from './normalizer'
3
+ import type { NormalizedBoxShadow, NormalizedTextShadow, BoxShadow, TextShadow, Transforms } from '../types'
2
4
 
3
- const preprocessor: Preprocessor = require('react-native-web/src/exports/StyleSheet/preprocess.js')
4
-
5
- type Preprocessor = {
6
- createTextShadowValue<T>(styles: any): T,
7
- createBoxShadowValue<T>(styles: any): T,
8
- createTransformValue<T>(transforms: any): T,
9
- }
10
-
11
- type NormalizedBoxShadow = {
12
- shadowColor: undefined,
13
- shadowOffset: undefined,
14
- shadowOpacity: undefined,
15
- shadowRadius: undefined,
16
- boxShadow?: string
17
- }
18
-
19
- type NormalizedTextShadow = {
20
- textShadowColor: undefined
21
- textShadowOffset: undefined
22
- textShadowRadius: undefined,
23
- textShadow?: string
24
- }
25
-
26
- const normalizeBoxShadow = <T extends {}>(styles: T): NormalizedBoxShadow => {
5
+ const normalizeBoxShadow = <T extends BoxShadow>(style: T): NormalizedBoxShadow => {
27
6
  const requiredBoxShadowProperties = [
28
7
  'shadowColor',
29
8
  'shadowOffset',
@@ -31,7 +10,7 @@ const normalizeBoxShadow = <T extends {}>(styles: T): NormalizedBoxShadow => {
31
10
  'shadowRadius'
32
11
  ]
33
12
 
34
- if (!requiredBoxShadowProperties.every(prop => prop in styles)) {
13
+ if (!requiredBoxShadowProperties.every(prop => prop in style)) {
35
14
  warn(`can't apply box shadow as you miss at least one of these properties: ${requiredBoxShadowProperties.join(', ')}`)
36
15
 
37
16
  return {
@@ -43,7 +22,7 @@ const normalizeBoxShadow = <T extends {}>(styles: T): NormalizedBoxShadow => {
43
22
  }
44
23
 
45
24
  return {
46
- boxShadow: preprocessor.createBoxShadowValue(styles),
25
+ boxShadow: preprocessor.createBoxShadowValue(style),
47
26
  shadowColor: undefined,
48
27
  shadowOffset: undefined,
49
28
  shadowOpacity: undefined,
@@ -51,14 +30,14 @@ const normalizeBoxShadow = <T extends {}>(styles: T): NormalizedBoxShadow => {
51
30
  }
52
31
  }
53
32
 
54
- const normalizeTextShadow = <T extends {}>(styles: T): NormalizedTextShadow => {
33
+ const normalizeTextShadow = <T extends TextShadow>(style: T): NormalizedTextShadow => {
55
34
  const requiredTextShadowProperties = [
56
35
  'textShadowColor',
57
36
  'textShadowOffset',
58
37
  'textShadowRadius'
59
38
  ]
60
39
 
61
- if (!requiredTextShadowProperties.every(prop => prop in styles)) {
40
+ if (!requiredTextShadowProperties.every(prop => prop in style)) {
62
41
  warn(`can't apply text shadow as you miss at least one of these properties: ${requiredTextShadowProperties.join(', ')}`)
63
42
 
64
43
  return {
@@ -69,33 +48,33 @@ const normalizeTextShadow = <T extends {}>(styles: T): NormalizedTextShadow => {
69
48
  }
70
49
 
71
50
  return {
72
- textShadow: preprocessor.createTextShadowValue(styles),
51
+ textShadow: preprocessor.createTextShadowValue(style),
73
52
  textShadowColor: undefined,
74
53
  textShadowOffset: undefined,
75
54
  textShadowRadius: undefined
76
55
  }
77
56
  }
78
57
 
79
- export const normalizeStyles = <T extends {}>(styles: T): T => {
80
- const normalizedTransform = ('transform' in styles && Array.isArray(styles.transform))
81
- ? { transform: preprocessor.createTransformValue(styles.transform) }
58
+ export const normalizeStyles = <T extends BoxShadow | TextShadow | { transform: Transforms }>(style: T): T => {
59
+ const normalizedTransform = ('transform' in style && Array.isArray(style.transform))
60
+ ? { transform: preprocessor.createTransformValue(style.transform) }
82
61
  : {}
83
62
 
84
63
  const normalizedBoxShadow = (
85
- 'shadowColor' in styles ||
86
- 'shadowOffset' in styles ||
87
- 'shadowOpacity' in styles ||
88
- 'shadowRadius' in styles
89
- ) ? normalizeBoxShadow(styles) : {}
64
+ 'shadowColor' in style ||
65
+ 'shadowOffset' in style ||
66
+ 'shadowOpacity' in style ||
67
+ 'shadowRadius' in style
68
+ ) ? normalizeBoxShadow(style as BoxShadow) : {}
90
69
 
91
70
  const normalizedTextShadow = (
92
- 'textShadowColor' in styles ||
93
- 'textShadowOffset' in styles ||
94
- 'textShadowRadius' in styles
95
- ) ? normalizeTextShadow(styles) : {}
71
+ 'textShadowColor' in style ||
72
+ 'textShadowOffset' in style ||
73
+ 'textShadowRadius' in style
74
+ ) ? normalizeTextShadow(style as TextShadow) : {}
96
75
 
97
76
  return {
98
- ...styles,
77
+ ...style,
99
78
  ...normalizedTransform,
100
79
  ...normalizedBoxShadow,
101
80
  ...normalizedTextShadow
@@ -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,7 +1,8 @@
1
- import type { Breakpoints, CustomNamedStyles, ScreenSize, SortedBreakpointEntries } from '../types'
1
+ import type { CustomNamedStyles, ScreenSize } from '../types'
2
2
  import { getValueForBreakpoint } from './breakpoints'
3
3
  import { normalizeStyles } from './normalizeStyles'
4
4
  import { isWeb } from './common'
5
+ import type { UnistylesBreakpoints } from '../global'
5
6
 
6
7
  /**
7
8
  * Proxies a function to parse its return value for custom media queries or breakpoints.
@@ -24,13 +25,12 @@ import { isWeb } from './common'
24
25
  * const proxifiedFunction = proxifyFunction(myFunction, 'sm', screenSize, breakpoints)
25
26
  * proxifiedFunction() // parsed style based on screenSize and breakpoints
26
27
  */
27
- export const proxifyFunction = <B extends Breakpoints>(
28
- fn: Function, breakpoint: keyof B & string,
29
- screenSize: ScreenSize,
30
- breakpointPairs: SortedBreakpointEntries<B>
28
+ export const proxifyFunction = (
29
+ fn: Function, breakpoint: keyof UnistylesBreakpoints & string,
30
+ screenSize: ScreenSize
31
31
  ): Function => new Proxy(fn, {
32
32
  apply: (target, thisArg, argumentsList) =>
33
- parseStyle(target.apply(thisArg, argumentsList), breakpoint, screenSize, breakpointPairs)
33
+ parseStyle(target.apply(thisArg, argumentsList), breakpoint, screenSize)
34
34
  })
35
35
 
36
36
  /**
@@ -58,15 +58,14 @@ export const proxifyFunction = <B extends Breakpoints>(
58
58
  * const parsedStyle = parseStyle(style, 'sm', screenSize, breakpoints)
59
59
  * // { fontSize: '12px' }
60
60
  */
61
- export const parseStyle = <T, B extends Breakpoints>(
62
- style: CustomNamedStyles<T, B>,
63
- breakpoint: keyof B & string,
64
- screenSize: ScreenSize,
65
- breakpointPairs: SortedBreakpointEntries<B>
61
+ export const parseStyle = <T>(
62
+ style: CustomNamedStyles<T>,
63
+ breakpoint: keyof UnistylesBreakpoints & string,
64
+ screenSize: ScreenSize
66
65
  ): T => {
67
66
  const entries = Object.entries(style) as [[
68
67
  keyof T,
69
- CustomNamedStyles<T, B> | Record<keyof B & string, string | number | undefined>]
68
+ CustomNamedStyles<T> | Record<keyof UnistylesBreakpoints & string, string | number | undefined>]
70
69
  ]
71
70
 
72
71
  const parsedStyles = Object
@@ -77,7 +76,7 @@ export const parseStyle = <T, B extends Breakpoints>(
77
76
  if (hasNestedProperties) {
78
77
  return [
79
78
  key,
80
- parseStyle(value as CustomNamedStyles<T, B>, breakpoint, screenSize, breakpointPairs)
79
+ parseStyle(value as CustomNamedStyles<T>, breakpoint, screenSize)
81
80
  ]
82
81
  }
83
82
 
@@ -86,7 +85,7 @@ export const parseStyle = <T, B extends Breakpoints>(
86
85
  if (isTransform && Array.isArray(value)) {
87
86
  return [
88
87
  key,
89
- value.map(value => parseStyle(value, breakpoint, screenSize, breakpointPairs))
88
+ value.map(value => parseStyle(value, breakpoint, screenSize))
90
89
  ]
91
90
  }
92
91
 
@@ -99,17 +98,16 @@ export const parseStyle = <T, B extends Breakpoints>(
99
98
 
100
99
  return [
101
100
  key,
102
- getValueForBreakpoint<B>(
103
- value as Record<keyof B & string, string | number | undefined>,
101
+ getValueForBreakpoint(
102
+ value as Record<keyof UnistylesBreakpoints & string, string | number | undefined>,
104
103
  breakpoint,
105
- screenSize,
106
- breakpointPairs
104
+ screenSize
107
105
  )
108
106
  ]
109
107
  })
110
108
  )
111
109
 
112
- return isWeb()
110
+ return isWeb
113
111
  ? normalizeStyles(parsedStyles)
114
112
  : parsedStyles
115
113
  }
@@ -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