react-native-unistyles 2.0.0-alpha.9 → 2.0.0-beta.1
Sign up to get free protection for your applications and to get access to all the features.
- package/android/CMakeLists.txt +28 -0
- package/android/build.gradle +40 -0
- package/android/src/main/cxx/cpp-adapter.cpp +108 -0
- package/android/src/main/java/com/unistyles/UnistylesModule.kt +159 -0
- package/android/src/main/java/com/unistyles/UnistylesPackage.kt +18 -0
- package/cxx/UnistylesRuntime.cpp +55 -2
- package/cxx/UnistylesRuntime.h +15 -9
- package/ios/UnistylesModule.mm +12 -2
- package/lib/commonjs/common.js +30 -16
- package/lib/commonjs/common.js.map +1 -1
- package/lib/commonjs/core/UnistyleRegistry.js +65 -3
- package/lib/commonjs/core/UnistyleRegistry.js.map +1 -1
- package/lib/commonjs/core/UnistylesRuntime.js +15 -16
- package/lib/commonjs/core/UnistylesRuntime.js.map +1 -1
- package/lib/commonjs/createStyleSheet.js +1 -6
- package/lib/commonjs/createStyleSheet.js.map +1 -1
- package/lib/commonjs/hooks/index.js +7 -0
- package/lib/commonjs/hooks/index.js.map +1 -1
- package/lib/commonjs/hooks/useUnistyles.js +11 -8
- package/lib/commonjs/hooks/useUnistyles.js.map +1 -1
- package/lib/commonjs/hooks/useVariants.js +14 -0
- package/lib/commonjs/hooks/useVariants.js.map +1 -0
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/plugins/index.js +38 -0
- package/lib/commonjs/plugins/index.js.map +1 -0
- package/lib/commonjs/plugins/normalizeWebStylesPlugin.js +12 -0
- package/lib/commonjs/plugins/normalizeWebStylesPlugin.js.map +1 -0
- package/lib/commonjs/plugins/normalizer/index.js +32 -0
- package/lib/commonjs/plugins/normalizer/index.js.map +1 -0
- package/lib/commonjs/plugins/normalizer/module.d.js.map +1 -0
- package/lib/commonjs/{utils/normalizeStyles.web.js → plugins/normalizer/normalizeStyle.js} +5 -5
- package/lib/commonjs/plugins/normalizer/normalizeStyle.js.map +1 -0
- package/lib/commonjs/{utils → plugins/normalizer}/normalizer.js +1 -1
- package/lib/commonjs/plugins/normalizer/normalizer.js.map +1 -0
- package/lib/commonjs/types/{mq.js → plugin.js} +1 -1
- package/lib/commonjs/types/{mq.js.map → plugin.js.map} +1 -1
- package/lib/commonjs/types/stylesheet.js +6 -0
- package/lib/commonjs/types/stylesheet.js.map +1 -0
- package/lib/commonjs/types/variants.js +2 -0
- package/lib/commonjs/{utils/module.d.js.map → types/variants.js.map} +1 -1
- package/lib/commonjs/useStyles.js +10 -20
- package/lib/commonjs/useStyles.js.map +1 -1
- package/lib/commonjs/utils/breakpoints.js +13 -59
- package/lib/commonjs/utils/breakpoints.js.map +1 -1
- package/lib/commonjs/utils/index.js +13 -51
- package/lib/commonjs/utils/index.js.map +1 -1
- package/lib/commonjs/utils/mq.js +36 -68
- package/lib/commonjs/utils/mq.js.map +1 -1
- package/lib/commonjs/utils/mqParser.js +4 -1
- package/lib/commonjs/utils/mqParser.js.map +1 -1
- package/lib/commonjs/utils/styles.js +35 -20
- package/lib/commonjs/utils/styles.js.map +1 -1
- package/lib/commonjs/utils/withPlugins.js +15 -0
- package/lib/commonjs/utils/withPlugins.js.map +1 -0
- package/lib/module/common.js +28 -17
- package/lib/module/common.js.map +1 -1
- package/lib/module/core/UnistyleRegistry.js +65 -3
- package/lib/module/core/UnistyleRegistry.js.map +1 -1
- package/lib/module/core/UnistylesRuntime.js +15 -16
- package/lib/module/core/UnistylesRuntime.js.map +1 -1
- package/lib/module/createStyleSheet.js +1 -6
- package/lib/module/createStyleSheet.js.map +1 -1
- package/lib/module/hooks/index.js +1 -0
- package/lib/module/hooks/index.js.map +1 -1
- package/lib/module/hooks/useUnistyles.js +12 -9
- package/lib/module/hooks/useUnistyles.js.map +1 -1
- package/lib/module/hooks/useVariants.js +7 -0
- package/lib/module/hooks/useVariants.js.map +1 -0
- package/lib/module/index.js.map +1 -1
- package/lib/module/plugins/index.js +3 -0
- package/lib/module/plugins/index.js.map +1 -0
- package/lib/module/plugins/normalizeWebStylesPlugin.js +6 -0
- package/lib/module/plugins/normalizeWebStylesPlugin.js.map +1 -0
- package/lib/module/plugins/normalizer/index.js +3 -0
- package/lib/module/plugins/normalizer/index.js.map +1 -0
- package/lib/module/plugins/normalizer/module.d.js.map +1 -0
- package/lib/module/{utils/normalizeStyles.web.js → plugins/normalizer/normalizeStyle.js} +3 -3
- package/lib/module/plugins/normalizer/normalizeStyle.js.map +1 -0
- package/lib/module/{utils → plugins/normalizer}/normalizer.js +1 -1
- package/lib/module/plugins/normalizer/normalizer.js.map +1 -0
- package/lib/module/types/plugin.js +2 -0
- package/lib/module/types/{mq.js.map → plugin.js.map} +1 -1
- package/lib/module/types/stylesheet.js +2 -0
- package/lib/module/types/stylesheet.js.map +1 -0
- package/lib/module/types/variants.js +2 -0
- package/lib/module/{utils/module.d.js.map → types/variants.js.map} +1 -1
- package/lib/module/useStyles.js +12 -22
- package/lib/module/useStyles.js.map +1 -1
- package/lib/module/utils/breakpoints.js +12 -56
- package/lib/module/utils/breakpoints.js.map +1 -1
- package/lib/module/utils/index.js +4 -6
- package/lib/module/utils/index.js.map +1 -1
- package/lib/module/utils/mq.js +35 -67
- package/lib/module/utils/mq.js.map +1 -1
- package/lib/module/utils/mqParser.js +3 -3
- package/lib/module/utils/mqParser.js.map +1 -1
- package/lib/module/utils/styles.js +35 -20
- package/lib/module/utils/styles.js.map +1 -1
- package/lib/module/utils/withPlugins.js +8 -0
- package/lib/module/utils/withPlugins.js.map +1 -0
- package/lib/typescript/src/common.d.ts +23 -12
- package/lib/typescript/src/common.d.ts.map +1 -1
- package/lib/typescript/src/core/UnistyleRegistry.d.ts +45 -4
- package/lib/typescript/src/core/UnistyleRegistry.d.ts.map +1 -1
- package/lib/typescript/src/core/UnistylesRuntime.d.ts +8 -9
- package/lib/typescript/src/core/UnistylesRuntime.d.ts.map +1 -1
- package/lib/typescript/src/core/index.d.ts +1 -0
- package/lib/typescript/src/core/index.d.ts.map +1 -1
- package/lib/typescript/src/createStyleSheet.d.ts +2 -2
- package/lib/typescript/src/createStyleSheet.d.ts.map +1 -1
- package/lib/typescript/src/global.d.ts.map +1 -1
- package/lib/typescript/src/hooks/index.d.ts +1 -0
- package/lib/typescript/src/hooks/index.d.ts.map +1 -1
- package/lib/typescript/src/hooks/useUnistyles.d.ts +2 -1
- package/lib/typescript/src/hooks/useUnistyles.d.ts.map +1 -1
- package/lib/typescript/src/hooks/useVariants.d.ts +3 -0
- package/lib/typescript/src/hooks/useVariants.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +51 -5
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/lib/typescript/src/plugins/index.d.ts +3 -0
- package/lib/typescript/src/plugins/index.d.ts.map +1 -0
- package/lib/typescript/src/plugins/normalizeWebStylesPlugin.d.ts +3 -0
- package/lib/typescript/src/plugins/normalizeWebStylesPlugin.d.ts.map +1 -0
- package/lib/typescript/src/plugins/normalizer/index.d.ts +3 -0
- package/lib/typescript/src/plugins/normalizer/index.d.ts.map +1 -0
- package/lib/typescript/src/plugins/normalizer/normalizeStyle.d.ts +3 -0
- package/lib/typescript/src/plugins/normalizer/normalizeStyle.d.ts.map +1 -0
- package/lib/typescript/src/{utils → plugins/normalizer}/normalizer.d.ts +1 -1
- package/lib/typescript/src/plugins/normalizer/normalizer.d.ts.map +1 -0
- package/lib/typescript/src/types/breakpoints.d.ts +25 -12
- package/lib/typescript/src/types/breakpoints.d.ts.map +1 -1
- package/lib/typescript/src/types/core.d.ts +6 -27
- package/lib/typescript/src/types/core.d.ts.map +1 -1
- package/lib/typescript/src/types/index.d.ts +4 -2
- package/lib/typescript/src/types/index.d.ts.map +1 -1
- package/lib/typescript/src/types/plugin.d.ts +7 -0
- package/lib/typescript/src/types/plugin.d.ts.map +1 -0
- package/lib/typescript/src/types/stylesheet.d.ts +40 -0
- package/lib/typescript/src/types/stylesheet.d.ts.map +1 -0
- package/lib/typescript/src/types/unistyles.d.ts +16 -11
- package/lib/typescript/src/types/unistyles.d.ts.map +1 -1
- package/lib/typescript/src/types/variants.d.ts +14 -0
- package/lib/typescript/src/types/variants.d.ts.map +1 -0
- package/lib/typescript/src/useStyles.d.ts +3 -3
- package/lib/typescript/src/useStyles.d.ts.map +1 -1
- package/lib/typescript/src/utils/breakpoints.d.ts +2 -5
- package/lib/typescript/src/utils/breakpoints.d.ts.map +1 -1
- package/lib/typescript/src/utils/index.d.ts +4 -6
- package/lib/typescript/src/utils/index.d.ts.map +1 -1
- package/lib/typescript/src/utils/mq.d.ts +15 -14
- package/lib/typescript/src/utils/mq.d.ts.map +1 -1
- package/lib/typescript/src/utils/mqParser.d.ts +14 -2
- package/lib/typescript/src/utils/mqParser.d.ts.map +1 -1
- package/lib/typescript/src/utils/styles.d.ts +3 -4
- package/lib/typescript/src/utils/styles.d.ts.map +1 -1
- package/lib/typescript/src/utils/withPlugins.d.ts +3 -0
- package/lib/typescript/src/utils/withPlugins.d.ts.map +1 -0
- package/package.json +19 -16
- package/src/__tests__/mocks.ts +24 -0
- package/src/common.ts +30 -14
- package/src/core/UnistyleRegistry.ts +66 -4
- package/src/core/UnistylesRuntime.ts +18 -20
- package/src/core/index.ts +1 -0
- package/src/createStyleSheet.ts +2 -8
- package/src/global.ts +1 -0
- package/src/hooks/index.ts +1 -0
- package/src/hooks/useUnistyles.ts +11 -9
- package/src/hooks/useVariants.ts +10 -0
- package/src/index.ts +3 -1
- package/src/plugins/index.ts +2 -0
- package/src/plugins/normalizeWebStylesPlugin.ts +7 -0
- package/src/plugins/normalizer/index.ts +2 -0
- package/src/{utils/normalizeStyles.web.ts → plugins/normalizer/normalizeStyle.ts} +3 -3
- package/src/{utils → plugins/normalizer}/normalizer.ts +3 -3
- package/src/types/breakpoints.ts +57 -25
- package/src/types/core.ts +9 -43
- package/src/types/index.ts +11 -2
- package/src/types/plugin.ts +7 -0
- package/src/types/stylesheet.ts +49 -0
- package/src/types/unistyles.ts +18 -13
- package/src/types/variants.ts +19 -0
- package/src/useStyles.ts +18 -25
- package/src/utils/breakpoints.ts +11 -67
- package/src/utils/index.ts +4 -6
- package/src/utils/mq.ts +31 -84
- package/src/utils/mqParser.ts +5 -5
- package/src/utils/styles.ts +44 -50
- package/src/utils/withPlugins.ts +13 -0
- package/lib/commonjs/utils/common.js +0 -25
- package/lib/commonjs/utils/common.js.map +0 -1
- package/lib/commonjs/utils/normalizeStyles.js +0 -10
- package/lib/commonjs/utils/normalizeStyles.js.map +0 -1
- package/lib/commonjs/utils/normalizeStyles.web.js.map +0 -1
- package/lib/commonjs/utils/normalizer.js.map +0 -1
- package/lib/module/types/mq.js +0 -2
- package/lib/module/utils/common.js +0 -17
- package/lib/module/utils/common.js.map +0 -1
- package/lib/module/utils/normalizeStyles.js +0 -3
- package/lib/module/utils/normalizeStyles.js.map +0 -1
- package/lib/module/utils/normalizeStyles.web.js.map +0 -1
- package/lib/module/utils/normalizer.js.map +0 -1
- package/lib/typescript/src/types/mq.d.ts +0 -3
- package/lib/typescript/src/types/mq.d.ts.map +0 -1
- package/lib/typescript/src/utils/common.d.ts +0 -12
- package/lib/typescript/src/utils/common.d.ts.map +0 -1
- package/lib/typescript/src/utils/normalizeStyles.d.ts +0 -2
- package/lib/typescript/src/utils/normalizeStyles.d.ts.map +0 -1
- package/lib/typescript/src/utils/normalizeStyles.web.d.ts +0 -5
- package/lib/typescript/src/utils/normalizeStyles.web.d.ts.map +0 -1
- package/lib/typescript/src/utils/normalizer.d.ts.map +0 -1
- package/src/types/mq.ts +0 -3
- package/src/utils/common.ts +0 -20
- package/src/utils/normalizeStyles.ts +0 -2
- /package/lib/commonjs/{utils → plugins/normalizer}/module.d.js +0 -0
- /package/lib/module/{utils → plugins/normalizer}/module.d.js +0 -0
- /package/src/{utils → plugins/normalizer}/module.d.ts +0 -0
package/src/common.ts
CHANGED
@@ -1,19 +1,35 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
import { Platform } from 'react-native'
|
2
|
+
|
3
|
+
export const warn = (message: string) => {
|
4
|
+
console.warn(`🦄 [react-native-unistyles]: ${message}`)
|
4
5
|
}
|
5
6
|
|
6
|
-
export
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
7
|
+
export const isWeb = Platform.OS === 'web'
|
8
|
+
export const isIOS = Platform.OS === 'ios'
|
9
|
+
export const isAndroid = Platform.OS === 'android'
|
10
|
+
export const isMobile = isIOS || isAndroid
|
11
|
+
export const isServer = typeof window === 'undefined'
|
12
|
+
|
13
|
+
export const ScreenOrientation = {
|
14
|
+
Landscape: 'landscape',
|
15
|
+
Portrait: 'portrait'
|
16
|
+
} as const
|
17
|
+
|
18
|
+
export enum UnistylesEventType {
|
19
|
+
Theme = 'theme',
|
20
|
+
Layout = 'layout',
|
21
|
+
Plugin = 'plugin'
|
13
22
|
}
|
14
23
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
24
|
+
export enum UnistylesError {
|
25
|
+
RuntimeUnavailable = 'Unistyles runtime is not available. Make sure you followed the installation instructions',
|
26
|
+
ThemeNotFound = 'You are trying to get a theme that is not registered with UnistylesRegistry',
|
27
|
+
ThemeNotRegistered = 'You are trying to set a theme that was not registered with UnistylesRegistry',
|
28
|
+
ThemeNotSelected = 'Your themes are registered, but you didn\'t select the initial theme',
|
29
|
+
ThemesCannotBeEmpty = 'You are trying to register empty themes object',
|
30
|
+
BreakpointsCannotBeEmpty = 'You are trying to register empty breakpoints object',
|
31
|
+
BreakpointsMustStartFromZero = 'You are trying to register breakpoints that don\'t start from 0',
|
32
|
+
InvalidPluginName = 'Plugin name can\'t start from reserved prefix __unistyles',
|
33
|
+
DuplicatePluginName = 'You are trying to register a plugin with a name that is already registered',
|
34
|
+
CantRemoveInternalPlugin = 'You are trying to remove an internal unistyles plugin'
|
19
35
|
}
|
@@ -1,8 +1,13 @@
|
|
1
|
-
import type { UnistylesBridge, UnistylesConfig } from '../types'
|
1
|
+
import type { UnistylesBridge, UnistylesConfig, UnistylesPlugin } from '../types'
|
2
2
|
import type { UnistylesBreakpoints, UnistylesThemes } from '../global'
|
3
|
+
import { isWeb, UnistylesError } from '../common'
|
4
|
+
import { normalizeWebStylesPlugin } from '../plugins'
|
3
5
|
|
4
6
|
export class UnistyleRegistry {
|
5
7
|
public config: UnistylesConfig = {}
|
8
|
+
public plugins: Array<UnistylesPlugin> = isWeb
|
9
|
+
? [normalizeWebStylesPlugin]
|
10
|
+
: []
|
6
11
|
public themeNames: Array<keyof UnistylesThemes> = []
|
7
12
|
public themes: UnistylesThemes = {} as UnistylesThemes
|
8
13
|
public breakpoints: UnistylesBreakpoints = {} as UnistylesBreakpoints
|
@@ -18,7 +23,10 @@ export class UnistyleRegistry {
|
|
18
23
|
this.unistylesBridge.themes = keys
|
19
24
|
this.themeNames = keys
|
20
25
|
|
21
|
-
return
|
26
|
+
return {
|
27
|
+
addBreakpoints: this.addBreakpoints,
|
28
|
+
addConfig: this.addConfig
|
29
|
+
}
|
22
30
|
}
|
23
31
|
|
24
32
|
public addBreakpoints = (breakpoints: UnistylesBreakpoints) => {
|
@@ -26,7 +34,10 @@ export class UnistyleRegistry {
|
|
26
34
|
this.unistylesBridge.useBreakpoints(breakpoints)
|
27
35
|
this.sortedBreakpointPairs = this.unistylesBridge.sortedBreakpointPairs
|
28
36
|
|
29
|
-
return
|
37
|
+
return {
|
38
|
+
addThemes: this.addThemes,
|
39
|
+
addConfig: this.addConfig
|
40
|
+
}
|
30
41
|
}
|
31
42
|
|
32
43
|
public addConfig = (config: UnistylesConfig) => {
|
@@ -36,6 +47,57 @@ export class UnistyleRegistry {
|
|
36
47
|
this.unistylesBridge.useAdaptiveThemes(config.adaptiveThemes)
|
37
48
|
}
|
38
49
|
|
39
|
-
|
50
|
+
if (config.experimentalPlugins) {
|
51
|
+
config.experimentalPlugins.forEach(plugin => this.addPlugin(plugin, false))
|
52
|
+
}
|
53
|
+
|
54
|
+
if (config.initialTheme) {
|
55
|
+
this.unistylesBridge.useTheme(config.initialTheme)
|
56
|
+
}
|
57
|
+
|
58
|
+
return {
|
59
|
+
addBreakpoints: this.addBreakpoints,
|
60
|
+
addThemes: this.addThemes
|
61
|
+
}
|
62
|
+
}
|
63
|
+
|
64
|
+
public getTheme = (forName: keyof UnistylesThemes) => {
|
65
|
+
if (this.themeNames.length === 0) {
|
66
|
+
return {} as UnistylesThemes[keyof UnistylesThemes]
|
67
|
+
}
|
68
|
+
|
69
|
+
if (this.hasTheme(forName)) {
|
70
|
+
return this.themes[forName]
|
71
|
+
}
|
72
|
+
|
73
|
+
if (this.unistylesBridge.themeName) {
|
74
|
+
throw new Error(UnistylesError.ThemeNotFound)
|
75
|
+
}
|
76
|
+
|
77
|
+
throw new Error(UnistylesError.ThemeNotSelected)
|
40
78
|
}
|
79
|
+
|
80
|
+
public addPlugin = (plugin: UnistylesPlugin, notify: boolean = true) => {
|
81
|
+
if (plugin.name.startsWith('__unistyles')) {
|
82
|
+
throw new Error(UnistylesError.InvalidPluginName)
|
83
|
+
}
|
84
|
+
|
85
|
+
if (this.plugins.some(({ name }) => name === plugin.name)) {
|
86
|
+
throw new Error(UnistylesError.DuplicatePluginName)
|
87
|
+
}
|
88
|
+
|
89
|
+
this.plugins = [plugin].concat(this.plugins)
|
90
|
+
this.unistylesBridge.addPlugin(plugin.name, notify)
|
91
|
+
}
|
92
|
+
|
93
|
+
public removePlugin = (plugin: UnistylesPlugin) => {
|
94
|
+
if (plugin.name.startsWith('__unistyles')) {
|
95
|
+
throw new Error(UnistylesError.CantRemoveInternalPlugin)
|
96
|
+
}
|
97
|
+
|
98
|
+
this.plugins = this.plugins.filter(({ name }) => name !== plugin.name)
|
99
|
+
this.unistylesBridge.removePlugin(plugin.name)
|
100
|
+
}
|
101
|
+
|
102
|
+
public hasTheme = (name: keyof UnistylesThemes) => name in this.themes
|
41
103
|
}
|
@@ -1,10 +1,10 @@
|
|
1
1
|
import { ScreenOrientation, UnistylesError } from '../common'
|
2
|
-
import type { UnistylesBridge } from '../types'
|
2
|
+
import type { UnistylesBridge, UnistylesPlugin } from '../types'
|
3
3
|
import type { UnistylesThemes } from '../global'
|
4
4
|
import type { UnistyleRegistry } from './UnistyleRegistry'
|
5
5
|
|
6
6
|
export class UnistylesRuntime {
|
7
|
-
constructor(private unistylesBridge: UnistylesBridge, private
|
7
|
+
constructor(private unistylesBridge: UnistylesBridge, private unistylesRegistry: UnistyleRegistry) {}
|
8
8
|
|
9
9
|
public get colorScheme() {
|
10
10
|
return this.unistylesBridge.colorScheme
|
@@ -14,10 +14,6 @@ export class UnistylesRuntime {
|
|
14
14
|
return this.unistylesBridge.hasAdaptiveThemes
|
15
15
|
}
|
16
16
|
|
17
|
-
public get sortedBreakpoints() {
|
18
|
-
return this.registry.sortedBreakpointPairs
|
19
|
-
}
|
20
|
-
|
21
17
|
public get themeName() {
|
22
18
|
return this.unistylesBridge.themeName
|
23
19
|
}
|
@@ -26,6 +22,10 @@ export class UnistylesRuntime {
|
|
26
22
|
return this.unistylesBridge.breakpoint
|
27
23
|
}
|
28
24
|
|
25
|
+
public get enabledPlugins() {
|
26
|
+
return this.unistylesBridge.enabledPlugins
|
27
|
+
}
|
28
|
+
|
29
29
|
public get screen() {
|
30
30
|
return {
|
31
31
|
width: this.unistylesBridge.screenWidth,
|
@@ -44,7 +44,11 @@ export class UnistylesRuntime {
|
|
44
44
|
}
|
45
45
|
|
46
46
|
public setTheme = (name: keyof UnistylesThemes) => {
|
47
|
-
if (this.
|
47
|
+
if (name === this.themeName) {
|
48
|
+
return
|
49
|
+
}
|
50
|
+
|
51
|
+
if (this.unistylesRegistry.hasTheme(name)) {
|
48
52
|
this.unistylesBridge.useTheme(name)
|
49
53
|
|
50
54
|
return true
|
@@ -53,21 +57,15 @@ export class UnistylesRuntime {
|
|
53
57
|
throw new Error(UnistylesError.ThemeNotRegistered)
|
54
58
|
}
|
55
59
|
|
56
|
-
public getTheme = (forName: keyof UnistylesThemes) => {
|
57
|
-
if (this.registry.themeNames.length === 0) {
|
58
|
-
return {} as UnistylesThemes[keyof UnistylesThemes]
|
59
|
-
}
|
60
|
-
|
61
|
-
if (!this.hasTheme(forName)) {
|
62
|
-
throw new Error(UnistylesError.ThemeNotFound)
|
63
|
-
}
|
64
|
-
|
65
|
-
return this.registry.themes[forName]
|
66
|
-
}
|
67
|
-
|
68
60
|
public setAdaptiveThemes = (enable: boolean) => {
|
69
61
|
this.unistylesBridge.useAdaptiveThemes(enable)
|
70
62
|
}
|
71
63
|
|
72
|
-
|
64
|
+
public addPlugin = (plugin: UnistylesPlugin) => {
|
65
|
+
this.unistylesRegistry.addPlugin(plugin)
|
66
|
+
}
|
67
|
+
|
68
|
+
public removePlugin = (plugin: UnistylesPlugin) => {
|
69
|
+
this.unistylesRegistry.removePlugin(plugin)
|
70
|
+
}
|
73
71
|
}
|
package/src/core/index.ts
CHANGED
package/src/createStyleSheet.ts
CHANGED
@@ -1,9 +1,3 @@
|
|
1
|
-
import type {
|
1
|
+
import type { StyleSheetWithSuperPowers } from './types'
|
2
2
|
|
3
|
-
export const createStyleSheet = <S
|
4
|
-
if (typeof styles === 'function') {
|
5
|
-
return styles as X
|
6
|
-
}
|
7
|
-
|
8
|
-
return styles as S
|
9
|
-
}
|
3
|
+
export const createStyleSheet = <S extends StyleSheetWithSuperPowers>(stylesheet: S): S => stylesheet
|
package/src/global.ts
CHANGED
package/src/hooks/index.ts
CHANGED
@@ -1,13 +1,14 @@
|
|
1
1
|
import { NativeEventEmitter, NativeModules } from 'react-native'
|
2
2
|
import { useEffect, useState } from 'react'
|
3
3
|
import { unistyles } from '../core'
|
4
|
-
import {
|
4
|
+
import { UnistylesEventType } from '../common'
|
5
5
|
import type { UnistylesEvents, UnistylesMobileLayoutEvent, UnistylesThemeEvent } from '../types'
|
6
6
|
|
7
7
|
const unistylesEvents = new NativeEventEmitter(NativeModules.Unistyles)
|
8
8
|
|
9
9
|
export const useUnistyles = () => {
|
10
|
-
const [
|
10
|
+
const [plugins, setPlugins] = useState(unistyles.runtime.enabledPlugins)
|
11
|
+
const [theme, setTheme] = useState(unistyles.registry.getTheme(unistyles.runtime.themeName))
|
11
12
|
const [layout, setLayout] = useState({
|
12
13
|
breakpoint: unistyles.runtime.breakpoint,
|
13
14
|
orientation: unistyles.runtime.orientation,
|
@@ -22,23 +23,23 @@ export const useUnistyles = () => {
|
|
22
23
|
'onChange',
|
23
24
|
(event: UnistylesEvents) => {
|
24
25
|
switch (event.type) {
|
25
|
-
case
|
26
|
+
case UnistylesEventType.Theme: {
|
26
27
|
const themeEvent = event as UnistylesThemeEvent
|
27
28
|
|
28
|
-
return setTheme(unistyles.
|
29
|
+
return setTheme(unistyles.registry.getTheme(themeEvent.payload.themeName))
|
29
30
|
}
|
30
|
-
case
|
31
|
+
case UnistylesEventType.Layout: {
|
31
32
|
const layoutEvent = event as UnistylesMobileLayoutEvent
|
32
33
|
|
33
34
|
return setLayout({
|
34
35
|
breakpoint: layoutEvent.payload.breakpoint,
|
35
36
|
orientation: layoutEvent.payload.orientation,
|
36
|
-
screenSize:
|
37
|
-
width: layoutEvent.payload.screen.width,
|
38
|
-
height: layoutEvent.payload.screen.height
|
39
|
-
}
|
37
|
+
screenSize: layoutEvent.payload.screen
|
40
38
|
})
|
41
39
|
}
|
40
|
+
case UnistylesEventType.Plugin: {
|
41
|
+
return setPlugins(unistyles.runtime.enabledPlugins)
|
42
|
+
}
|
42
43
|
default:
|
43
44
|
return
|
44
45
|
}
|
@@ -49,6 +50,7 @@ export const useUnistyles = () => {
|
|
49
50
|
}, [])
|
50
51
|
|
51
52
|
return {
|
53
|
+
plugins,
|
52
54
|
theme,
|
53
55
|
layout
|
54
56
|
}
|
@@ -0,0 +1,10 @@
|
|
1
|
+
import { useRef } from 'react'
|
2
|
+
import type { Optional } from '../types'
|
3
|
+
|
4
|
+
export const useVariants = (variantsMap?: Record<string, Optional<string>>) => {
|
5
|
+
const variantsRef = useRef<Optional<Record<string, Optional<string>>>>(variantsMap)
|
6
|
+
|
7
|
+
variantsRef.current = variantsMap
|
8
|
+
|
9
|
+
return variantsRef.current
|
10
|
+
}
|
package/src/index.ts
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
import { unistyles } from './core'
|
2
2
|
import { mq } from './utils'
|
3
3
|
import { useInitialTheme } from './hooks'
|
4
|
+
import type { UnistylesPlugin } from './types'
|
4
5
|
import type { UnistylesThemes, UnistylesBreakpoints } from './global'
|
5
6
|
import { ScreenOrientation } from './common'
|
6
7
|
import { useStyles } from './useStyles'
|
@@ -26,5 +27,6 @@ export {
|
|
26
27
|
|
27
28
|
export type {
|
28
29
|
UnistylesThemes,
|
29
|
-
UnistylesBreakpoints
|
30
|
+
UnistylesBreakpoints,
|
31
|
+
UnistylesPlugin
|
30
32
|
}
|
@@ -1,6 +1,6 @@
|
|
1
|
-
import { warn } from './common'
|
2
1
|
import { preprocessor } from './normalizer'
|
3
|
-
import
|
2
|
+
import { warn } from '../../common'
|
3
|
+
import type { NormalizedBoxShadow, NormalizedTextShadow, BoxShadow, TextShadow, RNStyle } from '../../types'
|
4
4
|
|
5
5
|
const normalizeBoxShadow = <T extends BoxShadow>(style: T): NormalizedBoxShadow => {
|
6
6
|
const requiredBoxShadowProperties = [
|
@@ -55,7 +55,7 @@ const normalizeTextShadow = <T extends TextShadow>(style: T): NormalizedTextShad
|
|
55
55
|
}
|
56
56
|
}
|
57
57
|
|
58
|
-
export const
|
58
|
+
export const normalizeStyle = <T extends RNStyle>(style: T): T => {
|
59
59
|
const normalizedTransform = ('transform' in style && Array.isArray(style.transform))
|
60
60
|
? { transform: preprocessor.createTransformValue(style.transform) }
|
61
61
|
: {}
|
@@ -1,7 +1,7 @@
|
|
1
1
|
// based on react-native-web normalizer
|
2
2
|
// https://github.com/necolas/react-native-web
|
3
3
|
import normalizeColors from '@react-native/normalize-colors'
|
4
|
-
import type { TextShadow, Transforms, BoxShadow } from '
|
4
|
+
import type { TextShadow, Transforms, BoxShadow, Nullable } from '../../types'
|
5
5
|
|
6
6
|
type Preprocessor = {
|
7
7
|
createTextShadowValue(style: TextShadow): string,
|
@@ -15,9 +15,9 @@ export const normalizeColor = (color: string, opacity: number = 1) => {
|
|
15
15
|
return color
|
16
16
|
}
|
17
17
|
|
18
|
-
const integer = normalizeColors(color) as number
|
18
|
+
const integer = normalizeColors(color) as Nullable<number>
|
19
19
|
|
20
|
-
// If the
|
20
|
+
// If the color is an unknown format, the return value is null
|
21
21
|
if (integer === null) {
|
22
22
|
return color
|
23
23
|
}
|
package/src/types/breakpoints.ts
CHANGED
@@ -1,29 +1,61 @@
|
|
1
|
-
import type { OpaqueColorValue } from 'react-native'
|
2
|
-
import type {
|
3
|
-
import type {
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
1
|
+
import type { ColorValue, OpaqueColorValue } from 'react-native'
|
2
|
+
import type { UnistylesTheme } from '../types'
|
3
|
+
import type { BreakpointsOrMediaQueries, ToDeepUnistyles } from './stylesheet'
|
4
|
+
import type { TransformStyles } from './core'
|
5
|
+
|
6
|
+
type ExtractTransformArray<T> = T extends object
|
7
|
+
? { [K in keyof T]: ExtractBreakpoints<T[K]> }
|
8
|
+
: never
|
9
|
+
|
10
|
+
type ExtractBreakpoints<T> = T extends object
|
11
|
+
? keyof T extends BreakpointsOrMediaQueries
|
12
|
+
? T[keyof T]
|
13
|
+
: T extends Array<ToDeepUnistyles<TransformStyles>>
|
14
|
+
? Array<ExtractTransformArray<T[number]>>
|
15
|
+
: {
|
16
|
+
[K in keyof T]: ExtractBreakpoints<T[K]>
|
17
|
+
}
|
18
|
+
: T
|
18
19
|
|
19
|
-
type
|
20
|
-
? (...args:
|
20
|
+
type ParseNestedObject<T> = T extends (...args: infer A) => infer R
|
21
|
+
? (...args: A) => ParseNestedObject<R>
|
21
22
|
: T extends object
|
22
|
-
? T extends
|
23
|
-
?
|
24
|
-
:
|
25
|
-
|
26
|
-
|
23
|
+
? T extends { variants: infer R }
|
24
|
+
? ParseVariants<FlattenVariants<R>> & ParseNestedObject<Omit<T, 'variants'>>
|
25
|
+
: {
|
26
|
+
[K in keyof T]: T[K] extends object
|
27
|
+
? T[K] extends OpaqueColorValue
|
28
|
+
? ColorValue
|
29
|
+
: ExtractBreakpoints<T[K]>
|
30
|
+
: T[K]
|
31
|
+
}
|
27
32
|
: T
|
28
33
|
|
29
|
-
|
34
|
+
type FlattenVariants<T> = T extends object
|
35
|
+
? {
|
36
|
+
[K in keyof T]: T[K] extends object
|
37
|
+
? {
|
38
|
+
[key in keyof T[K]]: T[K][key] extends object
|
39
|
+
? ParseNestedObject<T[K][key]>
|
40
|
+
: never
|
41
|
+
}
|
42
|
+
: never
|
43
|
+
}
|
44
|
+
: never
|
45
|
+
|
46
|
+
type ParseVariants<T> = T extends object
|
47
|
+
? T[keyof T] extends object
|
48
|
+
? UnionToIntersection<ParseVariants<T[keyof T]>>
|
49
|
+
: T
|
50
|
+
: T
|
51
|
+
|
52
|
+
type UnionToIntersection<U> =
|
53
|
+
(U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never
|
54
|
+
|
55
|
+
type ParseStyleKeys<T> = T extends object
|
56
|
+
? { [K in keyof T]: ParseNestedObject<T[K]> }
|
57
|
+
: never
|
58
|
+
|
59
|
+
export type ReactNativeStyleSheet<T> = T extends (theme: UnistylesTheme) => infer R
|
60
|
+
? ParseStyleKeys<R>
|
61
|
+
: ParseStyleKeys<T>
|
package/src/types/core.ts
CHANGED
@@ -15,19 +15,13 @@ import type {
|
|
15
15
|
} from 'react-native/Libraries/StyleSheet/StyleSheetTypes'
|
16
16
|
import type { ImageStyle, TextStyle, ViewStyle } from 'react-native'
|
17
17
|
import type { UnistylesBreakpoints, UnistylesThemes } from '../global'
|
18
|
-
import type { MediaQuery } from './mq'
|
19
18
|
|
20
|
-
type ShadowOffset = {
|
19
|
+
export type ShadowOffset = {
|
21
20
|
width: number,
|
22
21
|
height: number
|
23
22
|
}
|
24
23
|
|
25
|
-
export type
|
26
|
-
width: number,
|
27
|
-
height: number
|
28
|
-
}
|
29
|
-
|
30
|
-
type TransformStyles =
|
24
|
+
export type TransformStyles =
|
31
25
|
& PerpectiveTransform
|
32
26
|
& RotateTransform
|
33
27
|
& RotateXTransform
|
@@ -42,41 +36,13 @@ type TransformStyles =
|
|
42
36
|
& SkewYTransform
|
43
37
|
& MatrixTransform
|
44
38
|
|
45
|
-
type
|
46
|
-
|
47
|
-
|
48
|
-
transform?: Array<DeepUniStyle<TransformStyles>>
|
49
|
-
}
|
50
|
-
|
51
|
-
type UniStyle<V> = {
|
52
|
-
[innerKey in keyof UnistylesBreakpoints]?: V
|
53
|
-
} | {
|
54
|
-
[innerKey in MediaQuery]: V
|
55
|
-
}
|
56
|
-
|
57
|
-
type DeepUniStyle<T> = {
|
58
|
-
[K in keyof T]?: UniStyle<T[K]> | T[K]
|
59
|
-
}
|
60
|
-
|
61
|
-
// these props are treated differently to nest breakpoints and media queries
|
62
|
-
type NestedTypes = 'shadowOffset' | 'transform' | 'textShadowOffset'
|
63
|
-
|
64
|
-
type UnistyleView = DeepUniStyle<Omit<ViewStyle, NestedTypes>>
|
65
|
-
type UnistyleText = DeepUniStyle<Omit<TextStyle, NestedTypes>>
|
66
|
-
type UnistyleImage = DeepUniStyle<Omit<ImageStyle, NestedTypes>>
|
67
|
-
|
68
|
-
export type StaticStyles =
|
69
|
-
| UnistyleView
|
70
|
-
| UnistyleText
|
71
|
-
| UnistyleImage
|
72
|
-
& UnistyleNested
|
73
|
-
|
74
|
-
export type CustomNamedStyles<T> = {
|
75
|
-
[K in keyof T]: T[K] extends (...args: infer A) => StaticStyles
|
76
|
-
? (...args: A) => StaticStyles
|
77
|
-
: StaticStyles
|
39
|
+
export type ScreenSize = {
|
40
|
+
width: number,
|
41
|
+
height: number
|
78
42
|
}
|
79
43
|
|
80
|
-
export type
|
44
|
+
export type RNStyle = ViewStyle & TextStyle & ImageStyle
|
45
|
+
export type RNValue = ViewStyle[keyof ViewStyle] | TextStyle[keyof TextStyle] | ImageStyle[keyof ImageStyle]
|
46
|
+
export type NestedStyle = Record<keyof UnistylesBreakpoints | symbol, RNValue>
|
47
|
+
export type NestedStylePairs = Array<[keyof UnistylesBreakpoints | symbol, RNValue]>
|
81
48
|
export type UnistylesTheme = UnistylesThemes[keyof UnistylesThemes]
|
82
|
-
export type CreateStylesFactory<ST, Theme> = (theme: Theme) => ST
|
package/src/types/index.ts
CHANGED
@@ -1,6 +1,15 @@
|
|
1
1
|
export * from './normalizer'
|
2
2
|
export * from './unistyles'
|
3
3
|
export type { Optional, Nullable } from './common'
|
4
|
-
export type {
|
5
|
-
|
4
|
+
export type {
|
5
|
+
NestedStylePairs,
|
6
|
+
UnistylesTheme,
|
7
|
+
ScreenSize,
|
8
|
+
NestedStyle,
|
9
|
+
RNValue,
|
10
|
+
RNStyle
|
11
|
+
} from './core'
|
12
|
+
export type { StyleSheetWithSuperPowers, StyleSheet, AllAvailableKeys } from './stylesheet'
|
6
13
|
export type { ReactNativeStyleSheet } from './breakpoints'
|
14
|
+
export type { ExtractVariantNames } from './variants'
|
15
|
+
export type { UnistylesPlugin } from './plugin'
|
@@ -0,0 +1,49 @@
|
|
1
|
+
import type { ImageStyle, TextStyle, ViewStyle } from 'react-native'
|
2
|
+
import type { ShadowOffset, TransformStyles, UnistylesTheme } from './core'
|
3
|
+
import type { UnistylesBreakpoints } from '../global'
|
4
|
+
|
5
|
+
// these props are treated differently to nest breakpoints and media queries
|
6
|
+
type NestedKeys = 'shadowOffset' | 'transform' | 'textShadowOffset'
|
7
|
+
|
8
|
+
type UnistyleView = Omit<ViewStyle, NestedKeys>
|
9
|
+
type UnistyleText = Omit<TextStyle, NestedKeys>
|
10
|
+
type UnistyleImage = Omit<ImageStyle, NestedKeys>
|
11
|
+
|
12
|
+
type UnistyleNestedStyles = {
|
13
|
+
shadowOffset?: ToDeepUnistyles<ShadowOffset>,
|
14
|
+
textShadowOffset?: ToDeepUnistyles<ShadowOffset>,
|
15
|
+
transform?: Array<ToDeepUnistyles<TransformStyles>>
|
16
|
+
}
|
17
|
+
|
18
|
+
type Variants = {
|
19
|
+
variants?: {
|
20
|
+
[variantName: string]: {
|
21
|
+
[variant: string]: Omit<UnistylesValues, 'variants'>
|
22
|
+
}
|
23
|
+
}
|
24
|
+
}
|
25
|
+
|
26
|
+
export type ToDeepUnistyles<T> = {
|
27
|
+
[K in keyof T]?: T[K] | {
|
28
|
+
[key in BreakpointsOrMediaQueries]?: T[K]
|
29
|
+
}
|
30
|
+
}
|
31
|
+
|
32
|
+
type AllAvailableStyles = UnistyleView & UnistyleText & UnistyleImage & UnistyleNestedStyles
|
33
|
+
|
34
|
+
export type AllAvailableKeys = keyof (UnistyleView & UnistyleText & UnistyleImage)
|
35
|
+
export type BreakpointsOrMediaQueries = keyof UnistylesBreakpoints | symbol
|
36
|
+
|
37
|
+
export type UnistylesValues = {
|
38
|
+
[propName in AllAvailableKeys]?: AllAvailableStyles[propName] | {
|
39
|
+
[key in BreakpointsOrMediaQueries]?: AllAvailableStyles[propName]
|
40
|
+
}
|
41
|
+
} & Variants & {
|
42
|
+
[propName in NestedKeys]?: UnistyleNestedStyles[propName]
|
43
|
+
}
|
44
|
+
|
45
|
+
export type StyleSheet = {
|
46
|
+
[styleName: string]: UnistylesValues | ((...args: any) => UnistylesValues)
|
47
|
+
}
|
48
|
+
|
49
|
+
export type StyleSheetWithSuperPowers = ((theme: UnistylesTheme) => StyleSheet) | StyleSheet
|