react-native-unistyles 2.0.0-alpha.14 → 2.0.0-alpha.15

Sign up to get free protection for your applications and to get access to all the features.
Files changed (109) hide show
  1. package/cxx/UnistylesRuntime.h +6 -6
  2. package/lib/commonjs/common.js +15 -15
  3. package/lib/commonjs/common.js.map +1 -1
  4. package/lib/commonjs/core/UnistylesRuntime.js +1 -1
  5. package/lib/commonjs/core/UnistylesRuntime.js.map +1 -1
  6. package/lib/commonjs/createStyleSheet.js +1 -6
  7. package/lib/commonjs/createStyleSheet.js.map +1 -1
  8. package/lib/commonjs/hooks/index.js +7 -0
  9. package/lib/commonjs/hooks/index.js.map +1 -1
  10. package/lib/commonjs/hooks/useUnistyles.js +4 -7
  11. package/lib/commonjs/hooks/useUnistyles.js.map +1 -1
  12. package/lib/commonjs/hooks/useVariants.js +24 -0
  13. package/lib/commonjs/hooks/useVariants.js.map +1 -0
  14. package/lib/commonjs/plugins/normalizer/normalizer.js +1 -1
  15. package/lib/commonjs/plugins/normalizer/normalizer.js.map +1 -1
  16. package/lib/commonjs/types/stylesheet.js +6 -0
  17. package/lib/commonjs/types/stylesheet.js.map +1 -0
  18. package/lib/commonjs/useStyles.js +5 -4
  19. package/lib/commonjs/useStyles.js.map +1 -1
  20. package/lib/commonjs/utils/index.js +24 -0
  21. package/lib/commonjs/utils/index.js.map +1 -1
  22. package/lib/commonjs/utils/mq.js +42 -28
  23. package/lib/commonjs/utils/mq.js.map +1 -1
  24. package/lib/commonjs/utils/styles.js +14 -8
  25. package/lib/commonjs/utils/styles.js.map +1 -1
  26. package/lib/commonjs/utils/variants.js +33 -13
  27. package/lib/commonjs/utils/variants.js.map +1 -1
  28. package/lib/module/common.js +14 -14
  29. package/lib/module/common.js.map +1 -1
  30. package/lib/module/core/UnistylesRuntime.js +1 -1
  31. package/lib/module/core/UnistylesRuntime.js.map +1 -1
  32. package/lib/module/createStyleSheet.js +1 -6
  33. package/lib/module/createStyleSheet.js.map +1 -1
  34. package/lib/module/hooks/index.js +1 -0
  35. package/lib/module/hooks/index.js.map +1 -1
  36. package/lib/module/hooks/useUnistyles.js +5 -8
  37. package/lib/module/hooks/useUnistyles.js.map +1 -1
  38. package/lib/module/hooks/useVariants.js +17 -0
  39. package/lib/module/hooks/useVariants.js.map +1 -0
  40. package/lib/module/plugins/normalizer/normalizer.js +1 -1
  41. package/lib/module/plugins/normalizer/normalizer.js.map +1 -1
  42. package/lib/module/types/stylesheet.js +2 -0
  43. package/lib/module/types/stylesheet.js.map +1 -0
  44. package/lib/module/useStyles.js +6 -5
  45. package/lib/module/useStyles.js.map +1 -1
  46. package/lib/module/utils/index.js +1 -1
  47. package/lib/module/utils/index.js.map +1 -1
  48. package/lib/module/utils/mq.js +41 -27
  49. package/lib/module/utils/mq.js.map +1 -1
  50. package/lib/module/utils/styles.js +15 -9
  51. package/lib/module/utils/styles.js.map +1 -1
  52. package/lib/module/utils/variants.js +31 -11
  53. package/lib/module/utils/variants.js.map +1 -1
  54. package/lib/typescript/src/common.d.ts +10 -10
  55. package/lib/typescript/src/common.d.ts.map +1 -1
  56. package/lib/typescript/src/createStyleSheet.d.ts +2 -2
  57. package/lib/typescript/src/createStyleSheet.d.ts.map +1 -1
  58. package/lib/typescript/src/global.d.ts.map +1 -1
  59. package/lib/typescript/src/hooks/index.d.ts +1 -0
  60. package/lib/typescript/src/hooks/index.d.ts.map +1 -1
  61. package/lib/typescript/src/hooks/useUnistyles.d.ts.map +1 -1
  62. package/lib/typescript/src/hooks/useVariants.d.ts +3 -0
  63. package/lib/typescript/src/hooks/useVariants.d.ts.map +1 -0
  64. package/lib/typescript/src/plugins/normalizer/normalizer.d.ts.map +1 -1
  65. package/lib/typescript/src/types/breakpoints.d.ts +20 -14
  66. package/lib/typescript/src/types/breakpoints.d.ts.map +1 -1
  67. package/lib/typescript/src/types/core.d.ts +3 -26
  68. package/lib/typescript/src/types/core.d.ts.map +1 -1
  69. package/lib/typescript/src/types/index.d.ts +2 -1
  70. package/lib/typescript/src/types/index.d.ts.map +1 -1
  71. package/lib/typescript/src/types/mq.d.ts +2 -2
  72. package/lib/typescript/src/types/mq.d.ts.map +1 -1
  73. package/lib/typescript/src/types/stylesheet.d.ts +47 -0
  74. package/lib/typescript/src/types/stylesheet.d.ts.map +1 -0
  75. package/lib/typescript/src/types/unistyles.d.ts +5 -6
  76. package/lib/typescript/src/types/unistyles.d.ts.map +1 -1
  77. package/lib/typescript/src/types/variants.d.ts +13 -7
  78. package/lib/typescript/src/types/variants.d.ts.map +1 -1
  79. package/lib/typescript/src/useStyles.d.ts +3 -3
  80. package/lib/typescript/src/useStyles.d.ts.map +1 -1
  81. package/lib/typescript/src/utils/index.d.ts +1 -1
  82. package/lib/typescript/src/utils/index.d.ts.map +1 -1
  83. package/lib/typescript/src/utils/mq.d.ts +19 -13
  84. package/lib/typescript/src/utils/mq.d.ts.map +1 -1
  85. package/lib/typescript/src/utils/styles.d.ts +3 -3
  86. package/lib/typescript/src/utils/styles.d.ts.map +1 -1
  87. package/lib/typescript/src/utils/variants.d.ts +2 -2
  88. package/lib/typescript/src/utils/variants.d.ts.map +1 -1
  89. package/package.json +14 -14
  90. package/src/common.ts +10 -10
  91. package/src/core/UnistylesRuntime.ts +1 -1
  92. package/src/createStyleSheet.ts +2 -8
  93. package/src/global.ts +1 -0
  94. package/src/hooks/index.ts +1 -0
  95. package/src/hooks/useUnistyles.ts +5 -8
  96. package/src/hooks/useVariants.ts +23 -0
  97. package/src/plugins/normalizer/normalizer.ts +3 -3
  98. package/src/types/breakpoints.ts +47 -26
  99. package/src/types/core.ts +6 -42
  100. package/src/types/index.ts +1 -1
  101. package/src/types/mq.ts +2 -2
  102. package/src/types/stylesheet.ts +56 -0
  103. package/src/types/unistyles.ts +5 -6
  104. package/src/types/variants.ts +17 -9
  105. package/src/useStyles.ts +15 -15
  106. package/src/utils/index.ts +1 -1
  107. package/src/utils/mq.ts +56 -41
  108. package/src/utils/styles.ts +18 -17
  109. package/src/utils/variants.ts +41 -17
@@ -1,8 +1,8 @@
1
- import type { CustomNamedStyles, NestedStyle, RNStyle, UnistylesPlugin } from '../types'
1
+ import type { NestedStyle, UnistylesPlugin, StyleSheet, Optional } from '../types'
2
2
  import { getValueForBreakpoint } from './breakpoints'
3
3
  import type { UnistylesRuntime } from '../core'
4
4
  import { isAndroid, isIOS } from '../common'
5
- import { getStyleWithVariant } from './variants'
5
+ import { getStyleWithVariants } from './variants'
6
6
  import { withPlugins } from '../plugins'
7
7
 
8
8
  export const proxifyFunction = (
@@ -10,7 +10,7 @@ export const proxifyFunction = (
10
10
  fn: Function,
11
11
  plugins: Array<UnistylesPlugin>,
12
12
  runtime: UnistylesRuntime,
13
- variant?: string
13
+ variant?: Record<string, Optional<string>>
14
14
  ): Function => new Proxy(fn, {
15
15
  apply: (target, thisArg, argumentsList) =>
16
16
  parseStyle(key, target.apply(thisArg, argumentsList), plugins, runtime, variant)
@@ -24,41 +24,42 @@ export const isPlatformColor = <T extends {}>(value: T): boolean => {
24
24
  return isAndroid && 'resource_paths' in value && typeof value.resource_paths === 'object'
25
25
  }
26
26
 
27
- export const parseStyle = <T extends RNStyle>(
27
+ export const parseStyle = <T extends StyleSheet>(
28
28
  key: string,
29
- style: CustomNamedStyles<T>,
29
+ style: T,
30
30
  plugins: Array<UnistylesPlugin>,
31
31
  runtime: UnistylesRuntime,
32
- variant?: string
32
+ variant?: Record<string, Optional<string>>
33
33
  ): T => {
34
34
  const entries = Object
35
- .entries(getStyleWithVariant(style || {}, variant)) as Array<[keyof T, CustomNamedStyles<T> | NestedStyle]>
35
+ .entries(getStyleWithVariants(style || {}, variant)) as Array<[keyof T, StyleSheet]>
36
36
 
37
37
  const parsedStyles = Object
38
38
  .fromEntries(entries
39
39
  .map(([key, value]) => {
40
- const hasNestedProperties = key === 'shadowOffset' || key === 'textShadowOffset'
40
+ // dynamic functions
41
+ if (typeof value === 'function') {
42
+ return [key, value]
43
+ }
41
44
 
42
- if (hasNestedProperties) {
45
+ // nested objects
46
+ if (key === 'shadowOffset' || key === 'textShadowOffset') {
43
47
  return [
44
48
  key,
45
- parseStyle(key, value as CustomNamedStyles<T>, plugins, runtime, variant)
49
+ parseStyle(key, value, plugins, runtime, variant)
46
50
  ]
47
51
  }
48
52
 
49
- const isTransform = key === 'transform'
50
-
51
- if (isTransform && Array.isArray(value)) {
53
+ // transforms
54
+ if (key === 'transform' && Array.isArray(value)) {
52
55
  return [
53
56
  key,
54
57
  value.map(value => parseStyle(key, value, plugins, runtime, variant))
55
58
  ]
56
59
  }
57
60
 
58
- const isDynamicFunction = typeof value === 'function'
59
- const isValidStyle = typeof value !== 'object' || isPlatformColor(value)
60
-
61
- if (isDynamicFunction || isValidStyle) {
61
+ // values or platform colors
62
+ if (typeof value !== 'object' || isPlatformColor(value)) {
62
63
  return [key, value]
63
64
  }
64
65
 
@@ -1,37 +1,61 @@
1
- import type { Optional, CustomNamedStyles, NestedStyle } from '../types'
1
+ import type { Optional, StyleSheet, NestedStyle } from '../types'
2
2
 
3
- const getKeyForVariant = (value: Record<string, NestedStyle>, variant?: string): Optional<string> => {
4
- if (variant && variant in value) {
5
- return variant
6
- }
3
+ const getKeysForVariants = (
4
+ value: Record<string, NestedStyle>,
5
+ variants: Array<[string, Optional<string>]>
6
+ ): Array<[string, string]> => {
7
+ // case for no specified variants by user, we should fallback to 'default'
8
+ if (!variants.length) {
9
+ return Object
10
+ .entries(value)
11
+ .map(([key, value]) => {
12
+ if ('default' in value) {
13
+ return [key, 'default']
14
+ }
7
15
 
8
- if ('default' in value) {
9
- return 'default'
16
+ return undefined
17
+ })
18
+ .filter(Boolean) as Array<[string, string]>
10
19
  }
11
20
 
12
- return undefined
21
+ return variants
22
+ .map(([variantKey, variantValue]) => {
23
+ const variantStyle = value[variantKey]
24
+
25
+ if (variantStyle && variantValue && variantValue in variantStyle) {
26
+ return [variantKey, variantValue]
27
+ }
28
+
29
+ if (variantStyle && 'default' in variantStyle) {
30
+ return [variantKey, 'default']
31
+ }
32
+
33
+ return undefined
34
+ })
35
+ .filter(Boolean) as Array<[string, string]>
13
36
  }
14
37
 
15
- export const getStyleWithVariant = <T>(
16
- style: CustomNamedStyles<T>,
17
- variant?: string
38
+ export const getStyleWithVariants = (
39
+ style: StyleSheet,
40
+ variantValues?: Record<string, Optional<string>>
18
41
  ) => {
19
42
  if (!('variants' in style)) {
20
43
  return style
21
44
  }
22
45
 
23
- const variantKey = getKeyForVariant(
46
+ const keys = getKeysForVariants(
24
47
  style.variants as Record<string, NestedStyle>,
25
- variant
48
+ Object.entries(variantValues || {})
26
49
  )
27
- const variantValue = variantKey
28
- ? (style.variants as Record<string, NestedStyle>)[variantKey]
29
- : {}
50
+
51
+ const variantsValues = keys
52
+ .map(([key, nestedKey]) => ((style.variants as Record<string, Record<string, NestedStyle>>)[key] as Record<string, NestedStyle>)[nestedKey])
53
+ .reduce((acc, styles) => ({ ...acc, ...styles }), {})
30
54
 
31
55
  const { variants, ...otherStyles } = style
32
56
 
33
57
  return {
34
58
  ...otherStyles,
35
- ...variantValue
59
+ ...variantsValues
36
60
  }
37
61
  }