react-native-unistyles 3.7.0 → 3.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/android/src/main/java/com/unistyles/NativePlatform+android.kt +2 -15
- package/android/src/main/java/com/unistyles/NativePlatform+listener.kt +1 -1
- package/codemods/use-variants.js +155 -0
- package/components/native/TouchableHighlight/package.json +1 -1
- package/cxx/converters/TransformOriginConverter.cpp +3 -2
- package/cxx/core/DependencyTracker.h +162 -0
- package/cxx/core/StyleSheet.h +14 -0
- package/cxx/core/Unistyle.h +6 -3
- package/cxx/core/UnistylesRegistry.cpp +16 -2
- package/cxx/core/UnistylesRegistry.h +2 -1
- package/cxx/hybridObjects/HybridShadowRegistry.cpp +16 -5
- package/cxx/hybridObjects/HybridShadowRegistry.h +1 -1
- package/cxx/hybridObjects/HybridStyleSheet.cpp +8 -1
- package/cxx/hybridObjects/HybridUnistylesRuntime.cpp +1 -1
- package/cxx/parser/Parser.cpp +83 -31
- package/cxx/parser/Parser.h +5 -0
- package/cxx/shadowTree/NativeProps.h +23 -0
- package/cxx/shadowTree/ShadowTrafficController.h +5 -0
- package/cxx/shadowTree/ShadowTreeManager.cpp +73 -1
- package/ios/NativePlatform+ios.swift +4 -6
- package/ios/Unistyles.h +1 -0
- package/lib/commonjs/core/trackDependencies.js +10 -0
- package/lib/commonjs/core/trackDependencies.js.map +1 -0
- package/lib/commonjs/core/trackDependencies.metro.js +10 -0
- package/lib/commonjs/core/trackDependencies.metro.js.map +1 -0
- package/lib/commonjs/core/useProxifiedUnistyles/useProxifiedUnistyles.js +4 -0
- package/lib/commonjs/core/useProxifiedUnistyles/useProxifiedUnistyles.js.map +1 -1
- package/lib/commonjs/core/withUnistyles/withUnistyles.native.js +63 -14
- package/lib/commonjs/core/withUnistyles/withUnistyles.native.js.map +1 -1
- package/lib/commonjs/hooks/useMedia.js +6 -0
- package/lib/commonjs/hooks/useMedia.js.map +1 -1
- package/lib/commonjs/mocks.js +4 -2
- package/lib/commonjs/mocks.js.map +1 -1
- package/lib/commonjs/reanimated/variant/useUpdateVariantColor.js +13 -4
- package/lib/commonjs/reanimated/variant/useUpdateVariantColor.js.map +1 -1
- package/lib/commonjs/specs/ShadowRegistry/index.js +26 -1
- package/lib/commonjs/specs/ShadowRegistry/index.js.map +1 -1
- package/lib/commonjs/specs/StyleSheet/index.js +6 -1
- package/lib/commonjs/specs/StyleSheet/index.js.map +1 -1
- package/lib/commonjs/types/stylesheet.js.map +1 -1
- package/lib/commonjs/web/convert/index.js +2 -2
- package/lib/commonjs/web/convert/index.js.map +1 -1
- package/lib/commonjs/web/convert/style.js +6 -0
- package/lib/commonjs/web/convert/style.js.map +1 -1
- package/lib/commonjs/web/create.js +35 -6
- package/lib/commonjs/web/create.js.map +1 -1
- package/lib/commonjs/web/css/core.js +12 -2
- package/lib/commonjs/web/css/core.js.map +1 -1
- package/lib/commonjs/web/css/sheet.js +223 -0
- package/lib/commonjs/web/css/sheet.js.map +1 -0
- package/lib/commonjs/web/css/state.js +98 -78
- package/lib/commonjs/web/css/state.js.map +1 -1
- package/lib/commonjs/web/css/utils.js +1 -31
- package/lib/commonjs/web/css/utils.js.map +1 -1
- package/lib/commonjs/web/dependencyTracker.js +72 -0
- package/lib/commonjs/web/dependencyTracker.js.map +1 -0
- package/lib/commonjs/web/registry.js +73 -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 +83 -11
- package/lib/commonjs/web/shadowRegistry.js.map +1 -1
- package/lib/commonjs/web/types.js +3 -1
- package/lib/commonjs/web/types.js.map +1 -1
- package/lib/module/core/trackDependencies.js +6 -0
- package/lib/module/core/trackDependencies.js.map +1 -0
- package/lib/module/core/trackDependencies.metro.js +6 -0
- package/lib/module/core/trackDependencies.metro.js.map +1 -0
- package/lib/module/core/useProxifiedUnistyles/useProxifiedUnistyles.js +4 -0
- package/lib/module/core/useProxifiedUnistyles/useProxifiedUnistyles.js.map +1 -1
- package/lib/module/core/withUnistyles/withUnistyles.native.js +63 -15
- package/lib/module/core/withUnistyles/withUnistyles.native.js.map +1 -1
- package/lib/module/hooks/useMedia.js +6 -0
- package/lib/module/hooks/useMedia.js.map +1 -1
- package/lib/module/mocks.js +4 -2
- package/lib/module/mocks.js.map +1 -1
- package/lib/module/reanimated/variant/useUpdateVariantColor.js +15 -5
- package/lib/module/reanimated/variant/useUpdateVariantColor.js.map +1 -1
- package/lib/module/specs/ShadowRegistry/index.js +25 -1
- package/lib/module/specs/ShadowRegistry/index.js.map +1 -1
- package/lib/module/specs/StyleSheet/index.js +6 -1
- package/lib/module/specs/StyleSheet/index.js.map +1 -1
- package/lib/module/types/stylesheet.js.map +1 -1
- package/lib/module/web/convert/index.js +2 -2
- package/lib/module/web/convert/index.js.map +1 -1
- package/lib/module/web/convert/style.js +6 -0
- package/lib/module/web/convert/style.js.map +1 -1
- package/lib/module/web/create.js +35 -6
- package/lib/module/web/create.js.map +1 -1
- package/lib/module/web/css/core.js +10 -1
- package/lib/module/web/css/core.js.map +1 -1
- package/lib/module/web/css/sheet.js +216 -0
- package/lib/module/web/css/sheet.js.map +1 -0
- package/lib/module/web/css/state.js +98 -78
- package/lib/module/web/css/state.js.map +1 -1
- package/lib/module/web/css/utils.js +0 -29
- package/lib/module/web/css/utils.js.map +1 -1
- package/lib/module/web/dependencyTracker.js +67 -0
- package/lib/module/web/dependencyTracker.js.map +1 -0
- package/lib/module/web/registry.js +73 -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 +83 -11
- package/lib/module/web/shadowRegistry.js.map +1 -1
- package/lib/module/web/types.js +2 -0
- package/lib/module/web/types.js.map +1 -1
- package/lib/typescript/src/core/trackDependencies.d.ts +2 -0
- package/lib/typescript/src/core/trackDependencies.d.ts.map +1 -0
- package/lib/typescript/src/core/trackDependencies.metro.d.ts +2 -0
- package/lib/typescript/src/core/trackDependencies.metro.d.ts.map +1 -0
- package/lib/typescript/src/core/useProxifiedUnistyles/useProxifiedUnistyles.d.ts.map +1 -1
- package/lib/typescript/src/core/withUnistyles/withUnistyles.native.d.ts +2 -1
- package/lib/typescript/src/core/withUnistyles/withUnistyles.native.d.ts.map +1 -1
- package/lib/typescript/src/hooks/useMedia.d.ts.map +1 -1
- package/lib/typescript/src/reanimated/variant/useUpdateVariantColor.d.ts.map +1 -1
- package/lib/typescript/src/specs/ShadowRegistry/index.d.ts +1 -1
- package/lib/typescript/src/specs/ShadowRegistry/index.d.ts.map +1 -1
- package/lib/typescript/src/specs/StyleSheet/index.d.ts +3 -1
- package/lib/typescript/src/specs/StyleSheet/index.d.ts.map +1 -1
- package/lib/typescript/src/types/stylesheet.d.ts +3 -3
- package/lib/typescript/src/types/stylesheet.d.ts.map +1 -1
- package/lib/typescript/src/web/convert/style.d.ts.map +1 -1
- package/lib/typescript/src/web/create.d.ts.map +1 -1
- package/lib/typescript/src/web/css/core.d.ts +4 -1
- package/lib/typescript/src/web/css/core.d.ts.map +1 -1
- package/lib/typescript/src/web/css/sheet.d.ts +26 -0
- package/lib/typescript/src/web/css/sheet.d.ts.map +1 -0
- package/lib/typescript/src/web/css/state.d.ts +6 -6
- package/lib/typescript/src/web/css/state.d.ts.map +1 -1
- package/lib/typescript/src/web/css/utils.d.ts +0 -2
- package/lib/typescript/src/web/css/utils.d.ts.map +1 -1
- package/lib/typescript/src/web/dependencyTracker.d.ts +9 -0
- package/lib/typescript/src/web/dependencyTracker.d.ts.map +1 -0
- package/lib/typescript/src/web/registry.d.ts +9 -1
- package/lib/typescript/src/web/registry.d.ts.map +1 -1
- package/lib/typescript/src/web/runtime.d.ts.map +1 -1
- package/lib/typescript/src/web/shadowRegistry.d.ts.map +1 -1
- package/lib/typescript/src/web/types.d.ts +1 -0
- package/lib/typescript/src/web/types.d.ts.map +1 -1
- package/metro/index.d.ts +2 -12
- package/metro/index.js +57 -33
- package/package.json +64 -17
- package/plugin/index.d.ts +0 -7
- package/plugin/index.js +9 -15
- package/src/core/trackDependencies.metro.ts +3 -0
- package/src/core/trackDependencies.ts +3 -0
- package/src/core/useProxifiedUnistyles/useProxifiedUnistyles.ts +5 -0
- package/src/core/withUnistyles/withUnistyles.native.tsx +76 -17
- package/src/hooks/useMedia.ts +6 -0
- package/src/mocks.ts +5 -2
- package/src/reanimated/variant/useUpdateVariantColor.ts +15 -5
- package/src/specs/ShadowRegistry/index.ts +29 -2
- package/src/specs/StyleSheet/index.ts +4 -2
- package/src/types/stylesheet.ts +5 -4
- package/src/web/convert/index.ts +2 -2
- package/src/web/convert/style.ts +8 -0
- package/src/web/create.ts +45 -8
- package/src/web/css/core.ts +17 -3
- package/src/web/css/sheet.ts +274 -0
- package/src/web/css/state.ts +108 -95
- package/src/web/css/utils.ts +0 -42
- package/src/web/dependencyTracker.ts +71 -0
- package/src/web/registry.ts +88 -8
- package/src/web/runtime.ts +0 -1
- package/src/web/shadowRegistry.ts +101 -16
- package/src/web/types.ts +3 -0
- package/metro/babel-transformer.js +0 -1102
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
import { CONTENTS_CLASS_NAME } from '../types'
|
|
2
|
+
import { hyphenate, isServer } from '../utils'
|
|
3
|
+
|
|
4
|
+
type Styles = Map<string, any>
|
|
5
|
+
type StylesMap = Map<string, Map<string, Styles>>
|
|
6
|
+
type RuleParent = CSSStyleSheet | CSSGroupingRule
|
|
7
|
+
type Group = {
|
|
8
|
+
rule: CSSGroupingRule
|
|
9
|
+
rules: Array<CSSRule>
|
|
10
|
+
classes: Map<string, { rule: CSSStyleRule; declarations: string }>
|
|
11
|
+
rejected: Map<string, string>
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// Styles without a media query get a group too, so every group is an ordinary child list of one top level rule
|
|
15
|
+
const BASE_MEDIA_QUERY = '@media all'
|
|
16
|
+
|
|
17
|
+
const CHILD_SELECTOR = '_child > *'
|
|
18
|
+
// withUnistyles styles its first child, but a library can put a box-less view first (Gesture Handler 3 does)
|
|
19
|
+
// so also reach the only child of a display: contents view, unless it's another withUnistyles wrapper
|
|
20
|
+
// React writes an inline display:contents on the server and display: contents on the client
|
|
21
|
+
// :where() keeps the specificity of the original selector
|
|
22
|
+
const CONTENTS_CHILD_SELECTOR = `_child > :where([style^="display:contents"],[style^="display: contents"],[style*=";display:contents"],[style*="; display: contents"],.${CONTENTS_CLASS_NAME}):where(:not([class^="unistyles_"][class$="_child"])) > :where(:only-child)`
|
|
23
|
+
|
|
24
|
+
export const getSelector = (className: string) => {
|
|
25
|
+
const selector = className[0] === ':' ? className : `.${className}`
|
|
26
|
+
|
|
27
|
+
return selector.includes(CHILD_SELECTOR)
|
|
28
|
+
? `${selector},${selector.replace(CHILD_SELECTOR, CONTENTS_CHILD_SELECTOR)}`
|
|
29
|
+
: selector
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export const getDeclarations = (styles: Styles) => {
|
|
33
|
+
let declarations = ''
|
|
34
|
+
|
|
35
|
+
for (const [propertyKey, value] of styles) {
|
|
36
|
+
if (value === undefined) {
|
|
37
|
+
continue
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
declarations += `${hyphenate(propertyKey)}:${value};`
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return declarations
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const insertRule = (parent: RuleParent, rule: string, index = parent.cssRules.length) => {
|
|
47
|
+
try {
|
|
48
|
+
return parent.cssRules[parent.insertRule(rule, index)]
|
|
49
|
+
} catch {
|
|
50
|
+
// The browser can't parse it, like a pseudo-element it doesn't support yet: a text sheet drops it too
|
|
51
|
+
return undefined
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const indexOfRule = (parent: RuleParent, rule: CSSRule) => Array.prototype.indexOf.call(parent.cssRules, rule)
|
|
56
|
+
|
|
57
|
+
// Keeps the styles of CSSState in the style tag through the CSSOM, never its text, so another runtime keeps its rules
|
|
58
|
+
export class CSSSheet {
|
|
59
|
+
private tag: HTMLStyleElement | null = null
|
|
60
|
+
private sheet: CSSStyleSheet | null = null
|
|
61
|
+
private groups = new Map<string, Group | null>()
|
|
62
|
+
private themes = new Map<string, CSSRule>()
|
|
63
|
+
private hasServerStyles = false
|
|
64
|
+
|
|
65
|
+
constructor(
|
|
66
|
+
private themesCSS: Map<string, string>,
|
|
67
|
+
private styles: StylesMap,
|
|
68
|
+
) {
|
|
69
|
+
if (isServer()) {
|
|
70
|
+
return
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const existingTag = document.getElementById('unistyles-web')
|
|
74
|
+
|
|
75
|
+
if (existingTag) {
|
|
76
|
+
this.tag = existingTag as HTMLStyleElement
|
|
77
|
+
// A tag from another runtime has no text, its rules live in the CSSOM
|
|
78
|
+
this.hasServerStyles = Boolean(this.tag.textContent)
|
|
79
|
+
this.sheet = this.hasServerStyles ? null : this.tag.sheet
|
|
80
|
+
|
|
81
|
+
return
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
this.tag = document.createElement('style')
|
|
85
|
+
this.tag.id = 'unistyles-web'
|
|
86
|
+
document.head.appendChild(this.tag)
|
|
87
|
+
this.sheet = this.tag.sheet
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
refresh = () => {
|
|
91
|
+
const sheet = this.getSheet()
|
|
92
|
+
|
|
93
|
+
if (sheet) {
|
|
94
|
+
this.insertGroups(sheet)
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
syncClass = (mediaQuery: string, className: string) => {
|
|
99
|
+
const sheet = this.getSheet()
|
|
100
|
+
|
|
101
|
+
if (!sheet) {
|
|
102
|
+
return
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
this.insertGroups(sheet)
|
|
106
|
+
|
|
107
|
+
const group = this.groups.get(mediaQuery)
|
|
108
|
+
|
|
109
|
+
if (group) {
|
|
110
|
+
this.syncGroupClass(group, className, this.styles.get(mediaQuery)?.get(className))
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// False only when the browser rejects the theme, so CSSState keeps the CSS that is applied
|
|
115
|
+
syncTheme = (key: string, css = this.themesCSS.get(key)) => {
|
|
116
|
+
const sheet = this.getThemeSheet()
|
|
117
|
+
const current = this.themes.get(key)
|
|
118
|
+
|
|
119
|
+
if (!sheet || css === undefined) {
|
|
120
|
+
return true
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// The theme goes last on every update, so it wins over the same theme of a runtime added to the tag later
|
|
124
|
+
const rule = insertRule(sheet, css)
|
|
125
|
+
|
|
126
|
+
// Inserted before the old rule goes, so a theme the browser rejects keeps the previous variables
|
|
127
|
+
if (!rule) {
|
|
128
|
+
return false
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const currentIndex = current ? indexOfRule(sheet, current) : -1
|
|
132
|
+
|
|
133
|
+
if (currentIndex >= 0) {
|
|
134
|
+
sheet.deleteRule(currentIndex)
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
this.themes.set(key, rule)
|
|
138
|
+
|
|
139
|
+
return true
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
reset = () => {
|
|
143
|
+
const sheet = this.tag?.sheet
|
|
144
|
+
|
|
145
|
+
if (sheet && sheet === this.sheet) {
|
|
146
|
+
this.deleteOwnRules(sheet, new Set(Array.from(this.groups.values(), (group) => group?.rule)))
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
this.groups.clear()
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
private getSheet = () => {
|
|
153
|
+
if (!this.tag) {
|
|
154
|
+
return null
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
if (!this.tag.isConnected) {
|
|
158
|
+
document.head.appendChild(this.tag)
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// Another runtime on the tag may have cleared the server text and filled the tag through the CSSOM already
|
|
162
|
+
if (this.hasServerStyles) {
|
|
163
|
+
this.hasServerStyles = false
|
|
164
|
+
|
|
165
|
+
if (this.tag.textContent) {
|
|
166
|
+
this.tag.textContent = ''
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const sheet = this.tag.sheet
|
|
171
|
+
|
|
172
|
+
// A new sheet lost every rule: the server one was replaced, or the tag was rewritten or attached again
|
|
173
|
+
if (sheet && sheet !== this.sheet) {
|
|
174
|
+
this.sheet = sheet
|
|
175
|
+
this.insertAll(sheet)
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
return sheet
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
private getThemeSheet = () => {
|
|
182
|
+
if (this.hasServerStyles) {
|
|
183
|
+
return this.tag?.sheet ?? null
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
return this.getSheet()
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
private insertAll = (sheet: CSSStyleSheet) => {
|
|
190
|
+
this.groups.clear()
|
|
191
|
+
// A theme added on top of server styles another runtime already replaced is still in this sheet
|
|
192
|
+
this.deleteOwnRules(sheet, new Set(this.themes.values()))
|
|
193
|
+
this.themes.clear()
|
|
194
|
+
this.themesCSS.forEach((_, key) => this.syncTheme(key))
|
|
195
|
+
this.insertGroups(sheet)
|
|
196
|
+
this.groups.forEach((group, mediaQuery) => {
|
|
197
|
+
if (!group) {
|
|
198
|
+
return
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
this.styles.get(mediaQuery)?.forEach((styles, className) => this.syncGroupClass(group, className, styles))
|
|
202
|
+
})
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// Every group of the styles map gets created, empty ones too, so a group appended later is always the last one
|
|
206
|
+
private insertGroups = (sheet: CSSStyleSheet) => {
|
|
207
|
+
if (this.groups.size === this.styles.size) {
|
|
208
|
+
return
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
this.styles.forEach((_, mediaQuery) => {
|
|
212
|
+
if (this.groups.has(mediaQuery)) {
|
|
213
|
+
return
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const rule = insertRule(sheet, `${mediaQuery || BASE_MEDIA_QUERY}{}`) as CSSGroupingRule | undefined
|
|
217
|
+
|
|
218
|
+
this.groups.set(mediaQuery, rule ? { rule, rules: [], classes: new Map(), rejected: new Map() } : null)
|
|
219
|
+
})
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
private syncGroupClass = (group: Group, className: string, styles?: Styles) => {
|
|
223
|
+
const current = group.classes.get(className)
|
|
224
|
+
|
|
225
|
+
if (!styles) {
|
|
226
|
+
group.rejected.delete(className)
|
|
227
|
+
|
|
228
|
+
if (current) {
|
|
229
|
+
const index = group.rules.indexOf(current.rule)
|
|
230
|
+
|
|
231
|
+
group.rule.deleteRule(index)
|
|
232
|
+
group.rules.splice(index, 1)
|
|
233
|
+
group.classes.delete(className)
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
return
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
const declarations = getDeclarations(styles)
|
|
240
|
+
|
|
241
|
+
if (current) {
|
|
242
|
+
if (current.declarations !== declarations) {
|
|
243
|
+
current.rule.style.cssText = declarations
|
|
244
|
+
current.declarations = declarations
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
return
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
if (group.rejected.get(className) === declarations) {
|
|
251
|
+
return
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
const rule = insertRule(group.rule, `${getSelector(className)}{${declarations}}`) as CSSStyleRule | undefined
|
|
255
|
+
|
|
256
|
+
if (rule) {
|
|
257
|
+
group.rules.push(rule)
|
|
258
|
+
group.classes.set(className, { rule, declarations })
|
|
259
|
+
group.rejected.delete(className)
|
|
260
|
+
|
|
261
|
+
return
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
group.rejected.set(className, declarations)
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
private deleteOwnRules = (sheet: CSSStyleSheet, rules: Set<CSSRule | undefined>) => {
|
|
268
|
+
for (let index = sheet.cssRules.length - 1; index >= 0; index--) {
|
|
269
|
+
if (rules.has(sheet.cssRules[index])) {
|
|
270
|
+
sheet.deleteRule(index)
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
}
|
package/src/web/css/state.ts
CHANGED
|
@@ -3,72 +3,91 @@ import type { UnistylesServices } from '../types'
|
|
|
3
3
|
|
|
4
4
|
import { convertUnistyles } from '../convert'
|
|
5
5
|
import { hyphenate, isServer } from '../utils'
|
|
6
|
-
import { convertToCSS,
|
|
6
|
+
import { convertToCSS, getClassNameHash } from './core'
|
|
7
|
+
import { CSSSheet, getDeclarations, getSelector } from './sheet'
|
|
8
|
+
import { safeGetMap } from './utils'
|
|
7
9
|
|
|
8
10
|
type MapType = Map<string, Map<string, Map<string, any>>>
|
|
11
|
+
type ClassNames = Map<string, Set<string>>
|
|
9
12
|
type SetProps = {
|
|
10
13
|
mediaQuery?: string
|
|
11
14
|
className: string
|
|
12
|
-
isMq?: boolean
|
|
13
15
|
propertyKey: string
|
|
14
16
|
value: any
|
|
15
17
|
}
|
|
16
18
|
type HydrateState = Array<[string, Array<[string, Array<[string, any]>]>]>
|
|
17
19
|
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
map.set(key, newMap)
|
|
25
|
-
|
|
26
|
-
return newMap
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
return nextLevelMap
|
|
20
|
+
const forEachClassName = (
|
|
21
|
+
classNames: ClassNames | undefined,
|
|
22
|
+
callback: (mediaQuery: string, className: string) => void,
|
|
23
|
+
) => {
|
|
24
|
+
classNames?.forEach((names, mediaQuery) => names.forEach((className) => callback(mediaQuery, className)))
|
|
30
25
|
}
|
|
31
26
|
|
|
32
27
|
export class CSSState {
|
|
33
|
-
mainMap: MapType = new Map()
|
|
34
|
-
mqMap: MapType = new Map()
|
|
35
|
-
private styleTag: HTMLStyleElement | null = null
|
|
28
|
+
mainMap: MapType = new Map([['', new Map()]])
|
|
36
29
|
private themesCSS = new Map<string, string>()
|
|
30
|
+
private owners = new Map<string, ClassNames>()
|
|
31
|
+
private hydratedState?: ReturnType<CSSState['getState']>
|
|
32
|
+
private sheet = new CSSSheet(this.themesCSS, this.mainMap)
|
|
37
33
|
|
|
38
34
|
constructor(private services: UnistylesServices) {
|
|
39
|
-
|
|
40
|
-
|
|
35
|
+
// Some frameworks render useServerUnistyles only on the server (e.g. Expo Router's +html), so nothing hydrates the
|
|
36
|
+
// server styles in the browser. Only while the tag holds them: once emptied, another runtime sharing it took them
|
|
37
|
+
if (!isServer() && window.__UNISTYLES_STATE__ && document.getElementById('unistyles-web')?.textContent) {
|
|
38
|
+
this.hydrate(window.__UNISTYLES_STATE__)
|
|
39
|
+
document.querySelector('#unistyles-script')?.remove()
|
|
41
40
|
}
|
|
42
|
-
|
|
43
|
-
const ssrTag = document.getElementById('unistyles-web')
|
|
44
|
-
|
|
45
|
-
if (ssrTag) {
|
|
46
|
-
this.styleTag = ssrTag as HTMLStyleElement
|
|
47
|
-
|
|
48
|
-
return
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
this.styleTag = document.createElement('style')
|
|
52
|
-
this.styleTag.id = 'unistyles-web'
|
|
53
|
-
document.head.appendChild(this.styleTag)
|
|
54
41
|
}
|
|
55
42
|
|
|
56
|
-
set = ({ className, propertyKey, value, mediaQuery = ''
|
|
57
|
-
const
|
|
58
|
-
const secondLevelMap = safeGetMap(firstLevelMap, mediaQuery)
|
|
43
|
+
set = ({ className, propertyKey, value, mediaQuery = '' }: SetProps) => {
|
|
44
|
+
const secondLevelMap = safeGetMap(this.mainMap, mediaQuery)
|
|
59
45
|
const thirdLevelMap = safeGetMap(secondLevelMap, className)
|
|
60
46
|
|
|
61
47
|
thirdLevelMap.set(propertyKey, value)
|
|
48
|
+
|
|
49
|
+
// The owner comes from the class name, so hydrated styles have one too
|
|
50
|
+
const owner: ClassNames = safeGetMap(this.owners, getClassNameHash(className))
|
|
51
|
+
const classNames = owner.get(mediaQuery) ?? new Set()
|
|
52
|
+
|
|
53
|
+
classNames.add(className)
|
|
54
|
+
owner.set(mediaQuery, classNames)
|
|
62
55
|
}
|
|
63
56
|
|
|
64
57
|
add = (hash: string, values: UnistylesValues) => {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
58
|
+
const styles: MapType = new Map()
|
|
59
|
+
|
|
60
|
+
convertToCSS(hash, convertUnistyles(values, this.services.runtime), {
|
|
61
|
+
set: ({ className, propertyKey, value, mediaQuery = '' }) => {
|
|
62
|
+
safeGetMap(safeGetMap(styles, mediaQuery), className).set(propertyKey, value)
|
|
63
|
+
},
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
const previousClassNames = this.owners.get(hash)
|
|
67
|
+
|
|
68
|
+
forEachClassName(previousClassNames, (mediaQuery, className) => {
|
|
69
|
+
this.mainMap.get(mediaQuery)?.get(className)?.clear()
|
|
70
|
+
})
|
|
71
|
+
this.owners.delete(hash)
|
|
72
|
+
styles.forEach((classes, mediaQuery) => {
|
|
73
|
+
classes.forEach((properties, className) => {
|
|
74
|
+
properties.forEach((value, propertyKey) => this.set({ mediaQuery, className, propertyKey, value }))
|
|
75
|
+
})
|
|
76
|
+
})
|
|
68
77
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
78
|
+
const classNames = this.owners.get(hash)
|
|
79
|
+
|
|
80
|
+
forEachClassName(previousClassNames, (mediaQuery, className) => {
|
|
81
|
+
if (!classNames?.get(mediaQuery)?.has(className)) {
|
|
82
|
+
this.mainMap.get(mediaQuery)?.delete(className)
|
|
83
|
+
this.sheet.syncClass(mediaQuery, className)
|
|
84
|
+
}
|
|
85
|
+
})
|
|
86
|
+
forEachClassName(classNames, this.sheet.syncClass)
|
|
87
|
+
|
|
88
|
+
// syncClass already brings back the rules the tag lost, refresh does it when nothing was synced
|
|
89
|
+
if (!previousClassNames && !classNames) {
|
|
90
|
+
this.sheet.refresh()
|
|
72
91
|
}
|
|
73
92
|
}
|
|
74
93
|
|
|
@@ -92,30 +111,36 @@ export class CSSState {
|
|
|
92
111
|
// ScopedTheme renders an element with data-unistyles-theme (or data-unistyles-reset for `reset`),
|
|
93
112
|
// so the variables follow the closest scope and styles don't need to know it while they render
|
|
94
113
|
if (theme === 'light' || theme === 'dark') {
|
|
95
|
-
this.
|
|
114
|
+
this.setThemeCSS(
|
|
96
115
|
`media ${theme}`,
|
|
97
116
|
`@media (prefers-color-scheme: ${theme}){:root,[data-unistyles-reset]{${themeVars}}}`,
|
|
98
117
|
)
|
|
99
118
|
}
|
|
100
119
|
|
|
101
|
-
this.
|
|
120
|
+
this.setThemeCSS(
|
|
102
121
|
theme,
|
|
103
122
|
`:root.${theme},:root.${theme} [data-unistyles-reset],[data-unistyles-theme="${theme}"]{${themeVars}}`,
|
|
104
123
|
)
|
|
105
124
|
}
|
|
106
125
|
|
|
126
|
+
private setThemeCSS = (key: string, css: string) => {
|
|
127
|
+
if (this.sheet.syncTheme(key, css)) {
|
|
128
|
+
this.themesCSS.set(key, css)
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
107
132
|
remove = (hash: string) => {
|
|
108
|
-
const
|
|
133
|
+
const classNames = this.owners.get(hash)
|
|
109
134
|
|
|
110
|
-
this.
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
this.mqMap.forEach((styles) => {
|
|
115
|
-
styles.delete(hash)
|
|
116
|
-
styles.delete(childrenClassName)
|
|
135
|
+
this.owners.delete(hash)
|
|
136
|
+
forEachClassName(classNames, (mediaQuery, className) => {
|
|
137
|
+
this.mainMap.get(mediaQuery)?.delete(className)
|
|
138
|
+
this.sheet.syncClass(mediaQuery, className)
|
|
117
139
|
})
|
|
118
|
-
|
|
140
|
+
|
|
141
|
+
if (!classNames) {
|
|
142
|
+
this.sheet.refresh()
|
|
143
|
+
}
|
|
119
144
|
}
|
|
120
145
|
|
|
121
146
|
getStyles = () => {
|
|
@@ -123,23 +148,13 @@ export class CSSState {
|
|
|
123
148
|
return acc + themeCss
|
|
124
149
|
}, '')
|
|
125
150
|
|
|
126
|
-
const
|
|
151
|
+
for (const [mediaQuery, secondLevelMap] of this.mainMap) {
|
|
127
152
|
if (mediaQuery) {
|
|
128
153
|
styles += `${mediaQuery}{`
|
|
129
154
|
}
|
|
130
155
|
|
|
131
156
|
for (const [className, thirdLevelMap] of secondLevelMap) {
|
|
132
|
-
styles +=
|
|
133
|
-
|
|
134
|
-
for (const [propertyKey, value] of thirdLevelMap) {
|
|
135
|
-
if (value === undefined) {
|
|
136
|
-
continue
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
styles += `${hyphenate(propertyKey)}:${value};`
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
styles += '}'
|
|
157
|
+
styles += `${getSelector(className)}{${getDeclarations(thirdLevelMap)}}`
|
|
143
158
|
}
|
|
144
159
|
|
|
145
160
|
if (mediaQuery) {
|
|
@@ -147,43 +162,38 @@ export class CSSState {
|
|
|
147
162
|
}
|
|
148
163
|
}
|
|
149
164
|
|
|
150
|
-
for (const [mediaQuery, secondLevelMap] of this.mainMap) {
|
|
151
|
-
generate(mediaQuery, secondLevelMap)
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
for (const [mediaQuery, secondLevelMap] of this.mqMap) {
|
|
155
|
-
generate(mediaQuery, secondLevelMap)
|
|
156
|
-
}
|
|
157
|
-
|
|
158
165
|
return styles
|
|
159
166
|
}
|
|
160
167
|
|
|
161
168
|
getState = () => {
|
|
162
|
-
const
|
|
163
|
-
return
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
const mainState = getState(this.mainMap)
|
|
179
|
-
const mqState = getState(this.mqMap)
|
|
169
|
+
const mainState = Array.from(this.mainMap).map(([mediaQuery, classNames]) => {
|
|
170
|
+
return [
|
|
171
|
+
mediaQuery,
|
|
172
|
+
Array.from(classNames.entries()).map(([className, style]) => {
|
|
173
|
+
return [
|
|
174
|
+
className,
|
|
175
|
+
Array.from(style.entries()).map(([property, value]) => {
|
|
176
|
+
return [property, value]
|
|
177
|
+
}),
|
|
178
|
+
]
|
|
179
|
+
}),
|
|
180
|
+
]
|
|
181
|
+
}) as HydrateState
|
|
182
|
+
// Always empty, media query styles live in mainState. Kept for compatibility, drop it in 4.0
|
|
183
|
+
const mqState: HydrateState = []
|
|
180
184
|
const config = this.services.state.getConfig()
|
|
181
185
|
|
|
182
186
|
return { mainState, mqState, config }
|
|
183
187
|
}
|
|
184
188
|
|
|
185
|
-
hydrate = (
|
|
186
|
-
|
|
189
|
+
hydrate = (state: ReturnType<typeof this.getState>) => {
|
|
190
|
+
if (state === this.hydratedState) {
|
|
191
|
+
return
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
this.hydratedState = state
|
|
195
|
+
|
|
196
|
+
const hydrateState = (map: HydrateState) => {
|
|
187
197
|
map.forEach(([mediaQuery, classNames]) => {
|
|
188
198
|
classNames.forEach(([className, style]) => {
|
|
189
199
|
style.forEach(([propertyKey, value]) => {
|
|
@@ -192,19 +202,22 @@ export class CSSState {
|
|
|
192
202
|
propertyKey,
|
|
193
203
|
value,
|
|
194
204
|
mediaQuery,
|
|
195
|
-
isMq,
|
|
196
205
|
})
|
|
197
206
|
})
|
|
207
|
+
this.sheet.syncClass(mediaQuery, className)
|
|
198
208
|
})
|
|
199
209
|
})
|
|
200
210
|
}
|
|
201
211
|
|
|
202
|
-
hydrateState(mainState)
|
|
203
|
-
hydrateState(mqState
|
|
212
|
+
hydrateState(state.mainState)
|
|
213
|
+
hydrateState(state.mqState)
|
|
204
214
|
}
|
|
205
215
|
|
|
206
216
|
reset = () => {
|
|
207
|
-
this.mqMap.clear()
|
|
208
217
|
this.mainMap.clear()
|
|
218
|
+
this.mainMap.set('', new Map())
|
|
219
|
+
this.owners.clear()
|
|
220
|
+
this.hydratedState = undefined
|
|
221
|
+
this.sheet.reset()
|
|
209
222
|
}
|
|
210
223
|
}
|
package/src/web/css/utils.ts
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
import type { CSSState } from './state'
|
|
2
|
-
|
|
3
|
-
import { hyphenate } from '../utils'
|
|
4
|
-
|
|
5
1
|
export const safeGetMap = (map: Map<string, Map<string, any>>, key: string) => {
|
|
6
2
|
const nextLevelMap = map.get(key)
|
|
7
3
|
|
|
@@ -15,41 +11,3 @@ export const safeGetMap = (map: Map<string, Map<string, any>>, key: string) => {
|
|
|
15
11
|
|
|
16
12
|
return nextLevelMap
|
|
17
13
|
}
|
|
18
|
-
|
|
19
|
-
export const getStylesFromState = (state: CSSState) => {
|
|
20
|
-
let styles = ''
|
|
21
|
-
|
|
22
|
-
const generate = (mediaQuery: string, secondLevelMap: Map<string, Map<string, string>>) => {
|
|
23
|
-
if (mediaQuery) {
|
|
24
|
-
styles += `${mediaQuery}{`
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
for (const [className, thirdLevelMap] of secondLevelMap) {
|
|
28
|
-
styles += `.${className}{`
|
|
29
|
-
|
|
30
|
-
for (const [propertyKey, value] of thirdLevelMap) {
|
|
31
|
-
if (value === undefined) {
|
|
32
|
-
continue
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
styles += `${hyphenate(propertyKey)}:${value};`
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
styles += '}'
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
if (mediaQuery) {
|
|
42
|
-
styles += '}'
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
for (const [mediaQuery, secondLevelMap] of state.mainMap) {
|
|
47
|
-
generate(mediaQuery, secondLevelMap)
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
for (const [mediaQuery, secondLevelMap] of state.mqMap) {
|
|
51
|
-
generate(mediaQuery, secondLevelMap)
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
return styles
|
|
55
|
-
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import type { UnistylesMiniRuntime } from '../specs'
|
|
2
|
+
|
|
3
|
+
import { UnistyleDependency } from '../specs/NativePlatform/NativePlatform.nitro'
|
|
4
|
+
|
|
5
|
+
// Without the Babel plugin (react-native-unistyles/metro) StyleSheets don't carry `uni__dependencies`.
|
|
6
|
+
// Instead, the mini runtime records what a StyleSheet or a dynamic function reads while it runs.
|
|
7
|
+
const scopes: Array<Set<UnistyleDependency>> = []
|
|
8
|
+
|
|
9
|
+
// the same table as TrackedMiniRuntime in cxx/core/DependencyTracker.h
|
|
10
|
+
export const MINI_RUNTIME_DEPENDENCIES: Partial<Record<string, UnistyleDependency>> = {
|
|
11
|
+
themeName: UnistyleDependency.ThemeName,
|
|
12
|
+
breakpoint: UnistyleDependency.Breakpoints,
|
|
13
|
+
hasAdaptiveThemes: UnistyleDependency.AdaptiveThemes,
|
|
14
|
+
colorScheme: UnistyleDependency.ColorScheme,
|
|
15
|
+
screen: UnistyleDependency.Dimensions,
|
|
16
|
+
contentSizeCategory: UnistyleDependency.ContentSizeCategory,
|
|
17
|
+
pixelRatio: UnistyleDependency.PixelRatio,
|
|
18
|
+
fontScale: UnistyleDependency.FontScale,
|
|
19
|
+
rtl: UnistyleDependency.Rtl,
|
|
20
|
+
statusBar: UnistyleDependency.StatusBar,
|
|
21
|
+
navigationBar: UnistyleDependency.NavigationBar,
|
|
22
|
+
isPortrait: UnistyleDependency.Orientation,
|
|
23
|
+
isLandscape: UnistyleDependency.Orientation,
|
|
24
|
+
// insets are tracked by their own object, ime is a separate dependency
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const recordDependency = (dependency: UnistyleDependency) => {
|
|
28
|
+
scopes.at(-1)?.add(dependency)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// runs the function and returns every dependency it read, reads outside of any scope are ignored
|
|
32
|
+
export const trackDependencies = <T>(fn: () => T) => {
|
|
33
|
+
const dependencies = new Set<UnistyleDependency>()
|
|
34
|
+
|
|
35
|
+
scopes.push(dependencies)
|
|
36
|
+
|
|
37
|
+
try {
|
|
38
|
+
return { result: fn(), dependencies: Array.from(dependencies) }
|
|
39
|
+
} finally {
|
|
40
|
+
scopes.pop()
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// eg. rt.screen.width -> Dimensions, rt.insets.top -> Insets, rt.insets.ime -> Ime
|
|
45
|
+
export const createTrackedMiniRuntime = (miniRuntime: UnistylesMiniRuntime): UnistylesMiniRuntime => {
|
|
46
|
+
const insets = new Proxy(miniRuntime.insets, {
|
|
47
|
+
get: (target, property, receiver) => {
|
|
48
|
+
if (typeof property === 'string' && property in target) {
|
|
49
|
+
recordDependency(property === 'ime' ? UnistyleDependency.Ime : UnistyleDependency.Insets)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return Reflect.get(target, property, receiver)
|
|
53
|
+
},
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
return new Proxy(miniRuntime, {
|
|
57
|
+
get: (target, property, receiver) => {
|
|
58
|
+
const dependency = typeof property === 'string' ? MINI_RUNTIME_DEPENDENCIES[property] : undefined
|
|
59
|
+
|
|
60
|
+
if (dependency !== undefined) {
|
|
61
|
+
recordDependency(dependency)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (property === 'insets') {
|
|
65
|
+
return insets
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return Reflect.get(target, property, receiver)
|
|
69
|
+
},
|
|
70
|
+
})
|
|
71
|
+
}
|