react-native-unistyles 2.0.0-beta.1 → 2.0.0-beta.2
Sign up to get free protection for your applications and to get access to all the features.
- package/android/src/main/java/com/unistyles/UnistylesModule.kt +3 -3
- package/ios/UnistylesModule.mm +5 -5
- 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/hooks/useUnistyles.js +1 -1
- package/lib/commonjs/hooks/useUnistyles.js.map +1 -1
- package/lib/commonjs/utils/withPlugins.js +1 -1
- package/lib/commonjs/utils/withPlugins.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/hooks/useUnistyles.js +1 -1
- package/lib/module/hooks/useUnistyles.js.map +1 -1
- package/lib/module/utils/withPlugins.js +1 -1
- package/lib/module/utils/withPlugins.js.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/package.json +1 -1
- package/src/core/Unistyles.ts +2 -2
- package/src/core/UnistylesModule.web.ts +214 -0
- package/src/hooks/useUnistyles.ts +1 -1
- package/src/utils/withPlugins.ts +1 -1
@@ -117,7 +117,7 @@ class UnistylesModule(reactContext: ReactApplicationContext) : ReactContextBaseJ
|
|
117
117
|
|
118
118
|
reactApplicationContext
|
119
119
|
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
|
120
|
-
.emit("
|
120
|
+
.emit("__unistylesOnChange", body)
|
121
121
|
}
|
122
122
|
|
123
123
|
private fun onThemeChange(themeName: String) {
|
@@ -130,7 +130,7 @@ class UnistylesModule(reactContext: ReactApplicationContext) : ReactContextBaseJ
|
|
130
130
|
|
131
131
|
reactApplicationContext
|
132
132
|
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
|
133
|
-
.emit("
|
133
|
+
.emit("__unistylesOnChange", body)
|
134
134
|
}
|
135
135
|
|
136
136
|
private fun onPluginChange() {
|
@@ -140,7 +140,7 @@ class UnistylesModule(reactContext: ReactApplicationContext) : ReactContextBaseJ
|
|
140
140
|
|
141
141
|
reactApplicationContext
|
142
142
|
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
|
143
|
-
.emit("
|
143
|
+
.emit("__unistylesOnChange", body)
|
144
144
|
}
|
145
145
|
|
146
146
|
@ReactMethod
|
package/ios/UnistylesModule.mm
CHANGED
@@ -65,7 +65,7 @@ RCT_EXPORT_MODULE(Unistyles)
|
|
65
65
|
|
66
66
|
#pragma mark - Event emitter
|
67
67
|
- (NSArray<NSString *> *)supportedEvents {
|
68
|
-
return @[@"
|
68
|
+
return @[@"__unistylesOnChange"];
|
69
69
|
}
|
70
70
|
|
71
71
|
- (void)startObserving {
|
@@ -78,7 +78,7 @@ RCT_EXPORT_MODULE(Unistyles)
|
|
78
78
|
|
79
79
|
- (void)emitEvent:(NSString *)eventName withBody:(NSDictionary *)body {
|
80
80
|
if (self.hasListeners) {
|
81
|
-
[self sendEventWithName:@"
|
81
|
+
[self sendEventWithName:@"__unistylesOnChange" body:body];
|
82
82
|
}
|
83
83
|
}
|
84
84
|
|
@@ -127,7 +127,7 @@ void registerUnistylesHostObject(jsi::Runtime &runtime, UnistylesModule* weakSel
|
|
127
127
|
}
|
128
128
|
};
|
129
129
|
|
130
|
-
[weakSelf emitEvent:@"
|
130
|
+
[weakSelf emitEvent:@"__unistylesOnChange" withBody:body];
|
131
131
|
});
|
132
132
|
|
133
133
|
unistylesRuntime.get()->onLayoutChange([=](std::string breakpoint, std::string orientation, int width, int height) {
|
@@ -143,7 +143,7 @@ void registerUnistylesHostObject(jsi::Runtime &runtime, UnistylesModule* weakSel
|
|
143
143
|
}
|
144
144
|
};
|
145
145
|
|
146
|
-
[weakSelf emitEvent:@"
|
146
|
+
[weakSelf emitEvent:@"__unistylesOnChange" withBody:body];
|
147
147
|
});
|
148
148
|
|
149
149
|
unistylesRuntime.get()->onPluginChange([=]() {
|
@@ -152,7 +152,7 @@ void registerUnistylesHostObject(jsi::Runtime &runtime, UnistylesModule* weakSel
|
|
152
152
|
};
|
153
153
|
|
154
154
|
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
|
155
|
-
[weakSelf emitEvent:@"
|
155
|
+
[weakSelf emitEvent:@"__unistylesOnChange" withBody:body];
|
156
156
|
});
|
157
157
|
});
|
158
158
|
|
@@ -16,7 +16,7 @@ class Unistyles {
|
|
16
16
|
}
|
17
17
|
|
18
18
|
// @ts-ignore
|
19
|
-
this._bridge = global.__UNISTYLES__;
|
19
|
+
this._bridge = (_common.isWeb ? window : global).__UNISTYLES__;
|
20
20
|
this._registry = new _UnistyleRegistry.UnistyleRegistry(this._bridge);
|
21
21
|
this._runtime = new _UnistylesRuntime.UnistylesRuntime(this._bridge, this._registry);
|
22
22
|
}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"names":["_UnistylesModule","require","_UnistylesRuntime","_UnistyleRegistry","_common","Unistyles","constructor","isInstalled","UnistylesModule","install","Error","UnistylesError","RuntimeUnavailable","_bridge","global","__UNISTYLES__","_registry","UnistyleRegistry","_runtime","UnistylesRuntime","registry","runtime","unistyles","exports"],"sourceRoot":"../../../src","sources":["core/Unistyles.ts"],"mappings":";;;;;;AAAA,IAAAA,gBAAA,GAAAC,OAAA;AACA,IAAAC,iBAAA,GAAAD,OAAA;AACA,IAAAE,iBAAA,GAAAF,OAAA;AAEA,IAAAG,OAAA,GAAAH,OAAA;AAEA,MAAMI,SAAS,CAAC;EAKZC,WAAWA,CAAA,EAAG;IACV,MAAMC,WAAW,GAAGC,gCAAe,EAAEC,OAAO,CAAC,CAAC,IAAI,KAAK;IAEvD,IAAI,CAACF,WAAW,EAAE;MACd,MAAM,IAAIG,KAAK,CAACC,sBAAc,CAACC,kBAAkB,CAAC;IACtD;;IAEA;IACA,IAAI,CAACC,OAAO,GAAGC,MAAM,
|
1
|
+
{"version":3,"names":["_UnistylesModule","require","_UnistylesRuntime","_UnistyleRegistry","_common","Unistyles","constructor","isInstalled","UnistylesModule","install","Error","UnistylesError","RuntimeUnavailable","_bridge","isWeb","window","global","__UNISTYLES__","_registry","UnistyleRegistry","_runtime","UnistylesRuntime","registry","runtime","unistyles","exports"],"sourceRoot":"../../../src","sources":["core/Unistyles.ts"],"mappings":";;;;;;AAAA,IAAAA,gBAAA,GAAAC,OAAA;AACA,IAAAC,iBAAA,GAAAD,OAAA;AACA,IAAAE,iBAAA,GAAAF,OAAA;AAEA,IAAAG,OAAA,GAAAH,OAAA;AAEA,MAAMI,SAAS,CAAC;EAKZC,WAAWA,CAAA,EAAG;IACV,MAAMC,WAAW,GAAGC,gCAAe,EAAEC,OAAO,CAAC,CAAC,IAAI,KAAK;IAEvD,IAAI,CAACF,WAAW,EAAE;MACd,MAAM,IAAIG,KAAK,CAACC,sBAAc,CAACC,kBAAkB,CAAC;IACtD;;IAEA;IACA,IAAI,CAACC,OAAO,GAAG,CAACC,aAAK,GAAGC,MAAM,GAAGC,MAAM,EAAEC,aAAgC;IACzE,IAAI,CAACC,SAAS,GAAG,IAAIC,kCAAgB,CAAC,IAAI,CAACN,OAAO,CAAC;IACnD,IAAI,CAACO,QAAQ,GAAG,IAAIC,kCAAgB,CAAC,IAAI,CAACR,OAAO,EAAE,IAAI,CAACK,SAAS,CAAC;EACtE;EAEA,IAAWI,QAAQA,CAAA,EAAG;IAClB,OAAO,IAAI,CAACJ,SAAS;EACzB;EAEA,IAAWK,OAAOA,CAAA,EAAG;IACjB,OAAO,IAAI,CAACH,QAAQ;EACxB;AACJ;AAEO,MAAMI,SAAS,GAAAC,OAAA,CAAAD,SAAA,GAAG,IAAInB,SAAS,CAAC,CAAC"}
|
@@ -0,0 +1,187 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports.UnistylesModule = exports.UnistylesBridgeWeb = void 0;
|
7
|
+
var _reactNative = require("react-native");
|
8
|
+
var _plugins = require("../plugins");
|
9
|
+
class UnistylesBridgeWeb {
|
10
|
+
#timerRef = undefined;
|
11
|
+
#hasAdaptiveThemes = false;
|
12
|
+
#supportsAutomaticColorScheme = false;
|
13
|
+
#screenWidth = window.innerWidth;
|
14
|
+
#screenHeight = window.innerHeight;
|
15
|
+
#themes = [];
|
16
|
+
#colorScheme = this.getPreferredColorScheme();
|
17
|
+
#themeName = '';
|
18
|
+
#enabledPlugins = [_plugins.normalizeWebStylesPlugin.name];
|
19
|
+
#unistylesEvents = new _reactNative.NativeEventEmitter(_reactNative.NativeModules.Unistyles);
|
20
|
+
#sortedBreakpointPairs = [];
|
21
|
+
#breakpoint = '';
|
22
|
+
constructor() {
|
23
|
+
this.setupListeners();
|
24
|
+
}
|
25
|
+
install() {
|
26
|
+
// @ts-ignore
|
27
|
+
window.__UNISTYLES__ = new Proxy({}, {
|
28
|
+
get: (_target, prop) => {
|
29
|
+
switch (prop) {
|
30
|
+
case 'themeName':
|
31
|
+
return this.getTheme();
|
32
|
+
case 'screenWidth':
|
33
|
+
return this.#screenWidth;
|
34
|
+
case 'screenHeight':
|
35
|
+
return this.#screenHeight;
|
36
|
+
case 'breakpoint':
|
37
|
+
return this.#breakpoint || undefined;
|
38
|
+
case 'hasAdaptiveThemes':
|
39
|
+
return this.#hasAdaptiveThemes;
|
40
|
+
case 'sortedBreakpointPairs':
|
41
|
+
return this.#sortedBreakpointPairs;
|
42
|
+
case 'enabledPlugins':
|
43
|
+
return this.#enabledPlugins;
|
44
|
+
case 'colorScheme':
|
45
|
+
return this.#colorScheme;
|
46
|
+
case 'useTheme':
|
47
|
+
return themeName => this.useTheme(themeName);
|
48
|
+
case 'useBreakpoints':
|
49
|
+
return breakpoints => this.useBreakpoints(breakpoints);
|
50
|
+
case 'useAdaptiveThemes':
|
51
|
+
return enable => this.useAdaptiveThemes(enable);
|
52
|
+
case 'addPlugin':
|
53
|
+
return (pluginName, notify) => this.addPlugin(pluginName, notify);
|
54
|
+
case 'removePlugin':
|
55
|
+
return pluginName => this.removePlugin(pluginName);
|
56
|
+
default:
|
57
|
+
return Reflect.get(this, prop);
|
58
|
+
}
|
59
|
+
},
|
60
|
+
set: (target, prop, newValue, receiver) => {
|
61
|
+
switch (prop) {
|
62
|
+
case 'themes':
|
63
|
+
{
|
64
|
+
this.#themes = newValue;
|
65
|
+
this.#supportsAutomaticColorScheme = newValue.includes('light') && newValue.includes('dark');
|
66
|
+
return true;
|
67
|
+
}
|
68
|
+
case 'themeName':
|
69
|
+
{
|
70
|
+
this.#themeName = newValue;
|
71
|
+
this.emitThemeChange();
|
72
|
+
return true;
|
73
|
+
}
|
74
|
+
default:
|
75
|
+
return Reflect.set(target, prop, newValue, receiver);
|
76
|
+
}
|
77
|
+
}
|
78
|
+
});
|
79
|
+
return true;
|
80
|
+
}
|
81
|
+
useTheme(themeName) {
|
82
|
+
this.#themeName = themeName;
|
83
|
+
this.emitThemeChange();
|
84
|
+
}
|
85
|
+
useBreakpoints(breakpoints) {
|
86
|
+
this.#sortedBreakpointPairs = Object.entries(breakpoints).sort((_ref, _ref2) => {
|
87
|
+
let [, a] = _ref;
|
88
|
+
let [, b] = _ref2;
|
89
|
+
return (a ?? 0) - (b ?? 0);
|
90
|
+
});
|
91
|
+
this.#breakpoint = this.getBreakpointFromScreenWidth(this.#screenWidth);
|
92
|
+
}
|
93
|
+
useAdaptiveThemes(enable) {
|
94
|
+
this.#hasAdaptiveThemes = enable;
|
95
|
+
if (!this.#hasAdaptiveThemes || !this.#supportsAutomaticColorScheme) {
|
96
|
+
return;
|
97
|
+
}
|
98
|
+
if (this.#themeName !== this.#colorScheme) {
|
99
|
+
this.#themeName = this.#colorScheme;
|
100
|
+
this.emitThemeChange();
|
101
|
+
}
|
102
|
+
}
|
103
|
+
addPlugin(pluginName, notify) {
|
104
|
+
this.#enabledPlugins = [pluginName].concat(this.#enabledPlugins);
|
105
|
+
if (notify) {
|
106
|
+
this.emitPluginChange();
|
107
|
+
}
|
108
|
+
}
|
109
|
+
removePlugin(pluginName) {
|
110
|
+
this.#enabledPlugins = this.#enabledPlugins.filter(name => name !== pluginName);
|
111
|
+
this.emitPluginChange();
|
112
|
+
}
|
113
|
+
getTheme() {
|
114
|
+
if (this.#themes.length === 1) {
|
115
|
+
return this.#themes.at(0);
|
116
|
+
}
|
117
|
+
return this.#themeName;
|
118
|
+
}
|
119
|
+
setupListeners() {
|
120
|
+
window.addEventListener('resize', () => {
|
121
|
+
clearTimeout(this.#timerRef);
|
122
|
+
this.#timerRef = setTimeout(() => {
|
123
|
+
this.#screenWidth = window.innerWidth;
|
124
|
+
this.#screenHeight = window.innerHeight;
|
125
|
+
this.#breakpoint = this.getBreakpointFromScreenWidth(this.#screenWidth);
|
126
|
+
this.emitLayoutChange();
|
127
|
+
}, 100);
|
128
|
+
});
|
129
|
+
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', event => {
|
130
|
+
this.#colorScheme = event.matches ? 'dark' : 'light';
|
131
|
+
if (!this.#supportsAutomaticColorScheme || !this.#hasAdaptiveThemes) {
|
132
|
+
return;
|
133
|
+
}
|
134
|
+
if (this.#colorScheme !== this.#themeName) {
|
135
|
+
this.#themeName = this.#colorScheme;
|
136
|
+
this.emitThemeChange();
|
137
|
+
}
|
138
|
+
});
|
139
|
+
}
|
140
|
+
getBreakpointFromScreenWidth(width) {
|
141
|
+
const breakpoint = this.#sortedBreakpointPairs.find((_ref3, index, otherBreakpoints) => {
|
142
|
+
let [, value] = _ref3;
|
143
|
+
const minVal = value;
|
144
|
+
const maxVal = otherBreakpoints[index + 1]?.[1];
|
145
|
+
if (!maxVal) {
|
146
|
+
return true;
|
147
|
+
}
|
148
|
+
return width >= minVal && width < maxVal;
|
149
|
+
});
|
150
|
+
return breakpoint?.at(0);
|
151
|
+
}
|
152
|
+
getPreferredColorScheme() {
|
153
|
+
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
|
154
|
+
return 'dark';
|
155
|
+
}
|
156
|
+
return 'light';
|
157
|
+
}
|
158
|
+
emitPluginChange() {
|
159
|
+
this.#unistylesEvents.emit('__unistylesOnChange', {
|
160
|
+
type: 'plugin'
|
161
|
+
});
|
162
|
+
}
|
163
|
+
emitThemeChange() {
|
164
|
+
this.#unistylesEvents.emit('__unistylesOnChange', {
|
165
|
+
type: 'theme',
|
166
|
+
payload: {
|
167
|
+
themeName: this.#themeName
|
168
|
+
}
|
169
|
+
});
|
170
|
+
}
|
171
|
+
emitLayoutChange() {
|
172
|
+
this.#unistylesEvents.emit('__unistylesOnChange', {
|
173
|
+
type: 'layout',
|
174
|
+
payload: {
|
175
|
+
breakpoint: this.#breakpoint,
|
176
|
+
orientation: this.#screenWidth > this.#screenHeight ? 'landscape' : 'portrait',
|
177
|
+
screen: {
|
178
|
+
width: this.#screenWidth,
|
179
|
+
height: this.#screenHeight
|
180
|
+
}
|
181
|
+
}
|
182
|
+
});
|
183
|
+
}
|
184
|
+
}
|
185
|
+
exports.UnistylesBridgeWeb = UnistylesBridgeWeb;
|
186
|
+
const UnistylesModule = exports.UnistylesModule = new UnistylesBridgeWeb();
|
187
|
+
//# sourceMappingURL=UnistylesModule.web.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"names":["_reactNative","require","_plugins","UnistylesBridgeWeb","timerRef","undefined","hasAdaptiveThemes","supportsAutomaticColorScheme","screenWidth","window","innerWidth","screenHeight","innerHeight","themes","colorScheme","getPreferredColorScheme","themeName","enabledPlugins","normalizeWebStylesPlugin","name","unistylesEvents","NativeEventEmitter","NativeModules","Unistyles","sortedBreakpointPairs","breakpoint","constructor","setupListeners","install","__UNISTYLES__","Proxy","get","_target","prop","getTheme","useTheme","breakpoints","useBreakpoints","enable","useAdaptiveThemes","pluginName","notify","addPlugin","removePlugin","Reflect","set","target","newValue","receiver","includes","emitThemeChange","Object","entries","sort","_ref","_ref2","a","b","getBreakpointFromScreenWidth","concat","emitPluginChange","filter","length","at","addEventListener","clearTimeout","setTimeout","emitLayoutChange","matchMedia","event","matches","width","find","_ref3","index","otherBreakpoints","value","minVal","maxVal","emit","type","payload","orientation","screen","height","exports","UnistylesModule"],"sourceRoot":"../../../src","sources":["core/UnistylesModule.web.ts"],"mappings":";;;;;;AAAA,IAAAA,YAAA,GAAAC,OAAA;AAGA,IAAAC,QAAA,GAAAD,OAAA;AAEO,MAAME,kBAAkB,CAAC;EAC5B,CAACC,QAAQ,GAAmCC,SAAS;EACrD,CAACC,iBAAiB,GAAY,KAAK;EACnC,CAACC,4BAA4B,GAAG,KAAK;EACrC,CAACC,WAAW,GAAWC,MAAM,CAACC,UAAU;EACxC,CAACC,YAAY,GAAWF,MAAM,CAACG,WAAW;EAC1C,CAACC,MAAM,GAAiC,EAAE;EAC1C,CAACC,WAAW,GAAoB,IAAI,CAACC,uBAAuB,CAAC,CAAC;EAC9D,CAACC,SAAS,GAA0B,EAAE;EACtC,CAACC,cAAc,GAAkB,CAACC,iCAAwB,CAACC,IAAI,CAAC;EAChE,CAACC,eAAe,GAAG,IAAIC,+BAAkB,CAACC,0BAAa,CAACC,SAAS,CAAC;EAClE,CAACC,qBAAqB,GAAgD,EAAE;EACxE,CAACC,UAAU,GAA+B,EAAE;EAE5CC,WAAWA,CAAA,EAAG;IACV,IAAI,CAACC,cAAc,CAAC,CAAC;EACzB;EAEOC,OAAOA,CAAA,EAAG;IACb;IACAnB,MAAM,CAACoB,aAAa,GAAG,IAAIC,KAAK,CAAC,CAAC,CAAC,EAAE;MACjCC,GAAG,EAAEA,CAACC,OAAO,EAAEC,IAAI,KAAK;QACpB,QAAQA,IAAI;UACR,KAAK,WAAW;YACZ,OAAO,IAAI,CAACC,QAAQ,CAAC,CAAC;UAC1B,KAAK,aAAa;YACd,O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AAeA,CAAA,EAAG;IACtB,IAAI,CAAC,CAAC9B,eAAe,CAAC2D,IAAI,CAAC,qBAAqB,EAAE;MAC9CC,IAAI,EAAE,OAAO;MACbC,OAAO,EAAE;QACLjE,SAAS,EAAE,IAAI,CAAC,CAACA;MACrB;IACJ,CAAC,CAAC;EACN;EAEQmD,gBAAgBA,CAAA,EAAG;IACvB,IAAI,CAAC,CAAC/C,eAAe,CAAC2D,IAAI,CAAC,qBAAqB,EAAE;MAC9CC,IAAI,EAAE,QAAQ;MACdC,OAAO,EAAE;QACLxD,UAAU,EAAE,IAAI,CAAC,CAACA,UAAU;QAC5ByD,WAAW,EAAE,IAAI,CAAC,CAAC1E,WAAW,GAAG,IAAI,CAAC,CAACG,YAAY,GAC7C,WAAW,GACX,UAAU;QAChBwE,MAAM,EAAE;UACJZ,KAAK,EAAE,IAAI,CAAC,CAAC/D,WAAW;UACxB4E,MAAM,EAAE,IAAI,CAAC,CAACzE;QAClB;MACJ;IACJ,CAAC,CAAC;EACN;AACJ;AAAC0E,OAAA,CAAAlF,kBAAA,GAAAA,kBAAA;AAEM,MAAMmF,eAAe,GAAAD,OAAA,CAAAC,eAAA,GAAG,IAAInF,kBAAkB,CAAC,CAAC"}
|
@@ -21,7 +21,7 @@ const useUnistyles = () => {
|
|
21
21
|
}
|
22
22
|
});
|
23
23
|
(0, _react.useEffect)(() => {
|
24
|
-
const subscription = unistylesEvents.addListener('
|
24
|
+
const subscription = unistylesEvents.addListener('__unistylesOnChange', event => {
|
25
25
|
switch (event.type) {
|
26
26
|
case _common.UnistylesEventType.Theme:
|
27
27
|
{
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"names":["_reactNative","require","_react","_core","_common","unistylesEvents","NativeEventEmitter","NativeModules","Unistyles","useUnistyles","plugins","setPlugins","useState","unistyles","runtime","enabledPlugins","theme","setTheme","registry","getTheme","themeName","layout","setLayout","breakpoint","orientation","screenSize","width","screen","height","useEffect","subscription","addListener","event","type","UnistylesEventType","Theme","themeEvent","payload","Layout","layoutEvent","Plugin","remove","exports"],"sourceRoot":"../../../src","sources":["hooks/useUnistyles.ts"],"mappings":";;;;;;AAAA,IAAAA,YAAA,GAAAC,OAAA;AACA,IAAAC,MAAA,GAAAD,OAAA;AACA,IAAAE,KAAA,GAAAF,OAAA;AACA,IAAAG,OAAA,GAAAH,OAAA;AAGA,MAAMI,eAAe,GAAG,IAAIC,+BAAkB,CAACC,0BAAa,CAACC,SAAS,CAAC;AAEhE,MAAMC,YAAY,GAAGA,CAAA,KAAM;EAC9B,MAAM,CAACC,OAAO,EAAEC,UAAU,CAAC,GAAG,IAAAC,eAAQ,EAACC,eAAS,CAACC,OAAO,CAACC,cAAc,CAAC;EACxE,MAAM,CAACC,KAAK,EAAEC,QAAQ,CAAC,GAAG,IAAAL,eAAQ,EAACC,eAAS,CAACK,QAAQ,CAACC,QAAQ,CAACN,eAAS,CAACC,OAAO,CAACM,SAAS,CAAC,CAAC;EAC5F,MAAM,CAACC,MAAM,EAAEC,SAAS,CAAC,GAAG,IAAAV,eAAQ,EAAC;IACjCW,UAAU,EAAEV,eAAS,CAACC,OAAO,CAACS,UAAU;IACxCC,WAAW,EAAEX,eAAS,CAACC,OAAO,CAACU,WAAW;IAC1CC,UAAU,EAAE;MACRC,KAAK,EAAEb,eAAS,CAACC,OAAO,CAACa,MAAM,CAACD,KAAK;MACrCE,MAAM,EAAEf,eAAS,CAACC,OAAO,CAACa,MAAM,CAACC;IACrC;EACJ,CAAC,CAAC;EAEF,IAAAC,gBAAS,EAAC,MAAM;IACZ,MAAMC,YAAY,GAAGzB,eAAe,CAAC0B,WAAW,CAC5C,
|
1
|
+
{"version":3,"names":["_reactNative","require","_react","_core","_common","unistylesEvents","NativeEventEmitter","NativeModules","Unistyles","useUnistyles","plugins","setPlugins","useState","unistyles","runtime","enabledPlugins","theme","setTheme","registry","getTheme","themeName","layout","setLayout","breakpoint","orientation","screenSize","width","screen","height","useEffect","subscription","addListener","event","type","UnistylesEventType","Theme","themeEvent","payload","Layout","layoutEvent","Plugin","remove","exports"],"sourceRoot":"../../../src","sources":["hooks/useUnistyles.ts"],"mappings":";;;;;;AAAA,IAAAA,YAAA,GAAAC,OAAA;AACA,IAAAC,MAAA,GAAAD,OAAA;AACA,IAAAE,KAAA,GAAAF,OAAA;AACA,IAAAG,OAAA,GAAAH,OAAA;AAGA,MAAMI,eAAe,GAAG,IAAIC,+BAAkB,CAACC,0BAAa,CAACC,SAAS,CAAC;AAEhE,MAAMC,YAAY,GAAGA,CAAA,KAAM;EAC9B,MAAM,CAACC,OAAO,EAAEC,UAAU,CAAC,GAAG,IAAAC,eAAQ,EAACC,eAAS,CAACC,OAAO,CAACC,cAAc,CAAC;EACxE,MAAM,CAACC,KAAK,EAAEC,QAAQ,CAAC,GAAG,IAAAL,eAAQ,EAACC,eAAS,CAACK,QAAQ,CAACC,QAAQ,CAACN,eAAS,CAACC,OAAO,CAACM,SAAS,CAAC,CAAC;EAC5F,MAAM,CAACC,MAAM,EAAEC,SAAS,CAAC,GAAG,IAAAV,eAAQ,EAAC;IACjCW,UAAU,EAAEV,eAAS,CAACC,OAAO,CAACS,UAAU;IACxCC,WAAW,EAAEX,eAAS,CAACC,OAAO,CAACU,WAAW;IAC1CC,UAAU,EAAE;MACRC,KAAK,EAAEb,eAAS,CAACC,OAAO,CAACa,MAAM,CAACD,KAAK;MACrCE,MAAM,EAAEf,eAAS,CAACC,OAAO,CAACa,MAAM,CAACC;IACrC;EACJ,CAAC,CAAC;EAEF,IAAAC,gBAAS,EAAC,MAAM;IACZ,MAAMC,YAAY,GAAGzB,eAAe,CAAC0B,WAAW,CAC5C,qBAAqB,EACpBC,KAAsB,IAAK;MACxB,QAAQA,KAAK,CAACC,IAAI;QACd,KAAKC,0BAAkB,CAACC,KAAK;UAAE;YAC3B,MAAMC,UAAU,GAAGJ,KAA4B;YAE/C,OAAOf,QAAQ,CAACJ,eAAS,CAACK,QAAQ,CAACC,QAAQ,CAACiB,UAAU,CAACC,OAAO,CAACjB,SAAS,CAAC,CAAC;UAC9E;QACA,KAAKc,0BAAkB,CAACI,MAAM;UAAE;YAC5B,MAAMC,WAAW,GAAGP,KAAmC;YAEvD,OAAOV,SAAS,CAAC;cACbC,UAAU,EAAEgB,WAAW,CAACF,OAAO,CAACd,UAAU;cAC1CC,WAAW,EAAEe,WAAW,CAACF,OAAO,CAACb,WAAW;cAC5CC,UAAU,EAAEc,WAAW,CAACF,OAAO,CAACV;YACpC,CAAC,CAAC;UACN;QACA,KAAKO,0BAAkB,CAACM,MAAM;UAAE;YAC5B,OAAO7B,UAAU,CAACE,eAAS,CAACC,OAAO,CAACC,cAAc,CAAC;UACvD;QACA;UACI;MACR;IACJ,CACJ,CAAC;IAED,OAAOe,YAAY,CAACW,MAAM;EAC9B,CAAC,EAAE,EAAE,CAAC;EAEN,OAAO;IACH/B,OAAO;IACPM,KAAK;IACLK;EACJ,CAAC;AACL,CAAC;AAAAqB,OAAA,CAAAjC,YAAA,GAAAA,YAAA"}
|
@@ -7,7 +7,7 @@ exports.withPlugins = void 0;
|
|
7
7
|
var _core = require("../core");
|
8
8
|
const withPlugins = (key, style) => _core.unistyles.registry.plugins.reduce((acc, plugin) => {
|
9
9
|
if (plugin.onParsedStyle) {
|
10
|
-
return plugin.onParsedStyle(key,
|
10
|
+
return plugin.onParsedStyle(key, acc, _core.unistyles.runtime);
|
11
11
|
}
|
12
12
|
return acc;
|
13
13
|
}, style);
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"names":["_core","require","withPlugins","key","style","unistyles","registry","plugins","reduce","acc","plugin","onParsedStyle","runtime","exports"],"sourceRoot":"../../../src","sources":["utils/withPlugins.ts"],"mappings":";;;;;;AACA,IAAAA,KAAA,GAAAC,OAAA;AAEO,MAAMC,WAAW,GAAGA,CACvBC,GAAW,EACXC,KAAc,KACbC,eAAS,CAACC,QAAQ,CAACC,OAAO,CAACC,MAAM,CAAC,CAACC,GAAG,EAAEC,MAAM,KAAK;EACpD,IAAIA,MAAM,CAACC,aAAa,EAAE;IACtB,OAAOD,MAAM,CAACC,aAAa,CAACR,GAAG,
|
1
|
+
{"version":3,"names":["_core","require","withPlugins","key","style","unistyles","registry","plugins","reduce","acc","plugin","onParsedStyle","runtime","exports"],"sourceRoot":"../../../src","sources":["utils/withPlugins.ts"],"mappings":";;;;;;AACA,IAAAA,KAAA,GAAAC,OAAA;AAEO,MAAMC,WAAW,GAAGA,CACvBC,GAAW,EACXC,KAAc,KACbC,eAAS,CAACC,QAAQ,CAACC,OAAO,CAACC,MAAM,CAAC,CAACC,GAAG,EAAEC,MAAM,KAAK;EACpD,IAAIA,MAAM,CAACC,aAAa,EAAE;IACtB,OAAOD,MAAM,CAACC,aAAa,CAACR,GAAG,EAAEM,GAAG,EAAEJ,eAAS,CAACO,OAAO,CAAC;EAC5D;EAEA,OAAOH,GAAG;AACd,CAAC,EAAEL,KAAK,CAAC;AAAAS,OAAA,CAAAX,WAAA,GAAAA,WAAA"}
|
@@ -1,7 +1,7 @@
|
|
1
1
|
import { UnistylesModule } from './UnistylesModule';
|
2
2
|
import { UnistylesRuntime } from './UnistylesRuntime';
|
3
3
|
import { UnistyleRegistry } from './UnistyleRegistry';
|
4
|
-
import { UnistylesError } from '../common';
|
4
|
+
import { UnistylesError, isWeb } from '../common';
|
5
5
|
class Unistyles {
|
6
6
|
constructor() {
|
7
7
|
const isInstalled = UnistylesModule?.install() ?? false;
|
@@ -10,7 +10,7 @@ class Unistyles {
|
|
10
10
|
}
|
11
11
|
|
12
12
|
// @ts-ignore
|
13
|
-
this._bridge = global.__UNISTYLES__;
|
13
|
+
this._bridge = (isWeb ? window : global).__UNISTYLES__;
|
14
14
|
this._registry = new UnistyleRegistry(this._bridge);
|
15
15
|
this._runtime = new UnistylesRuntime(this._bridge, this._registry);
|
16
16
|
}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"names":["UnistylesModule","UnistylesRuntime","UnistyleRegistry","UnistylesError","Unistyles","constructor","isInstalled","install","Error","RuntimeUnavailable","_bridge","global","__UNISTYLES__","_registry","_runtime","registry","runtime","unistyles"],"sourceRoot":"../../../src","sources":["core/Unistyles.ts"],"mappings":"AAAA,SAASA,eAAe,QAAQ,mBAAmB;AACnD,SAASC,gBAAgB,QAAQ,oBAAoB;AACrD,SAASC,gBAAgB,QAAQ,oBAAoB;AAErD,SAASC,cAAc,QAAQ,WAAW;
|
1
|
+
{"version":3,"names":["UnistylesModule","UnistylesRuntime","UnistyleRegistry","UnistylesError","isWeb","Unistyles","constructor","isInstalled","install","Error","RuntimeUnavailable","_bridge","window","global","__UNISTYLES__","_registry","_runtime","registry","runtime","unistyles"],"sourceRoot":"../../../src","sources":["core/Unistyles.ts"],"mappings":"AAAA,SAASA,eAAe,QAAQ,mBAAmB;AACnD,SAASC,gBAAgB,QAAQ,oBAAoB;AACrD,SAASC,gBAAgB,QAAQ,oBAAoB;AAErD,SAASC,cAAc,EAAEC,KAAK,QAAQ,WAAW;AAEjD,MAAMC,SAAS,CAAC;EAKZC,WAAWA,CAAA,EAAG;IACV,MAAMC,WAAW,GAAGP,eAAe,EAAEQ,OAAO,CAAC,CAAC,IAAI,KAAK;IAEvD,IAAI,CAACD,WAAW,EAAE;MACd,MAAM,IAAIE,KAAK,CAACN,cAAc,CAACO,kBAAkB,CAAC;IACtD;;IAEA;IACA,IAAI,CAACC,OAAO,GAAG,CAACP,KAAK,GAAGQ,MAAM,GAAGC,MAAM,EAAEC,aAAgC;IACzE,IAAI,CAACC,SAAS,GAAG,IAAIb,gBAAgB,CAAC,IAAI,CAACS,OAAO,CAAC;IACnD,IAAI,CAACK,QAAQ,GAAG,IAAIf,gBAAgB,CAAC,IAAI,CAACU,OAAO,EAAE,IAAI,CAACI,SAAS,CAAC;EACtE;EAEA,IAAWE,QAAQA,CAAA,EAAG;IAClB,OAAO,IAAI,CAACF,SAAS;EACzB;EAEA,IAAWG,OAAOA,CAAA,EAAG;IACjB,OAAO,IAAI,CAACF,QAAQ;EACxB;AACJ;AAEA,OAAO,MAAMG,SAAS,GAAG,IAAId,SAAS,CAAC,CAAC"}
|
@@ -0,0 +1,180 @@
|
|
1
|
+
import { NativeEventEmitter, NativeModules } from 'react-native';
|
2
|
+
import { normalizeWebStylesPlugin } from '../plugins';
|
3
|
+
export class UnistylesBridgeWeb {
|
4
|
+
#timerRef = undefined;
|
5
|
+
#hasAdaptiveThemes = false;
|
6
|
+
#supportsAutomaticColorScheme = false;
|
7
|
+
#screenWidth = window.innerWidth;
|
8
|
+
#screenHeight = window.innerHeight;
|
9
|
+
#themes = [];
|
10
|
+
#colorScheme = this.getPreferredColorScheme();
|
11
|
+
#themeName = '';
|
12
|
+
#enabledPlugins = [normalizeWebStylesPlugin.name];
|
13
|
+
#unistylesEvents = new NativeEventEmitter(NativeModules.Unistyles);
|
14
|
+
#sortedBreakpointPairs = [];
|
15
|
+
#breakpoint = '';
|
16
|
+
constructor() {
|
17
|
+
this.setupListeners();
|
18
|
+
}
|
19
|
+
install() {
|
20
|
+
// @ts-ignore
|
21
|
+
window.__UNISTYLES__ = new Proxy({}, {
|
22
|
+
get: (_target, prop) => {
|
23
|
+
switch (prop) {
|
24
|
+
case 'themeName':
|
25
|
+
return this.getTheme();
|
26
|
+
case 'screenWidth':
|
27
|
+
return this.#screenWidth;
|
28
|
+
case 'screenHeight':
|
29
|
+
return this.#screenHeight;
|
30
|
+
case 'breakpoint':
|
31
|
+
return this.#breakpoint || undefined;
|
32
|
+
case 'hasAdaptiveThemes':
|
33
|
+
return this.#hasAdaptiveThemes;
|
34
|
+
case 'sortedBreakpointPairs':
|
35
|
+
return this.#sortedBreakpointPairs;
|
36
|
+
case 'enabledPlugins':
|
37
|
+
return this.#enabledPlugins;
|
38
|
+
case 'colorScheme':
|
39
|
+
return this.#colorScheme;
|
40
|
+
case 'useTheme':
|
41
|
+
return themeName => this.useTheme(themeName);
|
42
|
+
case 'useBreakpoints':
|
43
|
+
return breakpoints => this.useBreakpoints(breakpoints);
|
44
|
+
case 'useAdaptiveThemes':
|
45
|
+
return enable => this.useAdaptiveThemes(enable);
|
46
|
+
case 'addPlugin':
|
47
|
+
return (pluginName, notify) => this.addPlugin(pluginName, notify);
|
48
|
+
case 'removePlugin':
|
49
|
+
return pluginName => this.removePlugin(pluginName);
|
50
|
+
default:
|
51
|
+
return Reflect.get(this, prop);
|
52
|
+
}
|
53
|
+
},
|
54
|
+
set: (target, prop, newValue, receiver) => {
|
55
|
+
switch (prop) {
|
56
|
+
case 'themes':
|
57
|
+
{
|
58
|
+
this.#themes = newValue;
|
59
|
+
this.#supportsAutomaticColorScheme = newValue.includes('light') && newValue.includes('dark');
|
60
|
+
return true;
|
61
|
+
}
|
62
|
+
case 'themeName':
|
63
|
+
{
|
64
|
+
this.#themeName = newValue;
|
65
|
+
this.emitThemeChange();
|
66
|
+
return true;
|
67
|
+
}
|
68
|
+
default:
|
69
|
+
return Reflect.set(target, prop, newValue, receiver);
|
70
|
+
}
|
71
|
+
}
|
72
|
+
});
|
73
|
+
return true;
|
74
|
+
}
|
75
|
+
useTheme(themeName) {
|
76
|
+
this.#themeName = themeName;
|
77
|
+
this.emitThemeChange();
|
78
|
+
}
|
79
|
+
useBreakpoints(breakpoints) {
|
80
|
+
this.#sortedBreakpointPairs = Object.entries(breakpoints).sort((_ref, _ref2) => {
|
81
|
+
let [, a] = _ref;
|
82
|
+
let [, b] = _ref2;
|
83
|
+
return (a ?? 0) - (b ?? 0);
|
84
|
+
});
|
85
|
+
this.#breakpoint = this.getBreakpointFromScreenWidth(this.#screenWidth);
|
86
|
+
}
|
87
|
+
useAdaptiveThemes(enable) {
|
88
|
+
this.#hasAdaptiveThemes = enable;
|
89
|
+
if (!this.#hasAdaptiveThemes || !this.#supportsAutomaticColorScheme) {
|
90
|
+
return;
|
91
|
+
}
|
92
|
+
if (this.#themeName !== this.#colorScheme) {
|
93
|
+
this.#themeName = this.#colorScheme;
|
94
|
+
this.emitThemeChange();
|
95
|
+
}
|
96
|
+
}
|
97
|
+
addPlugin(pluginName, notify) {
|
98
|
+
this.#enabledPlugins = [pluginName].concat(this.#enabledPlugins);
|
99
|
+
if (notify) {
|
100
|
+
this.emitPluginChange();
|
101
|
+
}
|
102
|
+
}
|
103
|
+
removePlugin(pluginName) {
|
104
|
+
this.#enabledPlugins = this.#enabledPlugins.filter(name => name !== pluginName);
|
105
|
+
this.emitPluginChange();
|
106
|
+
}
|
107
|
+
getTheme() {
|
108
|
+
if (this.#themes.length === 1) {
|
109
|
+
return this.#themes.at(0);
|
110
|
+
}
|
111
|
+
return this.#themeName;
|
112
|
+
}
|
113
|
+
setupListeners() {
|
114
|
+
window.addEventListener('resize', () => {
|
115
|
+
clearTimeout(this.#timerRef);
|
116
|
+
this.#timerRef = setTimeout(() => {
|
117
|
+
this.#screenWidth = window.innerWidth;
|
118
|
+
this.#screenHeight = window.innerHeight;
|
119
|
+
this.#breakpoint = this.getBreakpointFromScreenWidth(this.#screenWidth);
|
120
|
+
this.emitLayoutChange();
|
121
|
+
}, 100);
|
122
|
+
});
|
123
|
+
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', event => {
|
124
|
+
this.#colorScheme = event.matches ? 'dark' : 'light';
|
125
|
+
if (!this.#supportsAutomaticColorScheme || !this.#hasAdaptiveThemes) {
|
126
|
+
return;
|
127
|
+
}
|
128
|
+
if (this.#colorScheme !== this.#themeName) {
|
129
|
+
this.#themeName = this.#colorScheme;
|
130
|
+
this.emitThemeChange();
|
131
|
+
}
|
132
|
+
});
|
133
|
+
}
|
134
|
+
getBreakpointFromScreenWidth(width) {
|
135
|
+
const breakpoint = this.#sortedBreakpointPairs.find((_ref3, index, otherBreakpoints) => {
|
136
|
+
let [, value] = _ref3;
|
137
|
+
const minVal = value;
|
138
|
+
const maxVal = otherBreakpoints[index + 1]?.[1];
|
139
|
+
if (!maxVal) {
|
140
|
+
return true;
|
141
|
+
}
|
142
|
+
return width >= minVal && width < maxVal;
|
143
|
+
});
|
144
|
+
return breakpoint?.at(0);
|
145
|
+
}
|
146
|
+
getPreferredColorScheme() {
|
147
|
+
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
|
148
|
+
return 'dark';
|
149
|
+
}
|
150
|
+
return 'light';
|
151
|
+
}
|
152
|
+
emitPluginChange() {
|
153
|
+
this.#unistylesEvents.emit('__unistylesOnChange', {
|
154
|
+
type: 'plugin'
|
155
|
+
});
|
156
|
+
}
|
157
|
+
emitThemeChange() {
|
158
|
+
this.#unistylesEvents.emit('__unistylesOnChange', {
|
159
|
+
type: 'theme',
|
160
|
+
payload: {
|
161
|
+
themeName: this.#themeName
|
162
|
+
}
|
163
|
+
});
|
164
|
+
}
|
165
|
+
emitLayoutChange() {
|
166
|
+
this.#unistylesEvents.emit('__unistylesOnChange', {
|
167
|
+
type: 'layout',
|
168
|
+
payload: {
|
169
|
+
breakpoint: this.#breakpoint,
|
170
|
+
orientation: this.#screenWidth > this.#screenHeight ? 'landscape' : 'portrait',
|
171
|
+
screen: {
|
172
|
+
width: this.#screenWidth,
|
173
|
+
height: this.#screenHeight
|
174
|
+
}
|
175
|
+
}
|
176
|
+
});
|
177
|
+
}
|
178
|
+
}
|
179
|
+
export const UnistylesModule = new UnistylesBridgeWeb();
|
180
|
+
//# sourceMappingURL=UnistylesModule.web.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"names":["NativeEventEmitter","NativeModules","normalizeWebStylesPlugin","UnistylesBridgeWeb","timerRef","undefined","hasAdaptiveThemes","supportsAutomaticColorScheme","screenWidth","window","innerWidth","screenHeight","innerHeight","themes","colorScheme","getPreferredColorScheme","themeName","enabledPlugins","name","unistylesEvents","Unistyles","sortedBreakpointPairs","breakpoint","constructor","setupListeners","install","__UNISTYLES__","Proxy","get","_target","prop","getTheme","useTheme","breakpoints","useBreakpoints","enable","useAdaptiveThemes","pluginName","notify","addPlugin","removePlugin","Reflect","set","target","newValue","receiver","includes","emitThemeChange","Object","entries","sort","_ref","_ref2","a","b","getBreakpointFromScreenWidth","concat","emitPluginChange","filter","length","at","addEventListener","clearTimeout","setTimeout","emitLayoutChange","matchMedia","event","matches","width","find","_ref3","index","otherBreakpoints","value","minVal","maxVal","emit","type","payload","orientation","screen","height","UnistylesModule"],"sourceRoot":"../../../src","sources":["core/UnistylesModule.web.ts"],"mappings":"AAAA,SAASA,kBAAkB,EAAEC,aAAa,QAAQ,cAAc;AAGhE,SAASC,wBAAwB,QAAQ,YAAY;AAErD,OAAO,MAAMC,kBAAkB,CAAC;EAC5B,CAACC,QAAQ,GAAmCC,SAAS;EACrD,CAACC,iBAAiB,GAAY,KAAK;EACnC,CAACC,4BAA4B,GAAG,KAAK;EACrC,CAACC,WAAW,GAAWC,MAAM,CAACC,UAAU;EACxC,CAACC,YAAY,GAAWF,MAAM,CAACG,WAAW;EAC1C,CAACC,MAAM,GAAiC,EAAE;EAC1C,CAACC,WAAW,GAAoB,IAAI,CAACC,uBAAuB,CAAC,CAAC;EAC9D,CAACC,SAAS,GAA0B,EAAE;EACtC,CAACC,cAAc,GAAkB,CAACf,wBAAwB,CAACgB,IAAI,CAAC;EAChE,CAACC,eAAe,GAAG,IAAInB,kBAAkB,CAACC,aAAa,CAACmB,SAAS,CAAC;EAClE,CAACC,qBAAqB,GAAgD,EAAE;EACxE,CAACC,UAAU,GAA+B,EAAE;EAE5CC,WAAWA,CAAA,EAAG;IACV,IAAI,CAACC,cAAc,CAAC,CAAC;EACzB;EAEOC,OAAOA,CAAA,EAAG;IACb;IACAhB,MAAM,CAACiB,aAAa,GAAG,IAAIC,KAAK,CAAC,CAAC,CAAC,EAAE;MACjCC,GAAG,EAAEA,CAACC,OAAO,EAAEC,IAAI,KAAK;QACpB,QAAQA,IAAI;UACR,KAAK,WAAW;YACZ,OAAO,IAAI,CAACC,QAAQ,CAAC,CAAC;UAC1B,KAAK,aAAa;YACd,OAAO,IAAI,CAAC,CAACvB,WAAW;UAC5B,KAAK,cAAc;YACf,OAAO,IAAI,CAAC,CAACG,YAAY;UAC7B,KAAK,YAAY;YACb,OAAO,IAAI,CAAC,CAAC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ACL9D,SAAS,EAAE,IAAI,CAAC,CAACA;MACrB;IACJ,CAAC,CAAC;EACN;EAEQgD,gBAAgBA,CAAA,EAAG;IACvB,IAAI,CAAC,CAAC7C,eAAe,CAACyD,IAAI,CAAC,qBAAqB,EAAE;MAC9CC,IAAI,EAAE,QAAQ;MACdC,OAAO,EAAE;QACLxD,UAAU,EAAE,IAAI,CAAC,CAACA,UAAU;QAC5ByD,WAAW,EAAE,IAAI,CAAC,CAACvE,WAAW,GAAG,IAAI,CAAC,CAACG,YAAY,GAC7C,WAAW,GACX,UAAU;QAChBqE,MAAM,EAAE;UACJZ,KAAK,EAAE,IAAI,CAAC,CAAC5D,WAAW;UACxByE,MAAM,EAAE,IAAI,CAAC,CAACtE;QAClB;MACJ;IACJ,CAAC,CAAC;EACN;AACJ;AAEA,OAAO,MAAMuE,eAAe,GAAG,IAAI/E,kBAAkB,CAAC,CAAC"}
|
@@ -15,7 +15,7 @@ export const useUnistyles = () => {
|
|
15
15
|
}
|
16
16
|
});
|
17
17
|
useEffect(() => {
|
18
|
-
const subscription = unistylesEvents.addListener('
|
18
|
+
const subscription = unistylesEvents.addListener('__unistylesOnChange', event => {
|
19
19
|
switch (event.type) {
|
20
20
|
case UnistylesEventType.Theme:
|
21
21
|
{
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"names":["NativeEventEmitter","NativeModules","useEffect","useState","unistyles","UnistylesEventType","unistylesEvents","Unistyles","useUnistyles","plugins","setPlugins","runtime","enabledPlugins","theme","setTheme","registry","getTheme","themeName","layout","setLayout","breakpoint","orientation","screenSize","width","screen","height","subscription","addListener","event","type","Theme","themeEvent","payload","Layout","layoutEvent","Plugin","remove"],"sourceRoot":"../../../src","sources":["hooks/useUnistyles.ts"],"mappings":"AAAA,SAASA,kBAAkB,EAAEC,aAAa,QAAQ,cAAc;AAChE,SAASC,SAAS,EAAEC,QAAQ,QAAQ,OAAO;AAC3C,SAASC,SAAS,QAAQ,SAAS;AACnC,SAASC,kBAAkB,QAAQ,WAAW;AAG9C,MAAMC,eAAe,GAAG,IAAIN,kBAAkB,CAACC,aAAa,CAACM,SAAS,CAAC;AAEvE,OAAO,MAAMC,YAAY,GAAGA,CAAA,KAAM;EAC9B,MAAM,CAACC,OAAO,EAAEC,UAAU,CAAC,GAAGP,QAAQ,CAACC,SAAS,CAACO,OAAO,CAACC,cAAc,CAAC;EACxE,MAAM,CAACC,KAAK,EAAEC,QAAQ,CAAC,GAAGX,QAAQ,CAACC,SAAS,CAACW,QAAQ,CAACC,QAAQ,CAACZ,SAAS,CAACO,OAAO,CAACM,SAAS,CAAC,CAAC;EAC5F,MAAM,CAACC,MAAM,EAAEC,SAAS,CAAC,GAAGhB,QAAQ,CAAC;IACjCiB,UAAU,EAAEhB,SAAS,CAACO,OAAO,CAACS,UAAU;IACxCC,WAAW,EAAEjB,SAAS,CAACO,OAAO,CAACU,WAAW;IAC1CC,UAAU,EAAE;MACRC,KAAK,EAAEnB,SAAS,CAACO,OAAO,CAACa,MAAM,CAACD,KAAK;MACrCE,MAAM,EAAErB,SAAS,CAACO,OAAO,CAACa,MAAM,CAACC;IACrC;EACJ,CAAC,CAAC;EAEFvB,SAAS,CAAC,MAAM;IACZ,MAAMwB,YAAY,GAAGpB,eAAe,CAACqB,WAAW,CAC5C,
|
1
|
+
{"version":3,"names":["NativeEventEmitter","NativeModules","useEffect","useState","unistyles","UnistylesEventType","unistylesEvents","Unistyles","useUnistyles","plugins","setPlugins","runtime","enabledPlugins","theme","setTheme","registry","getTheme","themeName","layout","setLayout","breakpoint","orientation","screenSize","width","screen","height","subscription","addListener","event","type","Theme","themeEvent","payload","Layout","layoutEvent","Plugin","remove"],"sourceRoot":"../../../src","sources":["hooks/useUnistyles.ts"],"mappings":"AAAA,SAASA,kBAAkB,EAAEC,aAAa,QAAQ,cAAc;AAChE,SAASC,SAAS,EAAEC,QAAQ,QAAQ,OAAO;AAC3C,SAASC,SAAS,QAAQ,SAAS;AACnC,SAASC,kBAAkB,QAAQ,WAAW;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,WAAW;cAC5CC,UAAU,EAAEY,WAAW,CAACF,OAAO,CAACR;YACpC,CAAC,CAAC;UACN;QACA,KAAKnB,kBAAkB,CAAC8B,MAAM;UAAE;YAC5B,OAAOzB,UAAU,CAACN,SAAS,CAACO,OAAO,CAACC,cAAc,CAAC;UACvD;QACA;UACI;MACR;IACJ,CACJ,CAAC;IAED,OAAOc,YAAY,CAACU,MAAM;EAC9B,CAAC,EAAE,EAAE,CAAC;EAEN,OAAO;IACH3B,OAAO;IACPI,KAAK;IACLK;EACJ,CAAC;AACL,CAAC"}
|
@@ -1,7 +1,7 @@
|
|
1
1
|
import { unistyles } from '../core';
|
2
2
|
export const withPlugins = (key, style) => unistyles.registry.plugins.reduce((acc, plugin) => {
|
3
3
|
if (plugin.onParsedStyle) {
|
4
|
-
return plugin.onParsedStyle(key,
|
4
|
+
return plugin.onParsedStyle(key, acc, unistyles.runtime);
|
5
5
|
}
|
6
6
|
return acc;
|
7
7
|
}, style);
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"names":["unistyles","withPlugins","key","style","registry","plugins","reduce","acc","plugin","onParsedStyle","runtime"],"sourceRoot":"../../../src","sources":["utils/withPlugins.ts"],"mappings":"AACA,SAASA,SAAS,QAAQ,SAAS;AAEnC,OAAO,MAAMC,WAAW,GAAGA,CACvBC,GAAW,EACXC,KAAc,KACbH,SAAS,CAACI,QAAQ,CAACC,OAAO,CAACC,MAAM,CAAC,CAACC,GAAG,EAAEC,MAAM,KAAK;EACpD,IAAIA,MAAM,CAACC,aAAa,EAAE;IACtB,OAAOD,MAAM,CAACC,aAAa,CAACP,GAAG,
|
1
|
+
{"version":3,"names":["unistyles","withPlugins","key","style","registry","plugins","reduce","acc","plugin","onParsedStyle","runtime"],"sourceRoot":"../../../src","sources":["utils/withPlugins.ts"],"mappings":"AACA,SAASA,SAAS,QAAQ,SAAS;AAEnC,OAAO,MAAMC,WAAW,GAAGA,CACvBC,GAAW,EACXC,KAAc,KACbH,SAAS,CAACI,QAAQ,CAACC,OAAO,CAACC,MAAM,CAAC,CAACC,GAAG,EAAEC,MAAM,KAAK;EACpD,IAAIA,MAAM,CAACC,aAAa,EAAE;IACtB,OAAOD,MAAM,CAACC,aAAa,CAACP,GAAG,EAAEK,GAAG,EAAEP,SAAS,CAACU,OAAO,CAAC;EAC5D;EAEA,OAAOH,GAAG;AACd,CAAC,EAAEJ,KAAK,CAAC"}
|
@@ -0,0 +1,19 @@
|
|
1
|
+
export declare class UnistylesBridgeWeb {
|
2
|
+
#private;
|
3
|
+
constructor();
|
4
|
+
install(): boolean;
|
5
|
+
private useTheme;
|
6
|
+
private useBreakpoints;
|
7
|
+
private useAdaptiveThemes;
|
8
|
+
private addPlugin;
|
9
|
+
private removePlugin;
|
10
|
+
private getTheme;
|
11
|
+
private setupListeners;
|
12
|
+
private getBreakpointFromScreenWidth;
|
13
|
+
private getPreferredColorScheme;
|
14
|
+
private emitPluginChange;
|
15
|
+
private emitThemeChange;
|
16
|
+
private emitLayoutChange;
|
17
|
+
}
|
18
|
+
export declare const UnistylesModule: UnistylesBridgeWeb;
|
19
|
+
//# sourceMappingURL=UnistylesModule.web.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"UnistylesModule.web.d.ts","sourceRoot":"","sources":["../../../../src/core/UnistylesModule.web.ts"],"names":[],"mappings":"AAKA,qBAAa,kBAAkB;;;IAkBpB,OAAO;IA0Dd,OAAO,CAAC,QAAQ;IAKhB,OAAO,CAAC,cAAc;IAOtB,OAAO,CAAC,iBAAiB;IAazB,OAAO,CAAC,SAAS;IAQjB,OAAO,CAAC,YAAY;IAKpB,OAAO,CAAC,QAAQ;IAShB,OAAO,CAAC,cAAc;IA6BtB,OAAO,CAAC,4BAA4B;IAgBpC,OAAO,CAAC,uBAAuB;IAQ/B,OAAO,CAAC,gBAAgB;IAMxB,OAAO,CAAC,eAAe;IASvB,OAAO,CAAC,gBAAgB;CAe3B;AAED,eAAO,MAAM,eAAe,oBAA2B,CAAA"}
|
package/package.json
CHANGED
package/src/core/Unistyles.ts
CHANGED
@@ -2,7 +2,7 @@ import { UnistylesModule } from './UnistylesModule'
|
|
2
2
|
import { UnistylesRuntime } from './UnistylesRuntime'
|
3
3
|
import { UnistyleRegistry } from './UnistyleRegistry'
|
4
4
|
import type { UnistylesBridge } from '../types'
|
5
|
-
import { UnistylesError } from '../common'
|
5
|
+
import { UnistylesError, isWeb } from '../common'
|
6
6
|
|
7
7
|
class Unistyles {
|
8
8
|
private _runtime: UnistylesRuntime
|
@@ -17,7 +17,7 @@ class Unistyles {
|
|
17
17
|
}
|
18
18
|
|
19
19
|
// @ts-ignore
|
20
|
-
this._bridge = global.__UNISTYLES__ as UnistylesBridge
|
20
|
+
this._bridge = (isWeb ? window : global).__UNISTYLES__ as UnistylesBridge
|
21
21
|
this._registry = new UnistyleRegistry(this._bridge)
|
22
22
|
this._runtime = new UnistylesRuntime(this._bridge, this._registry)
|
23
23
|
}
|
@@ -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()
|
package/src/utils/withPlugins.ts
CHANGED
@@ -6,7 +6,7 @@ export const withPlugins = (
|
|
6
6
|
style: RNStyle
|
7
7
|
) => unistyles.registry.plugins.reduce((acc, plugin) => {
|
8
8
|
if (plugin.onParsedStyle) {
|
9
|
-
return plugin.onParsedStyle(key,
|
9
|
+
return plugin.onParsedStyle(key, acc, unistyles.runtime)
|
10
10
|
}
|
11
11
|
|
12
12
|
return acc
|