react-native-unistyles 3.0.0-alpha.13 → 3.0.0-alpha.15
Sign up to get free protection for your applications and to get access to all the features.
- package/cxx/core/UnistyleWrapper.h +1 -1
- package/cxx/core/UnistylesCommitHook.cpp +13 -10
- package/cxx/core/UnistylesMountHook.cpp +3 -19
- package/cxx/hybridObjects/HybridStyleSheet.cpp +4 -3
- package/cxx/hybridObjects/HybridStyleSheet.h +1 -1
- package/cxx/parser/Parser.cpp +13 -9
- package/cxx/shadowTree/ShadowTreeManager.cpp +5 -5
- package/lib/commonjs/index.js +4 -0
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/specs/ShadowRegistry/index.js +7 -1
- package/lib/commonjs/specs/ShadowRegistry/index.js.map +1 -1
- package/lib/commonjs/web/convert/index.js +1 -1
- package/lib/commonjs/web/convert/index.js.map +1 -1
- package/lib/commonjs/web/create.js +25 -63
- package/lib/commonjs/web/create.js.map +1 -1
- package/lib/commonjs/web/createUnistylesComponent.js +61 -0
- package/lib/commonjs/web/createUnistylesComponent.js.map +1 -0
- package/lib/commonjs/web/index.js +12 -3
- package/lib/commonjs/web/index.js.map +1 -1
- package/lib/commonjs/web/listener/index.js +7 -0
- package/lib/commonjs/web/listener/index.js.map +1 -1
- package/lib/commonjs/web/listener/listenToDependencies.js +39 -0
- package/lib/commonjs/web/listener/listenToDependencies.js.map +1 -0
- package/lib/commonjs/web/listener/listener.js +1 -2
- package/lib/commonjs/web/listener/listener.js.map +1 -1
- package/lib/commonjs/web/mock.js +1 -10
- package/lib/commonjs/web/mock.js.map +1 -1
- package/lib/commonjs/web/pseudo.js +2 -3
- package/lib/commonjs/web/pseudo.js.map +1 -1
- package/lib/commonjs/web/registry.js +2 -4
- package/lib/commonjs/web/registry.js.map +1 -1
- package/lib/commonjs/web/runtime.js +0 -1
- package/lib/commonjs/web/runtime.js.map +1 -1
- package/lib/commonjs/web/shadowRegistry.js +120 -0
- package/lib/commonjs/web/shadowRegistry.js.map +1 -0
- package/lib/commonjs/web/state.js +0 -40
- package/lib/commonjs/web/state.js.map +1 -1
- package/lib/commonjs/web/utils.js +66 -14
- package/lib/commonjs/web/utils.js.map +1 -1
- package/lib/commonjs/web/variants/useVariants.js +15 -31
- package/lib/commonjs/web/variants/useVariants.js.map +1 -1
- package/lib/module/index.js +4 -0
- package/lib/module/index.js.map +1 -1
- package/lib/module/specs/ShadowRegistry/index.js +7 -1
- package/lib/module/specs/ShadowRegistry/index.js.map +1 -1
- package/lib/module/web/convert/index.js +1 -1
- package/lib/module/web/convert/index.js.map +1 -1
- package/lib/module/web/create.js +26 -64
- package/lib/module/web/create.js.map +1 -1
- package/lib/module/web/createUnistylesComponent.js +54 -0
- package/lib/module/web/createUnistylesComponent.js.map +1 -0
- package/lib/module/web/index.js +4 -2
- package/lib/module/web/index.js.map +1 -1
- package/lib/module/web/listener/index.js +1 -0
- package/lib/module/web/listener/index.js.map +1 -1
- package/lib/module/web/listener/listenToDependencies.js +34 -0
- package/lib/module/web/listener/listenToDependencies.js.map +1 -0
- package/lib/module/web/listener/listener.js +1 -1
- package/lib/module/web/listener/listener.js.map +1 -1
- package/lib/module/web/mock.js +0 -9
- package/lib/module/web/mock.js.map +1 -1
- package/lib/module/web/pseudo.js +2 -3
- package/lib/module/web/pseudo.js.map +1 -1
- package/lib/module/web/registry.js +2 -4
- package/lib/module/web/registry.js.map +1 -1
- package/lib/module/web/runtime.js +0 -1
- package/lib/module/web/runtime.js.map +1 -1
- package/lib/module/web/shadowRegistry.js +116 -0
- package/lib/module/web/shadowRegistry.js.map +1 -0
- package/lib/module/web/state.js +0 -40
- package/lib/module/web/state.js.map +1 -1
- package/lib/module/web/utils.js +59 -12
- package/lib/module/web/utils.js.map +1 -1
- package/lib/module/web/variants/useVariants.js +16 -32
- package/lib/module/web/variants/useVariants.js.map +1 -1
- package/lib/typescript/src/specs/ShadowRegistry/index.d.ts.map +1 -1
- package/lib/typescript/src/web/create.d.ts.map +1 -1
- package/lib/typescript/src/web/createUnistylesComponent.d.ts +3 -0
- package/lib/typescript/src/web/createUnistylesComponent.d.ts.map +1 -0
- package/lib/typescript/src/web/index.d.ts +2 -1
- package/lib/typescript/src/web/index.d.ts.map +1 -1
- package/lib/typescript/src/web/listener/index.d.ts +1 -0
- package/lib/typescript/src/web/listener/index.d.ts.map +1 -1
- package/lib/typescript/src/web/listener/listenToDependencies.d.ts +12 -0
- package/lib/typescript/src/web/listener/listenToDependencies.d.ts.map +1 -0
- package/lib/typescript/src/web/listener/listener.d.ts +2 -1
- package/lib/typescript/src/web/listener/listener.d.ts.map +1 -1
- package/lib/typescript/src/web/mock.d.ts +0 -9
- package/lib/typescript/src/web/mock.d.ts.map +1 -1
- package/lib/typescript/src/web/pseudo.d.ts +4 -2
- package/lib/typescript/src/web/pseudo.d.ts.map +1 -1
- package/lib/typescript/src/web/registry.d.ts +1 -1
- package/lib/typescript/src/web/registry.d.ts.map +1 -1
- package/lib/typescript/src/web/runtime.d.ts +0 -1
- package/lib/typescript/src/web/runtime.d.ts.map +1 -1
- package/lib/typescript/src/web/shadowRegistry.d.ts +17 -0
- package/lib/typescript/src/web/shadowRegistry.d.ts.map +1 -0
- package/lib/typescript/src/web/state.d.ts +0 -5
- package/lib/typescript/src/web/state.d.ts.map +1 -1
- package/lib/typescript/src/web/utils.d.ts +22 -7
- package/lib/typescript/src/web/utils.d.ts.map +1 -1
- package/lib/typescript/src/web/variants/useVariants.d.ts.map +1 -1
- package/nitrogen/generated/android/c++/JHybridNativePlatformSpec.hpp +6 -3
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/unistyles/HybridNativePlatformSpec.kt +8 -3
- package/nitrogen/generated/ios/c++/HybridNativePlatformSpecSwift.hpp +1 -1
- package/nitrogen/generated/ios/swift/HybridNativePlatformSpec.swift +1 -1
- package/nitrogen/generated/ios/swift/HybridNativePlatformSpecCxx.swift +14 -2
- package/nitrogen/generated/shared/c++/HybridNativePlatformSpec.hpp +3 -0
- package/nitrogen/generated/shared/c++/HybridUnistylesNavigationBarSpec.hpp +3 -0
- package/nitrogen/generated/shared/c++/HybridUnistylesRuntimeSpec.hpp +3 -0
- package/nitrogen/generated/shared/c++/HybridUnistylesShadowRegistrySpec.hpp +3 -0
- package/nitrogen/generated/shared/c++/HybridUnistylesStatusBarSpec.hpp +3 -0
- package/nitrogen/generated/shared/c++/HybridUnistylesStyleSheetSpec.cpp +1 -1
- package/nitrogen/generated/shared/c++/HybridUnistylesStyleSheetSpec.hpp +4 -1
- package/package.json +3 -3
- package/plugin/index.js +31 -0
- package/src/index.ts +3 -0
- package/src/specs/ShadowRegistry/index.ts +7 -1
- package/src/web/convert/index.ts +1 -1
- package/src/web/create.ts +24 -79
- package/src/web/createUnistylesComponent.tsx +56 -0
- package/src/web/index.ts +3 -2
- package/src/web/listener/index.ts +1 -0
- package/src/web/listener/listenToDependencies.ts +50 -0
- package/src/web/listener/listener.ts +1 -1
- package/src/web/mock.ts +0 -10
- package/src/web/pseudo.ts +131 -5
- package/src/web/registry.ts +3 -5
- package/src/web/runtime.ts +0 -2
- package/src/web/shadowRegistry.ts +121 -0
- package/src/web/state.ts +0 -44
- package/src/web/utils.ts +90 -16
- package/src/web/variants/useVariants.ts +18 -32
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
|
-
'
|
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
|
-
'
|
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.
|
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]
|
package/src/web/registry.ts
CHANGED
@@ -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
|
8
|
-
const
|
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:
|
11
|
+
$debugName: `${key}-${Math.random().toString(16).slice(10)}`,
|
14
12
|
}, typestyleStylesheet)
|
15
13
|
|
16
14
|
if (stylesheet._web?._css) {
|
package/src/web/runtime.ts
CHANGED
@@ -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()
|
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
|
-
import { equal, reduceObject } from '../utils'
|
5
|
-
import { UnistylesRegistry } from '../registry'
|
3
|
+
import { equal, keyInObject, reduceObject } from '../utils'
|
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,28 +25,14 @@ export const createUseVariants = (styles: ReactNativeStyleSheet<StyleSheet>, set
|
|
29
25
|
return result
|
30
26
|
}, [lastSelectedVariantsRef.current])
|
31
27
|
|
32
|
-
|
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
|
-
const selectedVariantStyles = selectedVariantStylesMap.get(key)
|
46
30
|
|
47
|
-
if (!
|
31
|
+
if (!styleEntry) {
|
48
32
|
return
|
49
33
|
}
|
50
34
|
|
51
|
-
|
52
|
-
UnistylesRegistry.updateStyles(unistyles, value, className)
|
53
|
-
}
|
35
|
+
const selectedVariantStyles = selectedVariantStylesMap.get(key)
|
54
36
|
|
55
37
|
Object.defineProperties(styleEntry, reduceObject(selectedVariantStyles ?? {}, value => ({
|
56
38
|
value,
|
@@ -58,18 +40,22 @@ export const createUseVariants = (styles: ReactNativeStyleSheet<StyleSheet>, set
|
|
58
40
|
configurable: true
|
59
41
|
})))
|
60
42
|
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
43
|
+
// Add __uni__variants to static styles
|
44
|
+
Object.keys(styleEntry).forEach(key => {
|
45
|
+
if (!key.startsWith('__uni__secrets__')) {
|
46
|
+
return
|
47
|
+
}
|
48
|
+
|
49
|
+
const secret = keyInObject(styleEntry, key) ? styleEntry[key] : undefined
|
50
|
+
|
51
|
+
if (!secret) {
|
52
|
+
return
|
53
|
+
}
|
66
54
|
|
67
|
-
|
68
|
-
|
55
|
+
Object.defineProperty(secret, '__uni__variants', {
|
56
|
+
value: selectedVariants
|
69
57
|
})
|
70
|
-
|
71
|
-
styleEntry[className] = className
|
72
|
-
}
|
58
|
+
})
|
73
59
|
})
|
74
60
|
}
|
75
61
|
|