react-native-unistyles 2.0.0-beta.1 → 2.0.0-beta.3
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/UnistyleRegistry.js +6 -2
- package/lib/commonjs/core/UnistyleRegistry.js.map +1 -1
- package/lib/commonjs/core/Unistyles.js +1 -1
- package/lib/commonjs/core/Unistyles.js.map +1 -1
- package/lib/commonjs/core/UnistylesModule.web.js +191 -0
- package/lib/commonjs/core/UnistylesModule.web.js.map +1 -0
- package/lib/commonjs/core/UnistylesRuntime.js +3 -0
- package/lib/commonjs/core/UnistylesRuntime.js.map +1 -1
- package/lib/commonjs/hooks/index.js +7 -0
- package/lib/commonjs/hooks/index.js.map +1 -1
- package/lib/commonjs/hooks/useCSS.js +9 -0
- package/lib/commonjs/hooks/useCSS.js.map +1 -0
- package/lib/commonjs/hooks/useCSS.web.js +46 -0
- package/lib/commonjs/hooks/useCSS.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/normalizer/index.js.map +1 -0
- package/lib/commonjs/normalizer/module.d.js.map +1 -0
- package/lib/commonjs/{plugins/normalizer → normalizer}/normalizeStyle.js +1 -1
- package/lib/commonjs/normalizer/normalizeStyle.js.map +1 -0
- package/lib/commonjs/normalizer/normalizer.js.map +1 -0
- package/lib/commonjs/plugins/cssMediaQueriesPlugin.js +14 -0
- package/lib/commonjs/plugins/cssMediaQueriesPlugin.js.map +1 -0
- package/lib/commonjs/plugins/index.js +3 -21
- package/lib/commonjs/plugins/index.js.map +1 -1
- package/lib/commonjs/plugins/normalizeWebStylesPlugin.js +1 -1
- package/lib/commonjs/useStyles.js +4 -1
- package/lib/commonjs/useStyles.js.map +1 -1
- package/lib/commonjs/utils/cssMediaQuery.js +250 -0
- package/lib/commonjs/utils/cssMediaQuery.js.map +1 -0
- package/lib/commonjs/utils/generateId.js +15 -0
- package/lib/commonjs/utils/generateId.js.map +1 -0
- package/lib/commonjs/utils/hash32.js +43 -0
- package/lib/commonjs/utils/hash32.js.map +1 -0
- package/lib/commonjs/utils/index.js +13 -0
- package/lib/commonjs/utils/index.js.map +1 -1
- package/lib/commonjs/utils/mqParser.js +2 -1
- package/lib/commonjs/utils/mqParser.js.map +1 -1
- package/lib/commonjs/utils/styles.js +9 -0
- package/lib/commonjs/utils/styles.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/UnistyleRegistry.js +7 -3
- package/lib/module/core/UnistyleRegistry.js.map +1 -1
- package/lib/module/core/Unistyles.js +2 -2
- package/lib/module/core/Unistyles.js.map +1 -1
- package/lib/module/core/UnistylesModule.web.js +184 -0
- package/lib/module/core/UnistylesModule.web.js.map +1 -0
- package/lib/module/core/UnistylesRuntime.js +3 -0
- package/lib/module/core/UnistylesRuntime.js.map +1 -1
- package/lib/module/hooks/index.js +1 -0
- package/lib/module/hooks/index.js.map +1 -1
- package/lib/module/hooks/useCSS.js +2 -0
- package/lib/module/hooks/useCSS.js.map +1 -0
- package/lib/module/hooks/useCSS.web.js +39 -0
- package/lib/module/hooks/useCSS.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/normalizer/index.js.map +1 -0
- package/lib/module/normalizer/module.d.js.map +1 -0
- package/lib/module/{plugins/normalizer → normalizer}/normalizeStyle.js +1 -1
- package/lib/module/normalizer/normalizeStyle.js.map +1 -0
- package/lib/module/normalizer/normalizer.js.map +1 -0
- package/lib/module/plugins/cssMediaQueriesPlugin.js +7 -0
- package/lib/module/plugins/cssMediaQueriesPlugin.js.map +1 -0
- package/lib/module/plugins/index.js +1 -1
- package/lib/module/plugins/index.js.map +1 -1
- package/lib/module/plugins/normalizeWebStylesPlugin.js +1 -1
- package/lib/module/plugins/normalizeWebStylesPlugin.js.map +1 -1
- package/lib/module/useStyles.js +5 -2
- package/lib/module/useStyles.js.map +1 -1
- package/lib/module/utils/cssMediaQuery.js +243 -0
- package/lib/module/utils/cssMediaQuery.js.map +1 -0
- package/lib/module/utils/generateId.js +7 -0
- package/lib/module/utils/generateId.js.map +1 -0
- package/lib/module/utils/hash32.js +36 -0
- package/lib/module/utils/hash32.js.map +1 -0
- package/lib/module/utils/index.js +2 -1
- package/lib/module/utils/index.js.map +1 -1
- package/lib/module/utils/mqParser.js +1 -1
- package/lib/module/utils/mqParser.js.map +1 -1
- package/lib/module/utils/styles.js +9 -0
- package/lib/module/utils/styles.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/UnistyleRegistry.d.ts.map +1 -1
- package/lib/typescript/src/core/UnistylesModule.web.d.ts +19 -0
- package/lib/typescript/src/core/UnistylesModule.web.d.ts.map +1 -0
- package/lib/typescript/src/core/UnistylesRuntime.d.ts +1 -0
- package/lib/typescript/src/core/UnistylesRuntime.d.ts.map +1 -1
- package/lib/typescript/src/hooks/index.d.ts +1 -0
- package/lib/typescript/src/hooks/index.d.ts.map +1 -1
- package/lib/typescript/src/hooks/useCSS.d.ts +3 -0
- package/lib/typescript/src/hooks/useCSS.d.ts.map +1 -0
- package/lib/typescript/src/hooks/useCSS.web.d.ts +3 -0
- package/lib/typescript/src/hooks/useCSS.web.d.ts.map +1 -0
- package/lib/typescript/src/normalizer/index.d.ts.map +1 -0
- package/lib/typescript/src/normalizer/normalizeStyle.d.ts +3 -0
- package/lib/typescript/src/normalizer/normalizeStyle.d.ts.map +1 -0
- package/lib/typescript/src/{plugins/normalizer → normalizer}/normalizer.d.ts +1 -1
- package/lib/typescript/src/normalizer/normalizer.d.ts.map +1 -0
- package/lib/typescript/src/plugins/cssMediaQueriesPlugin.d.ts +3 -0
- package/lib/typescript/src/plugins/cssMediaQueriesPlugin.d.ts.map +1 -0
- package/lib/typescript/src/plugins/index.d.ts +1 -1
- package/lib/typescript/src/plugins/index.d.ts.map +1 -1
- package/lib/typescript/src/types/unistyles.d.ts +2 -1
- package/lib/typescript/src/types/unistyles.d.ts.map +1 -1
- package/lib/typescript/src/useStyles.d.ts.map +1 -1
- package/lib/typescript/src/utils/cssMediaQuery.d.ts +4 -0
- package/lib/typescript/src/utils/cssMediaQuery.d.ts.map +1 -0
- package/lib/typescript/src/utils/generateId.d.ts +2 -0
- package/lib/typescript/src/utils/generateId.d.ts.map +1 -0
- package/lib/typescript/src/utils/hash32.d.ts +2 -0
- package/lib/typescript/src/utils/hash32.d.ts.map +1 -0
- package/lib/typescript/src/utils/index.d.ts +3 -1
- package/lib/typescript/src/utils/index.d.ts.map +1 -1
- package/lib/typescript/src/utils/mqParser.d.ts +2 -1
- package/lib/typescript/src/utils/mqParser.d.ts.map +1 -1
- package/lib/typescript/src/utils/styles.d.ts +1 -1
- package/lib/typescript/src/utils/styles.d.ts.map +1 -1
- package/package.json +3 -2
- package/src/core/UnistyleRegistry.ts +8 -3
- package/src/core/Unistyles.ts +2 -2
- package/src/core/UnistylesModule.web.ts +218 -0
- package/src/core/UnistylesRuntime.ts +4 -0
- package/src/hooks/index.ts +1 -0
- package/src/hooks/useCSS.ts +3 -0
- package/src/hooks/useCSS.web.ts +51 -0
- package/src/hooks/useUnistyles.ts +1 -1
- package/src/{plugins/normalizer → normalizer}/normalizeStyle.ts +2 -2
- package/src/{plugins/normalizer → normalizer}/normalizer.ts +2 -1
- package/src/plugins/cssMediaQueriesPlugin.ts +8 -0
- package/src/plugins/index.ts +1 -1
- package/src/plugins/normalizeWebStylesPlugin.ts +1 -1
- package/src/types/unistyles.ts +3 -2
- package/src/useStyles.ts +10 -2
- package/src/utils/cssMediaQuery.ts +268 -0
- package/src/utils/generateId.ts +10 -0
- package/src/utils/hash32.ts +53 -0
- package/src/utils/index.ts +3 -1
- package/src/utils/mqParser.ts +2 -2
- package/src/utils/styles.ts +10 -1
- package/src/utils/withPlugins.ts +1 -1
- package/lib/commonjs/plugins/normalizer/index.js.map +0 -1
- package/lib/commonjs/plugins/normalizer/module.d.js.map +0 -1
- package/lib/commonjs/plugins/normalizer/normalizeStyle.js.map +0 -1
- package/lib/commonjs/plugins/normalizer/normalizer.js.map +0 -1
- package/lib/module/plugins/normalizer/index.js.map +0 -1
- package/lib/module/plugins/normalizer/module.d.js.map +0 -1
- package/lib/module/plugins/normalizer/normalizeStyle.js.map +0 -1
- package/lib/module/plugins/normalizer/normalizer.js.map +0 -1
- package/lib/typescript/src/plugins/normalizer/index.d.ts.map +0 -1
- package/lib/typescript/src/plugins/normalizer/normalizeStyle.d.ts +0 -3
- package/lib/typescript/src/plugins/normalizer/normalizeStyle.d.ts.map +0 -1
- package/lib/typescript/src/plugins/normalizer/normalizer.d.ts.map +0 -1
- /package/lib/commonjs/{plugins/normalizer → normalizer}/index.js +0 -0
- /package/lib/commonjs/{plugins/normalizer → normalizer}/module.d.js +0 -0
- /package/lib/commonjs/{plugins/normalizer → normalizer}/normalizer.js +0 -0
- /package/lib/module/{plugins/normalizer → normalizer}/index.js +0 -0
- /package/lib/module/{plugins/normalizer → normalizer}/module.d.js +0 -0
- /package/lib/module/{plugins/normalizer → normalizer}/normalizer.js +0 -0
- /package/lib/typescript/src/{plugins/normalizer → normalizer}/index.d.ts +0 -0
- /package/src/{plugins/normalizer → normalizer}/index.ts +0 -0
- /package/src/{plugins/normalizer → normalizer}/module.d.ts +0 -0
@@ -3,11 +3,12 @@ type ParsedMqDimension = {
|
|
3
3
|
from: number;
|
4
4
|
to: number;
|
5
5
|
};
|
6
|
-
type UnistylesParsedMq = {
|
6
|
+
export type UnistylesParsedMq = {
|
7
7
|
width?: ParsedMqDimension;
|
8
8
|
height?: ParsedMqDimension;
|
9
9
|
};
|
10
10
|
export declare const parseMq: (mq: string) => UnistylesParsedMq;
|
11
|
+
export declare const isUnistylesMq: (mq: string) => boolean;
|
11
12
|
export declare const isValidMq: (parsedMq: UnistylesParsedMq) => boolean;
|
12
13
|
export declare const isWithinTheWidthAndHeight: (parsedMq: UnistylesParsedMq, screenSize: ScreenSize) => boolean;
|
13
14
|
export declare const getKeyForUnistylesMediaQuery: (mediaQueries: Array<[string, RNValue]>, screenSize: ScreenSize) => Optional<string>;
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"mqParser.d.ts","sourceRoot":"","sources":["../../../../src/utils/mqParser.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,UAAU,CAAA;AAM7D,KAAK,iBAAiB,GAAG;IACrB,IAAI,EAAE,MAAM,CAAC;IACb,EAAE,EAAE,MAAM,CAAA;CACb,CAAA;AAED,
|
1
|
+
{"version":3,"file":"mqParser.d.ts","sourceRoot":"","sources":["../../../../src/utils/mqParser.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,UAAU,CAAA;AAM7D,KAAK,iBAAiB,GAAG;IACrB,IAAI,EAAE,MAAM,CAAC;IACb,EAAE,EAAE,MAAM,CAAA;CACb,CAAA;AAED,MAAM,MAAM,iBAAiB,GAAG;IAC5B,KAAK,CAAC,EAAE,iBAAiB,CAAC;IAC1B,MAAM,CAAC,EAAE,iBAAiB,CAAA;CAC7B,CAAA;AAED,eAAO,MAAM,OAAO,OAAQ,MAAM,KAAG,iBAcpC,CAAA;AAED,eAAO,MAAM,aAAa,OAAQ,MAAM,YAAgC,CAAA;AAExE,eAAO,MAAM,SAAS,aAAc,iBAAiB,YAgBpD,CAAA;AAED,eAAO,MAAM,yBAAyB,aAAc,iBAAiB,cAAc,UAAU,KAAG,OAgB/F,CAAA;AAcD,eAAO,MAAM,4BAA4B,iBAAkB,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,cAAc,UAAU,KAAG,SAAS,MAAM,CAgB5H,CAAA"}
|
@@ -1,5 +1,5 @@
|
|
1
1
|
import type { Optional, RNStyle } from '../types';
|
2
2
|
export declare const proxifyFunction: (key: string, fn: Function, variant?: Record<string, Optional<string>>) => Function;
|
3
3
|
export declare const isPlatformColor: <T extends {}>(value: T) => boolean;
|
4
|
-
export declare const parseStyle: <T extends RNStyle>(style: T, variant?: Record<string, Optional<string
|
4
|
+
export declare const parseStyle: <T extends RNStyle>(style: T, variant?: Record<string, Optional<string>>, parseMediaQueries?: boolean) => T;
|
5
5
|
//# sourceMappingURL=styles.d.ts.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"styles.d.ts","sourceRoot":"","sources":["../../../../src/utils/styles.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,OAAO,EAAW,MAAM,UAAU,CAAA;AAK1D,eAAO,MAAM,eAAe,QACnB,MAAM,MACP,QAAQ,YACF,OAAO,MAAM,EAAE,SAAS,MAAM,CAAC,CAAC,KAC3C,QAED,CAAA;AAEF,eAAO,MAAM,eAAe,8BAA6B,OAMxD,CAAA;AAED,eAAO,MAAM,UAAU,0CAEV,OAAO,MAAM,EAAE,SAAS,MAAM,CAAC,CAAC,
|
1
|
+
{"version":3,"file":"styles.d.ts","sourceRoot":"","sources":["../../../../src/utils/styles.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,OAAO,EAAW,MAAM,UAAU,CAAA;AAK1D,eAAO,MAAM,eAAe,QACnB,MAAM,MACP,QAAQ,YACF,OAAO,MAAM,EAAE,SAAS,MAAM,CAAC,CAAC,KAC3C,QAED,CAAA;AAEF,eAAO,MAAM,eAAe,8BAA6B,OAMxD,CAAA;AAED,eAAO,MAAM,UAAU,0CAEV,OAAO,MAAM,EAAE,SAAS,MAAM,CAAC,CAAC,mCAkD9B,CAAA"}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "react-native-unistyles",
|
3
|
-
"version": "2.0.0-beta.
|
3
|
+
"version": "2.0.0-beta.3",
|
4
4
|
"description": "Level up your React Native StyleSheet",
|
5
5
|
"scripts": {
|
6
6
|
"test": "jest",
|
@@ -144,7 +144,8 @@
|
|
144
144
|
},
|
145
145
|
"eslintIgnore": [
|
146
146
|
"node_modules/",
|
147
|
-
"lib/"
|
147
|
+
"lib/",
|
148
|
+
"src/utils/hash32.ts"
|
148
149
|
],
|
149
150
|
"react-native-builder-bob": {
|
150
151
|
"source": "src",
|
@@ -1,7 +1,7 @@
|
|
1
1
|
import type { UnistylesBridge, UnistylesConfig, UnistylesPlugin } from '../types'
|
2
2
|
import type { UnistylesBreakpoints, UnistylesThemes } from '../global'
|
3
3
|
import { isWeb, UnistylesError } from '../common'
|
4
|
-
import { normalizeWebStylesPlugin } from '../plugins'
|
4
|
+
import { cssMediaQueriesPlugin, normalizeWebStylesPlugin } from '../plugins'
|
5
5
|
|
6
6
|
export class UnistyleRegistry {
|
7
7
|
public config: UnistylesConfig = {}
|
@@ -47,14 +47,19 @@ export class UnistyleRegistry {
|
|
47
47
|
this.unistylesBridge.useAdaptiveThemes(config.adaptiveThemes)
|
48
48
|
}
|
49
49
|
|
50
|
-
if (config.
|
51
|
-
config.
|
50
|
+
if (config.plugins) {
|
51
|
+
config.plugins.forEach(plugin => this.addPlugin(plugin, false))
|
52
52
|
}
|
53
53
|
|
54
54
|
if (config.initialTheme) {
|
55
55
|
this.unistylesBridge.useTheme(config.initialTheme)
|
56
56
|
}
|
57
57
|
|
58
|
+
if (config.experimentalCSSMediaQueries) {
|
59
|
+
this.plugins = [cssMediaQueriesPlugin].concat(this.plugins)
|
60
|
+
this.unistylesBridge.addPlugin(cssMediaQueriesPlugin.name, false)
|
61
|
+
}
|
62
|
+
|
58
63
|
return {
|
59
64
|
addBreakpoints: this.addBreakpoints,
|
60
65
|
addThemes: this.addThemes
|
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,218 @@
|
|
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
|
+
#breakpoints: UnistylesBreakpoints = {} as UnistylesBreakpoints
|
14
|
+
#colorScheme: ColorSchemeName = this.getPreferredColorScheme()
|
15
|
+
#themeName: keyof UnistylesThemes = '' as keyof UnistylesThemes
|
16
|
+
#enabledPlugins: Array<string> = [normalizeWebStylesPlugin.name]
|
17
|
+
#unistylesEvents = new NativeEventEmitter(NativeModules.Unistyles)
|
18
|
+
#sortedBreakpointPairs: Array<[keyof UnistylesBreakpoints, number]> = []
|
19
|
+
#breakpoint: keyof UnistylesBreakpoints = '' as keyof UnistylesBreakpoints
|
20
|
+
|
21
|
+
constructor() {
|
22
|
+
this.setupListeners()
|
23
|
+
}
|
24
|
+
|
25
|
+
public 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 'breakpoints':
|
39
|
+
return this.#breakpoints
|
40
|
+
case 'hasAdaptiveThemes':
|
41
|
+
return this.#hasAdaptiveThemes
|
42
|
+
case 'sortedBreakpointPairs':
|
43
|
+
return this.#sortedBreakpointPairs
|
44
|
+
case 'enabledPlugins':
|
45
|
+
return this.#enabledPlugins
|
46
|
+
case 'colorScheme':
|
47
|
+
return this.#colorScheme
|
48
|
+
case 'useTheme':
|
49
|
+
return (themeName: keyof UnistylesThemes) => this.useTheme(themeName)
|
50
|
+
case 'useBreakpoints':
|
51
|
+
return (breakpoints: UnistylesBreakpoints) => this.useBreakpoints(breakpoints)
|
52
|
+
case 'useAdaptiveThemes':
|
53
|
+
return (enable: boolean) => this.useAdaptiveThemes(enable)
|
54
|
+
case 'addPlugin':
|
55
|
+
return (pluginName: string, notify: boolean) => this.addPlugin(pluginName, notify)
|
56
|
+
case 'removePlugin':
|
57
|
+
return (pluginName: string) => this.removePlugin(pluginName)
|
58
|
+
default:
|
59
|
+
return Reflect.get(this, prop)
|
60
|
+
}
|
61
|
+
},
|
62
|
+
set: (target, prop, newValue, receiver) => {
|
63
|
+
switch (prop) {
|
64
|
+
case 'themes': {
|
65
|
+
this.#themes = newValue
|
66
|
+
this.#supportsAutomaticColorScheme = newValue.includes('light') && newValue.includes('dark')
|
67
|
+
|
68
|
+
return true
|
69
|
+
}
|
70
|
+
case 'themeName': {
|
71
|
+
this.#themeName = newValue as keyof UnistylesThemes
|
72
|
+
this.emitThemeChange()
|
73
|
+
|
74
|
+
return true
|
75
|
+
}
|
76
|
+
default:
|
77
|
+
return Reflect.set(target, prop, newValue, receiver)
|
78
|
+
}
|
79
|
+
}
|
80
|
+
})
|
81
|
+
|
82
|
+
return true
|
83
|
+
}
|
84
|
+
|
85
|
+
private useTheme(themeName: keyof UnistylesThemes) {
|
86
|
+
this.#themeName = themeName
|
87
|
+
this.emitThemeChange()
|
88
|
+
}
|
89
|
+
|
90
|
+
private useBreakpoints(breakpoints: UnistylesBreakpoints) {
|
91
|
+
this.#breakpoints = breakpoints
|
92
|
+
this.#sortedBreakpointPairs = Object
|
93
|
+
.entries(breakpoints)
|
94
|
+
.sort(([, a], [, b]) => (a ?? 0) - (b ?? 0)) as Array<[keyof UnistylesBreakpoints, number]>
|
95
|
+
this.#breakpoint = this.getBreakpointFromScreenWidth(this.#screenWidth)
|
96
|
+
}
|
97
|
+
|
98
|
+
private useAdaptiveThemes(enable: boolean) {
|
99
|
+
this.#hasAdaptiveThemes = enable
|
100
|
+
|
101
|
+
if (!this.#hasAdaptiveThemes || !this.#supportsAutomaticColorScheme) {
|
102
|
+
return
|
103
|
+
}
|
104
|
+
|
105
|
+
if (this.#themeName !== this.#colorScheme) {
|
106
|
+
this.#themeName = this.#colorScheme as keyof UnistylesThemes
|
107
|
+
this.emitThemeChange()
|
108
|
+
}
|
109
|
+
}
|
110
|
+
|
111
|
+
private addPlugin(pluginName: string, notify: boolean) {
|
112
|
+
this.#enabledPlugins = [pluginName].concat(this.#enabledPlugins)
|
113
|
+
|
114
|
+
if (notify) {
|
115
|
+
this.emitPluginChange()
|
116
|
+
}
|
117
|
+
}
|
118
|
+
|
119
|
+
private removePlugin(pluginName: string) {
|
120
|
+
this.#enabledPlugins = this.#enabledPlugins.filter(name => name !== pluginName)
|
121
|
+
this.emitPluginChange()
|
122
|
+
}
|
123
|
+
|
124
|
+
private getTheme(): keyof UnistylesThemes {
|
125
|
+
|
126
|
+
if (this.#themes.length === 1) {
|
127
|
+
return this.#themes.at(0) as keyof UnistylesThemes
|
128
|
+
}
|
129
|
+
|
130
|
+
return this.#themeName
|
131
|
+
}
|
132
|
+
|
133
|
+
private setupListeners() {
|
134
|
+
window.addEventListener('resize', () => {
|
135
|
+
clearTimeout(this.#timerRef)
|
136
|
+
|
137
|
+
this.#timerRef = setTimeout(() => {
|
138
|
+
this.#screenWidth = window.innerWidth
|
139
|
+
this.#screenHeight = window.innerHeight
|
140
|
+
this.#breakpoint = this.getBreakpointFromScreenWidth(this.#screenWidth)
|
141
|
+
|
142
|
+
this.emitLayoutChange()
|
143
|
+
}, 100)
|
144
|
+
})
|
145
|
+
|
146
|
+
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', event => {
|
147
|
+
this.#colorScheme = event.matches
|
148
|
+
? 'dark'
|
149
|
+
: 'light'
|
150
|
+
|
151
|
+
if (!this.#supportsAutomaticColorScheme || !this.#hasAdaptiveThemes) {
|
152
|
+
return
|
153
|
+
}
|
154
|
+
|
155
|
+
if (this.#colorScheme !== this.#themeName) {
|
156
|
+
this.#themeName = this.#colorScheme as keyof UnistylesThemes
|
157
|
+
this.emitThemeChange()
|
158
|
+
}
|
159
|
+
})
|
160
|
+
}
|
161
|
+
|
162
|
+
private getBreakpointFromScreenWidth(width: number): keyof UnistylesBreakpoints {
|
163
|
+
const breakpoint = this.#sortedBreakpointPairs
|
164
|
+
.find(([, value], index, otherBreakpoints) => {
|
165
|
+
const minVal = value
|
166
|
+
const maxVal = otherBreakpoints[index + 1]?.[1]
|
167
|
+
|
168
|
+
if (!maxVal) {
|
169
|
+
return true
|
170
|
+
}
|
171
|
+
|
172
|
+
return width >= minVal && width < maxVal
|
173
|
+
})
|
174
|
+
|
175
|
+
return breakpoint?.at(0) as keyof UnistylesBreakpoints
|
176
|
+
}
|
177
|
+
|
178
|
+
private getPreferredColorScheme() {
|
179
|
+
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
|
180
|
+
return 'dark'
|
181
|
+
}
|
182
|
+
|
183
|
+
return 'light'
|
184
|
+
}
|
185
|
+
|
186
|
+
private emitPluginChange() {
|
187
|
+
this.#unistylesEvents.emit('__unistylesOnChange', {
|
188
|
+
type: 'plugin'
|
189
|
+
})
|
190
|
+
}
|
191
|
+
|
192
|
+
private emitThemeChange() {
|
193
|
+
this.#unistylesEvents.emit('__unistylesOnChange', {
|
194
|
+
type: 'theme',
|
195
|
+
payload: {
|
196
|
+
themeName: this.#themeName
|
197
|
+
}
|
198
|
+
})
|
199
|
+
}
|
200
|
+
|
201
|
+
private emitLayoutChange() {
|
202
|
+
this.#unistylesEvents.emit('__unistylesOnChange', {
|
203
|
+
type: 'layout',
|
204
|
+
payload: {
|
205
|
+
breakpoint: this.#breakpoint,
|
206
|
+
orientation: this.#screenWidth > this.#screenHeight
|
207
|
+
? 'landscape'
|
208
|
+
: 'portrait',
|
209
|
+
screen: {
|
210
|
+
width: this.#screenWidth,
|
211
|
+
height: this.#screenHeight
|
212
|
+
}
|
213
|
+
}
|
214
|
+
})
|
215
|
+
}
|
216
|
+
}
|
217
|
+
|
218
|
+
export const UnistylesModule = new UnistylesBridgeWeb()
|
package/src/hooks/index.ts
CHANGED
@@ -0,0 +1,51 @@
|
|
1
|
+
import { useInsertionEffect, useRef } from 'react'
|
2
|
+
import { unistyles } from '../core'
|
3
|
+
import type { ReactNativeStyleSheet } from '../types'
|
4
|
+
import { generateReactNativeWebId } from '../utils'
|
5
|
+
|
6
|
+
export const useCSS = <T>(stylesheet: ReactNativeStyleSheet<T>) => {
|
7
|
+
const insertedIds = useRef<Array<string>>([])
|
8
|
+
|
9
|
+
useInsertionEffect(() => {
|
10
|
+
if (!unistyles.registry.config.experimentalCSSMediaQueries) {
|
11
|
+
return
|
12
|
+
}
|
13
|
+
|
14
|
+
Object
|
15
|
+
.entries(stylesheet)
|
16
|
+
.forEach(([_key, value]) => {
|
17
|
+
Object.entries(value!)
|
18
|
+
.forEach(([prop, val]) => {
|
19
|
+
if (!val.toString().includes('@media')) {
|
20
|
+
return
|
21
|
+
}
|
22
|
+
|
23
|
+
const id = generateReactNativeWebId(prop, '""')
|
24
|
+
|
25
|
+
if (insertedIds.current.includes(id)) {
|
26
|
+
return
|
27
|
+
}
|
28
|
+
|
29
|
+
const style = document.createElement('style')
|
30
|
+
|
31
|
+
style.id = id
|
32
|
+
style.innerHTML = val
|
33
|
+
|
34
|
+
document.head.appendChild(style)
|
35
|
+
insertedIds.current = [...insertedIds.current, id]
|
36
|
+
})
|
37
|
+
})
|
38
|
+
|
39
|
+
return () => {
|
40
|
+
insertedIds.current.forEach(id => {
|
41
|
+
const style = document.getElementById(id)
|
42
|
+
|
43
|
+
if (style) {
|
44
|
+
style.remove()
|
45
|
+
}
|
46
|
+
})
|
47
|
+
|
48
|
+
insertedIds.current = []
|
49
|
+
}
|
50
|
+
}, [stylesheet])
|
51
|
+
}
|
@@ -1,6 +1,6 @@
|
|
1
1
|
import { preprocessor } from './normalizer'
|
2
|
-
import { warn } from '
|
3
|
-
import type { NormalizedBoxShadow, NormalizedTextShadow, BoxShadow, TextShadow, RNStyle } from '
|
2
|
+
import { warn } from '../common'
|
3
|
+
import type { NormalizedBoxShadow, NormalizedTextShadow, BoxShadow, TextShadow, RNStyle } from '../types'
|
4
4
|
|
5
5
|
const normalizeBoxShadow = <T extends BoxShadow>(style: T): NormalizedBoxShadow => {
|
6
6
|
const requiredBoxShadowProperties = [
|
@@ -1,7 +1,7 @@
|
|
1
1
|
// based on react-native-web normalizer
|
2
2
|
// https://github.com/necolas/react-native-web
|
3
3
|
import normalizeColors from '@react-native/normalize-colors'
|
4
|
-
import type { TextShadow, Transforms, BoxShadow, Nullable } from '
|
4
|
+
import type { TextShadow, Transforms, BoxShadow, Nullable } from '../types'
|
5
5
|
|
6
6
|
type Preprocessor = {
|
7
7
|
createTextShadowValue(style: TextShadow): string,
|
@@ -37,6 +37,7 @@ export const normalizeColor = (color: string, opacity: number = 1) => {
|
|
37
37
|
}
|
38
38
|
|
39
39
|
export const normalizeNumericValue = (value: number) => value ? `${value}px` : value
|
40
|
+
|
40
41
|
const normalizeTransform = (key: string, value: number | string) => {
|
41
42
|
if (key.includes('scale')) {
|
42
43
|
return value
|
@@ -0,0 +1,8 @@
|
|
1
|
+
// prevent recursive import
|
2
|
+
import { createMediaQueryForStyles } from '../utils/cssMediaQuery'
|
3
|
+
import type { UnistylesPlugin } from '../types'
|
4
|
+
|
5
|
+
export const cssMediaQueriesPlugin: UnistylesPlugin = {
|
6
|
+
name: '__unistylesCSSMediaQueries',
|
7
|
+
onParsedStyle: (_key, styles, runtime) => createMediaQueryForStyles(styles, runtime)
|
8
|
+
}
|
package/src/plugins/index.ts
CHANGED
@@ -1,2 +1,2 @@
|
|
1
1
|
export { normalizeWebStylesPlugin } from './normalizeWebStylesPlugin'
|
2
|
-
export {
|
2
|
+
export { cssMediaQueriesPlugin } from './cssMediaQueriesPlugin'
|
package/src/types/unistyles.ts
CHANGED
@@ -7,8 +7,9 @@ export type ColorSchemeName = 'light' | 'dark' | 'unspecified'
|
|
7
7
|
|
8
8
|
export type UnistylesConfig = {
|
9
9
|
adaptiveThemes?: boolean,
|
10
|
-
|
11
|
-
|
10
|
+
initialTheme?: keyof UnistylesThemes,
|
11
|
+
plugins?: Array<UnistylesPlugin>,
|
12
|
+
experimentalCSSMediaQueries?: boolean
|
12
13
|
}
|
13
14
|
|
14
15
|
export type UnistylesBridge = {
|
package/src/useStyles.ts
CHANGED
@@ -1,9 +1,11 @@
|
|
1
1
|
import { useMemo } from 'react'
|
2
2
|
import { StyleSheet } from 'react-native'
|
3
3
|
import { parseStyle, proxifyFunction, withPlugins } from './utils'
|
4
|
-
import { useUnistyles, useVariants } from './hooks'
|
4
|
+
import { useCSS, useUnistyles, useVariants } from './hooks'
|
5
5
|
import type { UnistylesBreakpoints } from './global'
|
6
6
|
import type { ExtractVariantNames, ReactNativeStyleSheet, StyleSheetWithSuperPowers, UnistylesTheme } from './types'
|
7
|
+
import { unistyles } from './core'
|
8
|
+
import { isWeb } from './common'
|
7
9
|
|
8
10
|
type ParsedStylesheet<ST extends StyleSheetWithSuperPowers> = {
|
9
11
|
theme: UnistylesTheme,
|
@@ -33,11 +35,17 @@ export const useStyles = <ST extends StyleSheetWithSuperPowers>(
|
|
33
35
|
|
34
36
|
return StyleSheet.create({
|
35
37
|
...acc,
|
36
|
-
[key]: withPlugins(key, parseStyle(
|
38
|
+
[key]: withPlugins(key, parseStyle(
|
39
|
+
value,
|
40
|
+
variants,
|
41
|
+
!isWeb || !unistyles.registry.config.experimentalCSSMediaQueries)
|
42
|
+
)
|
37
43
|
})
|
38
44
|
}, {}), [parsedStyles, variants, plugins]
|
39
45
|
)
|
40
46
|
|
47
|
+
useCSS(dynamicStyleSheet as ReactNativeStyleSheet<ST>)
|
48
|
+
|
41
49
|
return {
|
42
50
|
theme,
|
43
51
|
breakpoint: layout.breakpoint,
|