react-native-unistyles 3.0.0-alpha.20 → 3.0.0-alpha.22
Sign up to get free protection for your applications and to get access to all the features.
- package/cxx/common/Constants.h +1 -0
- package/cxx/core/UnistyleWrapper.h +21 -41
- package/cxx/hybridObjects/HybridStyleSheet.cpp +1 -1
- package/cxx/hybridObjects/HybridStyleSheet.h +1 -1
- package/lib/commonjs/web/convert/boxShadow.js +13 -13
- package/lib/commonjs/web/convert/boxShadow.js.map +1 -1
- package/lib/commonjs/web/convert/index.js +9 -11
- package/lib/commonjs/web/convert/index.js.map +1 -1
- package/lib/commonjs/web/convert/pseudo.js.map +1 -0
- package/lib/commonjs/web/convert/style.js +11 -7
- package/lib/commonjs/web/convert/style.js.map +1 -1
- package/lib/commonjs/web/convert/textShadow.js +12 -12
- package/lib/commonjs/web/convert/textShadow.js.map +1 -1
- package/lib/commonjs/web/convert/transform.js +5 -5
- package/lib/commonjs/web/convert/transform.js.map +1 -1
- package/lib/commonjs/web/{listener/listener.js → listener.js} +2 -2
- package/lib/commonjs/web/listener.js.map +1 -0
- package/lib/commonjs/web/mq.js.map +1 -1
- package/lib/commonjs/web/registry.js +159 -21
- package/lib/commonjs/web/registry.js.map +1 -1
- package/lib/commonjs/web/shadowRegistry.js +50 -48
- package/lib/commonjs/web/shadowRegistry.js.map +1 -1
- package/lib/commonjs/web/state.js +8 -16
- package/lib/commonjs/web/state.js.map +1 -1
- package/lib/commonjs/web/{utils.js → utils/common.js} +9 -47
- package/lib/commonjs/web/utils/common.js.map +1 -0
- package/lib/commonjs/web/utils/index.js +28 -0
- package/lib/commonjs/web/utils/index.js.map +1 -0
- package/lib/commonjs/web/utils/unistyle.js +82 -0
- package/lib/commonjs/web/utils/unistyle.js.map +1 -0
- package/lib/module/web/convert/boxShadow.js +13 -13
- package/lib/module/web/convert/boxShadow.js.map +1 -1
- package/lib/module/web/convert/index.js +7 -9
- package/lib/module/web/convert/index.js.map +1 -1
- package/lib/module/web/convert/pseudo.js.map +1 -0
- package/lib/module/web/convert/style.js +11 -7
- package/lib/module/web/convert/style.js.map +1 -1
- package/lib/module/web/convert/textShadow.js +12 -12
- package/lib/module/web/convert/textShadow.js.map +1 -1
- package/lib/module/web/convert/transform.js +5 -5
- package/lib/module/web/convert/transform.js.map +1 -1
- package/lib/module/web/{listener/listener.js → listener.js} +2 -2
- package/lib/module/web/listener.js.map +1 -0
- package/lib/module/web/mq.js.map +1 -1
- package/lib/module/web/registry.js +160 -22
- package/lib/module/web/registry.js.map +1 -1
- package/lib/module/web/shadowRegistry.js +51 -49
- package/lib/module/web/shadowRegistry.js.map +1 -1
- package/lib/module/web/state.js +8 -16
- package/lib/module/web/state.js.map +1 -1
- package/lib/module/web/{utils.js → utils/common.js} +7 -39
- package/lib/module/web/utils/common.js.map +1 -0
- package/lib/module/web/utils/index.js +5 -0
- package/lib/module/web/utils/index.js.map +1 -0
- package/lib/module/web/utils/unistyle.js +69 -0
- package/lib/module/web/utils/unistyle.js.map +1 -0
- package/lib/typescript/expo-example/app/(tabs)/_layout.d.ts +3 -0
- package/lib/typescript/expo-example/app/(tabs)/_layout.d.ts.map +1 -0
- package/lib/typescript/expo-example/app/(tabs)/explore.d.ts +3 -0
- package/lib/typescript/expo-example/app/(tabs)/explore.d.ts.map +1 -0
- package/lib/typescript/expo-example/app/(tabs)/index.d.ts +3 -0
- package/lib/typescript/expo-example/app/(tabs)/index.d.ts.map +1 -0
- package/lib/typescript/expo-example/app/+html.d.ts +4 -0
- package/lib/typescript/expo-example/app/+html.d.ts.map +1 -0
- package/lib/typescript/expo-example/app/+not-found.d.ts +3 -0
- package/lib/typescript/expo-example/app/+not-found.d.ts.map +1 -0
- package/lib/typescript/expo-example/app/_layout.d.ts +3 -0
- package/lib/typescript/expo-example/app/_layout.d.ts.map +1 -0
- package/lib/typescript/src/specs/StyleSheet/UnistylesStyleSheet.nitro.d.ts +1 -1
- package/lib/typescript/src/specs/StyleSheet/UnistylesStyleSheet.nitro.d.ts.map +1 -1
- package/lib/typescript/src/types/stylesheet.d.ts +1 -1
- package/lib/typescript/src/types/stylesheet.d.ts.map +1 -1
- package/lib/typescript/src/web/convert/boxShadow.d.ts +7 -2
- package/lib/typescript/src/web/convert/boxShadow.d.ts.map +1 -1
- package/lib/typescript/src/web/convert/index.d.ts +1 -2
- package/lib/typescript/src/web/convert/index.d.ts.map +1 -1
- package/lib/typescript/src/web/convert/pseudo.d.ts.map +1 -0
- package/lib/typescript/src/web/convert/style.d.ts +1 -2
- package/lib/typescript/src/web/convert/style.d.ts.map +1 -1
- package/lib/typescript/src/web/convert/textShadow.d.ts +7 -2
- package/lib/typescript/src/web/convert/textShadow.d.ts.map +1 -1
- package/lib/typescript/src/web/convert/transform.d.ts +1 -2
- package/lib/typescript/src/web/convert/transform.d.ts.map +1 -1
- package/lib/typescript/src/web/{listener/listener.d.ts → listener.d.ts} +1 -1
- package/lib/typescript/src/web/listener.d.ts.map +1 -0
- package/lib/typescript/src/web/mq.d.ts +6 -2
- package/lib/typescript/src/web/mq.d.ts.map +1 -1
- package/lib/typescript/src/web/registry.d.ts +21 -8
- package/lib/typescript/src/web/registry.d.ts.map +1 -1
- package/lib/typescript/src/web/shadowRegistry.d.ts +3 -4
- package/lib/typescript/src/web/shadowRegistry.d.ts.map +1 -1
- package/lib/typescript/src/web/state.d.ts +2 -2
- package/lib/typescript/src/web/state.d.ts.map +1 -1
- package/lib/typescript/src/web/{utils.d.ts → utils/common.d.ts} +2 -20
- package/lib/typescript/src/web/utils/common.d.ts.map +1 -0
- package/lib/typescript/src/web/utils/index.d.ts +3 -0
- package/lib/typescript/src/web/utils/index.d.ts.map +1 -0
- package/lib/typescript/src/web/utils/unistyle.d.ts +20 -0
- package/lib/typescript/src/web/utils/unistyle.d.ts.map +1 -0
- package/nitrogen/generated/android/c++/JColorScheme.hpp +1 -1
- package/nitrogen/generated/android/c++/JFunc_void_std__vector_UnistyleDependency_.hpp +8 -8
- package/nitrogen/generated/android/c++/JHybridNativePlatformSpec.cpp +22 -22
- package/nitrogen/generated/android/c++/JOrientation.hpp +1 -1
- package/nitrogen/generated/android/c++/JUnistyleDependency.hpp +1 -1
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/unistyles/HybridNativePlatformSpec.kt +2 -2
- package/nitrogen/generated/android/{UnistylesOnLoad.cpp → unistylesOnLoad.cpp} +2 -2
- package/nitrogen/generated/android/{UnistylesOnLoad.hpp → unistylesOnLoad.hpp} +1 -1
- package/nitrogen/generated/ios/Unistyles+autolinking.rb +5 -3
- package/nitrogen/generated/ios/Unistyles-Swift-Cxx-Bridge.cpp +25 -0
- package/nitrogen/generated/ios/Unistyles-Swift-Cxx-Bridge.hpp +32 -32
- package/nitrogen/generated/ios/Unistyles-Swift-Cxx-Umbrella.hpp +0 -29
- package/nitrogen/generated/ios/swift/HybridNativePlatformSpec.swift +0 -12
- package/nitrogen/generated/ios/swift/HybridNativePlatformSpecCxx.swift +134 -110
- package/nitrogen/generated/shared/c++/ColorScheme.hpp +2 -2
- package/nitrogen/generated/shared/c++/HybridUnistylesStyleSheetSpec.cpp +1 -1
- package/nitrogen/generated/shared/c++/HybridUnistylesStyleSheetSpec.hpp +1 -1
- package/nitrogen/generated/shared/c++/Orientation.hpp +2 -2
- package/package.json +4 -15
- package/src/specs/StyleSheet/UnistylesStyleSheet.nitro.ts +1 -1
- package/src/types/stylesheet.ts +1 -1
- package/src/web/convert/boxShadow.ts +14 -15
- package/src/web/convert/index.ts +6 -13
- package/src/web/convert/style.ts +61 -10
- package/src/web/convert/textShadow.ts +13 -14
- package/src/web/convert/transform.ts +7 -8
- package/src/web/{listener/listener.ts → listener.ts} +2 -2
- package/src/web/mq.ts +1 -3
- package/src/web/registry.ts +202 -23
- package/src/web/shadowRegistry.ts +49 -49
- package/src/web/state.ts +13 -23
- package/src/web/{utils.ts → utils/common.ts} +6 -68
- package/src/web/utils/index.ts +2 -0
- package/src/web/utils/unistyle.ts +104 -0
- package/lib/commonjs/web/convert/breakpoint.js +0 -25
- package/lib/commonjs/web/convert/breakpoint.js.map +0 -1
- package/lib/commonjs/web/createUnistylesComponent.js +0 -61
- package/lib/commonjs/web/createUnistylesComponent.js.map +0 -1
- package/lib/commonjs/web/listener/index.js +0 -20
- package/lib/commonjs/web/listener/index.js.map +0 -1
- package/lib/commonjs/web/listener/listenToDependencies.js +0 -38
- package/lib/commonjs/web/listener/listenToDependencies.js.map +0 -1
- package/lib/commonjs/web/listener/listener.js.map +0 -1
- package/lib/commonjs/web/pseudo.js.map +0 -1
- package/lib/commonjs/web/utils.js.map +0 -1
- package/lib/module/web/convert/breakpoint.js +0 -20
- package/lib/module/web/convert/breakpoint.js.map +0 -1
- package/lib/module/web/createUnistylesComponent.js +0 -54
- package/lib/module/web/createUnistylesComponent.js.map +0 -1
- package/lib/module/web/listener/index.js +0 -5
- package/lib/module/web/listener/index.js.map +0 -1
- package/lib/module/web/listener/listenToDependencies.js +0 -33
- package/lib/module/web/listener/listenToDependencies.js.map +0 -1
- package/lib/module/web/listener/listener.js.map +0 -1
- package/lib/module/web/pseudo.js.map +0 -1
- package/lib/module/web/utils.js.map +0 -1
- package/lib/typescript/src/web/convert/breakpoint.d.ts +0 -3
- package/lib/typescript/src/web/convert/breakpoint.d.ts.map +0 -1
- package/lib/typescript/src/web/createUnistylesComponent.d.ts +0 -3
- package/lib/typescript/src/web/createUnistylesComponent.d.ts.map +0 -1
- package/lib/typescript/src/web/listener/index.d.ts +0 -3
- package/lib/typescript/src/web/listener/index.d.ts.map +0 -1
- package/lib/typescript/src/web/listener/listenToDependencies.d.ts +0 -12
- package/lib/typescript/src/web/listener/listenToDependencies.d.ts.map +0 -1
- package/lib/typescript/src/web/listener/listener.d.ts.map +0 -1
- package/lib/typescript/src/web/pseudo.d.ts.map +0 -1
- package/lib/typescript/src/web/utils.d.ts.map +0 -1
- package/src/web/convert/breakpoint.ts +0 -21
- package/src/web/createUnistylesComponent.tsx +0 -54
- package/src/web/listener/index.ts +0 -2
- package/src/web/listener/listenToDependencies.ts +0 -45
- /package/lib/commonjs/web/{pseudo.js → convert/pseudo.js} +0 -0
- /package/lib/module/web/{pseudo.js → convert/pseudo.js} +0 -0
- /package/lib/typescript/src/web/{pseudo.d.ts → convert/pseudo.d.ts} +0 -0
- /package/nitrogen/generated/android/{UnistylesOnLoad.kt → unistylesOnLoad.kt} +0 -0
- /package/src/web/{pseudo.ts → convert/pseudo.ts} +0 -0
package/src/web/convert/index.ts
CHANGED
@@ -1,8 +1,5 @@
|
|
1
|
-
import type { NestedCSSProperties } from 'typestyle/lib/types'
|
2
1
|
import type { UnistylesValues } from '../../types'
|
3
|
-
import {
|
4
|
-
import { isPseudo } from '../pseudo'
|
5
|
-
import { convertBreakpoint } from './breakpoint'
|
2
|
+
import { isPseudo } from './pseudo'
|
6
3
|
import { getStyle } from './style'
|
7
4
|
import { deepMergeObjects } from '../utils'
|
8
5
|
import { getTransformStyle } from './transform'
|
@@ -10,7 +7,7 @@ import { isBoxShadow, isTextShadow, isTransform } from './utils'
|
|
10
7
|
import { getTextShadowStyle } from './textShadow'
|
11
8
|
import { getBoxShadowStyle } from './boxShadow'
|
12
9
|
|
13
|
-
export const
|
10
|
+
export const convertUnistyles = (value: UnistylesValues) => {
|
14
11
|
// Flag to mark if textShadow is already created
|
15
12
|
let hasTextShadow = false
|
16
13
|
// Flag to mark if boxShadow is already created
|
@@ -27,13 +24,9 @@ export const convertToTypeStyle = (value: UnistylesValues) => {
|
|
27
24
|
|
28
25
|
// Pseudo classes :hover, :before etc.
|
29
26
|
if (isPseudo(unistylesKey)) {
|
30
|
-
const
|
27
|
+
const flattenValues = convertUnistyles(unistylesValue as UnistylesValues)
|
31
28
|
|
32
|
-
return {
|
33
|
-
$nest: {
|
34
|
-
[unistylesKey.replace('_', '&:')]: typestyleValues
|
35
|
-
}
|
36
|
-
}
|
29
|
+
return { [unistylesKey]: flattenValues }
|
37
30
|
}
|
38
31
|
|
39
32
|
// Text shadow
|
@@ -66,13 +59,13 @@ export const convertToTypeStyle = (value: UnistylesValues) => {
|
|
66
59
|
// Breakpoints
|
67
60
|
if (typeof unistylesValue === 'object' && unistylesValue !== null) {
|
68
61
|
return Object.entries(unistylesValue).map(([breakpointKey, breakpointValue]) => {
|
69
|
-
return
|
62
|
+
return { [breakpointKey]: getStyle(unistylesKey, breakpointValue) }
|
70
63
|
})
|
71
64
|
}
|
72
65
|
|
73
66
|
// Regular styles
|
74
67
|
return getStyle(unistylesKey, unistylesValue)
|
75
|
-
}) as Array<
|
68
|
+
}) as Array<Record<string, any>>
|
76
69
|
|
77
70
|
return deepMergeObjects(...stylesArray)
|
78
71
|
}
|
package/src/web/convert/style.ts
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
import { keyInObject } from '../utils'
|
2
|
-
import type { NestedCSSProperties } from 'typestyle/lib/types'
|
3
2
|
|
4
|
-
const
|
3
|
+
const SKIP_STYLES = [
|
5
4
|
'borderCurve',
|
6
5
|
'elevation',
|
7
6
|
'textAlignVertical',
|
@@ -10,6 +9,53 @@ const stylesToSkip = [
|
|
10
9
|
'tintColor'
|
11
10
|
]
|
12
11
|
|
12
|
+
const CSS_NUMBER_KEYS = [
|
13
|
+
'animationIterationCount',
|
14
|
+
'borderImageOutset',
|
15
|
+
'borderImageSlice',
|
16
|
+
'borderImageWidth',
|
17
|
+
'boxFlex',
|
18
|
+
'boxFlexGroup',
|
19
|
+
'boxOrdinalGroup',
|
20
|
+
'columnCount',
|
21
|
+
'columns',
|
22
|
+
'counterIncrement',
|
23
|
+
'counterReset',
|
24
|
+
'flex',
|
25
|
+
'flexGrow',
|
26
|
+
'flexPositive',
|
27
|
+
'flexShrink',
|
28
|
+
'flexNegative',
|
29
|
+
'flexOrder',
|
30
|
+
'fontWeight',
|
31
|
+
'gridArea',
|
32
|
+
'gridColumn',
|
33
|
+
'gridColumnEnd',
|
34
|
+
'gridColumnSpan',
|
35
|
+
'gridColumnStart',
|
36
|
+
'gridRow',
|
37
|
+
'gridRowEnd',
|
38
|
+
'gridRowSpan',
|
39
|
+
'gridRowStart',
|
40
|
+
'line-clamp',
|
41
|
+
'line-height',
|
42
|
+
'opacity',
|
43
|
+
'order',
|
44
|
+
'orphans',
|
45
|
+
'tabSize',
|
46
|
+
'widows',
|
47
|
+
'zIndex',
|
48
|
+
'zoom',
|
49
|
+
'fillOpacity',
|
50
|
+
'floodOpacity',
|
51
|
+
'stopOpacity',
|
52
|
+
'strokeDasharray',
|
53
|
+
'strokeDashoffset',
|
54
|
+
'strokeMiterlimit',
|
55
|
+
'strokeOpacity',
|
56
|
+
'strokeWidth'
|
57
|
+
]
|
58
|
+
|
13
59
|
const convertMap = {
|
14
60
|
marginHorizontal: (value: number) => ({
|
15
61
|
marginInline: value
|
@@ -73,22 +119,27 @@ const convertMap = {
|
|
73
119
|
}),
|
74
120
|
resizeMode: (value: string) => ({
|
75
121
|
backgroundSize: value
|
76
|
-
})
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
122
|
+
})
|
123
|
+
} as Record<PropertyKey, (value: any) => Record<string, any>>
|
124
|
+
|
125
|
+
const convertNumber = (key: string, value: any) => {
|
126
|
+
if (typeof value === 'number') {
|
127
|
+
return CSS_NUMBER_KEYS.includes(key, value) ? value : `${value}px`
|
128
|
+
}
|
129
|
+
|
130
|
+
return value
|
131
|
+
}
|
81
132
|
|
82
133
|
export const getStyle = (key: string, value: any) => {
|
83
|
-
if (
|
134
|
+
if (SKIP_STYLES.includes(key)) {
|
84
135
|
return {}
|
85
136
|
}
|
86
137
|
|
87
138
|
if (keyInObject(convertMap, key)) {
|
88
|
-
return convertMap[key]?.(value) ?? {}
|
139
|
+
return convertMap[key]?.(convertNumber(key, value)) ?? {}
|
89
140
|
}
|
90
141
|
|
91
142
|
return {
|
92
|
-
[key]: value
|
143
|
+
[key]: convertNumber(key, value)
|
93
144
|
}
|
94
145
|
}
|
@@ -1,8 +1,5 @@
|
|
1
|
-
import { media } from 'typestyle'
|
2
|
-
import type { NestedCSSProperties } from 'typestyle/lib/types'
|
3
1
|
import { deepMergeObjects, warn } from '../utils'
|
4
2
|
import { validateShadow } from './shadow'
|
5
|
-
import { convertBreakpoint } from './breakpoint'
|
6
3
|
import { TEXT_SHADOW_STYLES, type TextShadow } from './types'
|
7
4
|
import { extractShadowValue, normalizeColor, normalizeNumericValue } from './utils'
|
8
5
|
|
@@ -16,7 +13,7 @@ const createTextShadowValue = (style: TextShadow) => {
|
|
16
13
|
return `${offsetX} ${offsetY} ${radius} ${color}`
|
17
14
|
}
|
18
15
|
|
19
|
-
export const getTextShadowStyle = (styles: Record<string, any>)
|
16
|
+
export const getTextShadowStyle = (styles: Record<string, any>) => {
|
20
17
|
const missingStyles = TEXT_SHADOW_STYLES.filter(key => !(key in styles))
|
21
18
|
|
22
19
|
if (missingStyles.length) {
|
@@ -52,16 +49,18 @@ export const getTextShadowStyle = (styles: Record<string, any>): NestedCSSProper
|
|
52
49
|
const { width, height } = extractShadowValue('textShadowOffset', breakpoint, styles)
|
53
50
|
const radius = extractShadowValue('textShadowRadius', breakpoint, styles)
|
54
51
|
|
55
|
-
return
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
52
|
+
return {
|
53
|
+
[breakpoint]: {
|
54
|
+
textShadow: createTextShadowValue({
|
55
|
+
textShadowColor: color,
|
56
|
+
textShadowOffset: {
|
57
|
+
width,
|
58
|
+
height
|
59
|
+
},
|
60
|
+
textShadowRadius: radius
|
61
|
+
})
|
62
|
+
}
|
63
|
+
}
|
65
64
|
})
|
66
65
|
|
67
66
|
// Merge all breakpoints styles into one
|
@@ -1,8 +1,5 @@
|
|
1
|
-
import { media } from 'typestyle'
|
2
|
-
import type { NestedCSSProperties } from 'typestyle/lib/types'
|
3
1
|
import { deepMergeObjects, keyInObject } from '../utils'
|
4
2
|
import { normalizeNumericValue } from './utils'
|
5
|
-
import { convertBreakpoint } from './breakpoint'
|
6
3
|
|
7
4
|
type Transforms = Array<Record<string, any>>
|
8
5
|
|
@@ -39,7 +36,7 @@ const createTransformValue = (transforms: Transforms) => transforms
|
|
39
36
|
.filter(Boolean)
|
40
37
|
.join(' ')
|
41
38
|
|
42
|
-
export const getTransformStyle = (transforms: Transforms)
|
39
|
+
export const getTransformStyle = (transforms: Transforms) => {
|
43
40
|
const breakpoints = new Set<string>()
|
44
41
|
const normalTransforms: Transforms = []
|
45
42
|
|
@@ -78,12 +75,14 @@ export const getTransformStyle = (transforms: Transforms): NestedCSSProperties =
|
|
78
75
|
return []
|
79
76
|
})
|
80
77
|
|
81
|
-
return
|
82
|
-
|
83
|
-
|
78
|
+
return [{
|
79
|
+
[breakpoint]: {
|
80
|
+
transform: createTransformValue(transformsPerBreakpoint)
|
81
|
+
}
|
82
|
+
}]
|
84
83
|
})
|
85
84
|
|
86
|
-
return deepMergeObjects({
|
85
|
+
return deepMergeObjects<Record<string, any>>({
|
87
86
|
transform: createTransformValue(normalTransforms)
|
88
87
|
}, ...breakpointTransforms)
|
89
88
|
}
|
@@ -1,5 +1,5 @@
|
|
1
|
-
import { UnistyleDependency } from '
|
2
|
-
import { UnistylesRuntime } from '
|
1
|
+
import { UnistyleDependency } from '../specs/NativePlatform'
|
2
|
+
import { UnistylesRuntime } from './runtime'
|
3
3
|
|
4
4
|
class UnistylesListenerBuilder {
|
5
5
|
private isInitialized = false
|
package/src/web/mq.ts
CHANGED
@@ -1,10 +1,8 @@
|
|
1
|
-
import type { MediaQuery } from 'typestyle/lib/types'
|
2
|
-
|
3
1
|
const IS_UNISTYLES_REGEX = /:([hw])\[(\d+)(?:,\s*(\d+|Infinity))?]/
|
4
2
|
const UNISTYLES_WIDTH_REGEX = /:(w)\[(\d+)(?:,\s*(\d+|Infinity))?]/
|
5
3
|
const UNISTYLES_HEIGHT_REGEX = /:(h)\[(\d+)(?:,\s*(\d+|Infinity))?]/
|
6
4
|
|
7
|
-
export const parseMq = (mq: string)
|
5
|
+
export const parseMq = (mq: string) => {
|
8
6
|
const [, width, fromW, toW] = UNISTYLES_WIDTH_REGEX.exec(mq) || []
|
9
7
|
const [, height, fromH, toH] = UNISTYLES_HEIGHT_REGEX.exec(mq) || []
|
10
8
|
|
package/src/web/registry.ts
CHANGED
@@ -1,13 +1,52 @@
|
|
1
|
-
import {
|
2
|
-
import type { UnistylesValues } from '../types'
|
3
|
-
import { convertToTypeStyle } from './convert'
|
1
|
+
import type { UnistylesTheme, UnistylesValues } from '../types'
|
4
2
|
import type { StyleSheet, StyleSheetWithSuperPowers } from '../types/stylesheet'
|
5
3
|
import { UnistylesRuntime } from './runtime'
|
6
|
-
import { keyInObject } from './utils'
|
4
|
+
import { extractMediaQueryValue, keyInObject, getMediaQuery, generateHash, extractUnistyleDependencies } from './utils'
|
7
5
|
import { UnistylesListener } from './listener'
|
6
|
+
import { convertUnistyles } from './convert'
|
7
|
+
import type { UnistyleDependency } from '../specs/NativePlatform'
|
8
|
+
import type { UnistylesMiniRuntime } from '../specs'
|
9
|
+
import { getVariants } from './variants'
|
10
|
+
|
11
|
+
type AddProps = {
|
12
|
+
value: UnistylesValues,
|
13
|
+
stylesheet: StyleSheetWithSuperPowers<StyleSheet>,
|
14
|
+
key: string,
|
15
|
+
args: Array<any>,
|
16
|
+
variants: Record<string, any>
|
17
|
+
}
|
18
|
+
|
19
|
+
type ApplyRuleProps = {
|
20
|
+
hash: string,
|
21
|
+
key: string,
|
22
|
+
value: any,
|
23
|
+
sheet: CSSStyleSheet | CSSMediaRule
|
24
|
+
}
|
25
|
+
|
26
|
+
type RemoveReadonlyStyleKeys<T extends string> = T extends 'length' | 'parentRule' ? never : T
|
8
27
|
|
9
28
|
class UnistylesRegistryBuilder {
|
10
|
-
private stylesheets = new Map<StyleSheetWithSuperPowers<StyleSheet>, StyleSheet>()
|
29
|
+
private readonly stylesheets = new Map<StyleSheetWithSuperPowers<StyleSheet>, StyleSheet>()
|
30
|
+
private readonly stylesCounter = new Map<string, Set<UnistylesValues>>()
|
31
|
+
#styleTag: HTMLStyleElement | null = null
|
32
|
+
private readonly disposeListenersMap = new Map<object, VoidFunction>()
|
33
|
+
private readonly dependenciesMap = new Map<StyleSheetWithSuperPowers<StyleSheet>, Set<UnistyleDependency>>()
|
34
|
+
|
35
|
+
private get styleTag() {
|
36
|
+
const tag = this.#styleTag
|
37
|
+
|
38
|
+
if (!tag) {
|
39
|
+
const newTag = document.createElement('style')
|
40
|
+
|
41
|
+
newTag.id = 'unistyles-web'
|
42
|
+
this.#styleTag = newTag
|
43
|
+
document.head.appendChild(newTag)
|
44
|
+
|
45
|
+
return newTag
|
46
|
+
}
|
47
|
+
|
48
|
+
return tag
|
49
|
+
}
|
11
50
|
|
12
51
|
getComputedStylesheet = (stylesheet: StyleSheetWithSuperPowers<StyleSheet>) => {
|
13
52
|
if (typeof stylesheet !== 'function') {
|
@@ -21,39 +60,179 @@ class UnistylesRegistryBuilder {
|
|
21
60
|
}
|
22
61
|
|
23
62
|
const createdStylesheet = stylesheet(UnistylesRuntime.theme, UnistylesRuntime.miniRuntime)
|
24
|
-
|
25
|
-
|
63
|
+
const dependencies = Object.values(createdStylesheet).flatMap(value => extractUnistyleDependencies(value))
|
64
|
+
|
65
|
+
this.addDependenciesToStylesheet(stylesheet, dependencies)
|
66
|
+
this.stylesheets.set(stylesheet, createdStylesheet)
|
26
67
|
|
27
|
-
|
68
|
+
return createdStylesheet
|
69
|
+
}
|
70
|
+
|
71
|
+
addDependenciesToStylesheet = (stylesheet: (theme: UnistylesTheme, miniRuntime: UnistylesMiniRuntime) => StyleSheet, dependencies: Array<UnistyleDependency>) => {
|
72
|
+
this.disposeListenersMap.get(stylesheet)?.()
|
73
|
+
|
74
|
+
const dependenciesMap = this.dependenciesMap.get(stylesheet) ?? new Set(dependencies)
|
75
|
+
|
76
|
+
dependencies.forEach(dependency => dependenciesMap.add(dependency))
|
77
|
+
|
78
|
+
const dispose = UnistylesListener.addListeners(Array.from(dependenciesMap), () => {
|
28
79
|
const newComputedStylesheet = stylesheet(UnistylesRuntime.theme, UnistylesRuntime.miniRuntime)
|
29
80
|
|
30
81
|
this.stylesheets.set(stylesheet, newComputedStylesheet)
|
31
82
|
})
|
32
83
|
|
33
|
-
this.
|
84
|
+
this.dependenciesMap.set(stylesheet, dependenciesMap)
|
85
|
+
this.disposeListenersMap.set(stylesheet, dispose)
|
86
|
+
}
|
34
87
|
|
35
|
-
|
88
|
+
add = ({ key, value, stylesheet, args, variants }: AddProps) => {
|
89
|
+
const hash = generateHash(value)
|
90
|
+
const existingCounter = this.stylesCounter.get(hash)
|
91
|
+
|
92
|
+
if (!existingCounter || existingCounter.size === 0) {
|
93
|
+
const counter = new Set<UnistylesValues>()
|
94
|
+
const dependencies = extractUnistyleDependencies(value)
|
95
|
+
|
96
|
+
counter.add(value)
|
97
|
+
this.stylesCounter.set(hash, counter)
|
98
|
+
this.applyStyles(hash, convertUnistyles(value))
|
99
|
+
|
100
|
+
UnistylesListener.addListeners(dependencies, async () => {
|
101
|
+
// Move this callback to the end of the event loop
|
102
|
+
await Promise.resolve()
|
103
|
+
|
104
|
+
const newComputedStyleSheet = this.getComputedStylesheet(stylesheet)
|
105
|
+
const newValue = newComputedStyleSheet[key]!
|
106
|
+
const result = typeof newValue === 'function'
|
107
|
+
? newValue(...args)
|
108
|
+
: newValue
|
109
|
+
const { variantsResult } = Object.fromEntries(getVariants({ variantsResult: result }, variants))
|
110
|
+
const resultWithVariants = {
|
111
|
+
...result,
|
112
|
+
...variantsResult
|
113
|
+
}
|
114
|
+
|
115
|
+
this.applyStyles(hash, convertUnistyles(resultWithVariants))
|
116
|
+
})
|
117
|
+
|
118
|
+
return hash
|
119
|
+
}
|
120
|
+
|
121
|
+
existingCounter.add(value)
|
122
|
+
|
123
|
+
return hash
|
36
124
|
}
|
37
125
|
|
38
|
-
|
39
|
-
|
40
|
-
|
126
|
+
private applyStyles = (hash: string, styles: Record<string, any>) => {
|
127
|
+
Object.entries(styles).forEach(([key, value]) => {
|
128
|
+
if (!this.styleTag.sheet) {
|
129
|
+
return
|
130
|
+
}
|
131
|
+
|
132
|
+
if (typeof value === 'object' && !key.startsWith('_')) {
|
133
|
+
const mediaQuery = getMediaQuery(key)
|
134
|
+
const cssRules = Array.from(this.styleTag.sheet.cssRules)
|
135
|
+
let queryRule = cssRules.find(rule => {
|
136
|
+
if (!(rule instanceof CSSMediaRule)) {
|
137
|
+
return false
|
138
|
+
}
|
139
|
+
|
140
|
+
return rule.media.item(0)?.includes(mediaQuery)
|
141
|
+
}) ?? null
|
142
|
+
|
143
|
+
if (!queryRule) {
|
144
|
+
const mediaQueryValue = extractMediaQueryValue(mediaQuery)
|
145
|
+
const ruleIndex = mediaQueryValue
|
146
|
+
? cssRules.reduce<number | undefined>((acc, rule, ruleIndex) => {
|
147
|
+
if (!(rule instanceof CSSMediaRule)) {
|
148
|
+
return acc
|
149
|
+
}
|
150
|
+
|
151
|
+
const ruleMediaQueryValue = extractMediaQueryValue(rule.conditionText)
|
152
|
+
|
153
|
+
if (ruleMediaQueryValue === undefined) {
|
154
|
+
return
|
155
|
+
}
|
156
|
+
|
157
|
+
return ruleMediaQueryValue > mediaQueryValue ? ruleIndex : acc
|
158
|
+
}, cssRules.length)
|
159
|
+
: undefined
|
160
|
+
queryRule = this.styleTag.sheet.cssRules.item(this.styleTag.sheet.insertRule(`@media ${mediaQuery} {.${hash} {}}`, ruleIndex))
|
161
|
+
}
|
162
|
+
|
163
|
+
if (queryRule instanceof CSSMediaRule) {
|
164
|
+
Object.entries(value).forEach(([mqKey, mqValue]) => {
|
165
|
+
this.applyRule({
|
166
|
+
hash,
|
167
|
+
key: mqKey,
|
168
|
+
value: mqValue,
|
169
|
+
sheet: queryRule
|
170
|
+
})
|
171
|
+
})
|
172
|
+
}
|
173
|
+
|
174
|
+
return
|
175
|
+
}
|
176
|
+
|
177
|
+
// Pseudo
|
178
|
+
if (typeof value === 'object') {
|
179
|
+
Object.entries(value).forEach(([pseudoKey, pseudoValue]) => {
|
180
|
+
this.applyRule({
|
181
|
+
hash: `${hash}${key.replace('_', ':')}`,
|
182
|
+
key: pseudoKey,
|
183
|
+
value: pseudoValue,
|
184
|
+
sheet: this.styleTag.sheet!
|
185
|
+
})
|
186
|
+
})
|
41
187
|
|
42
|
-
|
43
|
-
|
44
|
-
}, typestyleStylesheet)
|
188
|
+
return
|
189
|
+
}
|
45
190
|
|
46
|
-
|
47
|
-
|
48
|
-
|
191
|
+
this.applyRule({
|
192
|
+
hash,
|
193
|
+
key,
|
194
|
+
value,
|
195
|
+
sheet: this.styleTag.sheet
|
196
|
+
})
|
197
|
+
})
|
198
|
+
}
|
199
|
+
|
200
|
+
private applyRule = ({ hash, key, value, sheet }: ApplyRuleProps) => {
|
201
|
+
let rule = Array.from(sheet.cssRules).find(rule => {
|
202
|
+
if (!(rule instanceof CSSStyleRule)) {
|
203
|
+
return false
|
204
|
+
}
|
205
|
+
|
206
|
+
// In unistyles pseudos are prefixed with ':' but in css some of them are prefixed with '::'
|
207
|
+
return rule.selectorText.replace('::', ':').includes(hash)
|
208
|
+
}) ?? null
|
209
|
+
|
210
|
+
if (!rule) {
|
211
|
+
rule = sheet.cssRules.item(sheet.insertRule(`.${hash} {}`))
|
212
|
+
}
|
213
|
+
|
214
|
+
if (!(rule instanceof CSSStyleRule) || !keyInObject(rule.style, key)) {
|
215
|
+
return
|
49
216
|
}
|
217
|
+
|
218
|
+
rule.style[key as RemoveReadonlyStyleKeys<typeof key>] = value
|
50
219
|
}
|
51
220
|
|
52
|
-
|
53
|
-
const
|
221
|
+
remove = (value: UnistylesValues) => {
|
222
|
+
const hash = generateHash(value)
|
223
|
+
const existingStyles = this.stylesCounter.get(hash)
|
224
|
+
|
225
|
+
if (!existingStyles) {
|
226
|
+
return
|
227
|
+
}
|
228
|
+
|
229
|
+
existingStyles.delete(value)
|
230
|
+
|
231
|
+
if (existingStyles.size === 0) {
|
232
|
+
const ruleIndex = Array.from(this.styleTag.sheet?.cssRules ?? []).findIndex(rule => rule.cssText.includes(`.${hash}`))
|
54
233
|
|
55
|
-
|
56
|
-
|
234
|
+
this.styleTag.sheet?.deleteRule(ruleIndex)
|
235
|
+
}
|
57
236
|
}
|
58
237
|
}
|
59
238
|
|
@@ -1,11 +1,8 @@
|
|
1
1
|
import type { UnistylesValues } from '../types'
|
2
|
-
import { listenToDependencies } from './listener'
|
3
2
|
import { UnistylesRegistry } from './registry'
|
4
|
-
import { createDoubleMap,
|
3
|
+
import { createDoubleMap, equal, extractSecrets, extractUnistyleDependencies, isInDocument } from './utils'
|
5
4
|
import { getVariants } from './variants'
|
6
5
|
|
7
|
-
type WebUnistyle = ReturnType<typeof UnistylesRegistry.createStyles>
|
8
|
-
|
9
6
|
type Style = UnistylesValues | ((...args: Array<any>) => UnistylesValues)
|
10
7
|
|
11
8
|
class UnistylesShadowRegistryBuilder {
|
@@ -17,9 +14,8 @@ class UnistylesShadowRegistryBuilder {
|
|
17
14
|
dispose = () => {}
|
18
15
|
// END MOCKS
|
19
16
|
|
20
|
-
private
|
21
|
-
private
|
22
|
-
private readonly stylesMap = createDoubleMap<HTMLElement, string, HTMLStyleElement>()
|
17
|
+
private resultsMap = createDoubleMap<HTMLElement, string, UnistylesValues>()
|
18
|
+
private classNamesMap = createDoubleMap<HTMLElement, string, Array<string>>()
|
23
19
|
|
24
20
|
add = (ref: any, _style?: Style | Array<Style>, _variants?: Record<string, any>, _args?: Array<any>) => {
|
25
21
|
// Style is not provided
|
@@ -43,13 +39,19 @@ class UnistylesShadowRegistryBuilder {
|
|
43
39
|
if (ref === null) {
|
44
40
|
const secrets = extractSecrets(_style)
|
45
41
|
|
46
|
-
secrets.forEach(({ __uni__refs }) => {
|
42
|
+
secrets.forEach(({ __uni__refs, __uni__key }) => {
|
47
43
|
__uni__refs.forEach(ref => {
|
48
44
|
if (isInDocument(ref)) {
|
49
45
|
return
|
50
46
|
}
|
51
47
|
|
52
|
-
this.
|
48
|
+
const oldResult = this.resultsMap.get(ref, __uni__key)
|
49
|
+
this.resultsMap.delete(ref, __uni__key)
|
50
|
+
this.classNamesMap.delete(ref, __uni__key)
|
51
|
+
|
52
|
+
if (oldResult) {
|
53
|
+
UnistylesRegistry.remove(oldResult)
|
54
|
+
}
|
53
55
|
})
|
54
56
|
})
|
55
57
|
|
@@ -62,63 +64,61 @@ class UnistylesShadowRegistryBuilder {
|
|
62
64
|
}
|
63
65
|
|
64
66
|
extractSecrets(_style).forEach(secret => {
|
65
|
-
const { __uni__key, __uni__stylesheet,
|
67
|
+
const { __uni__key, __uni__stylesheet, __uni__variants, __uni__args = [], __uni__refs } = secret
|
66
68
|
const newComputedStylesheet = UnistylesRegistry.getComputedStylesheet(__uni__stylesheet)
|
67
|
-
const style = newComputedStylesheet[__uni__key]
|
69
|
+
const style = newComputedStylesheet[__uni__key]!
|
68
70
|
const args = _args ?? __uni__args
|
69
|
-
const
|
71
|
+
const variants = _variants && Object.keys(_variants).length > 0 ? _variants : __uni__variants
|
72
|
+
const result = typeof style === 'function'
|
70
73
|
? style(...args)
|
71
74
|
: style
|
72
|
-
const
|
73
|
-
const { variants } = Object.fromEntries(getVariants({ variants: result }, _variants && Object.keys(_variants).length > 0 ? _variants : __uni__variants))
|
75
|
+
const { variantsResult } = Object.fromEntries(getVariants({ variantsResult: result }, variants))
|
74
76
|
const resultWithVariants = {
|
75
77
|
...result,
|
76
|
-
...
|
78
|
+
...variantsResult
|
77
79
|
}
|
78
|
-
const
|
79
|
-
const webUnistyle = storedWebUnistyle ?? UnistylesRegistry.createStyles(resultWithVariants, __uni__key)
|
80
|
+
const oldResult = this.resultsMap.get(ref, __uni__key)
|
80
81
|
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
className: webUnistyle.className,
|
88
|
-
unistyles: webUnistyle.unistyles,
|
89
|
-
}))
|
82
|
+
// If results are the same do nothing
|
83
|
+
if (equal(oldResult, resultWithVariants)) {
|
84
|
+
return
|
85
|
+
}
|
86
|
+
|
87
|
+
const oldClassNames = this.classNamesMap.get(ref, __uni__key)
|
90
88
|
|
91
|
-
|
92
|
-
|
89
|
+
// Remove old styles
|
90
|
+
if (oldResult) {
|
91
|
+
UnistylesRegistry.remove(oldResult)
|
92
|
+
}
|
93
93
|
|
94
|
-
|
94
|
+
// Remove old classnames from the ref
|
95
|
+
oldClassNames?.forEach(className => ref.classList.remove(className))
|
96
|
+
this.resultsMap.set(ref, __uni__key, resultWithVariants)
|
95
97
|
|
96
|
-
|
97
|
-
|
98
|
-
|
98
|
+
const className = UnistylesRegistry.add({
|
99
|
+
key: __uni__key,
|
100
|
+
args,
|
101
|
+
stylesheet: __uni__stylesheet,
|
102
|
+
value: resultWithVariants,
|
103
|
+
variants
|
104
|
+
})
|
105
|
+
const injectedClassNames: Array<string> = resultWithVariants?._web?._classNames ?? []
|
106
|
+
const newClassNames = injectedClassNames.concat(className)
|
107
|
+
const dependencies = extractUnistyleDependencies(resultWithVariants)
|
99
108
|
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
__uni__refs.add(ref)
|
104
|
-
this.stylesMap.set(ref, __uni__key, styleTag)
|
109
|
+
if (typeof __uni__stylesheet === 'function') {
|
110
|
+
// Add dependencies from dynamic styles to stylesheet
|
111
|
+
UnistylesRegistry.addDependenciesToStylesheet(__uni__stylesheet, dependencies)
|
105
112
|
}
|
106
113
|
|
107
|
-
|
108
|
-
|
109
|
-
|
114
|
+
__uni__refs.add(ref)
|
115
|
+
this.classNamesMap.set(ref, __uni__key, newClassNames)
|
116
|
+
// Add new classnames to the ref
|
117
|
+
ref.classList.add(...newClassNames)
|
110
118
|
})
|
111
119
|
}
|
112
120
|
|
113
|
-
remove = (
|
114
|
-
extractSecrets(style).forEach(({ __uni__key }) => {
|
115
|
-
this.webUnistylesMap.delete(ref, __uni__key)
|
116
|
-
this.disposeMap.get(ref, __uni__key)?.()
|
117
|
-
this.disposeMap.delete(ref, __uni__key)
|
118
|
-
this.stylesMap.get(ref, __uni__key)?.remove()
|
119
|
-
this.stylesMap.delete(ref, __uni__key)
|
120
|
-
})
|
121
|
-
}
|
121
|
+
remove = () => {}
|
122
122
|
}
|
123
123
|
|
124
124
|
export const UnistylesShadowRegistry = new UnistylesShadowRegistryBuilder()
|