react-native-unistyles 2.0.0-alpha.10 → 2.0.0-alpha.12
Sign up to get free protection for your applications and to get access to all the features.
- package/cxx/UnistylesRuntime.cpp +55 -1
- package/cxx/UnistylesRuntime.h +10 -4
- package/ios/UnistylesModule.mm +12 -2
- package/lib/commonjs/common.js +19 -6
- package/lib/commonjs/common.js.map +1 -1
- package/lib/commonjs/core/UnistyleRegistry.js +49 -1
- package/lib/commonjs/core/UnistyleRegistry.js.map +1 -1
- package/lib/commonjs/core/UnistylesRuntime.js +12 -16
- package/lib/commonjs/core/UnistylesRuntime.js.map +1 -1
- package/lib/commonjs/hooks/useUnistyles.js +8 -2
- package/lib/commonjs/hooks/useUnistyles.js.map +1 -1
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/plugins/index.js +45 -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/plugins/normalizer/normalizer.js.map +1 -0
- package/lib/commonjs/plugins/withPlugins.js +18 -0
- package/lib/commonjs/plugins/withPlugins.js.map +1 -0
- package/lib/commonjs/types/plugin.js +6 -0
- package/lib/{module/utils/module.d.js.map → commonjs/types/plugin.js.map} +1 -1
- 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 +5 -4
- package/lib/commonjs/useStyles.js.map +1 -1
- package/lib/commonjs/utils/breakpoints.js +15 -70
- package/lib/commonjs/utils/breakpoints.js.map +1 -1
- package/lib/commonjs/utils/index.js +12 -52
- package/lib/commonjs/utils/index.js.map +1 -1
- package/lib/commonjs/utils/mq.js +1 -1
- 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 +22 -11
- package/lib/commonjs/utils/styles.js.map +1 -1
- package/lib/commonjs/utils/variants.js +2 -5
- package/lib/commonjs/utils/variants.js.map +1 -1
- package/lib/module/common.js +17 -7
- package/lib/module/common.js.map +1 -1
- package/lib/module/core/UnistyleRegistry.js +49 -1
- package/lib/module/core/UnistyleRegistry.js.map +1 -1
- package/lib/module/core/UnistylesRuntime.js +12 -16
- package/lib/module/core/UnistylesRuntime.js.map +1 -1
- package/lib/module/hooks/useUnistyles.js +8 -2
- package/lib/module/hooks/useUnistyles.js.map +1 -1
- package/lib/module/index.js.map +1 -1
- package/lib/module/plugins/index.js +4 -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/plugins/normalizer/normalizer.js.map +1 -0
- package/lib/module/plugins/withPlugins.js +11 -0
- package/lib/module/plugins/withPlugins.js.map +1 -0
- package/lib/module/types/plugin.js +2 -0
- package/lib/module/types/plugin.js.map +1 -0
- package/lib/module/types/variants.js +2 -0
- package/lib/module/types/variants.js.map +1 -0
- package/lib/module/useStyles.js +5 -4
- package/lib/module/useStyles.js.map +1 -1
- package/lib/module/utils/breakpoints.js +13 -66
- package/lib/module/utils/breakpoints.js.map +1 -1
- package/lib/module/utils/index.js +2 -5
- package/lib/module/utils/index.js.map +1 -1
- package/lib/module/utils/mq.js +1 -1
- 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 +23 -12
- package/lib/module/utils/styles.js.map +1 -1
- package/lib/module/utils/variants.js +2 -5
- package/lib/module/utils/variants.js.map +1 -1
- package/lib/typescript/src/common.d.ts +16 -6
- package/lib/typescript/src/common.d.ts.map +1 -1
- package/lib/typescript/src/core/UnistyleRegistry.d.ts +7 -2
- package/lib/typescript/src/core/UnistyleRegistry.d.ts.map +1 -1
- package/lib/typescript/src/core/UnistylesRuntime.d.ts +7 -8
- 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/hooks/useUnistyles.d.ts +2 -1
- package/lib/typescript/src/hooks/useUnistyles.d.ts.map +1 -1
- package/lib/typescript/src/index.d.ts +4 -3
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/lib/typescript/src/plugins/index.d.ts +4 -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/plugins/withPlugins.d.ts +3 -0
- package/lib/typescript/src/plugins/withPlugins.d.ts.map +1 -0
- package/lib/typescript/src/types/breakpoints.d.ts +3 -4
- package/lib/typescript/src/types/breakpoints.d.ts.map +1 -1
- package/lib/typescript/src/types/core.d.ts +4 -9
- package/lib/typescript/src/types/core.d.ts.map +1 -1
- package/lib/typescript/src/types/index.d.ts +3 -1
- 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/unistyles.d.ts +11 -2
- package/lib/typescript/src/types/unistyles.d.ts.map +1 -1
- package/lib/typescript/src/types/variants.d.ts +8 -0
- package/lib/typescript/src/types/variants.d.ts.map +1 -0
- package/lib/typescript/src/useStyles.d.ts +2 -2
- 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 +2 -5
- package/lib/typescript/src/utils/index.d.ts.map +1 -1
- package/lib/typescript/src/utils/mqParser.d.ts +12 -0
- package/lib/typescript/src/utils/mqParser.d.ts.map +1 -1
- package/lib/typescript/src/utils/styles.d.ts +3 -3
- package/lib/typescript/src/utils/styles.d.ts.map +1 -1
- package/lib/typescript/src/utils/variants.d.ts +1 -1
- package/lib/typescript/src/utils/variants.d.ts.map +1 -1
- package/package.json +4 -1
- package/src/__tests__/mocks.ts +24 -0
- package/src/common.ts +22 -7
- package/src/core/UnistyleRegistry.ts +46 -2
- package/src/core/UnistylesRuntime.ts +14 -20
- package/src/core/index.ts +1 -0
- package/src/hooks/useUnistyles.ts +7 -2
- package/src/index.ts +3 -1
- package/src/plugins/index.ts +3 -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 +1 -1
- package/src/plugins/withPlugins.ts +17 -0
- package/src/types/breakpoints.ts +5 -4
- package/src/types/core.ts +5 -10
- package/src/types/index.ts +12 -1
- package/src/types/plugin.ts +7 -0
- package/src/types/unistyles.ts +14 -4
- package/src/types/variants.ts +11 -0
- package/src/useStyles.ts +12 -6
- package/src/utils/breakpoints.ts +10 -75
- package/src/utils/index.ts +2 -5
- package/src/utils/mq.ts +1 -1
- package/src/utils/mqParser.ts +4 -4
- package/src/utils/styles.ts +35 -20
- package/src/utils/variants.ts +5 -9
- 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/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/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/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/commonjs/{utils → plugins/normalizer}/normalizer.js +0 -0
- /package/lib/module/{utils → plugins/normalizer}/module.d.js +0 -0
- /package/lib/module/{utils → plugins/normalizer}/normalizer.js +0 -0
- /package/src/{utils → plugins/normalizer}/module.d.ts +0 -0
@@ -1,8 +1,10 @@
|
|
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 { UnistylesError } from '../common'
|
3
4
|
|
4
5
|
export class UnistyleRegistry {
|
5
6
|
public config: UnistylesConfig = {}
|
7
|
+
public plugins: Array<UnistylesPlugin> = []
|
6
8
|
public themeNames: Array<keyof UnistylesThemes> = []
|
7
9
|
public themes: UnistylesThemes = {} as UnistylesThemes
|
8
10
|
public breakpoints: UnistylesBreakpoints = {} as UnistylesBreakpoints
|
@@ -36,6 +38,48 @@ export class UnistyleRegistry {
|
|
36
38
|
this.unistylesBridge.useAdaptiveThemes(config.adaptiveThemes)
|
37
39
|
}
|
38
40
|
|
39
|
-
|
41
|
+
if (config.experimentalPlugins) {
|
42
|
+
config.experimentalPlugins.forEach(plugin => this.addPlugin(plugin, false))
|
43
|
+
}
|
44
|
+
|
45
|
+
if (config.initialTheme) {
|
46
|
+
this.unistylesBridge.useTheme(config.initialTheme)
|
47
|
+
}
|
40
48
|
}
|
49
|
+
|
50
|
+
public getTheme = (forName: keyof UnistylesThemes) => {
|
51
|
+
if (this.themeNames.length === 0) {
|
52
|
+
return {} as UnistylesThemes[keyof UnistylesThemes]
|
53
|
+
}
|
54
|
+
|
55
|
+
if (!this.hasTheme(forName)) {
|
56
|
+
throw new Error(UnistylesError.ThemeNotFound)
|
57
|
+
}
|
58
|
+
|
59
|
+
return this.themes[forName]
|
60
|
+
}
|
61
|
+
|
62
|
+
public addPlugin = (plugin: UnistylesPlugin, notify: boolean = true) => {
|
63
|
+
if (plugin.name.startsWith('__unistyles')) {
|
64
|
+
throw new Error(UnistylesError.InvalidPluginName)
|
65
|
+
}
|
66
|
+
|
67
|
+
if (this.plugins.some(({ name }) => name === plugin.name)) {
|
68
|
+
throw new Error(UnistylesError.DuplicatePluginName)
|
69
|
+
}
|
70
|
+
|
71
|
+
this.plugins = this.plugins.concat([plugin])
|
72
|
+
this.unistylesBridge.addPlugin(plugin.name, notify)
|
73
|
+
}
|
74
|
+
|
75
|
+
public removePlugin = (plugin: UnistylesPlugin) => {
|
76
|
+
if (plugin.name.startsWith('__unistyles')) {
|
77
|
+
throw new Error(UnistylesError.CantRemoveInternalPlugin)
|
78
|
+
}
|
79
|
+
|
80
|
+
this.plugins = this.plugins.filter(({ name }) => name !== plugin.name)
|
81
|
+
this.unistylesBridge.removePlugin(plugin.name)
|
82
|
+
}
|
83
|
+
|
84
|
+
public hasTheme = (name: keyof UnistylesThemes) => name in this.themes
|
41
85
|
}
|
@@ -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,7 @@ export class UnistylesRuntime {
|
|
44
44
|
}
|
45
45
|
|
46
46
|
public setTheme = (name: keyof UnistylesThemes) => {
|
47
|
-
if (this.hasTheme(name)) {
|
47
|
+
if (this.unistylesRegistry.hasTheme(name)) {
|
48
48
|
this.unistylesBridge.useTheme(name)
|
49
49
|
|
50
50
|
return true
|
@@ -53,21 +53,15 @@ export class UnistylesRuntime {
|
|
53
53
|
throw new Error(UnistylesError.ThemeNotRegistered)
|
54
54
|
}
|
55
55
|
|
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
56
|
public setAdaptiveThemes = (enable: boolean) => {
|
69
57
|
this.unistylesBridge.useAdaptiveThemes(enable)
|
70
58
|
}
|
71
59
|
|
72
|
-
|
60
|
+
public addPlugin = (plugin: UnistylesPlugin) => {
|
61
|
+
this.unistylesRegistry.addPlugin(plugin)
|
62
|
+
}
|
63
|
+
|
64
|
+
public removePlugin = (plugin: UnistylesPlugin) => {
|
65
|
+
this.unistylesRegistry.removePlugin(plugin)
|
66
|
+
}
|
73
67
|
}
|
package/src/core/index.ts
CHANGED
@@ -7,7 +7,8 @@ import type { UnistylesEvents, UnistylesMobileLayoutEvent, UnistylesThemeEvent }
|
|
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,
|
@@ -25,7 +26,7 @@ export const useUnistyles = () => {
|
|
25
26
|
case CxxUnistylesEventTypes.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
31
|
case CxxUnistylesEventTypes.Layout: {
|
31
32
|
const layoutEvent = event as UnistylesMobileLayoutEvent
|
@@ -39,6 +40,9 @@ export const useUnistyles = () => {
|
|
39
40
|
}
|
40
41
|
})
|
41
42
|
}
|
43
|
+
case CxxUnistylesEventTypes.Plugin: {
|
44
|
+
return setPlugins(unistyles.runtime.enabledPlugins)
|
45
|
+
}
|
42
46
|
default:
|
43
47
|
return
|
44
48
|
}
|
@@ -49,6 +53,7 @@ export const useUnistyles = () => {
|
|
49
53
|
}, [])
|
50
54
|
|
51
55
|
return {
|
56
|
+
plugins,
|
52
57
|
theme,
|
53
58
|
layout
|
54
59
|
}
|
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 } from '../../types'
|
5
5
|
|
6
6
|
type Preprocessor = {
|
7
7
|
createTextShadowValue(style: TextShadow): string,
|
@@ -0,0 +1,17 @@
|
|
1
|
+
import type { RNStyle } from '../types'
|
2
|
+
import { normalizeWebStylesPlugin } from './normalizeWebStylesPlugin'
|
3
|
+
import { isWeb } from '../common'
|
4
|
+
import { unistyles } from '../core'
|
5
|
+
|
6
|
+
const UNISTYLES_PLUGINS = isWeb
|
7
|
+
? [normalizeWebStylesPlugin]
|
8
|
+
: []
|
9
|
+
|
10
|
+
export const withPlugins = (key: string, style: RNStyle) => unistyles.registry.plugins.concat(UNISTYLES_PLUGINS)
|
11
|
+
.reduce((acc, plugin) => {
|
12
|
+
if (plugin.onParsedStyle) {
|
13
|
+
return plugin.onParsedStyle(key, acc, unistyles.runtime)
|
14
|
+
}
|
15
|
+
|
16
|
+
return acc
|
17
|
+
}, style)
|
package/src/types/breakpoints.ts
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
import type { OpaqueColorValue } from 'react-native'
|
2
2
|
import type { UnistylesBreakpoints } from '../global'
|
3
3
|
import type { MediaQuery } from './mq'
|
4
|
-
import type { VariantValue } from './core'
|
5
4
|
|
6
5
|
type WithEmptyObject<V> = keyof V extends never ? {} : V
|
7
6
|
|
@@ -17,15 +16,17 @@ type ExtractBreakpoints<T> = T extends Partial<Record<keyof UnistylesBreakpoints
|
|
17
16
|
: T[K]
|
18
17
|
}
|
19
18
|
|
20
|
-
type
|
19
|
+
type UnionToIntersection<U> =
|
20
|
+
(U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never
|
21
|
+
|
21
22
|
type RemoveKeysWithPrefix<T> = T extends (...args: Array<any>) => infer R
|
22
23
|
? (...args: Parameters<T>) => RemoveKeysWithPrefix<R>
|
23
24
|
: T extends object
|
24
25
|
? T extends OpaqueColorValue
|
25
26
|
? string
|
26
27
|
: T extends Record<string, infer _V>
|
27
|
-
? T extends { variants:
|
28
|
-
?
|
28
|
+
? T extends { variants: infer _V }
|
29
|
+
? Omit<T, 'variants'> & UnionToIntersection<_V[keyof _V]>
|
29
30
|
: { [K in keyof T as K extends MediaQuery ? keyof UnistylesBreakpoints & string : K]: RemoveKeysWithPrefix<T[K]> }
|
30
31
|
: { [K in keyof T]: RemoveKeysWithPrefix<T[K]> }
|
31
32
|
: T
|
package/src/types/core.ts
CHANGED
@@ -45,17 +45,14 @@ type TransformStyles =
|
|
45
45
|
type UnistyleNested = {
|
46
46
|
shadowOffset?: DeepUniStyle<ShadowOffset>,
|
47
47
|
textShadowOffset?: DeepUniStyle<ShadowOffset>,
|
48
|
-
transform?: Array<DeepUniStyle<TransformStyles
|
48
|
+
transform?: Array<DeepUniStyle<TransformStyles>>,
|
49
|
+
variants?: Record<string, UnistyleView | UnistyleText | UnistyleImage & Omit<UnistyleNested, 'variants'>>
|
49
50
|
}
|
50
51
|
|
51
52
|
type UniStyle<V> = {
|
52
53
|
[innerKey in keyof UnistylesBreakpoints]?: V
|
53
54
|
} | {
|
54
55
|
[innerKey in MediaQuery]: V
|
55
|
-
} | {
|
56
|
-
[innerKey in 'variants']?: {
|
57
|
-
[variant: string]: V
|
58
|
-
}
|
59
56
|
}
|
60
57
|
|
61
58
|
type DeepUniStyle<T> = {
|
@@ -81,11 +78,9 @@ export type CustomNamedStyles<T> = {
|
|
81
78
|
: StaticStyles
|
82
79
|
}
|
83
80
|
|
81
|
+
export type RNStyle = ViewStyle | TextStyle | ImageStyle
|
84
82
|
export type RNValue = number | string | undefined
|
85
|
-
export type
|
86
|
-
export type
|
87
|
-
variants: VariantValue
|
88
|
-
}
|
89
|
-
export type NestedStylePairs = Array<[keyof UnistylesBreakpoints | MediaQuery | 'variants', RNValue | VariantValue]>
|
83
|
+
export type NestedStyle = Record<keyof UnistylesBreakpoints | MediaQuery, RNValue>
|
84
|
+
export type NestedStylePairs = Array<[keyof UnistylesBreakpoints | MediaQuery, RNValue]>
|
90
85
|
export type UnistylesTheme = UnistylesThemes[keyof UnistylesThemes]
|
91
86
|
export type CreateStylesFactory<ST, Theme> = (theme: Theme) => ST
|
package/src/types/index.ts
CHANGED
@@ -2,5 +2,16 @@ export * from './normalizer'
|
|
2
2
|
export * from './unistyles'
|
3
3
|
export type { Optional, Nullable } from './common'
|
4
4
|
export type { MediaQuery } from './mq'
|
5
|
-
export type {
|
5
|
+
export type {
|
6
|
+
CustomNamedStyles,
|
7
|
+
NestedStylePairs,
|
8
|
+
UnistylesTheme,
|
9
|
+
CreateStylesFactory,
|
10
|
+
ScreenSize,
|
11
|
+
NestedStyle,
|
12
|
+
RNValue,
|
13
|
+
RNStyle
|
14
|
+
} from './core'
|
6
15
|
export type { ReactNativeStyleSheet } from './breakpoints'
|
16
|
+
export type { ExtractVariantNames } from './variants'
|
17
|
+
export type { UnistylesPlugin } from './plugin'
|
package/src/types/unistyles.ts
CHANGED
@@ -2,17 +2,21 @@ import { CxxUnistylesEventTypes, ScreenOrientation } from '../common'
|
|
2
2
|
import type { UnistylesThemes, UnistylesBreakpoints } from '../global'
|
3
3
|
import type { ScreenSize } from './core'
|
4
4
|
import type { Optional } from './common'
|
5
|
+
import type { UnistylesPlugin } from './plugin'
|
5
6
|
|
6
7
|
export type ColorSchemeName = Optional<'light' | 'dark'>
|
7
8
|
|
8
9
|
export type UnistylesConfig = {
|
9
|
-
adaptiveThemes?: boolean
|
10
|
+
adaptiveThemes?: boolean,
|
11
|
+
experimentalPlugins?: Array<UnistylesPlugin>,
|
12
|
+
initialTheme?: keyof UnistylesThemes
|
10
13
|
}
|
11
14
|
|
12
15
|
export type UnistylesBridge = {
|
13
16
|
// getters
|
14
17
|
screenWidth: number,
|
15
18
|
screenHeight: number,
|
19
|
+
enabledPlugins: Array<string>,
|
16
20
|
hasAdaptiveThemes: boolean,
|
17
21
|
themeName: keyof UnistylesThemes,
|
18
22
|
breakpoint: keyof UnistylesBreakpoints,
|
@@ -23,7 +27,9 @@ export type UnistylesBridge = {
|
|
23
27
|
themes: Array<keyof UnistylesThemes>,
|
24
28
|
useBreakpoints(breakpoints: UnistylesBreakpoints): void,
|
25
29
|
useTheme(name: keyof UnistylesThemes): void,
|
26
|
-
useAdaptiveThemes(enable: boolean): void
|
30
|
+
useAdaptiveThemes(enable: boolean): void,
|
31
|
+
addPlugin(pluginName: string, notify: boolean): void,
|
32
|
+
removePlugin(pluginName: string): void
|
27
33
|
}
|
28
34
|
|
29
35
|
export type UnistylesThemeEvent = {
|
@@ -38,8 +44,12 @@ export type UnistylesMobileLayoutEvent = {
|
|
38
44
|
payload: {
|
39
45
|
screen: ScreenSize,
|
40
46
|
breakpoint: keyof UnistylesBreakpoints,
|
41
|
-
orientation: ScreenOrientation
|
47
|
+
orientation: typeof ScreenOrientation[keyof typeof ScreenOrientation]
|
42
48
|
}
|
43
49
|
}
|
44
50
|
|
45
|
-
export type
|
51
|
+
export type UnistylesPluginEvent = {
|
52
|
+
type: CxxUnistylesEventTypes.Plugin
|
53
|
+
}
|
54
|
+
|
55
|
+
export type UnistylesEvents = UnistylesThemeEvent | UnistylesMobileLayoutEvent | UnistylesPluginEvent
|
@@ -0,0 +1,11 @@
|
|
1
|
+
export type ExtractVariantNames<T> = T extends object
|
2
|
+
? {
|
3
|
+
[K in keyof T]: T[K] extends { variants: infer V }
|
4
|
+
? V extends object
|
5
|
+
? { [VK in keyof V]: VK extends 'default'
|
6
|
+
? never : VK }[keyof V] | ExtractVariantNames<V>
|
7
|
+
: never
|
8
|
+
: ExtractVariantNames<T[K]>
|
9
|
+
}[keyof T]
|
10
|
+
: never
|
11
|
+
|
package/src/useStyles.ts
CHANGED
@@ -1,7 +1,13 @@
|
|
1
1
|
import { useMemo } from 'react'
|
2
2
|
import { StyleSheet } from 'react-native'
|
3
3
|
import { parseStyle, proxifyFunction } from './utils'
|
4
|
-
import type {
|
4
|
+
import type {
|
5
|
+
CreateStylesFactory,
|
6
|
+
CustomNamedStyles,
|
7
|
+
ExtractVariantNames,
|
8
|
+
ReactNativeStyleSheet,
|
9
|
+
UnistylesTheme
|
10
|
+
} from './types'
|
5
11
|
import { useUnistyles } from './hooks'
|
6
12
|
import type { UnistylesBreakpoints } from './global'
|
7
13
|
|
@@ -13,9 +19,9 @@ type ParsedStylesheet<ST extends CustomNamedStyles<ST>> = {
|
|
13
19
|
|
14
20
|
export const useStyles = <ST extends CustomNamedStyles<ST>>(
|
15
21
|
stylesheet?: ST | CreateStylesFactory<ST, UnistylesTheme>,
|
16
|
-
variant?: string
|
22
|
+
variant?: ExtractVariantNames<typeof stylesheet> & string
|
17
23
|
): ParsedStylesheet<ST> => {
|
18
|
-
const { theme, layout } = useUnistyles()
|
24
|
+
const { theme, layout, plugins } = useUnistyles()
|
19
25
|
const { screenSize, breakpoint } = layout
|
20
26
|
|
21
27
|
if (!stylesheet) {
|
@@ -38,15 +44,15 @@ export const useStyles = <ST extends CustomNamedStyles<ST>>(
|
|
38
44
|
if (typeof value === 'function') {
|
39
45
|
return {
|
40
46
|
...acc,
|
41
|
-
[key]: proxifyFunction(value, breakpoint, screenSize, variant)
|
47
|
+
[key]: proxifyFunction(key, value, breakpoint, screenSize, variant)
|
42
48
|
}
|
43
49
|
}
|
44
50
|
|
45
51
|
return StyleSheet.create({
|
46
52
|
...acc,
|
47
|
-
[key]: parseStyle<ST>(style, breakpoint, screenSize, variant)
|
53
|
+
[key]: parseStyle<ST>(key, style, breakpoint, screenSize, variant)
|
48
54
|
})
|
49
|
-
}, {} as ST), [breakpoint, screenSize, parsedStyles, variant]) as ReactNativeStyleSheet<ST>
|
55
|
+
}, {} as ST), [breakpoint, screenSize, parsedStyles, variant, plugins]) as ReactNativeStyleSheet<ST>
|
50
56
|
|
51
57
|
return {
|
52
58
|
theme,
|
package/src/utils/breakpoints.ts
CHANGED
@@ -1,62 +1,10 @@
|
|
1
1
|
import { unistyles } from '../core'
|
2
|
-
import {
|
3
|
-
import type { NestedStyle, NestedStylePairs, RNValue } from '../types'
|
2
|
+
import type { NestedStyle, NestedStylePairs, Optional, RNValue } from '../types'
|
4
3
|
import type { UnistylesBreakpoints } from '../global'
|
5
|
-
import { ScreenOrientation } from '../common'
|
4
|
+
import { ScreenOrientation, isMobile } from '../common'
|
6
5
|
import { getKeyForUnistylesMediaQuery } from './mqParser'
|
7
|
-
import { getKeyForVariant } from './variants'
|
8
6
|
|
9
|
-
export const
|
10
|
-
const sortedPairs = Object
|
11
|
-
.entries(breakpoints)
|
12
|
-
.sort((breakpoint1, breakpoint2) => {
|
13
|
-
const [, value1] = breakpoint1
|
14
|
-
const [, value2] = breakpoint2
|
15
|
-
|
16
|
-
return (value1 as number) - (value2 as number)
|
17
|
-
})
|
18
|
-
|
19
|
-
const sortedBreakpoints = Object.freeze(Object.fromEntries(sortedPairs)) as UnistylesBreakpoints
|
20
|
-
const breakpointValues = Object.values(sortedBreakpoints)
|
21
|
-
const [firstBreakpoint] = breakpointValues
|
22
|
-
|
23
|
-
if (firstBreakpoint !== 0) {
|
24
|
-
throwError('first breakpoint must start with 0')
|
25
|
-
}
|
26
|
-
|
27
|
-
if (breakpointValues.length !== new Set(breakpointValues).size) {
|
28
|
-
throwError('breakpoint values are duplicated')
|
29
|
-
}
|
30
|
-
|
31
|
-
return sortedBreakpoints
|
32
|
-
}
|
33
|
-
|
34
|
-
export const getBreakpointFromScreenWidth = (width: number, breakpointEntries: Array<[keyof UnistylesBreakpoints, UnistylesBreakpoints[keyof UnistylesBreakpoints]]>): keyof UnistylesBreakpoints & string => {
|
35
|
-
const [key] = breakpointEntries
|
36
|
-
.find(([, value], index, otherBreakpoints) => {
|
37
|
-
const minVal = value as number
|
38
|
-
const maxVal = otherBreakpoints[index + 1]?.[1]
|
39
|
-
|
40
|
-
if (!maxVal) {
|
41
|
-
return true
|
42
|
-
}
|
43
|
-
|
44
|
-
return width >= minVal && width < maxVal
|
45
|
-
}) as [keyof UnistylesBreakpoints & string, number]
|
46
|
-
|
47
|
-
return key
|
48
|
-
}
|
49
|
-
|
50
|
-
// todo move it to different file
|
51
|
-
export const getValueForNestedStyle = (value: NestedStyle, variant?: string): RNValue => {
|
52
|
-
// the highest priority is for variants
|
53
|
-
const variantKey = getKeyForVariant(value, variant)
|
54
|
-
|
55
|
-
if (variantKey) {
|
56
|
-
return value.variants[variantKey as string]
|
57
|
-
}
|
58
|
-
|
59
|
-
// then custom media queries
|
7
|
+
export const getValueForBreakpoint = (value: NestedStyle): Optional<RNValue> => {
|
60
8
|
const customMediaQueryKey = getKeyForUnistylesMediaQuery(
|
61
9
|
Object.entries(value) as NestedStylePairs,
|
62
10
|
unistyles.runtime.screen
|
@@ -66,44 +14,31 @@ export const getValueForNestedStyle = (value: NestedStyle, variant?: string): RN
|
|
66
14
|
return value[customMediaQueryKey]
|
67
15
|
}
|
68
16
|
|
69
|
-
|
70
|
-
// check if user defined any breakpoints
|
71
|
-
const hasBreakpoints = unistyles.runtime.sortedBreakpoints.length > 0
|
17
|
+
const hasBreakpoints = unistyles.registry.sortedBreakpointPairs.length > 0
|
72
18
|
|
73
|
-
|
74
|
-
|
75
|
-
return value[
|
76
|
-
unistyles.runtime.orientation === ScreenOrientation.Portrait
|
77
|
-
? Orientation.Portrait
|
78
|
-
: Orientation.Landscape
|
79
|
-
]
|
19
|
+
if (!hasBreakpoints && isMobile && (ScreenOrientation.Landscape in value || ScreenOrientation.Portrait in value)) {
|
20
|
+
return value[unistyles.runtime.orientation]
|
80
21
|
}
|
81
22
|
|
82
|
-
// let's get the current breakpoint
|
83
23
|
const breakpoint = unistyles.runtime.breakpoint
|
84
24
|
|
85
25
|
if (!breakpoint) {
|
86
26
|
return undefined
|
87
27
|
}
|
88
28
|
|
89
|
-
// if user defined breakpoints, then we look for the valid one
|
90
29
|
const directBreakpoint = value[breakpoint]
|
91
30
|
|
92
|
-
// if there is a direct key like 'sm' or 'md', or value for this key exists but its undefined
|
93
31
|
if (directBreakpoint || (breakpoint in value)) {
|
94
32
|
return directBreakpoint
|
95
33
|
}
|
96
34
|
|
97
|
-
|
98
|
-
const
|
99
|
-
const currentBreakpoint = breakpointPairs
|
35
|
+
const breakpointPairs = unistyles.registry.sortedBreakpointPairs
|
36
|
+
const currentBreakpointIndex = breakpointPairs
|
100
37
|
.findIndex(([key]) => key === breakpoint)
|
101
38
|
|
102
39
|
const availableBreakpoints = breakpointPairs
|
103
|
-
.filter(([key], index) => index <
|
40
|
+
.filter(([key], index) => index < currentBreakpointIndex && key in value)
|
104
41
|
.map(([key]) => key)
|
105
42
|
|
106
|
-
return
|
107
|
-
? value[availableBreakpoints[availableBreakpoints.length - 1] as keyof UnistylesBreakpoints & string]
|
108
|
-
: undefined
|
43
|
+
return value[availableBreakpoints[availableBreakpoints.length - 1] as keyof UnistylesBreakpoints & string]
|
109
44
|
}
|
package/src/utils/index.ts
CHANGED
@@ -1,8 +1,5 @@
|
|
1
|
-
export { normalizeStyles } from './normalizeStyles'
|
2
|
-
export * from './normalizer'
|
3
1
|
export { mq, MQSymbol } from './mq'
|
4
2
|
export { getKeyForVariant } from './variants'
|
5
|
-
export { getKeyForUnistylesMediaQuery } from './mqParser'
|
6
|
-
export {
|
3
|
+
export { getKeyForUnistylesMediaQuery, isWithinTheWidthAndHeight, isValidMq, parseMq } from './mqParser'
|
4
|
+
export { getValueForBreakpoint } from './breakpoints'
|
7
5
|
export { proxifyFunction, parseStyle } from './styles'
|
8
|
-
export { isServer, Orientation } from './common'
|
package/src/utils/mq.ts
CHANGED
@@ -78,7 +78,7 @@ const heightHandler = (wMin: Nullable<MQValue> = 0, wMax: MQValue = Infinity) =>
|
|
78
78
|
if (prop === MQProp.height || prop === MQProp.shortH) {
|
79
79
|
return (hMin: MQValue = 0, hMax: MQValue = Infinity) => new Proxy<FinalHandler>({} as FinalHandler, {
|
80
80
|
get: (target, prop, receiver) => {
|
81
|
-
if (prop === Symbol.toPrimitive || MQProp.toString) {
|
81
|
+
if (prop === Symbol.toPrimitive || prop === MQProp.toString) {
|
82
82
|
return () => `:w[${getMQValue(wMin)}, ${getMQValue(wMax)}]:h[${getMQValue(hMin)}, ${getMQValue(hMax)}]`
|
83
83
|
}
|
84
84
|
|
package/src/utils/mqParser.ts
CHANGED
@@ -14,7 +14,7 @@ type UnistylesParsedMq = {
|
|
14
14
|
height?: ParsedMqDimension
|
15
15
|
}
|
16
16
|
|
17
|
-
const parseMq = (mq: string): UnistylesParsedMq => {
|
17
|
+
export const parseMq = (mq: string): UnistylesParsedMq => {
|
18
18
|
const [, width, fromW, toW] = UNISTYLES_WIDTH_REGEX.exec(mq) || []
|
19
19
|
const [, height, fromH, toH] = UNISTYLES_HEIGHT_REGEX.exec(mq) || []
|
20
20
|
|
@@ -32,7 +32,7 @@ const parseMq = (mq: string): UnistylesParsedMq => {
|
|
32
32
|
|
33
33
|
const isUnistylesMq = (mq: string) => IS_UNISTYLES_REGEX.test(mq)
|
34
34
|
|
35
|
-
const isValidMq = (parsedMq: UnistylesParsedMq) => {
|
35
|
+
export const isValidMq = (parsedMq: UnistylesParsedMq) => {
|
36
36
|
const { width, height } = parsedMq
|
37
37
|
|
38
38
|
if (width && height) {
|
@@ -50,7 +50,7 @@ const isValidMq = (parsedMq: UnistylesParsedMq) => {
|
|
50
50
|
return false
|
51
51
|
}
|
52
52
|
|
53
|
-
const isWithinTheWidthAndHeight = (parsedMq: UnistylesParsedMq, screenSize: ScreenSize): boolean => {
|
53
|
+
export const isWithinTheWidthAndHeight = (parsedMq: UnistylesParsedMq, screenSize: ScreenSize): boolean => {
|
54
54
|
const { width, height } = parsedMq
|
55
55
|
|
56
56
|
if (width && height) {
|
@@ -80,7 +80,7 @@ const isWithinTheHeight = (height: UnistylesParsedMq['height'], screenHeight: nu
|
|
80
80
|
return screenHeight >= from && screenHeight <= to
|
81
81
|
}
|
82
82
|
|
83
|
-
export const getKeyForUnistylesMediaQuery = (mediaQueries: NestedStylePairs, screenSize: ScreenSize) => {
|
83
|
+
export const getKeyForUnistylesMediaQuery = (mediaQueries: NestedStylePairs, screenSize: ScreenSize): Optional<string> => {
|
84
84
|
const mq = mediaQueries.find(([key]) => {
|
85
85
|
if (!isUnistylesMq(key as string)) {
|
86
86
|
return false
|