react-native-unistyles 3.0.0-alpha.13 → 3.0.0-alpha.14

Sign up to get free protection for your applications and to get access to all the features.
Files changed (129) hide show
  1. package/cxx/core/UnistyleWrapper.h +1 -1
  2. package/cxx/core/UnistylesCommitHook.cpp +5 -2
  3. package/cxx/hybridObjects/HybridStyleSheet.cpp +1 -3
  4. package/cxx/hybridObjects/HybridStyleSheet.h +1 -1
  5. package/lib/commonjs/index.js +4 -0
  6. package/lib/commonjs/index.js.map +1 -1
  7. package/lib/commonjs/specs/ShadowRegistry/index.js +7 -1
  8. package/lib/commonjs/specs/ShadowRegistry/index.js.map +1 -1
  9. package/lib/commonjs/web/convert/index.js +1 -1
  10. package/lib/commonjs/web/convert/index.js.map +1 -1
  11. package/lib/commonjs/web/create.js +25 -63
  12. package/lib/commonjs/web/create.js.map +1 -1
  13. package/lib/commonjs/web/createUnistylesComponent.js +61 -0
  14. package/lib/commonjs/web/createUnistylesComponent.js.map +1 -0
  15. package/lib/commonjs/web/index.js +12 -3
  16. package/lib/commonjs/web/index.js.map +1 -1
  17. package/lib/commonjs/web/listener/index.js +7 -0
  18. package/lib/commonjs/web/listener/index.js.map +1 -1
  19. package/lib/commonjs/web/listener/listenToDependencies.js +39 -0
  20. package/lib/commonjs/web/listener/listenToDependencies.js.map +1 -0
  21. package/lib/commonjs/web/listener/listener.js +1 -2
  22. package/lib/commonjs/web/listener/listener.js.map +1 -1
  23. package/lib/commonjs/web/mock.js +1 -10
  24. package/lib/commonjs/web/mock.js.map +1 -1
  25. package/lib/commonjs/web/pseudo.js +2 -3
  26. package/lib/commonjs/web/pseudo.js.map +1 -1
  27. package/lib/commonjs/web/registry.js +2 -4
  28. package/lib/commonjs/web/registry.js.map +1 -1
  29. package/lib/commonjs/web/runtime.js +0 -1
  30. package/lib/commonjs/web/runtime.js.map +1 -1
  31. package/lib/commonjs/web/shadowRegistry.js +120 -0
  32. package/lib/commonjs/web/shadowRegistry.js.map +1 -0
  33. package/lib/commonjs/web/state.js +0 -40
  34. package/lib/commonjs/web/state.js.map +1 -1
  35. package/lib/commonjs/web/utils.js +66 -14
  36. package/lib/commonjs/web/utils.js.map +1 -1
  37. package/lib/commonjs/web/variants/useVariants.js +1 -34
  38. package/lib/commonjs/web/variants/useVariants.js.map +1 -1
  39. package/lib/module/index.js +4 -0
  40. package/lib/module/index.js.map +1 -1
  41. package/lib/module/specs/ShadowRegistry/index.js +7 -1
  42. package/lib/module/specs/ShadowRegistry/index.js.map +1 -1
  43. package/lib/module/web/convert/index.js +1 -1
  44. package/lib/module/web/convert/index.js.map +1 -1
  45. package/lib/module/web/create.js +26 -64
  46. package/lib/module/web/create.js.map +1 -1
  47. package/lib/module/web/createUnistylesComponent.js +54 -0
  48. package/lib/module/web/createUnistylesComponent.js.map +1 -0
  49. package/lib/module/web/index.js +4 -2
  50. package/lib/module/web/index.js.map +1 -1
  51. package/lib/module/web/listener/index.js +1 -0
  52. package/lib/module/web/listener/index.js.map +1 -1
  53. package/lib/module/web/listener/listenToDependencies.js +34 -0
  54. package/lib/module/web/listener/listenToDependencies.js.map +1 -0
  55. package/lib/module/web/listener/listener.js +1 -1
  56. package/lib/module/web/listener/listener.js.map +1 -1
  57. package/lib/module/web/mock.js +0 -9
  58. package/lib/module/web/mock.js.map +1 -1
  59. package/lib/module/web/pseudo.js +2 -3
  60. package/lib/module/web/pseudo.js.map +1 -1
  61. package/lib/module/web/registry.js +2 -4
  62. package/lib/module/web/registry.js.map +1 -1
  63. package/lib/module/web/runtime.js +0 -1
  64. package/lib/module/web/runtime.js.map +1 -1
  65. package/lib/module/web/shadowRegistry.js +116 -0
  66. package/lib/module/web/shadowRegistry.js.map +1 -0
  67. package/lib/module/web/state.js +0 -40
  68. package/lib/module/web/state.js.map +1 -1
  69. package/lib/module/web/utils.js +59 -12
  70. package/lib/module/web/utils.js.map +1 -1
  71. package/lib/module/web/variants/useVariants.js +1 -34
  72. package/lib/module/web/variants/useVariants.js.map +1 -1
  73. package/lib/typescript/src/specs/ShadowRegistry/index.d.ts.map +1 -1
  74. package/lib/typescript/src/web/create.d.ts.map +1 -1
  75. package/lib/typescript/src/web/createUnistylesComponent.d.ts +3 -0
  76. package/lib/typescript/src/web/createUnistylesComponent.d.ts.map +1 -0
  77. package/lib/typescript/src/web/index.d.ts +2 -1
  78. package/lib/typescript/src/web/index.d.ts.map +1 -1
  79. package/lib/typescript/src/web/listener/index.d.ts +1 -0
  80. package/lib/typescript/src/web/listener/index.d.ts.map +1 -1
  81. package/lib/typescript/src/web/listener/listenToDependencies.d.ts +12 -0
  82. package/lib/typescript/src/web/listener/listenToDependencies.d.ts.map +1 -0
  83. package/lib/typescript/src/web/listener/listener.d.ts +2 -1
  84. package/lib/typescript/src/web/listener/listener.d.ts.map +1 -1
  85. package/lib/typescript/src/web/mock.d.ts +0 -9
  86. package/lib/typescript/src/web/mock.d.ts.map +1 -1
  87. package/lib/typescript/src/web/pseudo.d.ts +4 -2
  88. package/lib/typescript/src/web/pseudo.d.ts.map +1 -1
  89. package/lib/typescript/src/web/registry.d.ts +1 -1
  90. package/lib/typescript/src/web/registry.d.ts.map +1 -1
  91. package/lib/typescript/src/web/runtime.d.ts +0 -1
  92. package/lib/typescript/src/web/runtime.d.ts.map +1 -1
  93. package/lib/typescript/src/web/shadowRegistry.d.ts +17 -0
  94. package/lib/typescript/src/web/shadowRegistry.d.ts.map +1 -0
  95. package/lib/typescript/src/web/state.d.ts +0 -5
  96. package/lib/typescript/src/web/state.d.ts.map +1 -1
  97. package/lib/typescript/src/web/utils.d.ts +22 -7
  98. package/lib/typescript/src/web/utils.d.ts.map +1 -1
  99. package/lib/typescript/src/web/variants/useVariants.d.ts.map +1 -1
  100. package/nitrogen/generated/android/c++/JHybridNativePlatformSpec.hpp +6 -3
  101. package/nitrogen/generated/android/kotlin/com/margelo/nitro/unistyles/HybridNativePlatformSpec.kt +8 -3
  102. package/nitrogen/generated/ios/c++/HybridNativePlatformSpecSwift.hpp +1 -1
  103. package/nitrogen/generated/ios/swift/HybridNativePlatformSpec.swift +1 -1
  104. package/nitrogen/generated/ios/swift/HybridNativePlatformSpecCxx.swift +14 -2
  105. package/nitrogen/generated/shared/c++/HybridNativePlatformSpec.hpp +3 -0
  106. package/nitrogen/generated/shared/c++/HybridUnistylesNavigationBarSpec.hpp +3 -0
  107. package/nitrogen/generated/shared/c++/HybridUnistylesRuntimeSpec.hpp +3 -0
  108. package/nitrogen/generated/shared/c++/HybridUnistylesShadowRegistrySpec.hpp +3 -0
  109. package/nitrogen/generated/shared/c++/HybridUnistylesStatusBarSpec.hpp +3 -0
  110. package/nitrogen/generated/shared/c++/HybridUnistylesStyleSheetSpec.cpp +1 -1
  111. package/nitrogen/generated/shared/c++/HybridUnistylesStyleSheetSpec.hpp +4 -1
  112. package/package.json +3 -3
  113. package/src/index.ts +3 -0
  114. package/src/specs/ShadowRegistry/index.ts +7 -1
  115. package/src/web/convert/index.ts +1 -1
  116. package/src/web/create.ts +24 -79
  117. package/src/web/createUnistylesComponent.tsx +56 -0
  118. package/src/web/index.ts +3 -2
  119. package/src/web/listener/index.ts +1 -0
  120. package/src/web/listener/listenToDependencies.ts +50 -0
  121. package/src/web/listener/listener.ts +1 -1
  122. package/src/web/mock.ts +0 -10
  123. package/src/web/pseudo.ts +131 -5
  124. package/src/web/registry.ts +3 -5
  125. package/src/web/runtime.ts +0 -2
  126. package/src/web/shadowRegistry.ts +121 -0
  127. package/src/web/state.ts +0 -44
  128. package/src/web/utils.ts +90 -16
  129. package/src/web/variants/useVariants.ts +1 -37
package/src/web/state.ts CHANGED
@@ -1,4 +1,3 @@
1
- import { type ReactElement, createElement, createRef } from 'react'
2
1
  import type { UnistylesTheme } from '../types'
3
2
  import type { UnistylesConfig } from '../specs/StyleSheet'
4
3
  import type { AppBreakpoint, AppThemeName } from '../specs/types'
@@ -10,7 +9,6 @@ import { UnistyleDependency } from '../specs/NativePlatform'
10
9
 
11
10
  class UnistylesStateBuilder {
12
11
  private readonly isSSR = isServer()
13
- readonly tags = [] as Array<ReactElement>
14
12
 
15
13
  themes = new Map<string, UnistylesTheme>()
16
14
  themeName?: AppThemeName
@@ -101,48 +99,6 @@ class UnistylesStateBuilder {
101
99
  })
102
100
  })
103
101
  }
104
-
105
- createTag() {
106
- if (!this.isSSR) {
107
- const tag = document.createElement('style')
108
-
109
- document.head.appendChild(tag)
110
-
111
- return tag
112
- }
113
-
114
- const tagRef = new Proxy(createRef<HTMLStyleElement>(), {
115
- set: (target, prop, value) => {
116
- // When ref is assigned
117
- if ('textContent' in value) {
118
- value.textContent = tag.textContent
119
- }
120
-
121
- return Reflect.set(target, prop, value)
122
- }
123
- })
124
- const tagElement = createElement('style', { ref: tagRef, key: this.tags.length }, '')
125
- const tag = new Proxy({ textContent: '' }, {
126
- set: (target, prop, value) => {
127
- if (prop !== 'textContent') {
128
- return false
129
- }
130
-
131
- // When css is updated
132
- target.textContent = value
133
-
134
- if (tagRef.current) {
135
- tagRef.current.textContent = value
136
- }
137
-
138
- return true
139
- }
140
- })
141
-
142
- this.tags.push(tagElement)
143
-
144
- return tag
145
- }
146
102
  }
147
103
 
148
104
  export const UnistylesState = new UnistylesStateBuilder()
package/src/web/utils.ts CHANGED
@@ -1,26 +1,11 @@
1
1
  import { ColorScheme, type AppThemeName} from '../specs/types'
2
- import type { UnistylesValues } from '../types'
2
+ import type { StyleSheet, StyleSheetWithSuperPowers, UnistylesValues } from '../types/stylesheet'
3
3
 
4
4
  export const reduceObject = <TObj extends Record<string, any>, TReducer>(
5
5
  obj: TObj,
6
6
  reducer: (value: TObj[keyof TObj], key: keyof TObj) => TReducer,
7
7
  ) => Object.fromEntries(Object.entries(obj).map(([key, value]) => [key, reducer(value as TObj[keyof TObj], key)])) as { [K in keyof TObj]: TReducer }
8
8
 
9
- export const toReactNativeClassName = (className: string | null, values: UnistylesValues) => {
10
- const returnValue = className ? {
11
- $$css: true,
12
- [className]: className
13
- } : {}
14
-
15
- Object.defineProperties(returnValue, reduceObject(values, value => ({
16
- value,
17
- enumerable: false,
18
- configurable: true
19
- })))
20
-
21
- return returnValue
22
- }
23
-
24
9
  export const keyInObject = <T extends Record<string, any>>(obj: T, key: PropertyKey): key is keyof T => key in obj
25
10
 
26
11
  export const schemeToTheme = (scheme: ColorScheme): AppThemeName => {
@@ -93,3 +78,92 @@ export const equal = <T>(a: T, b: T) => {
93
78
 
94
79
  return keysA.every(key => Object.is(a[key], b[key]) && Object.prototype.hasOwnProperty.call(b, key))
95
80
  }
81
+
82
+ type UnistyleSecrets = {
83
+ __uni__stylesheet: StyleSheetWithSuperPowers<StyleSheet>,
84
+ __uni__key: string,
85
+ __uni__refs: Set<HTMLElement>
86
+ __uni__variants: Record<string, any>
87
+ __uni__args?: Array<any>
88
+ }
89
+
90
+ export const assignSecrets = <T>(object: T, secrets: UnistyleSecrets) => {
91
+ // @ts-expect-error - assign secrets to object
92
+ object[`__uni__secrets__${Math.random().toString(16).slice(2)}`] = secrets
93
+
94
+ return object
95
+ }
96
+
97
+ export const extractSecrets = (object: any) => {
98
+ const secrets = Object.entries(object).reduce((acc, [key, value]) => {
99
+ if (key.startsWith('__uni__secrets__')) {
100
+ acc.push(value as UnistyleSecrets)
101
+ }
102
+
103
+ return acc
104
+ }, [] as Array<UnistyleSecrets>)
105
+
106
+ return secrets
107
+ }
108
+
109
+ export const getStyles = (values: UnistylesValues) => {
110
+ const returnValue = {}
111
+
112
+ Object.defineProperties(returnValue, reduceObject(values, value => ({
113
+ value,
114
+ enumerable: false,
115
+ configurable: true
116
+ })))
117
+
118
+ return returnValue
119
+ }
120
+
121
+ export const createDoubleMap = <TKey, TSecondKey, TValue>() => {
122
+ const map = new Map<TKey, Map<TSecondKey, TValue>>()
123
+
124
+ return {
125
+ get: (key: TKey, secondKey: TSecondKey) => {
126
+ const mapForKey = map.get(key)
127
+
128
+ if (!mapForKey) {
129
+ return undefined
130
+ }
131
+
132
+ return mapForKey.get(secondKey)
133
+ },
134
+ set: (key: TKey, secondKey: TSecondKey, value: TValue) => {
135
+ const mapForKey = map.get(key) ?? new Map<TSecondKey, TValue>()
136
+
137
+ map.set(key, mapForKey)
138
+ mapForKey.set(secondKey, value)
139
+ },
140
+ delete: (key: TKey, secondKey: TSecondKey) => {
141
+ const mapForKey = map.get(key)
142
+
143
+ if (!mapForKey) {
144
+ return
145
+ }
146
+
147
+ mapForKey.delete(secondKey)
148
+ },
149
+ forEach: (callback: (key: TKey, secondKey: TSecondKey, value: TValue) => void) => {
150
+ map.forEach((mapForKey, key) => {
151
+ mapForKey.forEach((value, secondKey) => {
152
+ callback(key, secondKey, value)
153
+ })
154
+ })
155
+ }
156
+ }
157
+ }
158
+
159
+ export const extractHiddenProperties = (object: any) => {
160
+ const hiddenProperties = Object.getOwnPropertyNames(object)
161
+
162
+ return Object.fromEntries(
163
+ hiddenProperties
164
+ .filter(key => !key.startsWith('__uni__'))
165
+ .map(key => [key, object[key]])
166
+ )
167
+ }
168
+
169
+ export const isInDocument = (element: HTMLElement) => document.body.contains(element)
@@ -1,14 +1,10 @@
1
1
  import { useMemo, useRef, useState } from 'react'
2
- import type { TypeStyle } from 'typestyle'
3
2
  import type { ReactNativeStyleSheet, StyleSheet } from '../../types'
4
3
  import { equal, reduceObject } from '../utils'
5
- import { UnistylesRegistry } from '../registry'
6
4
  import { getVariants } from './getVariants'
7
5
 
8
6
  export const createUseVariants = (styles: ReactNativeStyleSheet<StyleSheet>, setSelectedVariants: (variants: Record<string, any>) => void) => {
9
7
  const useVariants = (selectedVariants: Record<string, any>) => {
10
- const [unistylesMap] = useState(() => new Map<string, TypeStyle>())
11
- const [classNameMap] = useState(() => new Map<string, string>())
12
8
  const [selectedVariantStylesMap] = useState(() => new Map<string, Record<string, any>>())
13
9
  const lastSelectedVariantsRef = useRef<Record<string, any>>()
14
10
  // Variable that determines if variants have changed and we need to recalculate styles
@@ -29,47 +25,15 @@ export const createUseVariants = (styles: ReactNativeStyleSheet<StyleSheet>, set
29
25
  return result
30
26
  }, [lastSelectedVariantsRef.current])
31
27
 
32
- if (unistylesMap.size === 0 && combinedVariantStyles.length > 0) {
33
- combinedVariantStyles.forEach(([key, value]) => {
34
- const { className, unistyles } = UnistylesRegistry.createStyles(value, `variant-${key}`)
35
-
36
- unistylesMap.set(key, unistyles)
37
- classNameMap.set(key, className)
38
- })
39
- }
40
-
41
- combinedVariantStyles.forEach(([key, value]) => {
28
+ combinedVariantStyles.forEach(([key]) => {
42
29
  const styleEntry = styles[key]
43
- const unistyles = unistylesMap.get(key)
44
- const className = classNameMap.get(key)
45
30
  const selectedVariantStyles = selectedVariantStylesMap.get(key)
46
31
 
47
- if (!unistyles || !className) {
48
- return
49
- }
50
-
51
- if (variantsChanged) {
52
- UnistylesRegistry.updateStyles(unistyles, value, className)
53
- }
54
-
55
32
  Object.defineProperties(styleEntry, reduceObject(selectedVariantStyles ?? {}, value => ({
56
33
  value,
57
34
  enumerable: false,
58
35
  configurable: true
59
36
  })))
60
-
61
- if (styleEntry) {
62
- Object.keys(styleEntry ?? {}).forEach(key => {
63
- if (!key.startsWith('variant-')) {
64
- return
65
- }
66
-
67
- // @ts-expect-error - remove old variants
68
- delete styleEntry[key]
69
- })
70
- // @ts-expect-error - apply variant className
71
- styleEntry[className] = className
72
- }
73
37
  })
74
38
  }
75
39