react-native-unistyles 2.0.0-alpha.9 → 2.0.0-beta.2
Sign up to get free protection for your applications and to get access to all the features.
- package/android/CMakeLists.txt +28 -0
- package/android/build.gradle +40 -0
- package/android/src/main/cxx/cpp-adapter.cpp +108 -0
- package/android/src/main/java/com/unistyles/UnistylesModule.kt +159 -0
- package/android/src/main/java/com/unistyles/UnistylesPackage.kt +18 -0
- package/cxx/UnistylesRuntime.cpp +55 -2
- package/cxx/UnistylesRuntime.h +15 -9
- package/ios/UnistylesModule.mm +16 -6
- package/lib/commonjs/common.js +30 -16
- package/lib/commonjs/common.js.map +1 -1
- package/lib/commonjs/core/UnistyleRegistry.js +65 -3
- package/lib/commonjs/core/UnistyleRegistry.js.map +1 -1
- package/lib/commonjs/core/Unistyles.js +1 -1
- package/lib/commonjs/core/Unistyles.js.map +1 -1
- package/lib/commonjs/core/UnistylesModule.web.js +187 -0
- package/lib/commonjs/core/UnistylesModule.web.js.map +1 -0
- package/lib/commonjs/core/UnistylesRuntime.js +15 -16
- package/lib/commonjs/core/UnistylesRuntime.js.map +1 -1
- package/lib/commonjs/createStyleSheet.js +1 -6
- package/lib/commonjs/createStyleSheet.js.map +1 -1
- package/lib/commonjs/hooks/index.js +7 -0
- package/lib/commonjs/hooks/index.js.map +1 -1
- package/lib/commonjs/hooks/useUnistyles.js +12 -9
- package/lib/commonjs/hooks/useUnistyles.js.map +1 -1
- package/lib/commonjs/hooks/useVariants.js +14 -0
- package/lib/commonjs/hooks/useVariants.js.map +1 -0
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/plugins/index.js +38 -0
- package/lib/commonjs/plugins/index.js.map +1 -0
- package/lib/commonjs/plugins/normalizeWebStylesPlugin.js +12 -0
- package/lib/commonjs/plugins/normalizeWebStylesPlugin.js.map +1 -0
- package/lib/commonjs/plugins/normalizer/index.js +32 -0
- package/lib/commonjs/plugins/normalizer/index.js.map +1 -0
- package/lib/commonjs/plugins/normalizer/module.d.js.map +1 -0
- package/lib/commonjs/{utils/normalizeStyles.web.js → plugins/normalizer/normalizeStyle.js} +5 -5
- package/lib/commonjs/plugins/normalizer/normalizeStyle.js.map +1 -0
- package/lib/commonjs/{utils → plugins/normalizer}/normalizer.js +1 -1
- package/lib/commonjs/plugins/normalizer/normalizer.js.map +1 -0
- package/lib/commonjs/types/{mq.js → plugin.js} +1 -1
- package/lib/commonjs/types/{mq.js.map → plugin.js.map} +1 -1
- package/lib/commonjs/types/stylesheet.js +6 -0
- package/lib/commonjs/types/stylesheet.js.map +1 -0
- package/lib/commonjs/types/variants.js +2 -0
- package/lib/commonjs/{utils/module.d.js.map → types/variants.js.map} +1 -1
- package/lib/commonjs/useStyles.js +10 -20
- package/lib/commonjs/useStyles.js.map +1 -1
- package/lib/commonjs/utils/breakpoints.js +13 -59
- package/lib/commonjs/utils/breakpoints.js.map +1 -1
- package/lib/commonjs/utils/index.js +13 -51
- package/lib/commonjs/utils/index.js.map +1 -1
- package/lib/commonjs/utils/mq.js +36 -68
- package/lib/commonjs/utils/mq.js.map +1 -1
- package/lib/commonjs/utils/mqParser.js +4 -1
- package/lib/commonjs/utils/mqParser.js.map +1 -1
- package/lib/commonjs/utils/styles.js +35 -20
- package/lib/commonjs/utils/styles.js.map +1 -1
- package/lib/commonjs/utils/withPlugins.js +15 -0
- package/lib/commonjs/utils/withPlugins.js.map +1 -0
- package/lib/module/common.js +28 -17
- package/lib/module/common.js.map +1 -1
- package/lib/module/core/UnistyleRegistry.js +65 -3
- package/lib/module/core/UnistyleRegistry.js.map +1 -1
- package/lib/module/core/Unistyles.js +2 -2
- package/lib/module/core/Unistyles.js.map +1 -1
- package/lib/module/core/UnistylesModule.web.js +180 -0
- package/lib/module/core/UnistylesModule.web.js.map +1 -0
- package/lib/module/core/UnistylesRuntime.js +15 -16
- package/lib/module/core/UnistylesRuntime.js.map +1 -1
- package/lib/module/createStyleSheet.js +1 -6
- package/lib/module/createStyleSheet.js.map +1 -1
- package/lib/module/hooks/index.js +1 -0
- package/lib/module/hooks/index.js.map +1 -1
- package/lib/module/hooks/useUnistyles.js +13 -10
- package/lib/module/hooks/useUnistyles.js.map +1 -1
- package/lib/module/hooks/useVariants.js +7 -0
- package/lib/module/hooks/useVariants.js.map +1 -0
- package/lib/module/index.js.map +1 -1
- package/lib/module/plugins/index.js +3 -0
- package/lib/module/plugins/index.js.map +1 -0
- package/lib/module/plugins/normalizeWebStylesPlugin.js +6 -0
- package/lib/module/plugins/normalizeWebStylesPlugin.js.map +1 -0
- package/lib/module/plugins/normalizer/index.js +3 -0
- package/lib/module/plugins/normalizer/index.js.map +1 -0
- package/lib/module/plugins/normalizer/module.d.js.map +1 -0
- package/lib/module/{utils/normalizeStyles.web.js → plugins/normalizer/normalizeStyle.js} +3 -3
- package/lib/module/plugins/normalizer/normalizeStyle.js.map +1 -0
- package/lib/module/{utils → plugins/normalizer}/normalizer.js +1 -1
- package/lib/module/plugins/normalizer/normalizer.js.map +1 -0
- package/lib/module/types/plugin.js +2 -0
- package/lib/module/types/{mq.js.map → plugin.js.map} +1 -1
- package/lib/module/types/stylesheet.js +2 -0
- package/lib/module/types/stylesheet.js.map +1 -0
- package/lib/module/types/variants.js +2 -0
- package/lib/module/{utils/module.d.js.map → types/variants.js.map} +1 -1
- package/lib/module/useStyles.js +12 -22
- package/lib/module/useStyles.js.map +1 -1
- package/lib/module/utils/breakpoints.js +12 -56
- package/lib/module/utils/breakpoints.js.map +1 -1
- package/lib/module/utils/index.js +4 -6
- package/lib/module/utils/index.js.map +1 -1
- package/lib/module/utils/mq.js +35 -67
- package/lib/module/utils/mq.js.map +1 -1
- package/lib/module/utils/mqParser.js +3 -3
- package/lib/module/utils/mqParser.js.map +1 -1
- package/lib/module/utils/styles.js +35 -20
- package/lib/module/utils/styles.js.map +1 -1
- package/lib/module/utils/withPlugins.js +8 -0
- package/lib/module/utils/withPlugins.js.map +1 -0
- package/lib/typescript/src/common.d.ts +23 -12
- package/lib/typescript/src/common.d.ts.map +1 -1
- package/lib/typescript/src/core/UnistyleRegistry.d.ts +45 -4
- package/lib/typescript/src/core/UnistyleRegistry.d.ts.map +1 -1
- package/lib/typescript/src/core/UnistylesModule.web.d.ts +19 -0
- package/lib/typescript/src/core/UnistylesModule.web.d.ts.map +1 -0
- package/lib/typescript/src/core/UnistylesRuntime.d.ts +8 -9
- package/lib/typescript/src/core/UnistylesRuntime.d.ts.map +1 -1
- package/lib/typescript/src/core/index.d.ts +1 -0
- package/lib/typescript/src/core/index.d.ts.map +1 -1
- package/lib/typescript/src/createStyleSheet.d.ts +2 -2
- package/lib/typescript/src/createStyleSheet.d.ts.map +1 -1
- package/lib/typescript/src/global.d.ts.map +1 -1
- package/lib/typescript/src/hooks/index.d.ts +1 -0
- package/lib/typescript/src/hooks/index.d.ts.map +1 -1
- package/lib/typescript/src/hooks/useUnistyles.d.ts +2 -1
- package/lib/typescript/src/hooks/useUnistyles.d.ts.map +1 -1
- package/lib/typescript/src/hooks/useVariants.d.ts +3 -0
- package/lib/typescript/src/hooks/useVariants.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +51 -5
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/lib/typescript/src/plugins/index.d.ts +3 -0
- package/lib/typescript/src/plugins/index.d.ts.map +1 -0
- package/lib/typescript/src/plugins/normalizeWebStylesPlugin.d.ts +3 -0
- package/lib/typescript/src/plugins/normalizeWebStylesPlugin.d.ts.map +1 -0
- package/lib/typescript/src/plugins/normalizer/index.d.ts +3 -0
- package/lib/typescript/src/plugins/normalizer/index.d.ts.map +1 -0
- package/lib/typescript/src/plugins/normalizer/normalizeStyle.d.ts +3 -0
- package/lib/typescript/src/plugins/normalizer/normalizeStyle.d.ts.map +1 -0
- package/lib/typescript/src/{utils → plugins/normalizer}/normalizer.d.ts +1 -1
- package/lib/typescript/src/plugins/normalizer/normalizer.d.ts.map +1 -0
- package/lib/typescript/src/types/breakpoints.d.ts +25 -12
- package/lib/typescript/src/types/breakpoints.d.ts.map +1 -1
- package/lib/typescript/src/types/core.d.ts +6 -27
- package/lib/typescript/src/types/core.d.ts.map +1 -1
- package/lib/typescript/src/types/index.d.ts +4 -2
- package/lib/typescript/src/types/index.d.ts.map +1 -1
- package/lib/typescript/src/types/plugin.d.ts +7 -0
- package/lib/typescript/src/types/plugin.d.ts.map +1 -0
- package/lib/typescript/src/types/stylesheet.d.ts +40 -0
- package/lib/typescript/src/types/stylesheet.d.ts.map +1 -0
- package/lib/typescript/src/types/unistyles.d.ts +16 -11
- package/lib/typescript/src/types/unistyles.d.ts.map +1 -1
- package/lib/typescript/src/types/variants.d.ts +14 -0
- package/lib/typescript/src/types/variants.d.ts.map +1 -0
- package/lib/typescript/src/useStyles.d.ts +3 -3
- package/lib/typescript/src/useStyles.d.ts.map +1 -1
- package/lib/typescript/src/utils/breakpoints.d.ts +2 -5
- package/lib/typescript/src/utils/breakpoints.d.ts.map +1 -1
- package/lib/typescript/src/utils/index.d.ts +4 -6
- package/lib/typescript/src/utils/index.d.ts.map +1 -1
- package/lib/typescript/src/utils/mq.d.ts +15 -14
- package/lib/typescript/src/utils/mq.d.ts.map +1 -1
- package/lib/typescript/src/utils/mqParser.d.ts +14 -2
- package/lib/typescript/src/utils/mqParser.d.ts.map +1 -1
- package/lib/typescript/src/utils/styles.d.ts +3 -4
- package/lib/typescript/src/utils/styles.d.ts.map +1 -1
- package/lib/typescript/src/utils/withPlugins.d.ts +3 -0
- package/lib/typescript/src/utils/withPlugins.d.ts.map +1 -0
- package/package.json +19 -16
- package/src/__tests__/mocks.ts +24 -0
- package/src/common.ts +30 -14
- package/src/core/UnistyleRegistry.ts +66 -4
- package/src/core/Unistyles.ts +2 -2
- package/src/core/UnistylesModule.web.ts +214 -0
- package/src/core/UnistylesRuntime.ts +18 -20
- package/src/core/index.ts +1 -0
- package/src/createStyleSheet.ts +2 -8
- package/src/global.ts +1 -0
- package/src/hooks/index.ts +1 -0
- package/src/hooks/useUnistyles.ts +12 -10
- package/src/hooks/useVariants.ts +10 -0
- package/src/index.ts +3 -1
- package/src/plugins/index.ts +2 -0
- package/src/plugins/normalizeWebStylesPlugin.ts +7 -0
- package/src/plugins/normalizer/index.ts +2 -0
- package/src/{utils/normalizeStyles.web.ts → plugins/normalizer/normalizeStyle.ts} +3 -3
- package/src/{utils → plugins/normalizer}/normalizer.ts +3 -3
- package/src/types/breakpoints.ts +57 -25
- package/src/types/core.ts +9 -43
- package/src/types/index.ts +11 -2
- package/src/types/plugin.ts +7 -0
- package/src/types/stylesheet.ts +49 -0
- package/src/types/unistyles.ts +18 -13
- package/src/types/variants.ts +19 -0
- package/src/useStyles.ts +18 -25
- package/src/utils/breakpoints.ts +11 -67
- package/src/utils/index.ts +4 -6
- package/src/utils/mq.ts +31 -84
- package/src/utils/mqParser.ts +5 -5
- package/src/utils/styles.ts +44 -50
- package/src/utils/withPlugins.ts +13 -0
- package/lib/commonjs/utils/common.js +0 -25
- package/lib/commonjs/utils/common.js.map +0 -1
- package/lib/commonjs/utils/normalizeStyles.js +0 -10
- package/lib/commonjs/utils/normalizeStyles.js.map +0 -1
- package/lib/commonjs/utils/normalizeStyles.web.js.map +0 -1
- package/lib/commonjs/utils/normalizer.js.map +0 -1
- package/lib/module/types/mq.js +0 -2
- package/lib/module/utils/common.js +0 -17
- package/lib/module/utils/common.js.map +0 -1
- package/lib/module/utils/normalizeStyles.js +0 -3
- package/lib/module/utils/normalizeStyles.js.map +0 -1
- package/lib/module/utils/normalizeStyles.web.js.map +0 -1
- package/lib/module/utils/normalizer.js.map +0 -1
- package/lib/typescript/src/types/mq.d.ts +0 -3
- package/lib/typescript/src/types/mq.d.ts.map +0 -1
- package/lib/typescript/src/utils/common.d.ts +0 -12
- package/lib/typescript/src/utils/common.d.ts.map +0 -1
- package/lib/typescript/src/utils/normalizeStyles.d.ts +0 -2
- package/lib/typescript/src/utils/normalizeStyles.d.ts.map +0 -1
- package/lib/typescript/src/utils/normalizeStyles.web.d.ts +0 -5
- package/lib/typescript/src/utils/normalizeStyles.web.d.ts.map +0 -1
- package/lib/typescript/src/utils/normalizer.d.ts.map +0 -1
- package/src/types/mq.ts +0 -3
- package/src/utils/common.ts +0 -20
- package/src/utils/normalizeStyles.ts +0 -2
- /package/lib/commonjs/{utils → plugins/normalizer}/module.d.js +0 -0
- /package/lib/module/{utils → plugins/normalizer}/module.d.js +0 -0
- /package/src/{utils → plugins/normalizer}/module.d.ts +0 -0
@@ -0,0 +1,214 @@
|
|
1
|
+
import { NativeEventEmitter, NativeModules } from 'react-native'
|
2
|
+
import type { UnistylesThemes, UnistylesBreakpoints } from 'react-native-unistyles'
|
3
|
+
import type { ColorSchemeName } from '../types'
|
4
|
+
import { normalizeWebStylesPlugin } from '../plugins'
|
5
|
+
|
6
|
+
export class UnistylesBridgeWeb {
|
7
|
+
#timerRef?: ReturnType<typeof setTimeout> = undefined
|
8
|
+
#hasAdaptiveThemes: boolean = false
|
9
|
+
#supportsAutomaticColorScheme = false
|
10
|
+
#screenWidth: number = window.innerWidth
|
11
|
+
#screenHeight: number = window.innerHeight
|
12
|
+
#themes: Array<keyof UnistylesThemes> = []
|
13
|
+
#colorScheme: ColorSchemeName = this.getPreferredColorScheme()
|
14
|
+
#themeName: keyof UnistylesThemes = '' as keyof UnistylesThemes
|
15
|
+
#enabledPlugins: Array<string> = [normalizeWebStylesPlugin.name]
|
16
|
+
#unistylesEvents = new NativeEventEmitter(NativeModules.Unistyles)
|
17
|
+
#sortedBreakpointPairs: Array<[keyof UnistylesBreakpoints, number]> = []
|
18
|
+
#breakpoint: keyof UnistylesBreakpoints = '' as keyof UnistylesBreakpoints
|
19
|
+
|
20
|
+
constructor() {
|
21
|
+
this.setupListeners()
|
22
|
+
}
|
23
|
+
|
24
|
+
public install() {
|
25
|
+
// @ts-ignore
|
26
|
+
window.__UNISTYLES__ = new Proxy({}, {
|
27
|
+
get: (_target, prop) => {
|
28
|
+
switch (prop) {
|
29
|
+
case 'themeName':
|
30
|
+
return this.getTheme()
|
31
|
+
case 'screenWidth':
|
32
|
+
return this.#screenWidth
|
33
|
+
case 'screenHeight':
|
34
|
+
return this.#screenHeight
|
35
|
+
case 'breakpoint':
|
36
|
+
return this.#breakpoint || undefined
|
37
|
+
case 'hasAdaptiveThemes':
|
38
|
+
return this.#hasAdaptiveThemes
|
39
|
+
case 'sortedBreakpointPairs':
|
40
|
+
return this.#sortedBreakpointPairs
|
41
|
+
case 'enabledPlugins':
|
42
|
+
return this.#enabledPlugins
|
43
|
+
case 'colorScheme':
|
44
|
+
return this.#colorScheme
|
45
|
+
case 'useTheme':
|
46
|
+
return (themeName: keyof UnistylesThemes) => this.useTheme(themeName)
|
47
|
+
case 'useBreakpoints':
|
48
|
+
return (breakpoints: UnistylesBreakpoints) => this.useBreakpoints(breakpoints)
|
49
|
+
case 'useAdaptiveThemes':
|
50
|
+
return (enable: boolean) => this.useAdaptiveThemes(enable)
|
51
|
+
case 'addPlugin':
|
52
|
+
return (pluginName: string, notify: boolean) => this.addPlugin(pluginName, notify)
|
53
|
+
case 'removePlugin':
|
54
|
+
return (pluginName: string) => this.removePlugin(pluginName)
|
55
|
+
default:
|
56
|
+
return Reflect.get(this, prop)
|
57
|
+
}
|
58
|
+
},
|
59
|
+
set: (target, prop, newValue, receiver) => {
|
60
|
+
switch (prop) {
|
61
|
+
case 'themes': {
|
62
|
+
this.#themes = newValue
|
63
|
+
this.#supportsAutomaticColorScheme = newValue.includes('light') && newValue.includes('dark')
|
64
|
+
|
65
|
+
return true
|
66
|
+
}
|
67
|
+
case 'themeName': {
|
68
|
+
this.#themeName = newValue as keyof UnistylesThemes
|
69
|
+
this.emitThemeChange()
|
70
|
+
|
71
|
+
return true
|
72
|
+
}
|
73
|
+
default:
|
74
|
+
return Reflect.set(target, prop, newValue, receiver)
|
75
|
+
}
|
76
|
+
}
|
77
|
+
})
|
78
|
+
|
79
|
+
return true
|
80
|
+
}
|
81
|
+
|
82
|
+
private useTheme(themeName: keyof UnistylesThemes) {
|
83
|
+
this.#themeName = themeName
|
84
|
+
this.emitThemeChange()
|
85
|
+
}
|
86
|
+
|
87
|
+
private useBreakpoints(breakpoints: UnistylesBreakpoints) {
|
88
|
+
this.#sortedBreakpointPairs = Object
|
89
|
+
.entries(breakpoints)
|
90
|
+
.sort(([, a], [, b]) => (a ?? 0) - (b ?? 0)) as Array<[keyof UnistylesBreakpoints, number]>
|
91
|
+
this.#breakpoint = this.getBreakpointFromScreenWidth(this.#screenWidth)
|
92
|
+
}
|
93
|
+
|
94
|
+
private useAdaptiveThemes(enable: boolean) {
|
95
|
+
this.#hasAdaptiveThemes = enable
|
96
|
+
|
97
|
+
if (!this.#hasAdaptiveThemes || !this.#supportsAutomaticColorScheme) {
|
98
|
+
return
|
99
|
+
}
|
100
|
+
|
101
|
+
if (this.#themeName !== this.#colorScheme) {
|
102
|
+
this.#themeName = this.#colorScheme as keyof UnistylesThemes
|
103
|
+
this.emitThemeChange()
|
104
|
+
}
|
105
|
+
}
|
106
|
+
|
107
|
+
private addPlugin(pluginName: string, notify: boolean) {
|
108
|
+
this.#enabledPlugins = [pluginName].concat(this.#enabledPlugins)
|
109
|
+
|
110
|
+
if (notify) {
|
111
|
+
this.emitPluginChange()
|
112
|
+
}
|
113
|
+
}
|
114
|
+
|
115
|
+
private removePlugin(pluginName: string) {
|
116
|
+
this.#enabledPlugins = this.#enabledPlugins.filter(name => name !== pluginName)
|
117
|
+
this.emitPluginChange()
|
118
|
+
}
|
119
|
+
|
120
|
+
private getTheme(): keyof UnistylesThemes {
|
121
|
+
|
122
|
+
if (this.#themes.length === 1) {
|
123
|
+
return this.#themes.at(0) as keyof UnistylesThemes
|
124
|
+
}
|
125
|
+
|
126
|
+
return this.#themeName
|
127
|
+
}
|
128
|
+
|
129
|
+
private setupListeners() {
|
130
|
+
window.addEventListener('resize', () => {
|
131
|
+
clearTimeout(this.#timerRef)
|
132
|
+
|
133
|
+
this.#timerRef = setTimeout(() => {
|
134
|
+
this.#screenWidth = window.innerWidth
|
135
|
+
this.#screenHeight = window.innerHeight
|
136
|
+
this.#breakpoint = this.getBreakpointFromScreenWidth(this.#screenWidth)
|
137
|
+
|
138
|
+
this.emitLayoutChange()
|
139
|
+
}, 100)
|
140
|
+
})
|
141
|
+
|
142
|
+
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', event => {
|
143
|
+
this.#colorScheme = event.matches
|
144
|
+
? 'dark'
|
145
|
+
: 'light'
|
146
|
+
|
147
|
+
if (!this.#supportsAutomaticColorScheme || !this.#hasAdaptiveThemes) {
|
148
|
+
return
|
149
|
+
}
|
150
|
+
|
151
|
+
if (this.#colorScheme !== this.#themeName) {
|
152
|
+
this.#themeName = this.#colorScheme as keyof UnistylesThemes
|
153
|
+
this.emitThemeChange()
|
154
|
+
}
|
155
|
+
})
|
156
|
+
}
|
157
|
+
|
158
|
+
private getBreakpointFromScreenWidth(width: number): keyof UnistylesBreakpoints {
|
159
|
+
const breakpoint = this.#sortedBreakpointPairs
|
160
|
+
.find(([, value], index, otherBreakpoints) => {
|
161
|
+
const minVal = value
|
162
|
+
const maxVal = otherBreakpoints[index + 1]?.[1]
|
163
|
+
|
164
|
+
if (!maxVal) {
|
165
|
+
return true
|
166
|
+
}
|
167
|
+
|
168
|
+
return width >= minVal && width < maxVal
|
169
|
+
})
|
170
|
+
|
171
|
+
return breakpoint?.at(0) as keyof UnistylesBreakpoints
|
172
|
+
}
|
173
|
+
|
174
|
+
private getPreferredColorScheme() {
|
175
|
+
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
|
176
|
+
return 'dark'
|
177
|
+
}
|
178
|
+
|
179
|
+
return 'light'
|
180
|
+
}
|
181
|
+
|
182
|
+
private emitPluginChange() {
|
183
|
+
this.#unistylesEvents.emit('__unistylesOnChange', {
|
184
|
+
type: 'plugin'
|
185
|
+
})
|
186
|
+
}
|
187
|
+
|
188
|
+
private emitThemeChange() {
|
189
|
+
this.#unistylesEvents.emit('__unistylesOnChange', {
|
190
|
+
type: 'theme',
|
191
|
+
payload: {
|
192
|
+
themeName: this.#themeName
|
193
|
+
}
|
194
|
+
})
|
195
|
+
}
|
196
|
+
|
197
|
+
private emitLayoutChange() {
|
198
|
+
this.#unistylesEvents.emit('__unistylesOnChange', {
|
199
|
+
type: 'layout',
|
200
|
+
payload: {
|
201
|
+
breakpoint: this.#breakpoint,
|
202
|
+
orientation: this.#screenWidth > this.#screenHeight
|
203
|
+
? 'landscape'
|
204
|
+
: 'portrait',
|
205
|
+
screen: {
|
206
|
+
width: this.#screenWidth,
|
207
|
+
height: this.#screenHeight
|
208
|
+
}
|
209
|
+
}
|
210
|
+
})
|
211
|
+
}
|
212
|
+
}
|
213
|
+
|
214
|
+
export const UnistylesModule = new UnistylesBridgeWeb()
|
@@ -1,10 +1,10 @@
|
|
1
1
|
import { ScreenOrientation, UnistylesError } from '../common'
|
2
|
-
import type { UnistylesBridge } from '../types'
|
2
|
+
import type { UnistylesBridge, UnistylesPlugin } from '../types'
|
3
3
|
import type { UnistylesThemes } from '../global'
|
4
4
|
import type { UnistyleRegistry } from './UnistyleRegistry'
|
5
5
|
|
6
6
|
export class UnistylesRuntime {
|
7
|
-
constructor(private unistylesBridge: UnistylesBridge, private
|
7
|
+
constructor(private unistylesBridge: UnistylesBridge, private unistylesRegistry: UnistyleRegistry) {}
|
8
8
|
|
9
9
|
public get colorScheme() {
|
10
10
|
return this.unistylesBridge.colorScheme
|
@@ -14,10 +14,6 @@ export class UnistylesRuntime {
|
|
14
14
|
return this.unistylesBridge.hasAdaptiveThemes
|
15
15
|
}
|
16
16
|
|
17
|
-
public get sortedBreakpoints() {
|
18
|
-
return this.registry.sortedBreakpointPairs
|
19
|
-
}
|
20
|
-
|
21
17
|
public get themeName() {
|
22
18
|
return this.unistylesBridge.themeName
|
23
19
|
}
|
@@ -26,6 +22,10 @@ export class UnistylesRuntime {
|
|
26
22
|
return this.unistylesBridge.breakpoint
|
27
23
|
}
|
28
24
|
|
25
|
+
public get enabledPlugins() {
|
26
|
+
return this.unistylesBridge.enabledPlugins
|
27
|
+
}
|
28
|
+
|
29
29
|
public get screen() {
|
30
30
|
return {
|
31
31
|
width: this.unistylesBridge.screenWidth,
|
@@ -44,7 +44,11 @@ export class UnistylesRuntime {
|
|
44
44
|
}
|
45
45
|
|
46
46
|
public setTheme = (name: keyof UnistylesThemes) => {
|
47
|
-
if (this.
|
47
|
+
if (name === this.themeName) {
|
48
|
+
return
|
49
|
+
}
|
50
|
+
|
51
|
+
if (this.unistylesRegistry.hasTheme(name)) {
|
48
52
|
this.unistylesBridge.useTheme(name)
|
49
53
|
|
50
54
|
return true
|
@@ -53,21 +57,15 @@ export class UnistylesRuntime {
|
|
53
57
|
throw new Error(UnistylesError.ThemeNotRegistered)
|
54
58
|
}
|
55
59
|
|
56
|
-
public getTheme = (forName: keyof UnistylesThemes) => {
|
57
|
-
if (this.registry.themeNames.length === 0) {
|
58
|
-
return {} as UnistylesThemes[keyof UnistylesThemes]
|
59
|
-
}
|
60
|
-
|
61
|
-
if (!this.hasTheme(forName)) {
|
62
|
-
throw new Error(UnistylesError.ThemeNotFound)
|
63
|
-
}
|
64
|
-
|
65
|
-
return this.registry.themes[forName]
|
66
|
-
}
|
67
|
-
|
68
60
|
public setAdaptiveThemes = (enable: boolean) => {
|
69
61
|
this.unistylesBridge.useAdaptiveThemes(enable)
|
70
62
|
}
|
71
63
|
|
72
|
-
|
64
|
+
public addPlugin = (plugin: UnistylesPlugin) => {
|
65
|
+
this.unistylesRegistry.addPlugin(plugin)
|
66
|
+
}
|
67
|
+
|
68
|
+
public removePlugin = (plugin: UnistylesPlugin) => {
|
69
|
+
this.unistylesRegistry.removePlugin(plugin)
|
70
|
+
}
|
73
71
|
}
|
package/src/core/index.ts
CHANGED
package/src/createStyleSheet.ts
CHANGED
@@ -1,9 +1,3 @@
|
|
1
|
-
import type {
|
1
|
+
import type { StyleSheetWithSuperPowers } from './types'
|
2
2
|
|
3
|
-
export const createStyleSheet = <S
|
4
|
-
if (typeof styles === 'function') {
|
5
|
-
return styles as X
|
6
|
-
}
|
7
|
-
|
8
|
-
return styles as S
|
9
|
-
}
|
3
|
+
export const createStyleSheet = <S extends StyleSheetWithSuperPowers>(stylesheet: S): S => stylesheet
|
package/src/global.ts
CHANGED
package/src/hooks/index.ts
CHANGED
@@ -1,13 +1,14 @@
|
|
1
1
|
import { NativeEventEmitter, NativeModules } from 'react-native'
|
2
2
|
import { useEffect, useState } from 'react'
|
3
3
|
import { unistyles } from '../core'
|
4
|
-
import {
|
4
|
+
import { UnistylesEventType } from '../common'
|
5
5
|
import type { UnistylesEvents, UnistylesMobileLayoutEvent, UnistylesThemeEvent } from '../types'
|
6
6
|
|
7
7
|
const unistylesEvents = new NativeEventEmitter(NativeModules.Unistyles)
|
8
8
|
|
9
9
|
export const useUnistyles = () => {
|
10
|
-
const [
|
10
|
+
const [plugins, setPlugins] = useState(unistyles.runtime.enabledPlugins)
|
11
|
+
const [theme, setTheme] = useState(unistyles.registry.getTheme(unistyles.runtime.themeName))
|
11
12
|
const [layout, setLayout] = useState({
|
12
13
|
breakpoint: unistyles.runtime.breakpoint,
|
13
14
|
orientation: unistyles.runtime.orientation,
|
@@ -19,26 +20,26 @@ export const useUnistyles = () => {
|
|
19
20
|
|
20
21
|
useEffect(() => {
|
21
22
|
const subscription = unistylesEvents.addListener(
|
22
|
-
'
|
23
|
+
'__unistylesOnChange',
|
23
24
|
(event: UnistylesEvents) => {
|
24
25
|
switch (event.type) {
|
25
|
-
case
|
26
|
+
case UnistylesEventType.Theme: {
|
26
27
|
const themeEvent = event as UnistylesThemeEvent
|
27
28
|
|
28
|
-
return setTheme(unistyles.
|
29
|
+
return setTheme(unistyles.registry.getTheme(themeEvent.payload.themeName))
|
29
30
|
}
|
30
|
-
case
|
31
|
+
case UnistylesEventType.Layout: {
|
31
32
|
const layoutEvent = event as UnistylesMobileLayoutEvent
|
32
33
|
|
33
34
|
return setLayout({
|
34
35
|
breakpoint: layoutEvent.payload.breakpoint,
|
35
36
|
orientation: layoutEvent.payload.orientation,
|
36
|
-
screenSize:
|
37
|
-
width: layoutEvent.payload.screen.width,
|
38
|
-
height: layoutEvent.payload.screen.height
|
39
|
-
}
|
37
|
+
screenSize: layoutEvent.payload.screen
|
40
38
|
})
|
41
39
|
}
|
40
|
+
case UnistylesEventType.Plugin: {
|
41
|
+
return setPlugins(unistyles.runtime.enabledPlugins)
|
42
|
+
}
|
42
43
|
default:
|
43
44
|
return
|
44
45
|
}
|
@@ -49,6 +50,7 @@ export const useUnistyles = () => {
|
|
49
50
|
}, [])
|
50
51
|
|
51
52
|
return {
|
53
|
+
plugins,
|
52
54
|
theme,
|
53
55
|
layout
|
54
56
|
}
|
@@ -0,0 +1,10 @@
|
|
1
|
+
import { useRef } from 'react'
|
2
|
+
import type { Optional } from '../types'
|
3
|
+
|
4
|
+
export const useVariants = (variantsMap?: Record<string, Optional<string>>) => {
|
5
|
+
const variantsRef = useRef<Optional<Record<string, Optional<string>>>>(variantsMap)
|
6
|
+
|
7
|
+
variantsRef.current = variantsMap
|
8
|
+
|
9
|
+
return variantsRef.current
|
10
|
+
}
|
package/src/index.ts
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
import { unistyles } from './core'
|
2
2
|
import { mq } from './utils'
|
3
3
|
import { useInitialTheme } from './hooks'
|
4
|
+
import type { UnistylesPlugin } from './types'
|
4
5
|
import type { UnistylesThemes, UnistylesBreakpoints } from './global'
|
5
6
|
import { ScreenOrientation } from './common'
|
6
7
|
import { useStyles } from './useStyles'
|
@@ -26,5 +27,6 @@ export {
|
|
26
27
|
|
27
28
|
export type {
|
28
29
|
UnistylesThemes,
|
29
|
-
UnistylesBreakpoints
|
30
|
+
UnistylesBreakpoints,
|
31
|
+
UnistylesPlugin
|
30
32
|
}
|
@@ -1,6 +1,6 @@
|
|
1
|
-
import { warn } from './common'
|
2
1
|
import { preprocessor } from './normalizer'
|
3
|
-
import
|
2
|
+
import { warn } from '../../common'
|
3
|
+
import type { NormalizedBoxShadow, NormalizedTextShadow, BoxShadow, TextShadow, RNStyle } from '../../types'
|
4
4
|
|
5
5
|
const normalizeBoxShadow = <T extends BoxShadow>(style: T): NormalizedBoxShadow => {
|
6
6
|
const requiredBoxShadowProperties = [
|
@@ -55,7 +55,7 @@ const normalizeTextShadow = <T extends TextShadow>(style: T): NormalizedTextShad
|
|
55
55
|
}
|
56
56
|
}
|
57
57
|
|
58
|
-
export const
|
58
|
+
export const normalizeStyle = <T extends RNStyle>(style: T): T => {
|
59
59
|
const normalizedTransform = ('transform' in style && Array.isArray(style.transform))
|
60
60
|
? { transform: preprocessor.createTransformValue(style.transform) }
|
61
61
|
: {}
|
@@ -1,7 +1,7 @@
|
|
1
1
|
// based on react-native-web normalizer
|
2
2
|
// https://github.com/necolas/react-native-web
|
3
3
|
import normalizeColors from '@react-native/normalize-colors'
|
4
|
-
import type { TextShadow, Transforms, BoxShadow } from '
|
4
|
+
import type { TextShadow, Transforms, BoxShadow, Nullable } from '../../types'
|
5
5
|
|
6
6
|
type Preprocessor = {
|
7
7
|
createTextShadowValue(style: TextShadow): string,
|
@@ -15,9 +15,9 @@ export const normalizeColor = (color: string, opacity: number = 1) => {
|
|
15
15
|
return color
|
16
16
|
}
|
17
17
|
|
18
|
-
const integer = normalizeColors(color) as number
|
18
|
+
const integer = normalizeColors(color) as Nullable<number>
|
19
19
|
|
20
|
-
// If the
|
20
|
+
// If the color is an unknown format, the return value is null
|
21
21
|
if (integer === null) {
|
22
22
|
return color
|
23
23
|
}
|
package/src/types/breakpoints.ts
CHANGED
@@ -1,29 +1,61 @@
|
|
1
|
-
import type { OpaqueColorValue } from 'react-native'
|
2
|
-
import type {
|
3
|
-
import type {
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
1
|
+
import type { ColorValue, OpaqueColorValue } from 'react-native'
|
2
|
+
import type { UnistylesTheme } from '../types'
|
3
|
+
import type { BreakpointsOrMediaQueries, ToDeepUnistyles } from './stylesheet'
|
4
|
+
import type { TransformStyles } from './core'
|
5
|
+
|
6
|
+
type ExtractTransformArray<T> = T extends object
|
7
|
+
? { [K in keyof T]: ExtractBreakpoints<T[K]> }
|
8
|
+
: never
|
9
|
+
|
10
|
+
type ExtractBreakpoints<T> = T extends object
|
11
|
+
? keyof T extends BreakpointsOrMediaQueries
|
12
|
+
? T[keyof T]
|
13
|
+
: T extends Array<ToDeepUnistyles<TransformStyles>>
|
14
|
+
? Array<ExtractTransformArray<T[number]>>
|
15
|
+
: {
|
16
|
+
[K in keyof T]: ExtractBreakpoints<T[K]>
|
17
|
+
}
|
18
|
+
: T
|
18
19
|
|
19
|
-
type
|
20
|
-
? (...args:
|
20
|
+
type ParseNestedObject<T> = T extends (...args: infer A) => infer R
|
21
|
+
? (...args: A) => ParseNestedObject<R>
|
21
22
|
: T extends object
|
22
|
-
? T extends
|
23
|
-
?
|
24
|
-
:
|
25
|
-
|
26
|
-
|
23
|
+
? T extends { variants: infer R }
|
24
|
+
? ParseVariants<FlattenVariants<R>> & ParseNestedObject<Omit<T, 'variants'>>
|
25
|
+
: {
|
26
|
+
[K in keyof T]: T[K] extends object
|
27
|
+
? T[K] extends OpaqueColorValue
|
28
|
+
? ColorValue
|
29
|
+
: ExtractBreakpoints<T[K]>
|
30
|
+
: T[K]
|
31
|
+
}
|
27
32
|
: T
|
28
33
|
|
29
|
-
|
34
|
+
type FlattenVariants<T> = T extends object
|
35
|
+
? {
|
36
|
+
[K in keyof T]: T[K] extends object
|
37
|
+
? {
|
38
|
+
[key in keyof T[K]]: T[K][key] extends object
|
39
|
+
? ParseNestedObject<T[K][key]>
|
40
|
+
: never
|
41
|
+
}
|
42
|
+
: never
|
43
|
+
}
|
44
|
+
: never
|
45
|
+
|
46
|
+
type ParseVariants<T> = T extends object
|
47
|
+
? T[keyof T] extends object
|
48
|
+
? UnionToIntersection<ParseVariants<T[keyof T]>>
|
49
|
+
: T
|
50
|
+
: T
|
51
|
+
|
52
|
+
type UnionToIntersection<U> =
|
53
|
+
(U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never
|
54
|
+
|
55
|
+
type ParseStyleKeys<T> = T extends object
|
56
|
+
? { [K in keyof T]: ParseNestedObject<T[K]> }
|
57
|
+
: never
|
58
|
+
|
59
|
+
export type ReactNativeStyleSheet<T> = T extends (theme: UnistylesTheme) => infer R
|
60
|
+
? ParseStyleKeys<R>
|
61
|
+
: ParseStyleKeys<T>
|
package/src/types/core.ts
CHANGED
@@ -15,19 +15,13 @@ import type {
|
|
15
15
|
} from 'react-native/Libraries/StyleSheet/StyleSheetTypes'
|
16
16
|
import type { ImageStyle, TextStyle, ViewStyle } from 'react-native'
|
17
17
|
import type { UnistylesBreakpoints, UnistylesThemes } from '../global'
|
18
|
-
import type { MediaQuery } from './mq'
|
19
18
|
|
20
|
-
type ShadowOffset = {
|
19
|
+
export type ShadowOffset = {
|
21
20
|
width: number,
|
22
21
|
height: number
|
23
22
|
}
|
24
23
|
|
25
|
-
export type
|
26
|
-
width: number,
|
27
|
-
height: number
|
28
|
-
}
|
29
|
-
|
30
|
-
type TransformStyles =
|
24
|
+
export type TransformStyles =
|
31
25
|
& PerpectiveTransform
|
32
26
|
& RotateTransform
|
33
27
|
& RotateXTransform
|
@@ -42,41 +36,13 @@ type TransformStyles =
|
|
42
36
|
& SkewYTransform
|
43
37
|
& MatrixTransform
|
44
38
|
|
45
|
-
type
|
46
|
-
|
47
|
-
|
48
|
-
transform?: Array<DeepUniStyle<TransformStyles>>
|
49
|
-
}
|
50
|
-
|
51
|
-
type UniStyle<V> = {
|
52
|
-
[innerKey in keyof UnistylesBreakpoints]?: V
|
53
|
-
} | {
|
54
|
-
[innerKey in MediaQuery]: V
|
55
|
-
}
|
56
|
-
|
57
|
-
type DeepUniStyle<T> = {
|
58
|
-
[K in keyof T]?: UniStyle<T[K]> | T[K]
|
59
|
-
}
|
60
|
-
|
61
|
-
// these props are treated differently to nest breakpoints and media queries
|
62
|
-
type NestedTypes = 'shadowOffset' | 'transform' | 'textShadowOffset'
|
63
|
-
|
64
|
-
type UnistyleView = DeepUniStyle<Omit<ViewStyle, NestedTypes>>
|
65
|
-
type UnistyleText = DeepUniStyle<Omit<TextStyle, NestedTypes>>
|
66
|
-
type UnistyleImage = DeepUniStyle<Omit<ImageStyle, NestedTypes>>
|
67
|
-
|
68
|
-
export type StaticStyles =
|
69
|
-
| UnistyleView
|
70
|
-
| UnistyleText
|
71
|
-
| UnistyleImage
|
72
|
-
& UnistyleNested
|
73
|
-
|
74
|
-
export type CustomNamedStyles<T> = {
|
75
|
-
[K in keyof T]: T[K] extends (...args: infer A) => StaticStyles
|
76
|
-
? (...args: A) => StaticStyles
|
77
|
-
: StaticStyles
|
39
|
+
export type ScreenSize = {
|
40
|
+
width: number,
|
41
|
+
height: number
|
78
42
|
}
|
79
43
|
|
80
|
-
export type
|
44
|
+
export type RNStyle = ViewStyle & TextStyle & ImageStyle
|
45
|
+
export type RNValue = ViewStyle[keyof ViewStyle] | TextStyle[keyof TextStyle] | ImageStyle[keyof ImageStyle]
|
46
|
+
export type NestedStyle = Record<keyof UnistylesBreakpoints | symbol, RNValue>
|
47
|
+
export type NestedStylePairs = Array<[keyof UnistylesBreakpoints | symbol, RNValue]>
|
81
48
|
export type UnistylesTheme = UnistylesThemes[keyof UnistylesThemes]
|
82
|
-
export type CreateStylesFactory<ST, Theme> = (theme: Theme) => ST
|
package/src/types/index.ts
CHANGED
@@ -1,6 +1,15 @@
|
|
1
1
|
export * from './normalizer'
|
2
2
|
export * from './unistyles'
|
3
3
|
export type { Optional, Nullable } from './common'
|
4
|
-
export type {
|
5
|
-
|
4
|
+
export type {
|
5
|
+
NestedStylePairs,
|
6
|
+
UnistylesTheme,
|
7
|
+
ScreenSize,
|
8
|
+
NestedStyle,
|
9
|
+
RNValue,
|
10
|
+
RNStyle
|
11
|
+
} from './core'
|
12
|
+
export type { StyleSheetWithSuperPowers, StyleSheet, AllAvailableKeys } from './stylesheet'
|
6
13
|
export type { ReactNativeStyleSheet } from './breakpoints'
|
14
|
+
export type { ExtractVariantNames } from './variants'
|
15
|
+
export type { UnistylesPlugin } from './plugin'
|