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
@@ -24,9 +24,12 @@ namespace margelo::nitro::unistyles {
24
24
  /**
25
25
  * An abstract base class for `UnistylesStyleSheet`
26
26
  * Inherit this class to create instances of `HybridUnistylesStyleSheetSpec` in C++.
27
+ * You must explicitly call `HybridObject`'s constructor yourself, because it is virtual.
27
28
  * @example
28
29
  * ```cpp
29
30
  * class HybridUnistylesStyleSheet: public HybridUnistylesStyleSheetSpec {
31
+ * public:
32
+ * HybridUnistylesStyleSheet(...): HybridObject(TAG) { ... }
30
33
  * // ...
31
34
  * };
32
35
  * ```
@@ -42,7 +45,7 @@ namespace margelo::nitro::unistyles {
42
45
  public:
43
46
  // Properties
44
47
  virtual double getHairlineWidth() = 0;
45
- virtual double get___unid() = 0;
48
+ virtual double get__unid() = 0;
46
49
 
47
50
  public:
48
51
  // Methods
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-unistyles",
3
- "version": "3.0.0-alpha.13",
3
+ "version": "3.0.0-alpha.14",
4
4
  "description": "Level up your React Native StyleSheet",
5
5
  "scripts": {
6
6
  "test": "jest",
@@ -74,11 +74,11 @@
74
74
  "husky": "9.1.5",
75
75
  "jest": "29.7.0",
76
76
  "metro-react-native-babel-preset": "0.77.0",
77
- "nitro-codegen": "0.10.0",
77
+ "nitro-codegen": "0.11.0",
78
78
  "react": "18.3.1",
79
79
  "react-native": "0.75.2",
80
80
  "react-native-builder-bob": "0.30.0",
81
- "react-native-nitro-modules": "0.10.0",
81
+ "react-native-nitro-modules": "0.11.0",
82
82
  "react-native-web": "0.19.12",
83
83
  "react-test-renderer": "18.3.1",
84
84
  "release-it": "17.6.0",
package/src/index.ts CHANGED
@@ -3,6 +3,9 @@ import React from 'react'
3
3
  export * from './specs'
4
4
  export { mq } from './mq'
5
5
  export type { UnistylesThemes, UnistylesBreakpoints } from './global'
6
+ // TODO: export Native createUnistylesComponent
7
+ // can't import it this way, it will break native build
8
+ // export { createUnistylesComponent } from './web/createUnistylesComponent'
6
9
 
7
10
  // todo verify true min version
8
11
  const minReactVersionRequiredByUnistyles = '18.3.1'
@@ -27,7 +27,13 @@ const findShadowNodeForHandle = (handle: ViewHandle) => {
27
27
  }
28
28
 
29
29
  HybridShadowRegistry.add = (handle, style, variants, args) => {
30
- if (!handle || typeof style !== 'object') {
30
+ // virtualized nodes can be null
31
+ if (!handle || !style) {
32
+ return
33
+ }
34
+
35
+ // at this point unistyle can be only object or dynamic function
36
+ if (typeof style !== 'object' && typeof style !== 'function') {
31
37
  return
32
38
  }
33
39
 
@@ -21,7 +21,7 @@ export const convertToTypeStyle = (value: UnistylesValues) => {
21
21
  ...value._web
22
22
  }).flatMap(([unistylesKey, unistylesValue]) => {
23
23
  // Keys to omit
24
- if (['_css', '_web', 'variants', 'compoundVariants', 'uni__dependencies'].includes(unistylesKey)) {
24
+ if (['_css', '_web', 'variants', 'compoundVariants', 'uni__dependencies', '__unistyles-secrets__'].includes(unistylesKey) || unistylesKey.startsWith('variant-')) {
25
25
  return []
26
26
  }
27
27
 
package/src/web/create.ts CHANGED
@@ -1,105 +1,50 @@
1
- import type { TypeStyle } from 'typestyle'
2
1
  import type { ReactNativeStyleSheet } from '../types'
3
2
  import type { StyleSheetWithSuperPowers, StyleSheet } from '../types/stylesheet'
4
- import { UnistylesRegistry } from './registry'
5
- import { keyInObject, reduceObject, toReactNativeClassName } from './utils'
3
+ import { assignSecrets, reduceObject, getStyles } from './utils'
6
4
  import { UnistylesRuntime } from './runtime'
7
5
  import { createUseVariants, getVariants } from './variants'
8
- import { UnistylesListener } from './listener'
9
- import type { UnistyleDependency } from '../specs/NativePlatform'
10
-
11
- type ListenToDependenciesProps = {
12
- value: StyleSheet[keyof StyleSheet],
13
- key: PropertyKey,
14
- unistyles: TypeStyle,
15
- className: string,
16
- args?: Array<any>
17
- }
18
-
19
- type WebUnistyle = ReturnType<typeof UnistylesRegistry.createStyles>
20
6
 
21
7
  export const create = (stylesheet: StyleSheetWithSuperPowers<StyleSheet>) => {
22
8
  const computedStylesheet = typeof stylesheet === 'function'
23
9
  ? stylesheet(UnistylesRuntime.theme, UnistylesRuntime.miniRuntime)
24
10
  : stylesheet
25
- let lastlySelectedVariants: Record<string, any> = {}
26
-
27
- const listenToDependencies = ({ key, className, unistyles, value, args = [] } : ListenToDependenciesProps) => {
28
- const dependencies = ('uni__dependencies' in value ? value['uni__dependencies'] : []) as Array<UnistyleDependency>
29
-
30
- if (dependencies.length === 0) {
31
- return
32
- }
33
-
34
- return UnistylesListener.addListeners(dependencies, () => {
35
- const newComputedStylesheet = typeof stylesheet === 'function'
36
- ? stylesheet(UnistylesRuntime.theme, UnistylesRuntime.miniRuntime)
37
- : stylesheet
38
-
39
- if (!keyInObject(newComputedStylesheet, key)) {
40
- return
41
- }
42
-
43
- const value = newComputedStylesheet[key]!
44
- const result = typeof value === 'function'
45
- ? value(...args)
46
- : value
11
+ const selectedVariants = new Map<string, any>()
12
+
13
+ const copyVariants = () => Object.fromEntries(selectedVariants.entries())
14
+ const addSecrets = (value: any, key: string, args?: Array<any>) => assignSecrets(value, {
15
+ __uni__key: key,
16
+ __uni__refs: new Set(),
17
+ __uni__stylesheet: stylesheet,
18
+ __uni__args: args,
19
+ __uni__variants: copyVariants()
20
+ })
47
21
 
48
- UnistylesRegistry.updateStyles(unistyles, result, className)
49
- })
50
- }
22
+ const styles = reduceObject(computedStylesheet, (value, _key) => {
23
+ const key = String(_key)
51
24
 
52
- const styles = reduceObject(computedStylesheet, (value, key) => {
53
25
  if (typeof value === 'function') {
54
- const webUnistyleByRef = new Map<HTMLElement, WebUnistyle>()
55
- const disposeByRef = new Map<HTMLElement, VoidFunction | undefined>()
56
-
57
- return (...args: Array<any>) => {
58
- const [ref] = args.slice(-1)
26
+ const dynamicStyle = (...args: Array<any>) => {
59
27
  const result = value(...args)
60
- const variants = Object.fromEntries(getVariants({ [key]: result } as ReactNativeStyleSheet<StyleSheet>, lastlySelectedVariants))
28
+ const variants = Object.fromEntries(getVariants({ [key]: result } as ReactNativeStyleSheet<StyleSheet>, copyVariants()))
61
29
  const resultWithVariants = {
62
30
  ...result,
63
31
  ...variants[key]
64
32
  }
65
33
 
66
- if (ref instanceof HTMLElement) {
67
- const storedWebUnistyle = webUnistyleByRef.get(ref)
68
- const webUnistyle = storedWebUnistyle ?? UnistylesRegistry.createStyles(resultWithVariants, key)
69
-
70
- webUnistyleByRef.set(ref, webUnistyle)
71
- disposeByRef.get(ref)?.()
72
- disposeByRef.set(ref, listenToDependencies({
73
- key,
74
- value,
75
- unistyles: webUnistyle.unistyles,
76
- className: webUnistyle.className,
77
- args
78
- }))
79
- ref.classList.add(webUnistyle.className)
80
-
81
- if (storedWebUnistyle) {
82
- UnistylesRegistry.updateStyles(webUnistyle.unistyles, resultWithVariants, webUnistyle.className)
83
- }
84
-
85
-
86
- return
87
- }
88
-
89
- return toReactNativeClassName(null, resultWithVariants)
34
+ // Add secrets to result of dynamic styles function
35
+ return addSecrets(getStyles(resultWithVariants), key, args)
90
36
  }
91
- }
92
-
93
- const { className, unistyles } = UnistylesRegistry.createStyles(value, key)
94
37
 
95
- listenToDependencies({ key, value, unistyles, className })
38
+ // Add secrets to dynamic styles function
39
+ return addSecrets(dynamicStyle, key)
40
+ }
96
41
 
97
- return toReactNativeClassName(className, value)
42
+ // Add secrets to static styles
43
+ return addSecrets(getStyles(value), key)
98
44
  }) as ReactNativeStyleSheet<StyleSheet>
99
45
 
100
- createUseVariants(styles, newVariants => {
101
- lastlySelectedVariants = newVariants
102
- })
46
+ // Inject useVariants hook to styles
47
+ createUseVariants(styles, newVariants => Object.entries(newVariants).forEach(([key, value]) => selectedVariants.set(key, value)))
103
48
 
104
49
  return styles
105
50
  }
@@ -0,0 +1,56 @@
1
+ import React, { useEffect, useState } from 'react'
2
+ import { extractHiddenProperties, extractSecrets } from './utils'
3
+ import { UnistylesRuntime } from './runtime'
4
+ import { getVariants } from './variants'
5
+ import { UnistylesListener } from './listener'
6
+
7
+ const getStyles = (value: Record<string, any>) => {
8
+ const secrets = extractSecrets(value)
9
+
10
+ if (secrets.length === 0) {
11
+ return undefined
12
+ }
13
+
14
+ return secrets.reduce((acc, { __uni__stylesheet, __uni__key, __uni__args = [], __uni__variants }) => {
15
+ const newComputedStylesheet = typeof __uni__stylesheet === 'function'
16
+ ? __uni__stylesheet(UnistylesRuntime.theme, UnistylesRuntime.miniRuntime)
17
+ : __uni__stylesheet
18
+ const style = newComputedStylesheet[__uni__key]
19
+ const resultHidden = typeof style === 'function'
20
+ ? style(...__uni__args)
21
+ : style
22
+ const result = extractHiddenProperties(resultHidden)
23
+ const { variants } = Object.fromEntries(getVariants({ variants: result }, __uni__variants ))
24
+ const resultWithVariants = {
25
+ ...result,
26
+ ...variants
27
+ }
28
+
29
+ return {
30
+ ...acc,
31
+ ...resultWithVariants
32
+ }
33
+ }, {} as Record<string, any>)
34
+ }
35
+
36
+ export const createUnistylesComponent = <TProps extends object>(Component: React.ComponentType<TProps>) => (props: TProps) => {
37
+ const passedStyles = (props as TProps & { style: Record<string, any> }).style ?? {}
38
+ const [style, setStyle] = useState(getStyles(passedStyles))
39
+
40
+ useEffect(() => {
41
+ const newStyles = getStyles(passedStyles)
42
+ const dependencies = newStyles?.['uni__dependencies'] ?? []
43
+ const dispose = UnistylesListener.addListeners(dependencies, () => setStyle(getStyles(passedStyles)))
44
+
45
+ setStyle(newStyles)
46
+
47
+ return dispose
48
+ }, [passedStyles])
49
+
50
+ return (
51
+ <Component
52
+ {...props}
53
+ style={style}
54
+ />
55
+ )
56
+ }
package/src/web/index.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  import { create } from './create'
2
- import { UnistylesRuntime } from './runtime'
3
2
  import { UnistylesState } from './state'
4
3
  import { deepMergeObjects } from './utils'
5
4
 
@@ -19,6 +18,8 @@ export const StyleSheet = {
19
18
  }
20
19
 
21
20
  export { UnistylesRuntime } from './runtime'
21
+ export { UnistylesShadowRegistry } from './shadowRegistry'
22
22
  export * from './mock'
23
23
 
24
- export const getSSRUnistyles = () => UnistylesRuntime.getSSRUnistyles()
24
+ // TODO: Work on SSR
25
+ export const getSSRUnistyles = () => []
@@ -1 +1,2 @@
1
1
  export { UnistylesListener } from './listener'
2
+ export { listenToDependencies } from './listenToDependencies'
@@ -0,0 +1,50 @@
1
+ import type { TypeStyle } from 'typestyle'
2
+ import type { StyleSheet, StyleSheetWithSuperPowers } from '../../types/stylesheet'
3
+ import type { UnistyleDependency } from '../../specs/NativePlatform'
4
+ import { UnistylesListener } from './listener'
5
+ import { UnistylesRuntime } from '../runtime'
6
+ import { keyInObject } from '../utils'
7
+ import { UnistylesRegistry } from '../registry'
8
+
9
+ type ListenToDependenciesProps = {
10
+ key: PropertyKey,
11
+ unistyles: TypeStyle,
12
+ className: string,
13
+ stylesheet: StyleSheetWithSuperPowers<StyleSheet>
14
+ args?: Array<any>
15
+ }
16
+
17
+ export const listenToDependencies = ({ key, className, unistyles, args = [], stylesheet }: ListenToDependenciesProps) => {
18
+ const newComputedStylesheet = typeof stylesheet === 'function'
19
+ ? stylesheet(UnistylesRuntime.theme, UnistylesRuntime.miniRuntime)
20
+ : stylesheet
21
+ const _value = keyInObject(newComputedStylesheet, key) ? newComputedStylesheet[key] : undefined
22
+
23
+ if (!_value) {
24
+ return
25
+ }
26
+
27
+ const value = typeof _value === 'function' ? _value(...args) : _value
28
+ const dependencies = ('uni__dependencies' in value ? value['uni__dependencies'] : []) as Array<UnistyleDependency>
29
+
30
+ if (dependencies.length === 0) {
31
+ return
32
+ }
33
+
34
+ return UnistylesListener.addListeners(dependencies, () => {
35
+ const newComputedStylesheet = typeof stylesheet === 'function'
36
+ ? stylesheet(UnistylesRuntime.theme, UnistylesRuntime.miniRuntime)
37
+ : stylesheet
38
+
39
+ if (!keyInObject(newComputedStylesheet, key)) {
40
+ return
41
+ }
42
+
43
+ const value = newComputedStylesheet[key]!
44
+ const result = typeof value === 'function'
45
+ ? value(...args)
46
+ : value
47
+
48
+ UnistylesRegistry.updateStyles(unistyles, result, className)
49
+ })
50
+ }
@@ -1,7 +1,7 @@
1
1
  import { UnistyleDependency } from '../../specs/NativePlatform'
2
2
  import { UnistylesRuntime } from '../runtime'
3
3
 
4
- export class UnistylesListenerBuilder {
4
+ class UnistylesListenerBuilder {
5
5
  private isInitialized = false
6
6
  private listeners = Array.from({ length: Object.keys(UnistyleDependency).length / 2 }, () => new Set<VoidFunction>())
7
7
 
package/src/web/mock.ts CHANGED
@@ -23,13 +23,3 @@ export const NavigationBar: NavigationBarSpec = {
23
23
  __type: 'web',
24
24
  name: 'NavigationBar'
25
25
  }
26
-
27
- export const UnistylesShadowRegistry = {
28
- name: 'UnistylesShadowRegistry',
29
- __type: 'web',
30
- equals: () => true,
31
- toString: () => 'UnistylesShadowRegistry',
32
- dispose: () => {},
33
- add: () => {},
34
- remove: () => {},
35
- }
package/src/web/pseudo.ts CHANGED
@@ -1,11 +1,137 @@
1
- // TODO: Add complete list of pseudo selectors
2
1
  export const pseudos = [
2
+ '_-moz-broken',
3
+ '_-moz-drag-over',
4
+ '_-moz-first-node',
5
+ '_-moz-handler-blocked',
6
+ '_-moz-handler-crashed',
7
+ '_-moz-handler-disabled',
8
+ '_-moz-last-node',
9
+ '_-moz-loading',
10
+ '_-moz-locale-dir(ltr)',
11
+ '_-moz-locale-dir(rtl)',
12
+ '_-moz-only-whitespace',
13
+ '_-moz-submit-invalid',
14
+ '_-moz-suppressed',
15
+ '_-moz-user-disabled',
16
+ '_-moz-window-inactive',
17
+ '_active',
18
+ '_any-link',
19
+ '_autofill',
20
+ '_blank',
21
+ '_buffering',
22
+ '_checked',
23
+ '_current',
24
+ '_default',
25
+ '_defined',
26
+ '_dir(',
27
+ '_disabled',
28
+ '_empty',
29
+ '_enabled',
30
+ '_first',
31
+ '_first-child',
32
+ '_first-of-type',
33
+ '_focus',
34
+ '_focus-visible',
35
+ '_focus-within',
36
+ '_fullscreen',
37
+ '_future',
38
+ '_has(',
39
+ '_host',
40
+ '_host-context(',
41
+ '_host(',
3
42
  '_hover',
4
- '_css',
43
+ '_in-range',
44
+ '_indeterminate',
45
+ '_invalid',
46
+ '_is(',
47
+ '_lang(',
48
+ '_last-child',
49
+ '_last-of-type',
50
+ '_left',
51
+ '_link',
52
+ '_local-link',
53
+ '_modal',
54
+ '_muted',
55
+ '_not(',
56
+ '_nth-child(',
57
+ '_nth-last-child(',
58
+ '_nth-last-of-type(',
59
+ '_nth-of-type(',
60
+ '_only-child',
61
+ '_only-of-type',
62
+ '_optional',
63
+ '_out-of-range',
64
+ '_past',
65
+ '_paused',
66
+ '_picture-in-picture',
67
+ '_placeholder-shown',
68
+ '_playing',
69
+ '_popover-open',
70
+ '_read-only',
71
+ '_read-write',
72
+ '_required',
73
+ '_right',
74
+ '_scope',
75
+ '_seeking',
76
+ '_stalled',
77
+ '_state(',
78
+ '_target',
79
+ '_target-within',
80
+ '_user-invalid',
81
+ '_user-valid',
82
+ '_valid',
83
+ '_visited',
84
+ '_volume-locked',
85
+ '_where(',
86
+ '_-moz-color-swatch',
87
+ '_-moz-focus-inner',
88
+ '_-moz-list-bullet',
89
+ '_-moz-list-number',
90
+ '_-moz-meter-bar',
91
+ '_-moz-progress-bar',
92
+ '_-moz-range-progress',
93
+ '_-moz-range-thumb',
94
+ '_-moz-range-track',
95
+ '_-webkit-inner-spin-button',
96
+ '_-webkit-meter-barDeprecated',
97
+ '_-webkit-meter-even-less-good-value',
98
+ '_-webkit-meter-inner-element',
99
+ '_-webkit-meter-optimum-value',
100
+ '_-webkit-meter-suboptimum-value',
101
+ '_-webkit-progress-bar',
102
+ '_-webkit-progress-inner-element',
103
+ '_-webkit-progress-value',
104
+ '_-webkit-scrollbar',
105
+ '_-webkit-search-cancel-button',
106
+ '_-webkit-search-results-button',
107
+ '_-webkit-slider-runnable-track',
108
+ '_-webkit-slider-thumb',
109
+ '_after',
110
+ '_backdrop',
5
111
  '_before',
6
- '_after'
112
+ '_cue',
113
+ '_file-selector-button',
114
+ '_first-letter',
115
+ '_first-line',
116
+ '_grammar-error',
117
+ '_highlight(',
118
+ '_marker',
119
+ '_part(',
120
+ '_placeholder',
121
+ '_selection',
122
+ '_slotted(',
123
+ '_spelling-error',
124
+ '_target-text',
125
+ '_view-transition',
126
+ '_view-transition-group',
127
+ '_view-transition-image-pair',
128
+ '_view-transition-new',
129
+ '_view-transition-old',
7
130
  ] as const
8
131
 
9
- export const isPseudo = (selector: string) => pseudos.includes(selector as Pseudo)
132
+ export const isPseudo = (selector: string) => pseudos.some(pseudo => selector.startsWith(pseudo))
133
+
134
+ type AddDynamicSelector<T extends string> = T extends `${infer U}(` ? `${U}(${string})` : T
135
+
136
+ export type Pseudo = AddDynamicSelector<typeof pseudos[number]>
10
137
 
11
- export type Pseudo = typeof pseudos[number]
@@ -1,16 +1,14 @@
1
1
  import { createTypeStyle, TypeStyle } from 'typestyle'
2
2
  import type { UnistylesValues } from '../types'
3
3
  import { convertToTypeStyle } from './convert'
4
- import { UnistylesState } from './state'
5
4
 
6
5
  class UnistylesRegistryBuilder {
7
- createStyles = (stylesheet: UnistylesValues, key: string | number) => {
8
- const stylesTag = UnistylesState.createTag()
9
- const unistyles = createTypeStyle(stylesTag)
6
+ createStyles = (stylesheet: UnistylesValues, key: string) => {
7
+ const unistyles = createTypeStyle()
10
8
  const typestyleStylesheet = convertToTypeStyle(stylesheet)
11
9
 
12
10
  const className = unistyles.style({
13
- $debugName: String(key),
11
+ $debugName: `${key}-${Math.random().toString(16).slice(10)}`,
14
12
  }, typestyleStylesheet)
15
13
 
16
14
  if (stylesheet._web?._css) {
@@ -191,8 +191,6 @@ class UnistylesRuntimeBuilder {
191
191
 
192
192
  UnistylesState.themes.set(themeName, updater(oldTheme))
193
193
  }
194
-
195
- getSSRUnistyles = () => UnistylesState.tags
196
194
  }
197
195
 
198
196
  export const UnistylesRuntime = new UnistylesRuntimeBuilder()
@@ -0,0 +1,121 @@
1
+ import type { UnistylesValues } from '../types'
2
+ import { listenToDependencies } from './listener'
3
+ import { UnistylesRegistry } from './registry'
4
+ import { UnistylesRuntime } from './runtime'
5
+ import { createDoubleMap, extractHiddenProperties, extractSecrets, isInDocument } from './utils'
6
+ import { getVariants } from './variants'
7
+
8
+ type WebUnistyle = ReturnType<typeof UnistylesRegistry.createStyles>
9
+
10
+ type Style = UnistylesValues | ((...args: Array<any>) => UnistylesValues)
11
+
12
+ class UnistylesShadowRegistryBuilder {
13
+ // MOCKS
14
+ name = 'UnistylesShadowRegistry'
15
+ __type = 'web'
16
+ equals = () => true
17
+ toString = () => 'UnistylesShadowRegistry'
18
+ dispose = () => {}
19
+ // END MOCKS
20
+
21
+ private readonly webUnistylesMap = createDoubleMap<HTMLElement, string, WebUnistyle>()
22
+ private readonly disposeMap = createDoubleMap<HTMLElement, string, VoidFunction | undefined>()
23
+ private readonly stylesMap = createDoubleMap<HTMLElement, string, HTMLStyleElement>()
24
+
25
+ add = (ref: any, _style?: Style | Array<Style>, _variants?: Record<string, any>, _args?: Array<any>) => {
26
+ // Style is not provided
27
+ if (!_style) {
28
+ return
29
+ }
30
+
31
+ // Array of styles
32
+ if (Array.isArray(_style)) {
33
+ _style.forEach(style => this.add(ref, style, _variants, _args))
34
+
35
+ return
36
+ }
37
+
38
+ // Not a unistyle
39
+ if (!Object.keys(_style).some(key => key.startsWith('__uni__'))) {
40
+ return
41
+ }
42
+
43
+ // Ref is unmounted, remove style tags from the document
44
+ if (ref === null) {
45
+ const secrets = extractSecrets(_style)
46
+
47
+ secrets.forEach(({ __uni__refs }) => {
48
+ __uni__refs.forEach(ref => {
49
+ if (isInDocument(ref)) {
50
+ return
51
+ }
52
+
53
+ this.remove(ref, _style)
54
+ })
55
+ })
56
+
57
+ return
58
+ }
59
+
60
+ // Ref is not an HTMLElement
61
+ if (!(ref instanceof HTMLElement)) {
62
+ return
63
+ }
64
+
65
+ extractSecrets(_style).forEach(secret => {
66
+ const { __uni__key, __uni__stylesheet, __uni__refs, __uni__variants, __uni__args = [] } = secret
67
+ const newComputedStylesheet = typeof __uni__stylesheet === 'function'
68
+ ? __uni__stylesheet(UnistylesRuntime.theme, UnistylesRuntime.miniRuntime)
69
+ : __uni__stylesheet
70
+ const style = newComputedStylesheet[__uni__key]
71
+ const args = _args ?? __uni__args
72
+ const resultHidden = typeof style === 'function'
73
+ ? style(...args)
74
+ : style
75
+ const result = extractHiddenProperties(resultHidden)
76
+ const { variants } = Object.fromEntries(getVariants({ variants: result }, _variants && Object.keys(_variants).length > 0 ? _variants : __uni__variants))
77
+ const resultWithVariants = {
78
+ ...result,
79
+ ...variants
80
+ }
81
+ const storedWebUnistyle = this.webUnistylesMap.get(ref, __uni__key)
82
+ const webUnistyle = storedWebUnistyle ?? UnistylesRegistry.createStyles(resultWithVariants, __uni__key)
83
+
84
+ this.webUnistylesMap.set(ref, __uni__key, webUnistyle)
85
+ this.disposeMap.get(ref, __uni__key)?.()
86
+ this.disposeMap.set(ref, __uni__key, listenToDependencies({
87
+ key: __uni__key,
88
+ stylesheet: __uni__stylesheet,
89
+ args,
90
+ className: webUnistyle.className,
91
+ unistyles: webUnistyle.unistyles,
92
+ }))
93
+
94
+ if (!storedWebUnistyle) {
95
+ const styleTag = document.createElement('style')
96
+
97
+ ref.classList.add(webUnistyle.className)
98
+ webUnistyle.unistyles.setStylesTarget(styleTag)
99
+ document.head.appendChild(styleTag)
100
+ __uni__refs.add(ref)
101
+ this.stylesMap.set(ref, __uni__key, styleTag)
102
+ }
103
+
104
+ if (storedWebUnistyle) {
105
+ UnistylesRegistry.updateStyles(webUnistyle.unistyles, resultWithVariants, webUnistyle.className)
106
+ }
107
+ })
108
+ }
109
+
110
+ remove = (ref: HTMLElement, style: Style) => {
111
+ extractSecrets(style).forEach(({ __uni__key }) => {
112
+ this.webUnistylesMap.delete(ref, __uni__key)
113
+ this.disposeMap.get(ref, __uni__key)?.()
114
+ this.disposeMap.delete(ref, __uni__key)
115
+ this.stylesMap.get(ref, __uni__key)?.remove()
116
+ this.stylesMap.delete(ref, __uni__key)
117
+ })
118
+ }
119
+ }
120
+
121
+ export const UnistylesShadowRegistry = new UnistylesShadowRegistryBuilder()