react-native-unistyles 3.0.0-alpha.27 → 3.0.0-alpha.28
Sign up to get free protection for your applications and to get access to all the features.
- package/cxx/core/UnistylesRegistry.cpp +4 -8
- package/cxx/core/UnistylesRegistry.h +1 -1
- package/cxx/core/UnistylesState.cpp +2 -10
- package/cxx/core/UnistylesState.h +1 -1
- package/cxx/hybridObjects/HybridStyleSheet.cpp +1 -2
- package/cxx/parser/Parser.cpp +73 -4
- package/cxx/parser/Parser.h +2 -0
- package/lib/commonjs/core/index.js.map +1 -1
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/mq.js +1 -4
- package/lib/commonjs/mq.js.map +2 -1
- package/lib/commonjs/specs/NativePlatform/NativePlatform.nitro.js.map +1 -0
- package/lib/commonjs/specs/NavigtionBar/UnistylesNavigationBar.nitro.js.map +1 -1
- package/lib/commonjs/specs/NavigtionBar/index.js.map +2 -1
- package/lib/commonjs/specs/ShadowRegistry/ShadowRegistry.nitro.js.map +1 -1
- package/lib/commonjs/specs/ShadowRegistry/types.js +3 -0
- package/lib/commonjs/specs/ShadowRegistry/types.js.map +1 -1
- package/lib/commonjs/specs/StatusBar/UnistylesStatusBar.nitro.js.map +1 -1
- package/lib/commonjs/specs/StatusBar/index.js.map +2 -1
- package/lib/commonjs/specs/StyleSheet/UnistylesStyleSheet.nitro.js +2 -4
- package/lib/commonjs/specs/StyleSheet/UnistylesStyleSheet.nitro.js.map +1 -1
- package/lib/commonjs/specs/index.js.map +1 -1
- package/lib/commonjs/specs/types.js +10 -10
- package/lib/commonjs/specs/types.js.map +1 -1
- package/lib/commonjs/types/stylesheet.js.map +1 -1
- package/lib/commonjs/web/convert/index.js +1 -7
- package/lib/commonjs/web/convert/shadow.js +1 -5
- package/lib/commonjs/web/convert/shadow.js.map +2 -1
- package/lib/commonjs/web/convert/textShadow.js +1 -4
- package/lib/commonjs/web/convert/textShadow.js.map +2 -1
- package/lib/commonjs/web/convert/transform.js.map +2 -1
- package/lib/commonjs/web/create.js +1 -5
- package/lib/commonjs/web/create.js.map +2 -1
- package/lib/commonjs/web/mock.js.map +2 -1
- package/lib/commonjs/web/runtime.js.map +2 -1
- package/lib/commonjs/web/state.js.map +1 -1
- package/lib/commonjs/web/utils/common.js +1 -3
- package/lib/commonjs/web/utils/common.js.map +2 -1
- package/lib/commonjs/web/utils/unistyle.js.map +1 -1
- package/lib/commonjs/web/variants/index.js.map +1 -1
- package/lib/module/common.js.map +1 -1
- package/lib/module/core/useBreakpoint.js.map +1 -1
- package/lib/module/core/useBreakpoint.native.js.map +2 -1
- package/lib/module/core/useTheme.js.map +2 -1
- package/lib/module/core/useTheme.native.js.map +2 -1
- package/lib/module/index.js.map +1 -1
- package/lib/module/mq.js +1 -4
- package/lib/module/mq.js.map +2 -1
- package/lib/module/specs/NativePlatform/NativePlatform.nitro.js +5 -6
- package/lib/module/specs/NativePlatform/NativePlatform.nitro.js.map +1 -1
- package/lib/module/specs/NavigtionBar/UnistylesNavigationBar.nitro.js.map +1 -1
- package/lib/module/specs/NavigtionBar/index.js.map +2 -1
- package/lib/module/specs/ShadowRegistry/ShadowRegistry.nitro.js.map +1 -0
- package/lib/module/specs/ShadowRegistry/index.js.map +2 -1
- package/lib/module/specs/ShadowRegistry/types.js +2 -0
- package/lib/module/specs/ShadowRegistry/types.js.map +1 -1
- package/lib/module/specs/StatusBar/UnistylesStatusBar.nitro.js.map +1 -1
- package/lib/module/specs/StyleSheet/UnistylesStyleSheet.nitro.js +1 -1
- package/lib/module/specs/StyleSheet/UnistylesStyleSheet.nitro.js.map +1 -0
- package/lib/module/specs/TurboUnistyles/NativeTurboUnistyles.js +2 -3
- package/lib/module/specs/TurboUnistyles/NativeTurboUnistyles.js.map +2 -1
- package/lib/module/specs/UnistylesRuntime/UnistylesRuntime.nitro.js.map +1 -1
- package/lib/module/specs/index.web.js.map +2 -1
- package/lib/module/specs/types.js.map +1 -1
- package/lib/module/types/stylesheet.js.map +1 -1
- package/lib/module/web/convert/boxShadow.js +1 -4
- package/lib/module/web/convert/boxShadow.js.map +2 -1
- package/lib/module/web/convert/pseudo.js.map +2 -1
- package/lib/module/web/convert/shadow.js +1 -5
- package/lib/module/web/convert/shadow.js.map +2 -1
- package/lib/module/web/convert/style.js.map +2 -1
- package/lib/module/web/convert/textShadow.js +1 -4
- package/lib/module/web/convert/textShadow.js.map +2 -1
- package/lib/module/web/convert/transform.js.map +2 -1
- package/lib/module/web/convert/utils.js.map +2 -1
- package/lib/module/web/index.js +1 -2
- package/lib/module/web/index.js.map +2 -1
- package/lib/module/web/listener.js.map +2 -1
- package/lib/module/web/mock.js.map +2 -1
- package/lib/module/web/registry.js +1 -3
- package/lib/module/web/registry.js.map +2 -1
- package/lib/module/web/runtime.js.map +2 -1
- package/lib/module/web/shadowRegistry.js +1 -9
- package/lib/module/web/shadowRegistry.js.map +2 -1
- package/lib/module/web/state.js.map +1 -1
- package/lib/module/web/utils/index.js.map +1 -1
- package/lib/module/web/utils/unistyle.js.map +2 -1
- package/lib/module/web/variants/index.js.map +1 -1
- package/lib/module/web/variants/useVariants.js +1 -2
- package/lib/module/web/variants/useVariants.js.map +2 -1
- package/lib/typescript/src/global.d.ts +0 -2
- package/lib/typescript/src/global.d.ts.map +1 -1
- package/lib/typescript/src/types/stylesheet.d.ts +4 -2
- package/lib/typescript/src/types/stylesheet.d.ts.map +1 -1
- package/lib/typescript/src/web/create.d.ts +204 -0
- package/lib/typescript/src/web/create.d.ts.map +1 -1
- package/lib/typescript/src/web/index.d.ts +204 -0
- package/lib/typescript/src/web/index.d.ts.map +1 -1
- package/lib/typescript/src/web/runtime.d.ts +3 -3
- package/lib/typescript/src/web/runtime.d.ts.map +1 -1
- package/lib/typescript/src/web/state.d.ts +1 -1
- package/lib/typescript/src/web/state.d.ts.map +1 -1
- package/lib/typescript/src/web/utils/unistyle.d.ts +2 -2
- package/lib/typescript/src/web/utils/unistyle.d.ts.map +1 -1
- package/package.json +13 -13
- package/src/common.js +4 -0
- package/src/core/index.js +2 -0
- package/src/core/useBreakpoint.js +14 -0
- package/src/core/useBreakpoint.native.js +16 -0
- package/src/core/useTheme.js +14 -0
- package/src/core/useTheme.native.js +16 -0
- package/src/global.ts +1 -4
- package/src/index.js +3 -0
- package/src/mq.js +38 -0
- package/src/specs/NativePlatform/NativePlatform.nitro.js +18 -0
- package/src/specs/NativePlatform/index.js +1 -0
- package/src/specs/NavigtionBar/UnistylesNavigationBar.nitro.js +1 -0
- package/src/specs/NavigtionBar/index.js +9 -0
- package/src/specs/ShadowRegistry/ShadowRegistry.nitro.js +1 -0
- package/src/specs/ShadowRegistry/index.js +26 -0
- package/src/specs/ShadowRegistry/types.js +1 -0
- package/src/specs/StatusBar/UnistylesStatusBar.nitro.js +1 -0
- package/src/specs/StatusBar/index.js +25 -0
- package/src/specs/StyleSheet/UnistylesStyleSheet.nitro.js +1 -0
- package/src/specs/StyleSheet/index.js +13 -0
- package/src/specs/TurboUnistyles/NativeTurboUnistyles.js +2 -0
- package/src/specs/TurboUnistyles/index.js +1 -0
- package/src/specs/UnistylesRuntime/UnistylesRuntime.nitro.js +1 -0
- package/src/specs/UnistylesRuntime/index.js +20 -0
- package/src/specs/index.js +8 -0
- package/src/specs/index.web.js +2 -0
- package/src/specs/types.js +17 -0
- package/src/types/stylesheet.ts +5 -3
- package/src/web/convert/boxShadow.js +59 -0
- package/src/web/convert/index.js +56 -0
- package/src/web/convert/pseudo.js +131 -0
- package/src/web/convert/shadow.js +50 -0
- package/src/web/convert/style.js +137 -0
- package/src/web/convert/textShadow.js +56 -0
- package/src/web/convert/transform.js +65 -0
- package/src/web/convert/types.js +2 -0
- package/src/web/convert/utils.js +39 -0
- package/src/web/create.js +39 -0
- package/src/web/index.js +22 -0
- package/src/web/listener.js +26 -0
- package/src/web/mock.js +21 -0
- package/src/web/mq.js +14 -0
- package/src/web/registry.js +168 -0
- package/src/web/runtime.js +160 -0
- package/src/web/runtime.ts +1 -1
- package/src/web/shadowRegistry.js +99 -0
- package/src/web/state.ts +3 -3
- package/src/web/utils/common.js +79 -0
- package/src/web/utils/index.js +2 -0
- package/src/web/utils/unistyle.js +72 -0
- package/src/web/utils/unistyle.ts +2 -2
- package/src/web/variants/getVariants.js +26 -0
- package/src/web/variants/index.js +2 -0
- package/src/web/variants/useVariants.js +50 -0
@@ -0,0 +1,160 @@
|
|
1
|
+
import { UnistyleDependency } from '../specs/NativePlatform';
|
2
|
+
import { ColorScheme, Orientation } from '../specs/types';
|
3
|
+
import { WebContentSizeCategory } from '../types';
|
4
|
+
import { UnistylesListener } from './listener';
|
5
|
+
import { NavigationBar, StatusBar } from './mock';
|
6
|
+
import { UnistylesState } from './state';
|
7
|
+
import { hexToRGBA, isServer, schemeToTheme } from './utils';
|
8
|
+
class UnistylesRuntimeBuilder {
|
9
|
+
lightMedia = this.getLightMedia();
|
10
|
+
darkMedia = this.getDarkMedia();
|
11
|
+
getLightMedia() {
|
12
|
+
if (isServer()) {
|
13
|
+
return null;
|
14
|
+
}
|
15
|
+
if (!this.lightMedia) {
|
16
|
+
this.lightMedia = window.matchMedia('(prefers-color-scheme: light)');
|
17
|
+
}
|
18
|
+
return this.lightMedia;
|
19
|
+
}
|
20
|
+
getDarkMedia() {
|
21
|
+
if (isServer()) {
|
22
|
+
return null;
|
23
|
+
}
|
24
|
+
if (!this.darkMedia) {
|
25
|
+
this.darkMedia = window.matchMedia('(prefers-color-scheme: dark)');
|
26
|
+
}
|
27
|
+
return this.darkMedia;
|
28
|
+
}
|
29
|
+
get colorScheme() {
|
30
|
+
switch (true) {
|
31
|
+
case this.getLightMedia()?.matches:
|
32
|
+
return ColorScheme.Light;
|
33
|
+
case this.getDarkMedia()?.matches:
|
34
|
+
return ColorScheme.Dark;
|
35
|
+
default:
|
36
|
+
return ColorScheme.Unspecified;
|
37
|
+
}
|
38
|
+
}
|
39
|
+
get themeName() {
|
40
|
+
return UnistylesState.themeName;
|
41
|
+
}
|
42
|
+
get contentSizeCategory() {
|
43
|
+
return WebContentSizeCategory.Unspecified;
|
44
|
+
}
|
45
|
+
get breakpoint() {
|
46
|
+
return UnistylesState.breakpoint;
|
47
|
+
}
|
48
|
+
get orientation() {
|
49
|
+
if (isServer()) {
|
50
|
+
return Orientation.Portrait;
|
51
|
+
}
|
52
|
+
return screen.orientation.type.includes('portrait') ? Orientation.Portrait : Orientation.Landscape;
|
53
|
+
}
|
54
|
+
get theme() {
|
55
|
+
return this.getTheme(this.themeName);
|
56
|
+
}
|
57
|
+
get pixelRatio() {
|
58
|
+
return isServer() ? 1 : window.devicePixelRatio;
|
59
|
+
}
|
60
|
+
get screen() {
|
61
|
+
if (isServer()) {
|
62
|
+
return {
|
63
|
+
width: 0,
|
64
|
+
height: 0
|
65
|
+
};
|
66
|
+
}
|
67
|
+
return {
|
68
|
+
width: window.innerWidth,
|
69
|
+
height: window.innerHeight
|
70
|
+
};
|
71
|
+
}
|
72
|
+
get fontScale() {
|
73
|
+
return 1;
|
74
|
+
}
|
75
|
+
get insets() {
|
76
|
+
return {
|
77
|
+
top: 0,
|
78
|
+
bottom: 0,
|
79
|
+
left: 0,
|
80
|
+
right: 0,
|
81
|
+
ime: 0
|
82
|
+
};
|
83
|
+
}
|
84
|
+
get statusBar() {
|
85
|
+
return StatusBar;
|
86
|
+
}
|
87
|
+
get rtl() {
|
88
|
+
return isServer() ? true : document.documentElement.dir === 'rtl';
|
89
|
+
}
|
90
|
+
get hasAdaptiveThemes() {
|
91
|
+
return UnistylesState.hasAdaptiveThemes;
|
92
|
+
}
|
93
|
+
get navigationBar() {
|
94
|
+
return NavigationBar;
|
95
|
+
}
|
96
|
+
get miniRuntime() {
|
97
|
+
return {
|
98
|
+
colorScheme: this.colorScheme,
|
99
|
+
themeName: this.themeName,
|
100
|
+
contentSizeCategory: this.contentSizeCategory,
|
101
|
+
breakpoint: this.breakpoint,
|
102
|
+
isLandscape: this.orientation === Orientation.Landscape,
|
103
|
+
isPortrait: this.orientation === Orientation.Portrait,
|
104
|
+
pixelRatio: this.pixelRatio,
|
105
|
+
screen: this.screen,
|
106
|
+
fontScale: this.fontScale,
|
107
|
+
insets: this.insets,
|
108
|
+
statusBar: {
|
109
|
+
width: this.statusBar.width,
|
110
|
+
height: this.statusBar.height
|
111
|
+
},
|
112
|
+
navigationBar: {
|
113
|
+
width: this.navigationBar.width,
|
114
|
+
height: this.navigationBar.height,
|
115
|
+
},
|
116
|
+
rtl: this.rtl,
|
117
|
+
hasAdaptiveThemes: this.hasAdaptiveThemes
|
118
|
+
};
|
119
|
+
}
|
120
|
+
setTheme = (themeName) => {
|
121
|
+
if (themeName === UnistylesRuntime.themeName) {
|
122
|
+
return;
|
123
|
+
}
|
124
|
+
UnistylesState.themeName = themeName;
|
125
|
+
UnistylesListener.emitChange(UnistyleDependency.Theme);
|
126
|
+
UnistylesListener.emitChange(UnistyleDependency.ThemeName);
|
127
|
+
};
|
128
|
+
setAdaptiveThemes = (isEnabled) => {
|
129
|
+
UnistylesState.hasAdaptiveThemes = isEnabled;
|
130
|
+
if (!isEnabled) {
|
131
|
+
return;
|
132
|
+
}
|
133
|
+
this.setTheme(schemeToTheme(UnistylesRuntime.colorScheme));
|
134
|
+
};
|
135
|
+
setRootViewBackgroundColor = (hex, alpha) => {
|
136
|
+
if (isServer()) {
|
137
|
+
return;
|
138
|
+
}
|
139
|
+
document.documentElement.style.backgroundColor = alpha ? hexToRGBA(hex, alpha) : hex;
|
140
|
+
};
|
141
|
+
setImmersiveMode = () => { };
|
142
|
+
updateTheme = (themeName, updater) => {
|
143
|
+
const oldTheme = UnistylesState.themes.get(themeName);
|
144
|
+
if (!oldTheme) {
|
145
|
+
throw new Error(`🦄 Theme "${themeName}" is not registered!`);
|
146
|
+
}
|
147
|
+
UnistylesState.themes.set(themeName, updater(oldTheme));
|
148
|
+
};
|
149
|
+
getTheme = (themeName = this.themeName) => {
|
150
|
+
if (!themeName) {
|
151
|
+
throw new Error('🦄 No theme selected!');
|
152
|
+
}
|
153
|
+
const theme = UnistylesState.themes.get(themeName);
|
154
|
+
if (!theme) {
|
155
|
+
throw new Error(`🦄 Theme "${this.themeName}" is not registered!`);
|
156
|
+
}
|
157
|
+
return theme;
|
158
|
+
};
|
159
|
+
}
|
160
|
+
export const UnistylesRuntime = new UnistylesRuntimeBuilder();
|
package/src/web/runtime.ts
CHANGED
@@ -160,7 +160,7 @@ class UnistylesRuntimeBuilder {
|
|
160
160
|
return
|
161
161
|
}
|
162
162
|
|
163
|
-
this.setTheme(schemeToTheme(UnistylesRuntime.colorScheme))
|
163
|
+
this.setTheme(schemeToTheme(UnistylesRuntime.colorScheme) as AppThemeName)
|
164
164
|
}
|
165
165
|
|
166
166
|
setRootViewBackgroundColor = (hex: string, alpha?: number) => {
|
@@ -0,0 +1,99 @@
|
|
1
|
+
import { UnistylesRegistry } from './registry';
|
2
|
+
import { createDoubleMap, equal, extractSecrets, extractUnistyleDependencies, isInDocument } from './utils';
|
3
|
+
import { getVariants } from './variants';
|
4
|
+
class UnistylesShadowRegistryBuilder {
|
5
|
+
// MOCKS
|
6
|
+
name = 'UnistylesShadowRegistry';
|
7
|
+
__type = 'web';
|
8
|
+
equals = () => true;
|
9
|
+
toString = () => 'UnistylesShadowRegistry';
|
10
|
+
dispose = () => { };
|
11
|
+
// END MOCKS
|
12
|
+
resultsMap = createDoubleMap();
|
13
|
+
classNamesMap = createDoubleMap();
|
14
|
+
add = (ref, _style, _variants, _args) => {
|
15
|
+
// Style is not provided
|
16
|
+
if (!_style) {
|
17
|
+
return;
|
18
|
+
}
|
19
|
+
// Array of styles
|
20
|
+
if (Array.isArray(_style)) {
|
21
|
+
_style.forEach(style => this.add(ref, style, _variants, _args));
|
22
|
+
return;
|
23
|
+
}
|
24
|
+
// Not a unistyle
|
25
|
+
if (!Object.keys(_style).some(key => key.startsWith('__uni__'))) {
|
26
|
+
return;
|
27
|
+
}
|
28
|
+
// Ref is unmounted, remove style tags from the document
|
29
|
+
if (ref === null) {
|
30
|
+
const secrets = extractSecrets(_style);
|
31
|
+
secrets.forEach(({ __uni__refs, __uni__key }) => {
|
32
|
+
__uni__refs.forEach(ref => {
|
33
|
+
if (isInDocument(ref)) {
|
34
|
+
return;
|
35
|
+
}
|
36
|
+
const oldResult = this.resultsMap.get(ref, __uni__key);
|
37
|
+
this.resultsMap.delete(ref, __uni__key);
|
38
|
+
this.classNamesMap.delete(ref, __uni__key);
|
39
|
+
if (oldResult) {
|
40
|
+
UnistylesRegistry.remove(oldResult);
|
41
|
+
}
|
42
|
+
});
|
43
|
+
});
|
44
|
+
return;
|
45
|
+
}
|
46
|
+
// Ref is not an HTMLElement
|
47
|
+
if (!(ref instanceof HTMLElement)) {
|
48
|
+
return;
|
49
|
+
}
|
50
|
+
extractSecrets(_style).forEach(secret => {
|
51
|
+
const { __uni__key, __uni__stylesheet, __uni__variants, __uni__args = [], __uni__refs } = secret;
|
52
|
+
const newComputedStylesheet = UnistylesRegistry.getComputedStylesheet(__uni__stylesheet);
|
53
|
+
const style = newComputedStylesheet[__uni__key];
|
54
|
+
const args = _args ?? __uni__args;
|
55
|
+
const variants = _variants && Object.keys(_variants).length > 0 ? _variants : __uni__variants;
|
56
|
+
const result = typeof style === 'function'
|
57
|
+
? style(...args)
|
58
|
+
: style;
|
59
|
+
const { variantsResult } = Object.fromEntries(getVariants({ variantsResult: result }, variants));
|
60
|
+
const resultWithVariants = {
|
61
|
+
...result,
|
62
|
+
...variantsResult
|
63
|
+
};
|
64
|
+
const oldResult = this.resultsMap.get(ref, __uni__key);
|
65
|
+
// If results are the same do nothing
|
66
|
+
if (equal(oldResult, resultWithVariants)) {
|
67
|
+
return;
|
68
|
+
}
|
69
|
+
const oldClassNames = this.classNamesMap.get(ref, __uni__key);
|
70
|
+
// Remove old styles
|
71
|
+
if (oldResult) {
|
72
|
+
UnistylesRegistry.remove(oldResult);
|
73
|
+
}
|
74
|
+
// Remove old classnames from the ref
|
75
|
+
oldClassNames?.forEach(className => ref.classList.remove(className));
|
76
|
+
this.resultsMap.set(ref, __uni__key, resultWithVariants);
|
77
|
+
const className = UnistylesRegistry.add({
|
78
|
+
key: __uni__key,
|
79
|
+
args,
|
80
|
+
stylesheet: __uni__stylesheet,
|
81
|
+
value: resultWithVariants,
|
82
|
+
variants
|
83
|
+
});
|
84
|
+
const injectedClassNames = resultWithVariants?._web?._classNames ?? [];
|
85
|
+
const newClassNames = injectedClassNames.concat(className);
|
86
|
+
const dependencies = extractUnistyleDependencies(resultWithVariants);
|
87
|
+
if (typeof __uni__stylesheet === 'function') {
|
88
|
+
// Add dependencies from dynamic styles to stylesheet
|
89
|
+
UnistylesRegistry.addDependenciesToStylesheet(__uni__stylesheet, dependencies);
|
90
|
+
}
|
91
|
+
__uni__refs.add(ref);
|
92
|
+
this.classNamesMap.set(ref, __uni__key, newClassNames);
|
93
|
+
// Add new classnames to the ref
|
94
|
+
ref.classList.add(...newClassNames);
|
95
|
+
});
|
96
|
+
};
|
97
|
+
remove = () => { };
|
98
|
+
}
|
99
|
+
export const UnistylesShadowRegistry = new UnistylesShadowRegistryBuilder();
|
package/src/web/state.ts
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
import type { UnistylesTheme } from '../types'
|
2
2
|
import type { UnistylesConfig } from '../specs/StyleSheet'
|
3
|
-
import type { AppBreakpoint, AppThemeName } from '../specs/types'
|
3
|
+
import type { AppBreakpoint, AppTheme, AppThemeName } from '../specs/types'
|
4
4
|
import type { UnistylesBreakpoints, UnistylesThemes } from '../global'
|
5
5
|
import { UnistylesRuntime } from './runtime'
|
6
6
|
import { isServer, schemeToTheme } from './utils'
|
@@ -39,7 +39,7 @@ class UnistylesStateBuilder {
|
|
39
39
|
|
40
40
|
private initThemes = (themes = {} as UnistylesThemes) => {
|
41
41
|
Object.entries(themes).forEach(([themeName, theme]) => {
|
42
|
-
this.themes.set(themeName, theme)
|
42
|
+
this.themes.set(themeName, theme as AppTheme)
|
43
43
|
})
|
44
44
|
}
|
45
45
|
|
@@ -61,7 +61,7 @@ class UnistylesStateBuilder {
|
|
61
61
|
|
62
62
|
// Adaptive themes
|
63
63
|
if (settings.adaptiveThemes) {
|
64
|
-
this.themeName = schemeToTheme(UnistylesRuntime.colorScheme)
|
64
|
+
this.themeName = schemeToTheme(UnistylesRuntime.colorScheme) as AppThemeName
|
65
65
|
|
66
66
|
return
|
67
67
|
}
|
@@ -0,0 +1,79 @@
|
|
1
|
+
export const reduceObject = (obj, reducer) => Object.fromEntries(Object.entries(obj).map(([key, value]) => [key, reducer(value, key)]));
|
2
|
+
export const keyInObject = (obj, key) => key in obj;
|
3
|
+
export const isServer = () => typeof window === 'undefined';
|
4
|
+
/**
|
5
|
+
* Deeply merges properties of passed object
|
6
|
+
*/
|
7
|
+
export const deepMergeObjects = (...sources) => {
|
8
|
+
const target = {};
|
9
|
+
sources.forEach(source => {
|
10
|
+
Object.keys(source).forEach(key => {
|
11
|
+
const sourceValue = source[key];
|
12
|
+
const targetValue = target[key];
|
13
|
+
if (Object(sourceValue) == sourceValue && Object(targetValue) === targetValue) {
|
14
|
+
// @ts-expect-error - can't assign to generic
|
15
|
+
target[key] = deepMergeObjects(targetValue, sourceValue);
|
16
|
+
return;
|
17
|
+
}
|
18
|
+
// @ts-expect-error - can't assign to generic
|
19
|
+
target[key] = sourceValue;
|
20
|
+
});
|
21
|
+
});
|
22
|
+
return target;
|
23
|
+
};
|
24
|
+
export const warn = (message) => console.warn(`🦄 [react-native-unistyles] ${message}`);
|
25
|
+
export const equal = (a, b) => {
|
26
|
+
if (Object.is(a, b)) {
|
27
|
+
return true;
|
28
|
+
}
|
29
|
+
if (typeof a !== 'object'
|
30
|
+
|| a === null
|
31
|
+
|| typeof b !== 'object'
|
32
|
+
|| b === null) {
|
33
|
+
return false;
|
34
|
+
}
|
35
|
+
const keysA = Object.keys(a);
|
36
|
+
if (keysA.length !== Object.keys(b).length) {
|
37
|
+
return false;
|
38
|
+
}
|
39
|
+
return keysA.every(key => Object.is(a[key], b[key]) && Object.prototype.hasOwnProperty.call(b, key));
|
40
|
+
};
|
41
|
+
export const generateHash = (value) => {
|
42
|
+
const str = JSON.stringify(value);
|
43
|
+
let hasher = 5381;
|
44
|
+
let length = str.length;
|
45
|
+
while (length--)
|
46
|
+
hasher = (hasher * 33) ^ str.charCodeAt(length);
|
47
|
+
return `unistyles-${(hasher >>> 0).toString(36)}`;
|
48
|
+
};
|
49
|
+
export const createDoubleMap = () => {
|
50
|
+
const map = new Map();
|
51
|
+
return {
|
52
|
+
get: (key, secondKey) => {
|
53
|
+
const mapForKey = map.get(key);
|
54
|
+
if (!mapForKey) {
|
55
|
+
return undefined;
|
56
|
+
}
|
57
|
+
return mapForKey.get(secondKey);
|
58
|
+
},
|
59
|
+
set: (key, secondKey, value) => {
|
60
|
+
const mapForKey = map.get(key) ?? new Map();
|
61
|
+
map.set(key, mapForKey);
|
62
|
+
mapForKey.set(secondKey, value);
|
63
|
+
},
|
64
|
+
delete: (key, secondKey) => {
|
65
|
+
const mapForKey = map.get(key);
|
66
|
+
if (!mapForKey) {
|
67
|
+
return;
|
68
|
+
}
|
69
|
+
mapForKey.delete(secondKey);
|
70
|
+
},
|
71
|
+
forEach: (callback) => {
|
72
|
+
map.forEach((mapForKey, key) => {
|
73
|
+
mapForKey.forEach((value, secondKey) => {
|
74
|
+
callback(key, secondKey, value);
|
75
|
+
});
|
76
|
+
});
|
77
|
+
}
|
78
|
+
};
|
79
|
+
};
|
@@ -0,0 +1,72 @@
|
|
1
|
+
import { ColorScheme, Orientation } from '../../specs/types';
|
2
|
+
import { isUnistylesMq, parseMq } from '../mq';
|
3
|
+
import { UnistylesState } from '../state';
|
4
|
+
import { keyInObject, reduceObject } from './common';
|
5
|
+
export const schemeToTheme = (scheme) => {
|
6
|
+
switch (scheme) {
|
7
|
+
case ColorScheme.Dark:
|
8
|
+
return 'dark';
|
9
|
+
case ColorScheme.Light:
|
10
|
+
default:
|
11
|
+
return 'light';
|
12
|
+
}
|
13
|
+
};
|
14
|
+
export const hexToRGBA = (hex, opacity) => hex
|
15
|
+
.replace('#', '')
|
16
|
+
.split(/(?=(?:..)*$)/)
|
17
|
+
.map(x => parseInt(x, 16))
|
18
|
+
.filter(num => !isNaN(num))
|
19
|
+
.reduce((acc, color) => `${acc}${color},`, 'rgba(')
|
20
|
+
.concat(`${opacity})`);
|
21
|
+
export const assignSecrets = (object, secrets) => {
|
22
|
+
// @ts-expect-error - assign secrets to object
|
23
|
+
object[`__uni__secrets__${Math.random().toString(16).slice(2)}`] = secrets;
|
24
|
+
return object;
|
25
|
+
};
|
26
|
+
export const extractSecrets = (object) => {
|
27
|
+
const secrets = Object.entries(object).reduce((acc, [key, value]) => {
|
28
|
+
if (key.startsWith('__uni__secrets__')) {
|
29
|
+
acc.push(value);
|
30
|
+
}
|
31
|
+
return acc;
|
32
|
+
}, []);
|
33
|
+
return secrets;
|
34
|
+
};
|
35
|
+
export const getStyles = (values) => {
|
36
|
+
const returnValue = {};
|
37
|
+
Object.defineProperties(returnValue, reduceObject(values, value => ({
|
38
|
+
value,
|
39
|
+
enumerable: false,
|
40
|
+
configurable: true
|
41
|
+
})));
|
42
|
+
return returnValue;
|
43
|
+
};
|
44
|
+
export const isInDocument = (element) => document.body.contains(element);
|
45
|
+
export const extractMediaQueryValue = (query) => {
|
46
|
+
const [_, px] = query.match(/(\d+)px/) ?? [];
|
47
|
+
if (!px) {
|
48
|
+
return undefined;
|
49
|
+
}
|
50
|
+
const value = Number(px);
|
51
|
+
return isNaN(value) ? undefined : value;
|
52
|
+
};
|
53
|
+
export const getMediaQuery = (query) => {
|
54
|
+
if (Object.values(Orientation).includes(query)) {
|
55
|
+
return `(orientation: ${query})`;
|
56
|
+
}
|
57
|
+
if (isUnistylesMq(query)) {
|
58
|
+
const { minWidth, maxWidth, minHeight, maxHeight } = parseMq(query);
|
59
|
+
return [
|
60
|
+
minWidth ? `(min-width: ${minWidth}px)` : undefined,
|
61
|
+
maxWidth ? `(max-width: ${maxWidth}px)` : undefined,
|
62
|
+
minHeight ? `(min-height: ${minHeight}px)` : undefined,
|
63
|
+
maxHeight ? `(max-height: ${maxHeight}px)` : undefined
|
64
|
+
].filter(Boolean).join(' and ');
|
65
|
+
}
|
66
|
+
const minWidth = UnistylesState.breakpoints && keyInObject(UnistylesState.breakpoints, query) ? UnistylesState.breakpoints[query] : undefined;
|
67
|
+
return `(min-width: ${minWidth ?? 0}px)`;
|
68
|
+
};
|
69
|
+
export const extractUnistyleDependencies = (value) => {
|
70
|
+
const dependencies = keyInObject(value, 'uni__dependencies') ? value.uni__dependencies : [];
|
71
|
+
return dependencies;
|
72
|
+
};
|
@@ -1,11 +1,11 @@
|
|
1
1
|
import type { UnistyleDependency } from '../../specs/NativePlatform'
|
2
|
-
import {
|
2
|
+
import { ColorScheme, Orientation } from '../../specs/types'
|
3
3
|
import type { StyleSheet, StyleSheetWithSuperPowers, UnistylesValues } from '../../types/stylesheet'
|
4
4
|
import { isUnistylesMq, parseMq } from '../mq'
|
5
5
|
import { UnistylesState } from '../state'
|
6
6
|
import { keyInObject, reduceObject } from './common'
|
7
7
|
|
8
|
-
export const schemeToTheme = (scheme: ColorScheme):
|
8
|
+
export const schemeToTheme = (scheme: ColorScheme): string => {
|
9
9
|
switch (scheme) {
|
10
10
|
case ColorScheme.Dark:
|
11
11
|
return 'dark'
|
@@ -0,0 +1,26 @@
|
|
1
|
+
import { deepMergeObjects } from '../utils';
|
2
|
+
const hasVariants = (value) => 'variants' in value[1];
|
3
|
+
export const getVariants = (styles, selectedVariants) => {
|
4
|
+
return Object.entries(styles)
|
5
|
+
.filter(hasVariants)
|
6
|
+
.filter(([_key, { variants }]) => Object.keys(variants).some(variant => variant in variants))
|
7
|
+
.map(([key, { variants, compoundVariants = [] }]) => {
|
8
|
+
const variantStyles = Object.entries(variants).flatMap(([variant, styles]) => {
|
9
|
+
const selectedVariant = selectedVariants[variant];
|
10
|
+
const selectedVariantStyles = styles[selectedVariant] ?? styles['default'];
|
11
|
+
if (!selectedVariantStyles) {
|
12
|
+
return [];
|
13
|
+
}
|
14
|
+
return selectedVariantStyles;
|
15
|
+
});
|
16
|
+
const compoundVariantStyles = compoundVariants.flatMap(compoundVariant => {
|
17
|
+
const { styles, ...conditions } = compoundVariant;
|
18
|
+
if (Object.entries(conditions).some(([variant, value]) => String(selectedVariants[variant]) !== String(value))) {
|
19
|
+
return [];
|
20
|
+
}
|
21
|
+
return styles;
|
22
|
+
});
|
23
|
+
const mergedVariantStyles = deepMergeObjects(...variantStyles, ...compoundVariantStyles);
|
24
|
+
return [key, mergedVariantStyles];
|
25
|
+
});
|
26
|
+
};
|
@@ -0,0 +1,50 @@
|
|
1
|
+
import { useMemo, useRef, useState } from 'react';
|
2
|
+
import { equal, keyInObject, reduceObject } from '../utils';
|
3
|
+
import { getVariants } from './getVariants';
|
4
|
+
export const createUseVariants = (styles, setSelectedVariants) => {
|
5
|
+
const useVariants = (selectedVariants) => {
|
6
|
+
const [selectedVariantStylesMap] = useState(() => new Map());
|
7
|
+
const lastSelectedVariantsRef = useRef();
|
8
|
+
// Variable that determines if variants have changed and we need to recalculate styles
|
9
|
+
const variantsChanged = !equal(lastSelectedVariantsRef.current, selectedVariants);
|
10
|
+
if (variantsChanged) {
|
11
|
+
lastSelectedVariantsRef.current = selectedVariants;
|
12
|
+
setSelectedVariants(selectedVariants);
|
13
|
+
}
|
14
|
+
const combinedVariantStyles = useMemo(() => {
|
15
|
+
const result = getVariants(styles, selectedVariants);
|
16
|
+
result.forEach(([key, value]) => {
|
17
|
+
selectedVariantStylesMap.set(key, value);
|
18
|
+
});
|
19
|
+
return result;
|
20
|
+
}, [lastSelectedVariantsRef.current]);
|
21
|
+
combinedVariantStyles.forEach(([key]) => {
|
22
|
+
const styleEntry = styles[key];
|
23
|
+
if (!styleEntry) {
|
24
|
+
return;
|
25
|
+
}
|
26
|
+
const selectedVariantStyles = selectedVariantStylesMap.get(key);
|
27
|
+
Object.defineProperties(styleEntry, reduceObject(selectedVariantStyles ?? {}, value => ({
|
28
|
+
value,
|
29
|
+
enumerable: false,
|
30
|
+
configurable: true
|
31
|
+
})));
|
32
|
+
// Add __uni__variants to static styles
|
33
|
+
Object.keys(styleEntry).forEach(key => {
|
34
|
+
if (!key.startsWith('__uni__secrets__')) {
|
35
|
+
return;
|
36
|
+
}
|
37
|
+
const secret = keyInObject(styleEntry, key) ? styleEntry[key] : undefined;
|
38
|
+
if (!secret) {
|
39
|
+
return;
|
40
|
+
}
|
41
|
+
Object.defineProperty(secret, '__uni__variants', {
|
42
|
+
value: selectedVariants
|
43
|
+
});
|
44
|
+
});
|
45
|
+
});
|
46
|
+
};
|
47
|
+
Object.defineProperty(styles, 'useVariants', {
|
48
|
+
value: useVariants
|
49
|
+
});
|
50
|
+
};
|