react-native-unistyles 2.0.0-rc.1 → 2.0.0-rc.3
Sign up to get free protection for your applications and to get access to all the features.
- package/android/src/main/cxx/cpp-adapter.cpp +26 -2
- package/android/src/main/java/com/unistyles/UnistylesModule.kt +29 -3
- package/cxx/UnistylesRuntime.cpp +10 -0
- package/cxx/UnistylesRuntime.h +10 -2
- package/ios/UnistylesHelpers.h +1 -0
- package/ios/UnistylesHelpers.mm +32 -0
- package/ios/UnistylesModule.mm +39 -2
- package/lib/commonjs/common.js +21 -1
- package/lib/commonjs/common.js.map +1 -1
- package/lib/commonjs/core/UnistyleRegistry.js +15 -25
- package/lib/commonjs/core/UnistyleRegistry.js.map +1 -1
- package/lib/commonjs/core/UnistylesModule.js +190 -2
- package/lib/commonjs/core/UnistylesModule.js.map +1 -1
- package/lib/commonjs/core/UnistylesModule.native.js +9 -0
- package/lib/commonjs/core/UnistylesModule.native.js.map +1 -0
- package/lib/commonjs/core/UnistylesRuntime.js +8 -0
- package/lib/commonjs/core/UnistylesRuntime.js.map +1 -1
- package/lib/commonjs/hooks/useCSS.js +36 -1
- package/lib/commonjs/hooks/useCSS.js.map +1 -1
- package/lib/commonjs/hooks/useCSS.native.js +9 -0
- package/lib/commonjs/hooks/useCSS.native.js.map +1 -0
- package/lib/commonjs/hooks/useUnistyles.js +8 -1
- package/lib/commonjs/hooks/useUnistyles.js.map +1 -1
- package/lib/commonjs/index.js +12 -0
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/normalizer/normalizer.js +1 -2
- package/lib/commonjs/normalizer/normalizer.js.map +1 -1
- package/lib/commonjs/useStyles.js +2 -3
- package/lib/commonjs/useStyles.js.map +1 -1
- package/lib/commonjs/utils/breakpoints.js +2 -11
- package/lib/commonjs/utils/breakpoints.js.map +1 -1
- package/lib/commonjs/utils/cssMediaQuery.js +3 -9
- package/lib/commonjs/utils/cssMediaQuery.js.map +1 -1
- package/lib/commonjs/utils/mq.js +12 -36
- package/lib/commonjs/utils/mq.js.map +1 -1
- package/lib/commonjs/utils/mqParser.js +1 -2
- package/lib/commonjs/utils/mqParser.js.map +1 -1
- package/lib/commonjs/utils/styles.js +34 -39
- package/lib/commonjs/utils/styles.js.map +1 -1
- package/lib/module/common.js +20 -0
- package/lib/module/common.js.map +1 -1
- package/lib/module/core/UnistyleRegistry.js +15 -25
- package/lib/module/core/UnistyleRegistry.js.map +1 -1
- package/lib/module/core/UnistylesModule.js +189 -2
- package/lib/module/core/UnistylesModule.js.map +1 -1
- package/lib/module/core/UnistylesModule.native.js +3 -0
- package/lib/module/core/UnistylesModule.native.js.map +1 -0
- package/lib/module/core/UnistylesRuntime.js +8 -0
- package/lib/module/core/UnistylesRuntime.js.map +1 -1
- package/lib/module/hooks/useCSS.js +36 -1
- package/lib/module/hooks/useCSS.js.map +1 -1
- package/lib/module/hooks/useCSS.native.js +2 -0
- package/lib/module/hooks/useCSS.native.js.map +1 -0
- package/lib/module/hooks/useUnistyles.js +8 -1
- package/lib/module/hooks/useUnistyles.js.map +1 -1
- package/lib/module/index.js +2 -2
- package/lib/module/index.js.map +1 -1
- package/lib/module/normalizer/normalizer.js +1 -2
- package/lib/module/normalizer/normalizer.js.map +1 -1
- package/lib/module/useStyles.js +2 -3
- package/lib/module/useStyles.js.map +1 -1
- package/lib/module/utils/breakpoints.js +2 -11
- package/lib/module/utils/breakpoints.js.map +1 -1
- package/lib/module/utils/cssMediaQuery.js +3 -9
- package/lib/module/utils/cssMediaQuery.js.map +1 -1
- package/lib/module/utils/mq.js +12 -36
- package/lib/module/utils/mq.js.map +1 -1
- package/lib/module/utils/mqParser.js +1 -2
- package/lib/module/utils/mqParser.js.map +1 -1
- package/lib/module/utils/styles.js +34 -39
- package/lib/module/utils/styles.js.map +1 -1
- package/lib/typescript/src/common.d.ts +19 -1
- package/lib/typescript/src/common.d.ts.map +1 -1
- package/lib/typescript/src/core/UnistylesModule.d.ts +17 -4
- package/lib/typescript/src/core/UnistylesModule.d.ts.map +1 -1
- package/lib/typescript/src/core/UnistylesModule.native.d.ts +6 -0
- package/lib/typescript/src/core/UnistylesModule.native.d.ts.map +1 -0
- package/lib/typescript/src/core/UnistylesRuntime.d.ts +5 -0
- package/lib/typescript/src/core/UnistylesRuntime.d.ts.map +1 -1
- package/lib/typescript/src/hooks/useCSS.d.ts +1 -1
- package/lib/typescript/src/hooks/useCSS.d.ts.map +1 -1
- package/lib/typescript/src/hooks/useCSS.native.d.ts +3 -0
- package/lib/typescript/src/hooks/useCSS.native.d.ts.map +1 -0
- package/lib/typescript/src/hooks/useUnistyles.d.ts +1 -0
- package/lib/typescript/src/hooks/useUnistyles.d.ts.map +1 -1
- package/lib/typescript/src/index.d.ts +2 -2
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/lib/typescript/src/types/unistyles.d.ts +9 -2
- package/lib/typescript/src/types/unistyles.d.ts.map +1 -1
- package/package.json +6 -8
- package/src/common.ts +21 -1
- package/src/core/UnistylesModule.native.ts +7 -0
- package/src/core/UnistylesModule.ts +227 -4
- package/src/core/UnistylesRuntime.ts +8 -0
- package/src/hooks/useCSS.native.ts +3 -0
- package/src/hooks/useCSS.ts +49 -1
- package/src/hooks/useUnistyles.ts +9 -2
- package/src/index.ts +3 -1
- package/src/types/unistyles.ts +10 -2
- package/src/useStyles.ts +1 -1
- package/lib/commonjs/core/UnistylesModule.web.js +0 -199
- package/lib/commonjs/core/UnistylesModule.web.js.map +0 -1
- package/lib/commonjs/hooks/useCSS.web.js +0 -46
- package/lib/commonjs/hooks/useCSS.web.js.map +0 -1
- package/lib/module/core/UnistylesModule.web.js +0 -192
- package/lib/module/core/UnistylesModule.web.js.map +0 -1
- package/lib/module/hooks/useCSS.web.js +0 -39
- package/lib/module/hooks/useCSS.web.js.map +0 -1
- package/lib/typescript/src/core/UnistylesModule.web.d.ts +0 -19
- package/lib/typescript/src/core/UnistylesModule.web.d.ts.map +0 -1
- package/lib/typescript/src/hooks/useCSS.web.d.ts +0 -3
- package/lib/typescript/src/hooks/useCSS.web.d.ts.map +0 -1
- package/src/core/UnistylesModule.web.ts +0 -227
- package/src/hooks/useCSS.web.ts +0 -51
@@ -27,7 +27,8 @@ Java_com_unistyles_UnistylesModule_nativeInstall(
|
|
27
27
|
jlong jsi,
|
28
28
|
jint screenWidth,
|
29
29
|
jint screenHeight,
|
30
|
-
jstring colorScheme
|
30
|
+
jstring colorScheme,
|
31
|
+
jstring contentSizeCategory
|
31
32
|
) {
|
32
33
|
auto runtime = reinterpret_cast<facebook::jsi::Runtime *>(jsi);
|
33
34
|
|
@@ -43,10 +44,15 @@ Java_com_unistyles_UnistylesModule_nativeInstall(
|
|
43
44
|
std::string colorSchemeStr(colorSchemeChars);
|
44
45
|
env->ReleaseStringUTFChars(colorScheme, colorSchemeChars);
|
45
46
|
|
47
|
+
const char *contentSizeCategoryChars = env->GetStringUTFChars(contentSizeCategory, nullptr);
|
48
|
+
std::string contentSizeCategoryStr(contentSizeCategoryChars);
|
49
|
+
env->ReleaseStringUTFChars(contentSizeCategory, contentSizeCategoryChars);
|
50
|
+
|
46
51
|
unistylesRuntime = std::make_shared<UnistylesRuntime>(
|
47
52
|
screenWidth,
|
48
53
|
screenHeight,
|
49
|
-
colorSchemeStr
|
54
|
+
colorSchemeStr,
|
55
|
+
contentSizeCategoryStr
|
50
56
|
);
|
51
57
|
|
52
58
|
unistylesRuntime->onThemeChange([=](const std::string &theme) {
|
@@ -79,6 +85,16 @@ Java_com_unistyles_UnistylesModule_nativeInstall(
|
|
79
85
|
env->DeleteLocalRef(cls);
|
80
86
|
});
|
81
87
|
|
88
|
+
unistylesRuntime->onContentSizeCategoryChange([=](const std::string &contentSizeCategory) {
|
89
|
+
jstring contentSizeCategoryStr = env->NewStringUTF(contentSizeCategory.c_str());
|
90
|
+
jclass cls = env->GetObjectClass(unistylesModule);
|
91
|
+
jmethodID methodId = env->GetMethodID(cls, "onContentSizeCategoryChange", "(Ljava/lang/String;)V");
|
92
|
+
|
93
|
+
env->CallVoidMethod(unistylesModule, methodId, contentSizeCategoryStr);
|
94
|
+
env->DeleteLocalRef(contentSizeCategoryStr);
|
95
|
+
env->DeleteLocalRef(cls);
|
96
|
+
});
|
97
|
+
|
82
98
|
jsi::Object hostObject = jsi::Object::createFromHostObject(*runtime, unistylesRuntime);
|
83
99
|
|
84
100
|
runtime->global().setProperty(*runtime, "__UNISTYLES__", std::move(hostObject));
|
@@ -106,3 +122,11 @@ Java_com_unistyles_UnistylesModule_nativeOnAppearanceChange(JNIEnv *env, jobject
|
|
106
122
|
unistylesRuntime->handleAppearanceChange(env->GetStringUTFChars(colorScheme, nullptr));
|
107
123
|
}
|
108
124
|
}
|
125
|
+
|
126
|
+
extern "C"
|
127
|
+
JNIEXPORT void JNICALL
|
128
|
+
Java_com_unistyles_UnistylesModule_nativeOnContentSizeCategoryChange(JNIEnv *env, jobject thiz, jstring contentSizeCategory) {
|
129
|
+
if (unistylesRuntime != nullptr) {
|
130
|
+
unistylesRuntime->handleContentSizeCategoryChange(env->GetStringUTFChars(contentSizeCategory, nullptr));
|
131
|
+
}
|
132
|
+
}
|
@@ -15,6 +15,7 @@ import com.facebook.react.bridge.ReactContextBaseJavaModule
|
|
15
15
|
import com.facebook.react.bridge.ReactMethod
|
16
16
|
import com.facebook.react.modules.core.DeviceEventManagerModule
|
17
17
|
|
18
|
+
|
18
19
|
class UnistylesModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext), LifecycleEventListener {
|
19
20
|
private val configurationChangeReceiver = object : BroadcastReceiver() {
|
20
21
|
override fun onReceive(context: Context, intent: Intent) {
|
@@ -54,6 +55,7 @@ class UnistylesModule(reactContext: ReactApplicationContext) : ReactContextBaseJ
|
|
54
55
|
this.nativeOnAppearanceChange(
|
55
56
|
config["colorScheme"] as String
|
56
57
|
)
|
58
|
+
this.nativeOnContentSizeCategoryChange(config["contentSizeCategory"] as String)
|
57
59
|
}
|
58
60
|
}
|
59
61
|
|
@@ -64,11 +66,20 @@ class UnistylesModule(reactContext: ReactApplicationContext) : ReactContextBaseJ
|
|
64
66
|
Configuration.UI_MODE_NIGHT_NO -> "light"
|
65
67
|
else -> "unspecified"
|
66
68
|
}
|
69
|
+
val fontScale = reactApplicationContext.resources.configuration.fontScale
|
70
|
+
val contentSizeCategory = when {
|
71
|
+
fontScale <= 0.85f -> "Small"
|
72
|
+
fontScale <= 1.0f -> "Default"
|
73
|
+
fontScale <= 1.15f -> "Large"
|
74
|
+
fontScale <= 1.3f -> "ExtraLarge"
|
75
|
+
else -> "Huge"
|
76
|
+
}
|
67
77
|
|
68
78
|
return mapOf(
|
69
79
|
"width" to (displayMetrics.widthPixels / displayMetrics.density).toInt(),
|
70
80
|
"height" to (displayMetrics.heightPixels / displayMetrics.density).toInt(),
|
71
|
-
"colorScheme" to colorScheme
|
81
|
+
"colorScheme" to colorScheme,
|
82
|
+
"contentSizeCategory" to contentSizeCategory
|
72
83
|
)
|
73
84
|
}
|
74
85
|
|
@@ -85,7 +96,8 @@ class UnistylesModule(reactContext: ReactApplicationContext) : ReactContextBaseJ
|
|
85
96
|
it.get(),
|
86
97
|
config["width"] as Int,
|
87
98
|
config["height"] as Int,
|
88
|
-
config["colorScheme"] as String
|
99
|
+
config["colorScheme"] as String,
|
100
|
+
config["contentSizeCategory"] as String
|
89
101
|
)
|
90
102
|
|
91
103
|
Log.i(NAME, "Installed Unistyles \uD83E\uDD84!")
|
@@ -99,10 +111,11 @@ class UnistylesModule(reactContext: ReactApplicationContext) : ReactContextBaseJ
|
|
99
111
|
}
|
100
112
|
}
|
101
113
|
|
102
|
-
private external fun nativeInstall(jsi: Long, width: Int, height: Int, colorScheme: String)
|
114
|
+
private external fun nativeInstall(jsi: Long, width: Int, height: Int, colorScheme: String, contentSizeCategory: String)
|
103
115
|
private external fun nativeDestroy()
|
104
116
|
private external fun nativeOnOrientationChange(width: Int, height: Int)
|
105
117
|
private external fun nativeOnAppearanceChange(colorScheme: String)
|
118
|
+
private external fun nativeOnContentSizeCategoryChange(contentSizeCategory: String)
|
106
119
|
|
107
120
|
//endregion
|
108
121
|
//region Event emitter
|
@@ -147,6 +160,19 @@ class UnistylesModule(reactContext: ReactApplicationContext) : ReactContextBaseJ
|
|
147
160
|
.emit("__unistylesOnChange", body)
|
148
161
|
}
|
149
162
|
|
163
|
+
private fun onContentSizeCategoryChange(contentSizeCategory: String) {
|
164
|
+
val body = Arguments.createMap().apply {
|
165
|
+
putString("type", "dynamicTypeSize")
|
166
|
+
putMap("payload", Arguments.createMap().apply {
|
167
|
+
putString("contentSizeCategory", contentSizeCategory)
|
168
|
+
})
|
169
|
+
}
|
170
|
+
|
171
|
+
reactApplicationContext
|
172
|
+
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
|
173
|
+
.emit("__unistylesOnChange", body)
|
174
|
+
}
|
175
|
+
|
150
176
|
@ReactMethod
|
151
177
|
fun addListener(eventName: String?) = Unit
|
152
178
|
|
package/cxx/UnistylesRuntime.cpp
CHANGED
@@ -11,6 +11,7 @@ std::vector<jsi::PropNameID> UnistylesRuntime::getPropertyNames(jsi::Runtime& ru
|
|
11
11
|
// getters
|
12
12
|
properties.push_back(jsi::PropNameID::forUtf8(runtime, std::string("screenWidth")));
|
13
13
|
properties.push_back(jsi::PropNameID::forUtf8(runtime, std::string("screenHeight")));
|
14
|
+
properties.push_back(jsi::PropNameID::forUtf8(runtime, std::string("contentSizeCategory")));
|
14
15
|
properties.push_back(jsi::PropNameID::forUtf8(runtime, std::string("hasAdaptiveThemes")));
|
15
16
|
properties.push_back(jsi::PropNameID::forUtf8(runtime, std::string("themeName")));
|
16
17
|
properties.push_back(jsi::PropNameID::forUtf8(runtime, std::string("breakpoint")));
|
@@ -43,6 +44,10 @@ jsi::Value UnistylesRuntime::get(jsi::Runtime& runtime, const jsi::PropNameID& p
|
|
43
44
|
if (propName == "hasAdaptiveThemes") {
|
44
45
|
return jsi::Value(this->hasAdaptiveThemes);
|
45
46
|
}
|
47
|
+
|
48
|
+
if (propName == "contentSizeCategory") {
|
49
|
+
return jsi::Value(jsi::String::createFromUtf8(runtime, this->contentSizeCategory));
|
50
|
+
}
|
46
51
|
|
47
52
|
if (propName == "themeName") {
|
48
53
|
return !this->themeName.empty()
|
@@ -293,6 +298,11 @@ void UnistylesRuntime::handleAppearanceChange(std::string colorScheme) {
|
|
293
298
|
}
|
294
299
|
}
|
295
300
|
|
301
|
+
void UnistylesRuntime::handleContentSizeCategoryChange(std::string contentSizeCategory) {
|
302
|
+
this->contentSizeCategory = contentSizeCategory;
|
303
|
+
this->onContentSizeCategoryChangeCallback(contentSizeCategory);
|
304
|
+
}
|
305
|
+
|
296
306
|
jsi::Value UnistylesRuntime::getThemeOrFail(jsi::Runtime& runtime) {
|
297
307
|
if (this->themes.size() == 1) {
|
298
308
|
std::string themeName = this->themes.at(0);
|
package/cxx/UnistylesRuntime.h
CHANGED
@@ -20,18 +20,21 @@ class JSI_EXPORT UnistylesRuntime : public jsi::HostObject {
|
|
20
20
|
private:
|
21
21
|
std::function<void(std::string)> onThemeChangeCallback;
|
22
22
|
std::function<void(std::string breakpoint, std::string orientation, int screenWidth, int screenHeight)> onLayoutChangeCallback;
|
23
|
+
std::function<void(std::string)> onContentSizeCategoryChangeCallback;
|
23
24
|
std::function<void()> onPluginChangeCallback;
|
24
25
|
|
25
26
|
int screenWidth;
|
26
27
|
int screenHeight;
|
27
28
|
std::string colorScheme;
|
29
|
+
std::string contentSizeCategory;
|
28
30
|
|
29
31
|
public:
|
30
32
|
UnistylesRuntime(
|
31
33
|
int screenWidth,
|
32
34
|
int screenHeight,
|
33
|
-
std::string colorScheme
|
34
|
-
|
35
|
+
std::string colorScheme,
|
36
|
+
std::string contentSizeCategory
|
37
|
+
): screenWidth(screenWidth), screenHeight(screenHeight), colorScheme(colorScheme), contentSizeCategory(contentSizeCategory) {}
|
35
38
|
|
36
39
|
bool hasAdaptiveThemes;
|
37
40
|
bool supportsAutomaticColorScheme;
|
@@ -53,6 +56,10 @@ public:
|
|
53
56
|
void onPluginChange(std::function<void()> callback) {
|
54
57
|
this->onPluginChangeCallback = callback;
|
55
58
|
}
|
59
|
+
|
60
|
+
void onContentSizeCategoryChange(std::function<void(std::string)> callback) {
|
61
|
+
this->onContentSizeCategoryChangeCallback = callback;
|
62
|
+
}
|
56
63
|
|
57
64
|
jsi::Value get(jsi::Runtime&, const jsi::PropNameID& name) override;
|
58
65
|
void set(jsi::Runtime& runtime, const jsi::PropNameID& propNameId, const jsi::Value& value) override;
|
@@ -60,6 +67,7 @@ public:
|
|
60
67
|
|
61
68
|
void handleScreenSizeChange(int width, int height);
|
62
69
|
void handleAppearanceChange(std::string colorScheme);
|
70
|
+
void handleContentSizeCategoryChange(std::string contentSizeCategory);
|
63
71
|
|
64
72
|
jsi::Value getThemeOrFail(jsi::Runtime&);
|
65
73
|
std::string getBreakpointFromScreenWidth(int width, const std::vector<std::pair<std::string, double>>& sortedBreakpointEntries);
|
package/ios/UnistylesHelpers.h
CHANGED
package/ios/UnistylesHelpers.mm
CHANGED
@@ -15,6 +15,38 @@ std::string getColorScheme() {
|
|
15
15
|
}
|
16
16
|
}
|
17
17
|
|
18
|
+
std::string getContentSizeCategory(UIContentSizeCategory contentSizeCategory) {
|
19
|
+
if ([contentSizeCategory isEqualToString:UIContentSizeCategoryExtraExtraExtraLarge]) {
|
20
|
+
return std::string([@"xxxLarge" UTF8String]);
|
21
|
+
}
|
22
|
+
|
23
|
+
if ([contentSizeCategory isEqualToString:UIContentSizeCategoryExtraExtraLarge]) {
|
24
|
+
return std::string([@"xxLarge" UTF8String]);
|
25
|
+
}
|
26
|
+
|
27
|
+
if ([contentSizeCategory isEqualToString:UIContentSizeCategoryExtraLarge]) {
|
28
|
+
return std::string([@"xLarge" UTF8String]);
|
29
|
+
}
|
30
|
+
|
31
|
+
if ([contentSizeCategory isEqualToString:UIContentSizeCategoryLarge]) {
|
32
|
+
return std::string([@"Large" UTF8String]);
|
33
|
+
}
|
34
|
+
|
35
|
+
if ([contentSizeCategory isEqualToString:UIContentSizeCategoryMedium]) {
|
36
|
+
return std::string([@"Medium" UTF8String]);
|
37
|
+
}
|
38
|
+
|
39
|
+
if ([contentSizeCategory isEqualToString:UIContentSizeCategorySmall]) {
|
40
|
+
return std::string([@"Small" UTF8String]);
|
41
|
+
}
|
42
|
+
|
43
|
+
if ([contentSizeCategory isEqualToString:UIContentSizeCategoryExtraSmall]) {
|
44
|
+
return std::string([@"xSmall" UTF8String]);
|
45
|
+
}
|
46
|
+
|
47
|
+
return std::string([@"unspecified" UTF8String]);
|
48
|
+
}
|
49
|
+
|
18
50
|
NSString* cxxStringToNSString(std::string cxxString) {
|
19
51
|
return [NSString stringWithUTF8String:cxxString.c_str()];
|
20
52
|
}
|
package/ios/UnistylesModule.mm
CHANGED
@@ -24,6 +24,10 @@ RCT_EXPORT_MODULE(Unistyles)
|
|
24
24
|
selector:@selector(appearanceChanged:)
|
25
25
|
name:RCTUserInterfaceStyleDidChangeNotification
|
26
26
|
object:nil];
|
27
|
+
[[NSNotificationCenter defaultCenter] addObserver:self
|
28
|
+
selector:@selector(contentSizeCategoryDidChange:)
|
29
|
+
name:UIContentSizeCategoryDidChangeNotification
|
30
|
+
object:nil];
|
27
31
|
}
|
28
32
|
|
29
33
|
return self;
|
@@ -34,7 +38,15 @@ RCT_EXPORT_MODULE(Unistyles)
|
|
34
38
|
self.unistylesRuntime = nullptr;
|
35
39
|
}
|
36
40
|
|
37
|
-
[[NSNotificationCenter defaultCenter] removeObserver:self
|
41
|
+
[[NSNotificationCenter defaultCenter] removeObserver:self
|
42
|
+
name: UIContentSizeCategoryDidChangeNotification
|
43
|
+
object: nil];
|
44
|
+
[[NSNotificationCenter defaultCenter] removeObserver:self
|
45
|
+
name: RCTUserInterfaceStyleDidChangeNotification
|
46
|
+
object: nil];
|
47
|
+
[[NSNotificationCenter defaultCenter] removeObserver:self
|
48
|
+
name: UIDeviceOrientationDidChangeNotification
|
49
|
+
object: nil];
|
38
50
|
}
|
39
51
|
|
40
52
|
|
@@ -63,6 +75,14 @@ RCT_EXPORT_MODULE(Unistyles)
|
|
63
75
|
}
|
64
76
|
}
|
65
77
|
|
78
|
+
- (void)contentSizeCategoryDidChange:(NSNotification *)notification {
|
79
|
+
UIContentSizeCategory contentSizeCategory = [[UIApplication sharedApplication] preferredContentSizeCategory];
|
80
|
+
|
81
|
+
if (self.unistylesRuntime != nullptr) {
|
82
|
+
((UnistylesRuntime*)self.unistylesRuntime)->handleContentSizeCategoryChange(getContentSizeCategory(contentSizeCategory));
|
83
|
+
}
|
84
|
+
}
|
85
|
+
|
66
86
|
#pragma mark - Event emitter
|
67
87
|
- (NSArray<NSString *> *)supportedEvents {
|
68
88
|
return @[@"__unistylesOnChange"];
|
@@ -112,11 +132,17 @@ void registerUnistylesHostObject(jsi::Runtime &runtime, UnistylesModule* weakSel
|
|
112
132
|
CGFloat initialScreenWidth = [UIScreen mainScreen].bounds.size.width;
|
113
133
|
CGFloat initialScreenHeight = [UIScreen mainScreen].bounds.size.height;
|
114
134
|
std::string initialColorScheme = getColorScheme();
|
135
|
+
__block UIContentSizeCategory contentSizeCategory;
|
136
|
+
|
137
|
+
dispatch_sync(dispatch_get_main_queue(), ^{
|
138
|
+
contentSizeCategory = [[UIApplication sharedApplication] preferredContentSizeCategory];
|
139
|
+
});
|
115
140
|
|
116
141
|
auto unistylesRuntime = std::make_shared<UnistylesRuntime>(
|
117
142
|
(int)initialScreenWidth,
|
118
143
|
(int)initialScreenHeight,
|
119
|
-
initialColorScheme
|
144
|
+
initialColorScheme,
|
145
|
+
getContentSizeCategory(contentSizeCategory)
|
120
146
|
);
|
121
147
|
|
122
148
|
unistylesRuntime.get()->onThemeChange([=](std::string theme) {
|
@@ -155,6 +181,17 @@ void registerUnistylesHostObject(jsi::Runtime &runtime, UnistylesModule* weakSel
|
|
155
181
|
[weakSelf emitEvent:@"__unistylesOnChange" withBody:body];
|
156
182
|
});
|
157
183
|
});
|
184
|
+
|
185
|
+
unistylesRuntime.get()->onContentSizeCategoryChange([=](std::string contentSizeCategory) {
|
186
|
+
NSDictionary *body = @{
|
187
|
+
@"type": @"dynamicTypeSize",
|
188
|
+
@"payload": @{
|
189
|
+
@"contentSizeCategory": cxxStringToNSString(contentSizeCategory)
|
190
|
+
}
|
191
|
+
};
|
192
|
+
|
193
|
+
[weakSelf emitEvent:@"__unistylesOnChange" withBody:body];
|
194
|
+
});
|
158
195
|
|
159
196
|
weakSelf.unistylesRuntime = unistylesRuntime.get();
|
160
197
|
|
package/lib/commonjs/common.js
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
4
4
|
value: true
|
5
5
|
});
|
6
|
-
exports.warn = exports.isWeb = exports.isServer = exports.isMobile = exports.isIOS = exports.isAndroid = exports.UnistylesEventType = exports.UnistylesError = exports.ScreenOrientation = void 0;
|
6
|
+
exports.warn = exports.isWeb = exports.isServer = exports.isMobile = exports.isIOS = exports.isAndroid = exports.UnistylesEventType = exports.UnistylesError = exports.ScreenOrientation = exports.IOSContentSizeCategory = exports.AndroidContentSizeCategory = void 0;
|
7
7
|
var _reactNative = require("react-native");
|
8
8
|
const warn = message => {
|
9
9
|
console.warn(`🦄 [react-native-unistyles]: ${message}`);
|
@@ -18,10 +18,30 @@ const ScreenOrientation = exports.ScreenOrientation = {
|
|
18
18
|
Landscape: 'landscape',
|
19
19
|
Portrait: 'portrait'
|
20
20
|
};
|
21
|
+
let IOSContentSizeCategory = exports.IOSContentSizeCategory = /*#__PURE__*/function (IOSContentSizeCategory) {
|
22
|
+
IOSContentSizeCategory["ExtraExtraExtraLarge"] = "xxxLarge";
|
23
|
+
IOSContentSizeCategory["ExtraExtraLarge"] = "xxLarge";
|
24
|
+
IOSContentSizeCategory["ExtraLarge"] = "xLarge";
|
25
|
+
IOSContentSizeCategory["Large"] = "Large";
|
26
|
+
IOSContentSizeCategory["Medium"] = "Medium";
|
27
|
+
IOSContentSizeCategory["Small"] = "Small";
|
28
|
+
IOSContentSizeCategory["ExtraSmall"] = "xSmall";
|
29
|
+
IOSContentSizeCategory["Unspecified"] = "unspecified";
|
30
|
+
return IOSContentSizeCategory;
|
31
|
+
}({});
|
32
|
+
let AndroidContentSizeCategory = exports.AndroidContentSizeCategory = /*#__PURE__*/function (AndroidContentSizeCategory) {
|
33
|
+
AndroidContentSizeCategory["Small"] = "Small";
|
34
|
+
AndroidContentSizeCategory["Default"] = "Default";
|
35
|
+
AndroidContentSizeCategory["Large"] = "Large";
|
36
|
+
AndroidContentSizeCategory["ExtraLarge"] = "ExtraLarge";
|
37
|
+
AndroidContentSizeCategory["Huge"] = "Huge";
|
38
|
+
return AndroidContentSizeCategory;
|
39
|
+
}({});
|
21
40
|
let UnistylesEventType = exports.UnistylesEventType = /*#__PURE__*/function (UnistylesEventType) {
|
22
41
|
UnistylesEventType["Theme"] = "theme";
|
23
42
|
UnistylesEventType["Layout"] = "layout";
|
24
43
|
UnistylesEventType["Plugin"] = "plugin";
|
44
|
+
UnistylesEventType["DynamicTypeSize"] = "dynamicTypeSize";
|
25
45
|
return UnistylesEventType;
|
26
46
|
}({});
|
27
47
|
let UnistylesError = exports.UnistylesError = /*#__PURE__*/function (UnistylesError) {
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"names":["_reactNative","require","warn","message","console","exports","isWeb","Platform","OS","isIOS","isAndroid","isMobile","isServer","window","ScreenOrientation","Landscape","Portrait","UnistylesEventType","UnistylesError"],"sourceRoot":"../../src","sources":["common.ts"],"mappings":";;;;;;AAAA,IAAAA,YAAA,GAAAC,OAAA;AAEO,MAAMC,IAAI,GAAIC,OAAe,IAAK;EACrCC,OAAO,CAACF,IAAI,CAAE,gCAA+BC,OAAQ,EAAC,CAAC;AAC3D,CAAC;AAAAE,OAAA,CAAAH,IAAA,GAAAA,IAAA;AAEM,MAAMI,KAAK,GAAAD,OAAA,CAAAC,KAAA,GAAGC,qBAAQ,CAACC,EAAE,KAAK,KAAK;AACnC,MAAMC,KAAK,GAAAJ,OAAA,CAAAI,KAAA,GAAGF,qBAAQ,CAACC,EAAE,KAAK,KAAK;AACnC,MAAME,SAAS,GAAAL,OAAA,CAAAK,SAAA,GAAGH,qBAAQ,CAACC,EAAE,KAAK,SAAS;AAC3C,MAAMG,QAAQ,GAAAN,OAAA,CAAAM,QAAA,GAAGF,KAAK,IAAIC,SAAS;AACnC,MAAME,QAAQ,GAAAP,OAAA,CAAAO,QAAA,GAAG,OAAOC,MAAM,KAAK,WAAW;AAE9C,MAAMC,iBAAiB,GAAAT,OAAA,CAAAS,iBAAA,GAAG;EAC7BC,SAAS,EAAE,WAAW;EACtBC,QAAQ,EAAE;AACd,CAAU;AAAA,IAEEC,
|
1
|
+
{"version":3,"names":["_reactNative","require","warn","message","console","exports","isWeb","Platform","OS","isIOS","isAndroid","isMobile","isServer","window","ScreenOrientation","Landscape","Portrait","IOSContentSizeCategory","AndroidContentSizeCategory","UnistylesEventType","UnistylesError"],"sourceRoot":"../../src","sources":["common.ts"],"mappings":";;;;;;AAAA,IAAAA,YAAA,GAAAC,OAAA;AAEO,MAAMC,IAAI,GAAIC,OAAe,IAAK;EACrCC,OAAO,CAACF,IAAI,CAAE,gCAA+BC,OAAQ,EAAC,CAAC;AAC3D,CAAC;AAAAE,OAAA,CAAAH,IAAA,GAAAA,IAAA;AAEM,MAAMI,KAAK,GAAAD,OAAA,CAAAC,KAAA,GAAGC,qBAAQ,CAACC,EAAE,KAAK,KAAK;AACnC,MAAMC,KAAK,GAAAJ,OAAA,CAAAI,KAAA,GAAGF,qBAAQ,CAACC,EAAE,KAAK,KAAK;AACnC,MAAME,SAAS,GAAAL,OAAA,CAAAK,SAAA,GAAGH,qBAAQ,CAACC,EAAE,KAAK,SAAS;AAC3C,MAAMG,QAAQ,GAAAN,OAAA,CAAAM,QAAA,GAAGF,KAAK,IAAIC,SAAS;AACnC,MAAME,QAAQ,GAAAP,OAAA,CAAAO,QAAA,GAAG,OAAOC,MAAM,KAAK,WAAW;AAE9C,MAAMC,iBAAiB,GAAAT,OAAA,CAAAS,iBAAA,GAAG;EAC7BC,SAAS,EAAE,WAAW;EACtBC,QAAQ,EAAE;AACd,CAAU;AAAA,IAEEC,sBAAsB,GAAAZ,OAAA,CAAAY,sBAAA,0BAAtBA,sBAAsB;EAAtBA,sBAAsB;EAAtBA,sBAAsB;EAAtBA,sBAAsB;EAAtBA,sBAAsB;EAAtBA,sBAAsB;EAAtBA,sBAAsB;EAAtBA,sBAAsB;EAAtBA,sBAAsB;EAAA,OAAtBA,sBAAsB;AAAA;AAAA,IAWtBC,0BAA0B,GAAAb,OAAA,CAAAa,0BAAA,0BAA1BA,0BAA0B;EAA1BA,0BAA0B;EAA1BA,0BAA0B;EAA1BA,0BAA0B;EAA1BA,0BAA0B;EAA1BA,0BAA0B;EAAA,OAA1BA,0BAA0B;AAAA;AAAA,IAQ1BC,kBAAkB,GAAAd,OAAA,CAAAc,kBAAA,0BAAlBA,kBAAkB;EAAlBA,kBAAkB;EAAlBA,kBAAkB;EAAlBA,kBAAkB;EAAlBA,kBAAkB;EAAA,OAAlBA,kBAAkB;AAAA;AAAA,IAOlBC,cAAc,GAAAf,OAAA,CAAAe,cAAA,0BAAdA,cAAc;EAAdA,cAAc;EAAdA,cAAc;EAAdA,cAAc;EAAdA,cAAc;EAAdA,cAAc;EAAdA,cAAc;EAAdA,cAAc;EAAdA,cAAc;EAAdA,cAAc;EAAdA,cAAc;EAAA,OAAdA,cAAc;AAAA"}
|
@@ -67,35 +67,25 @@ class UnistyleRegistry {
|
|
67
67
|
}
|
68
68
|
throw new Error(_common.UnistylesError.ThemeNotSelected);
|
69
69
|
};
|
70
|
-
addPlugin = (
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
})) {
|
83
|
-
throw new Error(_common.UnistylesError.DuplicatePluginName);
|
84
|
-
}
|
85
|
-
_this.plugins = [plugin].concat(_this.plugins);
|
86
|
-
_this.unistylesBridge.addPlugin(plugin.name, notify);
|
87
|
-
};
|
88
|
-
})();
|
70
|
+
addPlugin = (plugin, notify = true) => {
|
71
|
+
if (plugin.name.startsWith('__unistyles')) {
|
72
|
+
throw new Error(_common.UnistylesError.InvalidPluginName);
|
73
|
+
}
|
74
|
+
if (this.plugins.some(({
|
75
|
+
name
|
76
|
+
}) => name === plugin.name)) {
|
77
|
+
throw new Error(_common.UnistylesError.DuplicatePluginName);
|
78
|
+
}
|
79
|
+
this.plugins = [plugin].concat(this.plugins);
|
80
|
+
this.unistylesBridge.addPlugin(plugin.name, notify);
|
81
|
+
};
|
89
82
|
removePlugin = plugin => {
|
90
83
|
if (plugin.name.startsWith('__unistyles')) {
|
91
84
|
throw new Error(_common.UnistylesError.CantRemoveInternalPlugin);
|
92
85
|
}
|
93
|
-
this.plugins = this.plugins.filter(
|
94
|
-
|
95
|
-
|
96
|
-
} = _ref2;
|
97
|
-
return name !== plugin.name;
|
98
|
-
});
|
86
|
+
this.plugins = this.plugins.filter(({
|
87
|
+
name
|
88
|
+
}) => name !== plugin.name);
|
99
89
|
this.unistylesBridge.removePlugin(plugin.name);
|
100
90
|
};
|
101
91
|
hasTheme = name => name in this.themes;
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"names":["_common","require","_plugins","UnistyleRegistry","config","plugins","isWeb","normalizeWebStylesPlugin","themeNames","themes","breakpoints","sortedBreakpointPairs","constructor","unistylesBridge","addThemes","keys","Object","addBreakpoints","addConfig","useBreakpoints","adaptiveThemes","useAdaptiveThemes","forEach","plugin","addPlugin","initialTheme","useTheme","experimentalCSSMediaQueries","cssMediaQueriesPlugin","concat","name","getTheme","forName","length","hasTheme","themeName","Error","UnistylesError","ThemeNotFound","ThemeNotSelected","
|
1
|
+
{"version":3,"names":["_common","require","_plugins","UnistyleRegistry","config","plugins","isWeb","normalizeWebStylesPlugin","themeNames","themes","breakpoints","sortedBreakpointPairs","constructor","unistylesBridge","addThemes","keys","Object","addBreakpoints","addConfig","useBreakpoints","adaptiveThemes","useAdaptiveThemes","forEach","plugin","addPlugin","initialTheme","useTheme","experimentalCSSMediaQueries","cssMediaQueriesPlugin","concat","name","getTheme","forName","length","hasTheme","themeName","Error","UnistylesError","ThemeNotFound","ThemeNotSelected","notify","startsWith","InvalidPluginName","some","DuplicatePluginName","removePlugin","CantRemoveInternalPlugin","filter","exports"],"sourceRoot":"../../../src","sources":["core/UnistyleRegistry.ts"],"mappings":"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"}
|
@@ -3,7 +3,195 @@
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
4
4
|
value: true
|
5
5
|
});
|
6
|
-
exports.UnistylesModule = void 0;
|
6
|
+
exports.UnistylesModule = exports.UnistylesBridgeWeb = void 0;
|
7
7
|
var _reactNative = require("react-native");
|
8
|
-
|
8
|
+
var _plugins = require("../plugins");
|
9
|
+
var _common = require("../common");
|
10
|
+
class UnistylesBridgeWeb {
|
11
|
+
#timerRef = undefined;
|
12
|
+
#hasAdaptiveThemes = false;
|
13
|
+
#supportsAutomaticColorScheme = false;
|
14
|
+
#screenWidth = _common.isServer ? undefined : window.innerWidth;
|
15
|
+
#screenHeight = _common.isServer ? undefined : window.innerHeight;
|
16
|
+
#themes = [];
|
17
|
+
#breakpoints = {};
|
18
|
+
#colorScheme = this.getPreferredColorScheme();
|
19
|
+
#themeName = '';
|
20
|
+
#enabledPlugins = [_plugins.normalizeWebStylesPlugin.name];
|
21
|
+
#unistylesEvents = new _reactNative.NativeEventEmitter(_reactNative.NativeModules.Unistyles);
|
22
|
+
#sortedBreakpointPairs = [];
|
23
|
+
#breakpoint = '';
|
24
|
+
#contentSizeCategory = 'unspecified';
|
25
|
+
constructor() {
|
26
|
+
if (!_common.isServer) {
|
27
|
+
this.setupListeners();
|
28
|
+
this.#screenWidth = window.innerWidth;
|
29
|
+
this.#screenHeight = window.innerHeight;
|
30
|
+
}
|
31
|
+
}
|
32
|
+
install() {
|
33
|
+
// @ts-ignore
|
34
|
+
// eslint-disable-next-line no-undef
|
35
|
+
globalThis.__UNISTYLES__ = new Proxy({}, {
|
36
|
+
get: (_target, prop) => {
|
37
|
+
switch (prop) {
|
38
|
+
case 'themeName':
|
39
|
+
return this.getTheme();
|
40
|
+
case 'screenWidth':
|
41
|
+
return this.#screenWidth;
|
42
|
+
case 'screenHeight':
|
43
|
+
return this.#screenHeight;
|
44
|
+
case 'contentSizeCategory':
|
45
|
+
return this.#contentSizeCategory;
|
46
|
+
case 'breakpoint':
|
47
|
+
return this.#breakpoint || undefined;
|
48
|
+
case 'breakpoints':
|
49
|
+
return this.#breakpoints;
|
50
|
+
case 'hasAdaptiveThemes':
|
51
|
+
return this.#hasAdaptiveThemes;
|
52
|
+
case 'sortedBreakpointPairs':
|
53
|
+
return this.#sortedBreakpointPairs;
|
54
|
+
case 'enabledPlugins':
|
55
|
+
return this.#enabledPlugins;
|
56
|
+
case 'colorScheme':
|
57
|
+
return this.#colorScheme;
|
58
|
+
case 'useTheme':
|
59
|
+
return themeName => this.useTheme(themeName);
|
60
|
+
case 'useBreakpoints':
|
61
|
+
return breakpoints => this.useBreakpoints(breakpoints);
|
62
|
+
case 'useAdaptiveThemes':
|
63
|
+
return enable => this.useAdaptiveThemes(enable);
|
64
|
+
case 'addPlugin':
|
65
|
+
return (pluginName, notify) => this.addPlugin(pluginName, notify);
|
66
|
+
case 'removePlugin':
|
67
|
+
return pluginName => this.removePlugin(pluginName);
|
68
|
+
default:
|
69
|
+
return Reflect.get(this, prop);
|
70
|
+
}
|
71
|
+
},
|
72
|
+
set: (target, prop, newValue, receiver) => {
|
73
|
+
switch (prop) {
|
74
|
+
case 'themes':
|
75
|
+
{
|
76
|
+
this.#themes = newValue;
|
77
|
+
this.#supportsAutomaticColorScheme = newValue.includes('light') && newValue.includes('dark');
|
78
|
+
return true;
|
79
|
+
}
|
80
|
+
case 'themeName':
|
81
|
+
{
|
82
|
+
this.#themeName = newValue;
|
83
|
+
this.emitThemeChange();
|
84
|
+
return true;
|
85
|
+
}
|
86
|
+
default:
|
87
|
+
return Reflect.set(target, prop, newValue, receiver);
|
88
|
+
}
|
89
|
+
}
|
90
|
+
});
|
91
|
+
return true;
|
92
|
+
}
|
93
|
+
useTheme(themeName) {
|
94
|
+
this.#themeName = themeName;
|
95
|
+
this.emitThemeChange();
|
96
|
+
}
|
97
|
+
useBreakpoints(breakpoints) {
|
98
|
+
this.#breakpoints = breakpoints;
|
99
|
+
this.#sortedBreakpointPairs = Object.entries(breakpoints).sort(([, a], [, b]) => (a ?? 0) - (b ?? 0));
|
100
|
+
if (!_common.isServer) {
|
101
|
+
this.#breakpoint = this.getBreakpointFromScreenWidth(this.#screenWidth);
|
102
|
+
}
|
103
|
+
}
|
104
|
+
useAdaptiveThemes(enable) {
|
105
|
+
this.#hasAdaptiveThemes = enable;
|
106
|
+
if (!this.#hasAdaptiveThemes || !this.#supportsAutomaticColorScheme) {
|
107
|
+
return;
|
108
|
+
}
|
109
|
+
if (this.#themeName !== this.#colorScheme) {
|
110
|
+
this.#themeName = this.#colorScheme;
|
111
|
+
this.emitThemeChange();
|
112
|
+
}
|
113
|
+
}
|
114
|
+
addPlugin(pluginName, notify) {
|
115
|
+
this.#enabledPlugins = [pluginName].concat(this.#enabledPlugins);
|
116
|
+
if (notify) {
|
117
|
+
this.emitPluginChange();
|
118
|
+
}
|
119
|
+
}
|
120
|
+
removePlugin(pluginName) {
|
121
|
+
this.#enabledPlugins = this.#enabledPlugins.filter(name => name !== pluginName);
|
122
|
+
this.emitPluginChange();
|
123
|
+
}
|
124
|
+
getTheme() {
|
125
|
+
if (this.#themes.length === 1) {
|
126
|
+
return this.#themes.at(0);
|
127
|
+
}
|
128
|
+
return this.#themeName;
|
129
|
+
}
|
130
|
+
setupListeners() {
|
131
|
+
window.addEventListener('resize', () => {
|
132
|
+
clearTimeout(this.#timerRef);
|
133
|
+
this.#timerRef = setTimeout(() => {
|
134
|
+
this.#screenWidth = window.innerWidth;
|
135
|
+
this.#screenHeight = window.innerHeight;
|
136
|
+
this.#breakpoint = this.getBreakpointFromScreenWidth(this.#screenWidth);
|
137
|
+
this.emitLayoutChange();
|
138
|
+
}, 100);
|
139
|
+
});
|
140
|
+
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', event => {
|
141
|
+
this.#colorScheme = event.matches ? 'dark' : 'light';
|
142
|
+
if (!this.#supportsAutomaticColorScheme || !this.#hasAdaptiveThemes) {
|
143
|
+
return;
|
144
|
+
}
|
145
|
+
if (this.#colorScheme !== this.#themeName) {
|
146
|
+
this.#themeName = this.#colorScheme;
|
147
|
+
this.emitThemeChange();
|
148
|
+
}
|
149
|
+
});
|
150
|
+
}
|
151
|
+
getBreakpointFromScreenWidth(width) {
|
152
|
+
const breakpoint = this.#sortedBreakpointPairs.find(([, value], index, otherBreakpoints) => {
|
153
|
+
const minVal = value;
|
154
|
+
const maxVal = otherBreakpoints[index + 1]?.[1];
|
155
|
+
if (!maxVal) {
|
156
|
+
return true;
|
157
|
+
}
|
158
|
+
return width >= minVal && width < maxVal;
|
159
|
+
});
|
160
|
+
return breakpoint?.at(0);
|
161
|
+
}
|
162
|
+
getPreferredColorScheme() {
|
163
|
+
if (!_common.isServer && window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
|
164
|
+
return 'dark';
|
165
|
+
}
|
166
|
+
return 'light';
|
167
|
+
}
|
168
|
+
emitPluginChange() {
|
169
|
+
this.#unistylesEvents.emit('__unistylesOnChange', {
|
170
|
+
type: 'plugin'
|
171
|
+
});
|
172
|
+
}
|
173
|
+
emitThemeChange() {
|
174
|
+
this.#unistylesEvents.emit('__unistylesOnChange', {
|
175
|
+
type: 'theme',
|
176
|
+
payload: {
|
177
|
+
themeName: this.#themeName
|
178
|
+
}
|
179
|
+
});
|
180
|
+
}
|
181
|
+
emitLayoutChange() {
|
182
|
+
this.#unistylesEvents.emit('__unistylesOnChange', {
|
183
|
+
type: 'layout',
|
184
|
+
payload: {
|
185
|
+
breakpoint: this.#breakpoint,
|
186
|
+
orientation: this.#screenWidth > this.#screenHeight ? 'landscape' : 'portrait',
|
187
|
+
screen: {
|
188
|
+
width: this.#screenWidth,
|
189
|
+
height: this.#screenHeight
|
190
|
+
}
|
191
|
+
}
|
192
|
+
});
|
193
|
+
}
|
194
|
+
}
|
195
|
+
exports.UnistylesBridgeWeb = UnistylesBridgeWeb;
|
196
|
+
const UnistylesModule = exports.UnistylesModule = new UnistylesBridgeWeb();
|
9
197
|
//# sourceMappingURL=UnistylesModule.js.map
|