react-native-unistyles 2.0.0-alpha.1 → 2.0.0-alpha.11
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +4 -4
- package/cxx/UnistylesRuntime.cpp +255 -0
- package/cxx/UnistylesRuntime.h +60 -0
- package/ios/UnistylesHelpers.h +4 -0
- package/ios/UnistylesHelpers.mm +20 -0
- package/ios/UnistylesModule.h +10 -0
- package/ios/UnistylesModule.mm +158 -0
- package/lib/commonjs/common.js +26 -0
- package/lib/commonjs/common.js.map +1 -0
- package/lib/commonjs/core/UnistyleRegistry.js +38 -0
- package/lib/commonjs/core/UnistyleRegistry.js.map +1 -0
- package/lib/commonjs/core/Unistyles.js +31 -0
- package/lib/commonjs/core/Unistyles.js.map +1 -0
- package/lib/commonjs/core/UnistylesModule.js +9 -0
- package/lib/commonjs/core/UnistylesModule.js.map +1 -0
- package/lib/commonjs/core/UnistylesRuntime.js +66 -0
- package/lib/commonjs/core/UnistylesRuntime.js.map +1 -0
- package/lib/commonjs/core/index.js +13 -0
- package/lib/commonjs/core/index.js.map +1 -0
- package/lib/commonjs/createStyleSheet.js +14 -0
- package/lib/commonjs/createStyleSheet.js.map +1 -0
- package/lib/commonjs/global.js +2 -0
- package/lib/commonjs/global.js.map +1 -0
- package/lib/commonjs/hooks/index.js +10 -3
- package/lib/commonjs/hooks/index.js.map +1 -1
- package/lib/commonjs/hooks/useInitialTheme.js +17 -0
- package/lib/commonjs/hooks/useInitialTheme.js.map +1 -0
- package/lib/commonjs/hooks/useUnistyles.js +54 -0
- package/lib/commonjs/hooks/useUnistyles.js.map +1 -0
- package/lib/commonjs/index.js +35 -6
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/types/common.js +2 -0
- package/lib/commonjs/types/{mediaQueries.js.map → common.js.map} +1 -1
- package/lib/commonjs/types/index.js +26 -0
- package/lib/commonjs/types/index.js.map +1 -1
- package/lib/commonjs/types/mq.js +6 -0
- package/lib/{module/types/mediaQueries.js.map → commonjs/types/mq.js.map} +1 -1
- package/lib/commonjs/types/normalizer.js +6 -0
- package/lib/commonjs/types/normalizer.js.map +1 -0
- package/lib/commonjs/types/unistyles.js +6 -0
- package/lib/commonjs/types/unistyles.js.map +1 -0
- package/lib/commonjs/types/variants.js +2 -0
- package/lib/commonjs/types/variants.js.map +1 -0
- package/lib/commonjs/useStyles.js +49 -0
- package/lib/commonjs/useStyles.js.map +1 -0
- package/lib/commonjs/utils/breakpoints.js +33 -80
- package/lib/commonjs/utils/breakpoints.js.map +1 -1
- package/lib/commonjs/utils/common.js +10 -3
- package/lib/commonjs/utils/common.js.map +1 -1
- package/lib/commonjs/utils/index.js +46 -17
- package/lib/commonjs/utils/index.js.map +1 -1
- package/lib/commonjs/utils/module.d.js +2 -0
- package/lib/commonjs/utils/module.d.js.map +1 -0
- package/lib/commonjs/utils/mq.js +89 -0
- package/lib/commonjs/utils/mq.js.map +1 -0
- package/lib/commonjs/utils/mqParser.js +86 -0
- package/lib/commonjs/utils/mqParser.js.map +1 -0
- package/lib/commonjs/utils/normalizeStyles.web.js +13 -13
- package/lib/commonjs/utils/normalizeStyles.web.js.map +1 -1
- package/lib/commonjs/utils/normalizer.js +89 -0
- package/lib/commonjs/utils/normalizer.js.map +1 -0
- package/lib/commonjs/utils/styles.js +29 -58
- package/lib/commonjs/utils/styles.js.map +1 -1
- package/lib/commonjs/utils/variants.js +17 -0
- package/lib/commonjs/utils/variants.js.map +1 -0
- package/lib/module/common.js +22 -0
- package/lib/module/common.js.map +1 -0
- package/lib/module/core/UnistyleRegistry.js +31 -0
- package/lib/module/core/UnistyleRegistry.js.map +1 -0
- package/lib/module/core/Unistyles.js +25 -0
- package/lib/module/core/Unistyles.js.map +1 -0
- package/lib/module/core/UnistylesModule.js +3 -0
- package/lib/module/core/UnistylesModule.js.map +1 -0
- package/lib/module/core/UnistylesRuntime.js +59 -0
- package/lib/module/core/UnistylesRuntime.js.map +1 -0
- package/lib/module/core/index.js +2 -0
- package/lib/module/core/index.js.map +1 -0
- package/lib/module/createStyleSheet.js +7 -0
- package/lib/module/createStyleSheet.js.map +1 -0
- package/lib/module/global.js +2 -0
- package/lib/module/global.js.map +1 -0
- package/lib/module/hooks/index.js +2 -1
- package/lib/module/hooks/index.js.map +1 -1
- package/lib/module/hooks/useInitialTheme.js +10 -0
- package/lib/module/hooks/useInitialTheme.js.map +1 -0
- package/lib/module/hooks/useUnistyles.js +47 -0
- package/lib/module/hooks/useUnistyles.js.map +1 -0
- package/lib/module/index.js +13 -2
- package/lib/module/index.js.map +1 -1
- package/lib/module/types/common.js +2 -0
- package/lib/module/types/common.js.map +1 -0
- package/lib/module/types/index.js +2 -1
- package/lib/module/types/index.js.map +1 -1
- package/lib/module/types/mq.js +2 -0
- package/lib/module/types/mq.js.map +1 -0
- package/lib/module/types/normalizer.js +2 -0
- package/lib/module/types/normalizer.js.map +1 -0
- package/lib/module/types/unistyles.js +2 -0
- package/lib/module/types/unistyles.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 +42 -0
- package/lib/module/useStyles.js.map +1 -0
- package/lib/module/utils/breakpoints.js +34 -82
- package/lib/module/utils/breakpoints.js.map +1 -1
- package/lib/module/utils/common.js +9 -1
- package/lib/module/utils/common.js.map +1 -1
- package/lib/module/utils/index.js +5 -1
- package/lib/module/utils/index.js.map +1 -1
- package/lib/module/utils/module.d.js +2 -0
- package/lib/module/utils/module.d.js.map +1 -0
- package/lib/module/utils/mq.js +83 -0
- package/lib/module/utils/mq.js.map +1 -0
- package/lib/module/utils/mqParser.js +79 -0
- package/lib/module/utils/mqParser.js.map +1 -0
- package/lib/module/utils/normalizeStyles.web.js +13 -13
- package/lib/module/utils/normalizeStyles.web.js.map +1 -1
- package/lib/module/utils/normalizer.js +79 -0
- package/lib/module/utils/normalizer.js.map +1 -0
- package/lib/module/utils/styles.js +28 -59
- package/lib/module/utils/styles.js.map +1 -1
- package/lib/module/utils/variants.js +10 -0
- package/lib/module/utils/variants.js.map +1 -0
- package/lib/typescript/src/common.d.ts +17 -0
- package/lib/typescript/src/common.d.ts.map +1 -0
- package/lib/typescript/src/core/UnistyleRegistry.d.ts +15 -0
- package/lib/typescript/src/core/UnistyleRegistry.d.ts.map +1 -0
- package/lib/typescript/src/core/Unistyles.d.ts +13 -0
- package/lib/typescript/src/core/Unistyles.d.ts.map +1 -0
- package/lib/typescript/src/core/UnistylesModule.d.ts +6 -0
- package/lib/typescript/src/core/UnistylesModule.d.ts.map +1 -0
- package/lib/typescript/src/core/UnistylesRuntime.d.ts +24 -0
- package/lib/typescript/src/core/UnistylesRuntime.d.ts.map +1 -0
- package/lib/typescript/src/core/index.d.ts +2 -0
- package/lib/typescript/src/core/index.d.ts.map +1 -0
- package/lib/typescript/src/createStyleSheet.d.ts +3 -0
- package/lib/typescript/src/createStyleSheet.d.ts.map +1 -0
- package/lib/typescript/src/global.d.ts +7 -0
- package/lib/typescript/src/global.d.ts.map +1 -0
- package/lib/typescript/src/hooks/index.d.ts +2 -1
- package/lib/typescript/src/hooks/index.d.ts.map +1 -1
- package/lib/typescript/src/hooks/useInitialTheme.d.ts +3 -0
- package/lib/typescript/src/hooks/useInitialTheme.d.ts.map +1 -0
- package/lib/typescript/src/hooks/useUnistyles.d.ts +12 -0
- package/lib/typescript/src/hooks/useUnistyles.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +14 -2
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/lib/typescript/src/types/breakpoints.d.ts +12 -13
- package/lib/typescript/src/types/breakpoints.d.ts.map +1 -1
- package/lib/typescript/src/types/common.d.ts +3 -0
- package/lib/typescript/src/types/common.d.ts.map +1 -0
- package/lib/typescript/src/types/core.d.ts +28 -18
- package/lib/typescript/src/types/core.d.ts.map +1 -1
- package/lib/typescript/src/types/index.d.ts +7 -2
- package/lib/typescript/src/types/index.d.ts.map +1 -1
- package/lib/typescript/src/types/mq.d.ts +3 -0
- package/lib/typescript/src/types/mq.d.ts.map +1 -0
- package/lib/typescript/src/types/normalizer.d.ts +20 -0
- package/lib/typescript/src/types/normalizer.d.ts.map +1 -0
- package/lib/typescript/src/types/unistyles.d.ts +37 -0
- package/lib/typescript/src/types/unistyles.d.ts.map +1 -0
- 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 +10 -0
- package/lib/typescript/src/useStyles.d.ts.map +1 -0
- package/lib/typescript/src/utils/breakpoints.d.ts +5 -62
- package/lib/typescript/src/utils/breakpoints.d.ts.map +1 -1
- package/lib/typescript/src/utils/common.d.ts +9 -1
- package/lib/typescript/src/utils/common.d.ts.map +1 -1
- package/lib/typescript/src/utils/index.d.ts +5 -1
- package/lib/typescript/src/utils/index.d.ts.map +1 -1
- package/lib/typescript/src/utils/mq.d.ts +21 -0
- package/lib/typescript/src/utils/mq.d.ts.map +1 -0
- package/lib/typescript/src/utils/mqParser.d.ts +3 -0
- package/lib/typescript/src/utils/mqParser.d.ts.map +1 -0
- package/lib/typescript/src/utils/normalizeStyles.web.d.ts +4 -1
- package/lib/typescript/src/utils/normalizeStyles.web.d.ts.map +1 -1
- package/lib/typescript/src/utils/normalizer.d.ts +11 -0
- package/lib/typescript/src/utils/normalizer.d.ts.map +1 -0
- package/lib/typescript/src/utils/styles.d.ts +5 -49
- package/lib/typescript/src/utils/styles.d.ts.map +1 -1
- package/lib/typescript/src/utils/variants.d.ts +3 -0
- package/lib/typescript/src/utils/variants.d.ts.map +1 -0
- package/package.json +33 -15
- package/react-native-unistyles.podspec +22 -0
- package/src/common.ts +19 -0
- package/src/core/UnistyleRegistry.ts +41 -0
- package/src/core/Unistyles.ts +34 -0
- package/src/core/UnistylesModule.ts +7 -0
- package/src/core/UnistylesRuntime.ts +73 -0
- package/src/core/index.ts +1 -0
- package/src/createStyleSheet.ts +9 -0
- package/src/global.ts +5 -0
- package/src/hooks/index.ts +2 -1
- package/src/hooks/useInitialTheme.ts +11 -0
- package/src/hooks/useUnistyles.ts +55 -0
- package/src/index.ts +30 -2
- package/src/types/breakpoints.ts +21 -20
- package/src/types/common.ts +2 -0
- package/src/types/core.ts +36 -24
- package/src/types/index.ts +7 -9
- package/src/types/mq.ts +3 -0
- package/src/types/normalizer.ts +29 -0
- package/src/types/unistyles.ts +45 -0
- package/src/types/variants.ts +11 -0
- package/src/useStyles.ts +62 -0
- package/src/utils/breakpoints.ts +46 -86
- package/src/utils/common.ts +10 -1
- package/src/utils/index.ts +5 -8
- package/src/utils/module.d.ts +3 -0
- package/src/utils/mq.ts +106 -0
- package/src/utils/mqParser.ts +99 -0
- package/src/utils/normalizeStyles.web.ts +21 -42
- package/src/utils/normalizer.ts +99 -0
- package/src/utils/styles.ts +46 -70
- package/src/utils/variants.ts +13 -0
- package/lib/commonjs/UnistylesTheme.js +0 -21
- package/lib/commonjs/UnistylesTheme.js.map +0 -1
- package/lib/commonjs/createUnistyles.js +0 -57
- package/lib/commonjs/createUnistyles.js.map +0 -1
- package/lib/commonjs/hooks/useDimensions.js +0 -10
- package/lib/commonjs/hooks/useDimensions.js.map +0 -1
- package/lib/commonjs/hooks/useDimensions.web.js +0 -31
- package/lib/commonjs/hooks/useDimensions.web.js.map +0 -1
- package/lib/commonjs/types/mediaQueries.js +0 -2
- package/lib/commonjs/utils/mediaQueries.js +0 -189
- package/lib/commonjs/utils/mediaQueries.js.map +0 -1
- package/lib/module/UnistylesTheme.js +0 -12
- package/lib/module/UnistylesTheme.js.map +0 -1
- package/lib/module/createUnistyles.js +0 -50
- package/lib/module/createUnistyles.js.map +0 -1
- package/lib/module/hooks/useDimensions.js +0 -3
- package/lib/module/hooks/useDimensions.js.map +0 -1
- package/lib/module/hooks/useDimensions.web.js +0 -24
- package/lib/module/hooks/useDimensions.web.js.map +0 -1
- package/lib/module/types/mediaQueries.js +0 -2
- package/lib/module/utils/mediaQueries.js +0 -176
- package/lib/module/utils/mediaQueries.js.map +0 -1
- package/lib/typescript/src/UnistylesTheme.d.ts +0 -9
- package/lib/typescript/src/UnistylesTheme.d.ts.map +0 -1
- package/lib/typescript/src/createUnistyles.d.ts +0 -10
- package/lib/typescript/src/createUnistyles.d.ts.map +0 -1
- package/lib/typescript/src/hooks/useDimensions.d.ts +0 -3
- package/lib/typescript/src/hooks/useDimensions.d.ts.map +0 -1
- package/lib/typescript/src/hooks/useDimensions.web.d.ts +0 -3
- package/lib/typescript/src/hooks/useDimensions.web.d.ts.map +0 -1
- package/lib/typescript/src/types/mediaQueries.d.ts +0 -8
- package/lib/typescript/src/types/mediaQueries.d.ts.map +0 -1
- package/lib/typescript/src/utils/mediaQueries.d.ts +0 -130
- package/lib/typescript/src/utils/mediaQueries.d.ts.map +0 -1
- package/src/UnistylesTheme.tsx +0 -17
- package/src/__tests__/createUnistyles.spec.tsx +0 -192
- package/src/createUnistyles.ts +0 -70
- package/src/hooks/useDimensions.ts +0 -4
- package/src/hooks/useDimensions.web.ts +0 -30
- package/src/types/mediaQueries.ts +0 -10
- package/src/utils/mediaQueries.ts +0 -201
package/package.json
CHANGED
@@ -1,9 +1,10 @@
|
|
1
1
|
{
|
2
2
|
"name": "react-native-unistyles",
|
3
|
-
"version": "2.0.0-alpha.
|
3
|
+
"version": "2.0.0-alpha.11",
|
4
4
|
"description": "Level up your React Native StyleSheet",
|
5
5
|
"scripts": {
|
6
6
|
"test": "jest",
|
7
|
+
"test:coverage": "jest --coverage",
|
7
8
|
"tsc": "node_modules/typescript/bin/tsc --noEmit",
|
8
9
|
"lint": "eslint . --ext .ts,.tsx",
|
9
10
|
"prepare": "husky install && bob build",
|
@@ -18,7 +19,19 @@
|
|
18
19
|
"files": [
|
19
20
|
"src",
|
20
21
|
"lib",
|
22
|
+
"ios",
|
23
|
+
"android",
|
24
|
+
"cxx",
|
25
|
+
"react-native-unistyles.podspec",
|
26
|
+
"!lib/typescript/examples",
|
27
|
+
"!ios/build",
|
28
|
+
"!android/build",
|
29
|
+
"!android/gradle",
|
30
|
+
"!android/gradlew",
|
31
|
+
"!android/gradlew.bat",
|
32
|
+
"!android/local.properties",
|
21
33
|
"!**/*.spec.ts",
|
34
|
+
"!**/*.spec.tsx",
|
22
35
|
"!**/.*"
|
23
36
|
],
|
24
37
|
"keywords": [
|
@@ -37,24 +50,24 @@
|
|
37
50
|
"registry": "https://registry.npmjs.org/"
|
38
51
|
},
|
39
52
|
"devDependencies": {
|
40
|
-
"@commitlint/config-conventional": "
|
53
|
+
"@commitlint/config-conventional": "18.1.0",
|
41
54
|
"@react-native/eslint-config": "0.74.0",
|
55
|
+
"@react-native/normalize-colors": "0.74.1",
|
42
56
|
"@release-it/conventional-changelog": "7.0.2",
|
43
57
|
"@testing-library/react-hooks": "8.0.1",
|
44
|
-
"@types/jest": "29.5.
|
45
|
-
"@types/react": "18.2.
|
46
|
-
"@
|
47
|
-
"@typescript-eslint/eslint-plugin": "6.
|
48
|
-
"@typescript-eslint/
|
49
|
-
"
|
50
|
-
"
|
51
|
-
"
|
52
|
-
"eslint": "8.51.0",
|
58
|
+
"@types/jest": "29.5.7",
|
59
|
+
"@types/react": "18.2.34",
|
60
|
+
"@typescript-eslint/eslint-plugin": "6.9.1",
|
61
|
+
"@typescript-eslint/eslint-plugin-tslint": "6.9.1",
|
62
|
+
"@typescript-eslint/parser": "6.9.1",
|
63
|
+
"commitlint": "18.2.0",
|
64
|
+
"concurrently": "8.2.2",
|
65
|
+
"eslint": "8.53.0",
|
53
66
|
"eslint-config-codemask": "1.1.7",
|
54
67
|
"eslint-plugin-functional": "6.0.0",
|
55
|
-
"eslint-plugin-import": "2.
|
68
|
+
"eslint-plugin-import": "2.29.0",
|
56
69
|
"eslint-plugin-jsdoc": "46.8.2",
|
57
|
-
"eslint-plugin-jsx-a11y": "6.
|
70
|
+
"eslint-plugin-jsx-a11y": "6.8.0",
|
58
71
|
"eslint-plugin-nested-if": "1.0.0",
|
59
72
|
"eslint-plugin-no-else": "0.2.2",
|
60
73
|
"eslint-plugin-no-loops": "0.3.0",
|
@@ -72,6 +85,7 @@
|
|
72
85
|
"typescript": "5.2.2"
|
73
86
|
},
|
74
87
|
"peerDependencies": {
|
88
|
+
"@react-native/normalize-colors": "*",
|
75
89
|
"react": "*",
|
76
90
|
"react-native": "*",
|
77
91
|
"react-native-web": "*"
|
@@ -82,7 +96,7 @@
|
|
82
96
|
}
|
83
97
|
},
|
84
98
|
"workspaces": [
|
85
|
-
"
|
99
|
+
"examples/expo",
|
86
100
|
"docs"
|
87
101
|
],
|
88
102
|
"packageManager": "yarn@3.6.1",
|
@@ -92,9 +106,13 @@
|
|
92
106
|
"jest": {
|
93
107
|
"preset": "react-native",
|
94
108
|
"modulePathIgnorePatterns": [
|
95
|
-
"<rootDir>/
|
109
|
+
"<rootDir>/examples/expo/node_modules",
|
110
|
+
"<rootDir>/examples/ssr/node_modules",
|
96
111
|
"<rootDir>/docs/node_modules",
|
97
112
|
"<rootDir>/lib/"
|
113
|
+
],
|
114
|
+
"coverageReporters": [
|
115
|
+
"html"
|
98
116
|
]
|
99
117
|
},
|
100
118
|
"commitlint": {
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require "json"
|
2
|
+
|
3
|
+
package = JSON.parse(File.read(File.join(__dir__, "package.json")))
|
4
|
+
|
5
|
+
Pod::Spec.new do |s|
|
6
|
+
s.name = package["name"]
|
7
|
+
s.version = package["version"]
|
8
|
+
s.summary = package["description"]
|
9
|
+
s.homepage = package["homepage"]
|
10
|
+
s.license = package["license"]
|
11
|
+
s.authors = package["author"]
|
12
|
+
|
13
|
+
s.platforms = { :ios => min_ios_version_supported }
|
14
|
+
s.source = { :git => package["repository"], :tag => "#{s.version}" }
|
15
|
+
|
16
|
+
s.source_files = [
|
17
|
+
"ios/*.{h,mm}",
|
18
|
+
"cxx/*.{h,cpp}"
|
19
|
+
]
|
20
|
+
|
21
|
+
s.dependency "React-Core"
|
22
|
+
end
|
package/src/common.ts
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
export enum CxxUnistylesEventTypes {
|
2
|
+
Theme = 'theme',
|
3
|
+
Layout = 'layout'
|
4
|
+
}
|
5
|
+
|
6
|
+
export enum UnistylesError {
|
7
|
+
RuntimeUnavailable = 'UNISTYLES_ERROR_RUNTIME_UNAVAILABLE',
|
8
|
+
ThemeNotFound = 'UNISTYLES_ERROR_THEME_NOT_FOUND',
|
9
|
+
ThemeNotRegistered = 'UNISTYLES_ERROR_THEME_NOT_REGISTERED',
|
10
|
+
ThemesCannotBeEmpty = 'UNISTYLES_ERROR_THEMES_CANNOT_BE_EMPTY',
|
11
|
+
BreakpointsCannotBeEmpty = 'UNISTYLES_ERROR_BREAKPOINTS_CANNOT_BE_EMPTY',
|
12
|
+
BreakpointsMustStartFromZero = 'UNISTYLES_ERROR_BREAKPOINTS_MUST_START_FROM_ZERO',
|
13
|
+
}
|
14
|
+
|
15
|
+
// todo to string?
|
16
|
+
export enum ScreenOrientation {
|
17
|
+
Portrait = 1,
|
18
|
+
Landscape = 2
|
19
|
+
}
|
@@ -0,0 +1,41 @@
|
|
1
|
+
import type { UnistylesBridge, UnistylesConfig } from '../types'
|
2
|
+
import type { UnistylesBreakpoints, UnistylesThemes } from '../global'
|
3
|
+
|
4
|
+
export class UnistyleRegistry {
|
5
|
+
public config: UnistylesConfig = {}
|
6
|
+
public themeNames: Array<keyof UnistylesThemes> = []
|
7
|
+
public themes: UnistylesThemes = {} as UnistylesThemes
|
8
|
+
public breakpoints: UnistylesBreakpoints = {} as UnistylesBreakpoints
|
9
|
+
public sortedBreakpointPairs: Array<[keyof UnistylesBreakpoints, UnistylesBreakpoints[keyof UnistylesBreakpoints]]> = []
|
10
|
+
|
11
|
+
constructor(private unistylesBridge: UnistylesBridge) {}
|
12
|
+
|
13
|
+
public addThemes = (themes: UnistylesThemes) => {
|
14
|
+
this.themes = themes
|
15
|
+
|
16
|
+
const keys = Object.keys(themes) as Array<keyof UnistylesThemes>
|
17
|
+
|
18
|
+
this.unistylesBridge.themes = keys
|
19
|
+
this.themeNames = keys
|
20
|
+
|
21
|
+
return this
|
22
|
+
}
|
23
|
+
|
24
|
+
public addBreakpoints = (breakpoints: UnistylesBreakpoints) => {
|
25
|
+
this.breakpoints = breakpoints
|
26
|
+
this.unistylesBridge.useBreakpoints(breakpoints)
|
27
|
+
this.sortedBreakpointPairs = this.unistylesBridge.sortedBreakpointPairs
|
28
|
+
|
29
|
+
return this
|
30
|
+
}
|
31
|
+
|
32
|
+
public addConfig = (config: UnistylesConfig) => {
|
33
|
+
this.config = config
|
34
|
+
|
35
|
+
if (config.adaptiveThemes) {
|
36
|
+
this.unistylesBridge.useAdaptiveThemes(config.adaptiveThemes)
|
37
|
+
}
|
38
|
+
|
39
|
+
return this
|
40
|
+
}
|
41
|
+
}
|
@@ -0,0 +1,34 @@
|
|
1
|
+
import { UnistylesModule } from './UnistylesModule'
|
2
|
+
import { UnistylesRuntime } from './UnistylesRuntime'
|
3
|
+
import { UnistyleRegistry } from './UnistyleRegistry'
|
4
|
+
import type { UnistylesBridge } from '../types'
|
5
|
+
import { UnistylesError } from '../common'
|
6
|
+
|
7
|
+
class Unistyles {
|
8
|
+
private _runtime: UnistylesRuntime
|
9
|
+
private _registry: UnistyleRegistry
|
10
|
+
private _bridge: UnistylesBridge
|
11
|
+
|
12
|
+
constructor() {
|
13
|
+
const isInstalled = UnistylesModule?.install() ?? false
|
14
|
+
|
15
|
+
if (!isInstalled) {
|
16
|
+
throw new Error(UnistylesError.RuntimeUnavailable)
|
17
|
+
}
|
18
|
+
|
19
|
+
// @ts-ignore
|
20
|
+
this._bridge = global.__UNISTYLES__ as UnistylesBridge
|
21
|
+
this._registry = new UnistyleRegistry(this._bridge)
|
22
|
+
this._runtime = new UnistylesRuntime(this._bridge, this._registry)
|
23
|
+
}
|
24
|
+
|
25
|
+
public get registry() {
|
26
|
+
return this._registry
|
27
|
+
}
|
28
|
+
|
29
|
+
public get runtime() {
|
30
|
+
return this._runtime
|
31
|
+
}
|
32
|
+
}
|
33
|
+
|
34
|
+
export const unistyles = new Unistyles()
|
@@ -0,0 +1,73 @@
|
|
1
|
+
import { ScreenOrientation, UnistylesError } from '../common'
|
2
|
+
import type { UnistylesBridge } from '../types'
|
3
|
+
import type { UnistylesThemes } from '../global'
|
4
|
+
import type { UnistyleRegistry } from './UnistyleRegistry'
|
5
|
+
|
6
|
+
export class UnistylesRuntime {
|
7
|
+
constructor(private unistylesBridge: UnistylesBridge, private registry: UnistyleRegistry) {}
|
8
|
+
|
9
|
+
public get colorScheme() {
|
10
|
+
return this.unistylesBridge.colorScheme
|
11
|
+
}
|
12
|
+
|
13
|
+
public get hasAdaptiveThemes() {
|
14
|
+
return this.unistylesBridge.hasAdaptiveThemes
|
15
|
+
}
|
16
|
+
|
17
|
+
public get sortedBreakpoints() {
|
18
|
+
return this.registry.sortedBreakpointPairs
|
19
|
+
}
|
20
|
+
|
21
|
+
public get themeName() {
|
22
|
+
return this.unistylesBridge.themeName
|
23
|
+
}
|
24
|
+
|
25
|
+
public get breakpoint() {
|
26
|
+
return this.unistylesBridge.breakpoint
|
27
|
+
}
|
28
|
+
|
29
|
+
public get screen() {
|
30
|
+
return {
|
31
|
+
width: this.unistylesBridge.screenWidth,
|
32
|
+
height: this.unistylesBridge.screenHeight
|
33
|
+
}
|
34
|
+
}
|
35
|
+
|
36
|
+
public get orientation() {
|
37
|
+
const { width, height } = this.screen
|
38
|
+
|
39
|
+
if (width > height) {
|
40
|
+
return ScreenOrientation.Landscape
|
41
|
+
}
|
42
|
+
|
43
|
+
return ScreenOrientation.Portrait
|
44
|
+
}
|
45
|
+
|
46
|
+
public setTheme = (name: keyof UnistylesThemes) => {
|
47
|
+
if (this.hasTheme(name)) {
|
48
|
+
this.unistylesBridge.useTheme(name)
|
49
|
+
|
50
|
+
return true
|
51
|
+
}
|
52
|
+
|
53
|
+
throw new Error(UnistylesError.ThemeNotRegistered)
|
54
|
+
}
|
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
|
+
public setAdaptiveThemes = (enable: boolean) => {
|
69
|
+
this.unistylesBridge.useAdaptiveThemes(enable)
|
70
|
+
}
|
71
|
+
|
72
|
+
private hasTheme = (name: keyof UnistylesThemes) => name in this.registry.themes
|
73
|
+
}
|
@@ -0,0 +1 @@
|
|
1
|
+
export { unistyles } from './Unistyles'
|
@@ -0,0 +1,9 @@
|
|
1
|
+
import type { CustomNamedStyles, UnistylesTheme } from './types'
|
2
|
+
|
3
|
+
export const createStyleSheet = <S, X>(styles: S | CustomNamedStyles<S> | X | ((theme: UnistylesTheme) => X | CustomNamedStyles<X>)): S | X => {
|
4
|
+
if (typeof styles === 'function') {
|
5
|
+
return styles as X
|
6
|
+
}
|
7
|
+
|
8
|
+
return styles as S
|
9
|
+
}
|
package/src/global.ts
ADDED
package/src/hooks/index.ts
CHANGED
@@ -1 +1,2 @@
|
|
1
|
-
export {
|
1
|
+
export { useInitialTheme } from './useInitialTheme'
|
2
|
+
export { useUnistyles } from './useUnistyles'
|
@@ -0,0 +1,11 @@
|
|
1
|
+
import { useMemo } from 'react'
|
2
|
+
import { unistyles } from '../core'
|
3
|
+
import type { UnistylesThemes } from '../global'
|
4
|
+
|
5
|
+
export const useInitialTheme = (forName: keyof UnistylesThemes) => {
|
6
|
+
useMemo(() => {
|
7
|
+
if (!unistyles.runtime.themeName) {
|
8
|
+
unistyles.runtime.setTheme(forName)
|
9
|
+
}
|
10
|
+
}, [])
|
11
|
+
}
|
@@ -0,0 +1,55 @@
|
|
1
|
+
import { NativeEventEmitter, NativeModules } from 'react-native'
|
2
|
+
import { useEffect, useState } from 'react'
|
3
|
+
import { unistyles } from '../core'
|
4
|
+
import { CxxUnistylesEventTypes } from '../common'
|
5
|
+
import type { UnistylesEvents, UnistylesMobileLayoutEvent, UnistylesThemeEvent } from '../types'
|
6
|
+
|
7
|
+
const unistylesEvents = new NativeEventEmitter(NativeModules.Unistyles)
|
8
|
+
|
9
|
+
export const useUnistyles = () => {
|
10
|
+
const [theme, setTheme] = useState(unistyles.runtime.getTheme(unistyles.runtime.themeName))
|
11
|
+
const [layout, setLayout] = useState({
|
12
|
+
breakpoint: unistyles.runtime.breakpoint,
|
13
|
+
orientation: unistyles.runtime.orientation,
|
14
|
+
screenSize: {
|
15
|
+
width: unistyles.runtime.screen.width,
|
16
|
+
height: unistyles.runtime.screen.height
|
17
|
+
}
|
18
|
+
})
|
19
|
+
|
20
|
+
useEffect(() => {
|
21
|
+
const subscription = unistylesEvents.addListener(
|
22
|
+
'onChange',
|
23
|
+
(event: UnistylesEvents) => {
|
24
|
+
switch (event.type) {
|
25
|
+
case CxxUnistylesEventTypes.Theme: {
|
26
|
+
const themeEvent = event as UnistylesThemeEvent
|
27
|
+
|
28
|
+
return setTheme(unistyles.runtime.getTheme(themeEvent.payload.themeName))
|
29
|
+
}
|
30
|
+
case CxxUnistylesEventTypes.Layout: {
|
31
|
+
const layoutEvent = event as UnistylesMobileLayoutEvent
|
32
|
+
|
33
|
+
return setLayout({
|
34
|
+
breakpoint: layoutEvent.payload.breakpoint,
|
35
|
+
orientation: layoutEvent.payload.orientation,
|
36
|
+
screenSize: {
|
37
|
+
width: layoutEvent.payload.screen.width,
|
38
|
+
height: layoutEvent.payload.screen.height
|
39
|
+
}
|
40
|
+
})
|
41
|
+
}
|
42
|
+
default:
|
43
|
+
return
|
44
|
+
}
|
45
|
+
}
|
46
|
+
)
|
47
|
+
|
48
|
+
return subscription.remove
|
49
|
+
}, [])
|
50
|
+
|
51
|
+
return {
|
52
|
+
theme,
|
53
|
+
layout
|
54
|
+
}
|
55
|
+
}
|
package/src/index.ts
CHANGED
@@ -1,2 +1,30 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
import { unistyles } from './core'
|
2
|
+
import { mq } from './utils'
|
3
|
+
import { useInitialTheme } from './hooks'
|
4
|
+
import type { UnistylesThemes, UnistylesBreakpoints } from './global'
|
5
|
+
import { ScreenOrientation } from './common'
|
6
|
+
import { useStyles } from './useStyles'
|
7
|
+
import { createStyleSheet } from './createStyleSheet'
|
8
|
+
|
9
|
+
const UnistylesRegistry = {
|
10
|
+
addThemes: unistyles.registry.addThemes,
|
11
|
+
addBreakpoints: unistyles.registry.addBreakpoints,
|
12
|
+
addConfig: unistyles.registry.addConfig
|
13
|
+
}
|
14
|
+
|
15
|
+
const UnistylesRuntime = unistyles.runtime
|
16
|
+
|
17
|
+
export {
|
18
|
+
mq,
|
19
|
+
useStyles,
|
20
|
+
useInitialTheme,
|
21
|
+
createStyleSheet,
|
22
|
+
ScreenOrientation,
|
23
|
+
UnistylesRegistry,
|
24
|
+
UnistylesRuntime
|
25
|
+
}
|
26
|
+
|
27
|
+
export type {
|
28
|
+
UnistylesThemes,
|
29
|
+
UnistylesBreakpoints
|
30
|
+
}
|
package/src/types/breakpoints.ts
CHANGED
@@ -1,33 +1,34 @@
|
|
1
|
-
import type {
|
2
|
-
|
3
|
-
|
4
|
-
export type SortedBreakpointEntries<B extends Breakpoints> = [[keyof B & string, number]]
|
5
|
-
|
6
|
-
export type ScreenSize = {
|
7
|
-
width: number,
|
8
|
-
height: number
|
9
|
-
}
|
10
|
-
|
11
|
-
export type CreateStylesFactory<ST, Theme> = (theme: Theme) => ST
|
1
|
+
import type { OpaqueColorValue } from 'react-native'
|
2
|
+
import type { UnistylesBreakpoints } from '../global'
|
3
|
+
import type { MediaQuery } from './mq'
|
12
4
|
|
13
5
|
type WithEmptyObject<V> = keyof V extends never ? {} : V
|
14
6
|
|
15
|
-
|
7
|
+
type ExtractBreakpoints<T> = T extends Partial<Record<keyof UnistylesBreakpoints & string, infer V>>
|
16
8
|
? WithEmptyObject<V>
|
17
9
|
: T extends (...args: infer A) => infer R
|
18
|
-
? (...args: A) => ExtractBreakpoints<R
|
10
|
+
? (...args: A) => ExtractBreakpoints<R>
|
19
11
|
: {
|
20
12
|
[K in keyof T]: T[K] extends (...args: infer A) => infer R
|
21
|
-
? (...args: A) => ExtractBreakpoints<R
|
13
|
+
? (...args: A) => ExtractBreakpoints<R>
|
22
14
|
: T[K] extends object
|
23
|
-
? ExtractBreakpoints<T[K]
|
15
|
+
? ExtractBreakpoints<T[K]>
|
24
16
|
: T[K]
|
25
17
|
}
|
26
18
|
|
27
|
-
|
28
|
-
? (
|
19
|
+
type UnionToIntersection<U> =
|
20
|
+
(U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never
|
21
|
+
|
22
|
+
type RemoveKeysWithPrefix<T> = T extends (...args: Array<any>) => infer R
|
23
|
+
? (...args: Parameters<T>) => RemoveKeysWithPrefix<R>
|
29
24
|
: T extends object
|
30
|
-
? T extends
|
31
|
-
?
|
32
|
-
:
|
25
|
+
? T extends OpaqueColorValue
|
26
|
+
? string
|
27
|
+
: T extends Record<string, infer _V>
|
28
|
+
? T extends { variants: infer _V }
|
29
|
+
? Omit<T, 'variants'> & UnionToIntersection<_V[keyof _V]>
|
30
|
+
: { [K in keyof T as K extends MediaQuery ? keyof UnistylesBreakpoints & string : K]: RemoveKeysWithPrefix<T[K]> }
|
31
|
+
: { [K in keyof T]: RemoveKeysWithPrefix<T[K]> }
|
33
32
|
: T
|
33
|
+
|
34
|
+
export type ReactNativeStyleSheet<T> = ExtractBreakpoints<RemoveKeysWithPrefix<T>>
|
package/src/types/core.ts
CHANGED
@@ -14,14 +14,19 @@ import type {
|
|
14
14
|
TranslateYTransform
|
15
15
|
} from 'react-native/Libraries/StyleSheet/StyleSheetTypes'
|
16
16
|
import type { ImageStyle, TextStyle, ViewStyle } from 'react-native'
|
17
|
-
import type {
|
18
|
-
import type {
|
17
|
+
import type { UnistylesBreakpoints, UnistylesThemes } from '../global'
|
18
|
+
import type { MediaQuery } from './mq'
|
19
19
|
|
20
20
|
type ShadowOffset = {
|
21
21
|
width: number,
|
22
22
|
height: number
|
23
23
|
}
|
24
24
|
|
25
|
+
export type ScreenSize = {
|
26
|
+
width: number,
|
27
|
+
height: number
|
28
|
+
}
|
29
|
+
|
25
30
|
type TransformStyles =
|
26
31
|
& PerpectiveTransform
|
27
32
|
& RotateTransform
|
@@ -37,37 +42,44 @@ type TransformStyles =
|
|
37
42
|
& SkewYTransform
|
38
43
|
& MatrixTransform
|
39
44
|
|
40
|
-
type UnistyleNested
|
41
|
-
shadowOffset?: DeepUniStyle<ShadowOffset
|
42
|
-
textShadowOffset?: DeepUniStyle<ShadowOffset
|
43
|
-
transform?: Array<DeepUniStyle<TransformStyles
|
45
|
+
type UnistyleNested = {
|
46
|
+
shadowOffset?: DeepUniStyle<ShadowOffset>,
|
47
|
+
textShadowOffset?: DeepUniStyle<ShadowOffset>,
|
48
|
+
transform?: Array<DeepUniStyle<TransformStyles>>,
|
49
|
+
variants?: Record<string, UnistyleView | UnistyleText | UnistyleImage & Omit<UnistyleNested, 'variants'>>
|
44
50
|
}
|
45
51
|
|
46
|
-
type UniStyle<V
|
47
|
-
[innerKey in keyof
|
52
|
+
type UniStyle<V> = {
|
53
|
+
[innerKey in keyof UnistylesBreakpoints]?: V
|
48
54
|
} | {
|
49
|
-
[innerKey in
|
50
|
-
}
|
55
|
+
[innerKey in MediaQuery]: V
|
56
|
+
}
|
51
57
|
|
52
|
-
type DeepUniStyle<T
|
53
|
-
[K in keyof T]?: UniStyle<T[K]
|
58
|
+
type DeepUniStyle<T> = {
|
59
|
+
[K in keyof T]?: UniStyle<T[K]> | T[K]
|
54
60
|
}
|
55
61
|
|
56
62
|
// these props are treated differently to nest breakpoints and media queries
|
57
63
|
type NestedTypes = 'shadowOffset' | 'transform' | 'textShadowOffset'
|
58
64
|
|
59
|
-
type UnistyleView
|
60
|
-
type UnistyleText
|
61
|
-
type UnistyleImage
|
65
|
+
type UnistyleView = DeepUniStyle<Omit<ViewStyle, NestedTypes>>
|
66
|
+
type UnistyleText = DeepUniStyle<Omit<TextStyle, NestedTypes>>
|
67
|
+
type UnistyleImage = DeepUniStyle<Omit<ImageStyle, NestedTypes>>
|
62
68
|
|
63
|
-
export type StaticStyles
|
64
|
-
| UnistyleView
|
65
|
-
| UnistyleText
|
66
|
-
| UnistyleImage
|
67
|
-
& UnistyleNested
|
69
|
+
export type StaticStyles =
|
70
|
+
| UnistyleView
|
71
|
+
| UnistyleText
|
72
|
+
| UnistyleImage
|
73
|
+
& UnistyleNested
|
68
74
|
|
69
|
-
export type CustomNamedStyles<T
|
70
|
-
[K in keyof T]: T[K] extends (...args: infer A) => StaticStyles
|
71
|
-
? (...args: A) => StaticStyles
|
72
|
-
: StaticStyles
|
75
|
+
export type CustomNamedStyles<T> = {
|
76
|
+
[K in keyof T]: T[K] extends (...args: infer A) => StaticStyles
|
77
|
+
? (...args: A) => StaticStyles
|
78
|
+
: StaticStyles
|
73
79
|
}
|
80
|
+
|
81
|
+
export type RNValue = number | string | undefined
|
82
|
+
export type NestedStyle = Record<keyof UnistylesBreakpoints | MediaQuery, RNValue>
|
83
|
+
export type NestedStylePairs = Array<[keyof UnistylesBreakpoints | MediaQuery, RNValue]>
|
84
|
+
export type UnistylesTheme = UnistylesThemes[keyof UnistylesThemes]
|
85
|
+
export type CreateStylesFactory<ST, Theme> = (theme: Theme) => ST
|
package/src/types/index.ts
CHANGED
@@ -1,9 +1,7 @@
|
|
1
|
-
export
|
2
|
-
export
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
SortedBreakpointEntries
|
9
|
-
} from './breakpoints'
|
1
|
+
export * from './normalizer'
|
2
|
+
export * from './unistyles'
|
3
|
+
export type { Optional, Nullable } from './common'
|
4
|
+
export type { MediaQuery } from './mq'
|
5
|
+
export type { CustomNamedStyles, NestedStylePairs, UnistylesTheme, CreateStylesFactory, ScreenSize, NestedStyle, RNValue } from './core'
|
6
|
+
export type { ReactNativeStyleSheet } from './breakpoints'
|
7
|
+
export type { ExtractVariantNames } from './variants'
|
package/src/types/mq.ts
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
import type { ShadowStyleIOS, TextStyle, TransformsStyle } from 'react-native'
|
2
|
+
|
3
|
+
type TransformArrayElement<T> = T extends Array<infer U> ? U : never
|
4
|
+
type BoxShadow = Required<ShadowStyleIOS>
|
5
|
+
type TextShadow = Required<Pick<TextStyle, 'textShadowColor' | 'textShadowOffset' | 'textShadowRadius'>>
|
6
|
+
type Transforms = Array<TransformArrayElement<TransformsStyle['transform']>>
|
7
|
+
|
8
|
+
type NormalizedBoxShadow = {
|
9
|
+
shadowColor: undefined,
|
10
|
+
shadowOffset: undefined,
|
11
|
+
shadowOpacity: undefined,
|
12
|
+
shadowRadius: undefined,
|
13
|
+
boxShadow?: string
|
14
|
+
}
|
15
|
+
|
16
|
+
type NormalizedTextShadow = {
|
17
|
+
textShadowColor: undefined
|
18
|
+
textShadowOffset: undefined
|
19
|
+
textShadowRadius: undefined,
|
20
|
+
textShadow?: string
|
21
|
+
}
|
22
|
+
|
23
|
+
export type {
|
24
|
+
BoxShadow,
|
25
|
+
TextShadow,
|
26
|
+
Transforms,
|
27
|
+
NormalizedBoxShadow,
|
28
|
+
NormalizedTextShadow
|
29
|
+
}
|