react-native-unistyles 2.0.0-alpha.2 → 2.0.0-alpha.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. package/ios/UnistylesHelpers.h +4 -0
  2. package/ios/UnistylesHelpers.mm +20 -0
  3. package/ios/UnistylesModule.h +2 -2
  4. package/ios/UnistylesModule.mm +83 -38
  5. package/lib/commonjs/UnistyleRegistry.js +15 -7
  6. package/lib/commonjs/UnistyleRegistry.js.map +1 -1
  7. package/lib/commonjs/UnistylesRuntime.js +29 -15
  8. package/lib/commonjs/UnistylesRuntime.js.map +1 -1
  9. package/lib/commonjs/hooks/useDimensions.web.js +7 -5
  10. package/lib/commonjs/hooks/useDimensions.web.js.map +1 -1
  11. package/lib/commonjs/index.js +4 -3
  12. package/lib/commonjs/index.js.map +1 -1
  13. package/lib/commonjs/types/cxx.js +9 -5
  14. package/lib/commonjs/types/cxx.js.map +1 -1
  15. package/lib/commonjs/useInitialTheme.js +1 -1
  16. package/lib/commonjs/useInitialTheme.js.map +1 -1
  17. package/lib/commonjs/useUnistyles.js +5 -7
  18. package/lib/commonjs/useUnistyles.js.map +1 -1
  19. package/lib/commonjs/utils/breakpoints.js.map +1 -1
  20. package/lib/module/UnistyleRegistry.js +15 -7
  21. package/lib/module/UnistyleRegistry.js.map +1 -1
  22. package/lib/module/UnistylesRuntime.js +30 -16
  23. package/lib/module/UnistylesRuntime.js.map +1 -1
  24. package/lib/module/hooks/useDimensions.web.js +8 -6
  25. package/lib/module/hooks/useDimensions.web.js.map +1 -1
  26. package/lib/module/index.js +3 -2
  27. package/lib/module/index.js.map +1 -1
  28. package/lib/module/types/cxx.js +8 -4
  29. package/lib/module/types/cxx.js.map +1 -1
  30. package/lib/module/useInitialTheme.js +2 -2
  31. package/lib/module/useInitialTheme.js.map +1 -1
  32. package/lib/module/useUnistyles.js +5 -7
  33. package/lib/module/useUnistyles.js.map +1 -1
  34. package/lib/module/utils/breakpoints.js.map +1 -1
  35. package/lib/typescript/src/UnistyleRegistry.d.ts +3 -2
  36. package/lib/typescript/src/UnistyleRegistry.d.ts.map +1 -1
  37. package/lib/typescript/src/UnistylesRuntime.d.ts +14 -46
  38. package/lib/typescript/src/UnistylesRuntime.d.ts.map +1 -1
  39. package/lib/typescript/src/global.d.ts +1 -0
  40. package/lib/typescript/src/global.d.ts.map +1 -1
  41. package/lib/typescript/src/hooks/useDimensions.web.d.ts.map +1 -1
  42. package/lib/typescript/src/index.d.ts +3 -2
  43. package/lib/typescript/src/index.d.ts.map +1 -1
  44. package/lib/typescript/src/types/cxx.d.ts +21 -13
  45. package/lib/typescript/src/types/cxx.d.ts.map +1 -1
  46. package/lib/typescript/src/useStyles.d.ts +3 -39
  47. package/lib/typescript/src/useStyles.d.ts.map +1 -1
  48. package/lib/typescript/src/useUnistyles.d.ts +2 -38
  49. package/lib/typescript/src/useUnistyles.d.ts.map +1 -1
  50. package/package.json +12 -13
  51. package/react-native-unistyles.podspec +2 -1
  52. package/src/UnistyleRegistry.ts +19 -8
  53. package/src/UnistylesRuntime.ts +31 -16
  54. package/src/global.ts +3 -1
  55. package/src/hooks/useDimensions.web.ts +9 -6
  56. package/src/index.ts +4 -2
  57. package/src/types/cxx.ts +25 -14
  58. package/src/useInitialTheme.ts +2 -2
  59. package/src/useUnistyles.ts +5 -6
  60. package/src/utils/breakpoints.ts +1 -1
  61. package/ios/UnistylesRuntime.h +0 -30
  62. package/ios/UnistylesRuntime.mm +0 -191
  63. package/lib/typescript/examples/expo/src/App.d.ts +0 -3
  64. package/lib/typescript/examples/expo/src/App.d.ts.map +0 -1
  65. package/lib/typescript/examples/expo/src/examples/Cxx.d.ts +0 -3
  66. package/lib/typescript/examples/expo/src/examples/Cxx.d.ts.map +0 -1
  67. package/lib/typescript/examples/expo/src/examples/index.d.ts +0 -2
  68. package/lib/typescript/examples/expo/src/examples/index.d.ts.map +0 -1
  69. package/lib/typescript/examples/expo/src/index.d.ts +0 -2
  70. package/lib/typescript/examples/expo/src/index.d.ts.map +0 -1
  71. package/lib/typescript/examples/expo/src/styles/breakpoints.d.ts +0 -8
  72. package/lib/typescript/examples/expo/src/styles/breakpoints.d.ts.map +0 -1
  73. package/lib/typescript/examples/expo/src/styles/index.d.ts +0 -16
  74. package/lib/typescript/examples/expo/src/styles/index.d.ts.map +0 -1
  75. package/lib/typescript/examples/expo/src/styles/theme.d.ts +0 -40
  76. package/lib/typescript/examples/expo/src/styles/theme.d.ts.map +0 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-unistyles",
3
- "version": "2.0.0-alpha.2",
3
+ "version": "2.0.0-alpha.4",
4
4
  "description": "Level up your React Native StyleSheet",
5
5
  "scripts": {
6
6
  "test": "jest",
@@ -41,25 +41,24 @@
41
41
  "registry": "https://registry.npmjs.org/"
42
42
  },
43
43
  "devDependencies": {
44
- "@commitlint/config-conventional": "17.8.0",
44
+ "@commitlint/config-conventional": "18.1.0",
45
45
  "@react-native/eslint-config": "0.74.0",
46
46
  "@react-native/normalize-colors": "0.74.1",
47
47
  "@release-it/conventional-changelog": "7.0.2",
48
48
  "@testing-library/react-hooks": "8.0.1",
49
- "@types/jest": "29.5.6",
50
- "@types/react": "18.2.28",
51
- "@types/react-native": "0.72.3",
52
- "@typescript-eslint/eslint-plugin": "6.8.0",
53
- "@typescript-eslint/eslint-plugin-tslint": "6.8.0",
54
- "@typescript-eslint/parser": "6.8.0",
55
- "commitlint": "17.8.0",
56
- "concurrently": "8.2.1",
57
- "eslint": "8.51.0",
49
+ "@types/jest": "29.5.7",
50
+ "@types/react": "18.2.34",
51
+ "@typescript-eslint/eslint-plugin": "6.9.1",
52
+ "@typescript-eslint/eslint-plugin-tslint": "6.9.1",
53
+ "@typescript-eslint/parser": "6.9.1",
54
+ "commitlint": "18.2.0",
55
+ "concurrently": "8.2.2",
56
+ "eslint": "8.53.0",
58
57
  "eslint-config-codemask": "1.1.7",
59
58
  "eslint-plugin-functional": "6.0.0",
60
- "eslint-plugin-import": "2.28.1",
59
+ "eslint-plugin-import": "2.29.0",
61
60
  "eslint-plugin-jsdoc": "46.8.2",
62
- "eslint-plugin-jsx-a11y": "6.7.1",
61
+ "eslint-plugin-jsx-a11y": "6.8.0",
63
62
  "eslint-plugin-nested-if": "1.0.0",
64
63
  "eslint-plugin-no-else": "0.2.2",
65
64
  "eslint-plugin-no-loops": "0.3.0",
@@ -14,7 +14,8 @@ Pod::Spec.new do |s|
14
14
  s.source = { :git => package["repository"], :tag => "#{s.version}" }
15
15
 
16
16
  s.source_files = [
17
- "ios/*.{h,m,mm}"
17
+ "ios/*.{h,mm}",
18
+ "cxx/*.{h,cpp}"
18
19
  ]
19
20
 
20
21
  s.dependency "React-Core"
@@ -2,17 +2,22 @@ import type { UnistylesBridge, UnistylesConfig } from './types'
2
2
  import type { UnistylesThemes, UnistylesBreakpoints } from './global'
3
3
 
4
4
  export class UnistyleRegistry {
5
- public isClosed = false
5
+ public config: UnistylesConfig = {}
6
+ public themeNames: Array<keyof UnistylesThemes> = []
6
7
  public themes: UnistylesThemes = {} as UnistylesThemes
7
8
  public breakpoints: UnistylesBreakpoints = {} as UnistylesBreakpoints
8
9
  public sortedBreakpointPairs: Array<[keyof UnistylesBreakpoints, UnistylesBreakpoints[keyof UnistylesBreakpoints]]> = []
9
- public config: UnistylesConfig = {}
10
10
 
11
11
  constructor(private unistylesBridge: UnistylesBridge) {}
12
12
 
13
13
  public addThemes = (themes: UnistylesThemes) => {
14
14
  this.themes = themes
15
15
 
16
+ const keys = Object.keys(themes) as Array<keyof UnistylesThemes>
17
+
18
+ this.unistylesBridge.themes = keys
19
+ this.themeNames = keys
20
+
16
21
  return this
17
22
  }
18
23
 
@@ -26,14 +31,20 @@ export class UnistyleRegistry {
26
31
  public addConfig = (config: UnistylesConfig) => {
27
32
  this.config = config
28
33
 
29
- if (config.featureFlags && config.featureFlags.length > 0) {
30
- this.unistylesBridge.useFeatureFlags(config.featureFlags)
31
- }
32
-
33
- if (config.colorScheme) {
34
- this.unistylesBridge.useColorScheme(config.colorScheme)
34
+ if (config.adaptiveThemes) {
35
+ this.unistylesBridge.useAdaptiveThemes(config.adaptiveThemes)
35
36
  }
36
37
 
37
38
  return this
38
39
  }
40
+
41
+ public dangerouslyUnregister = () => {
42
+ this.config = {}
43
+ this.themeNames = []
44
+ this.themes = {} as UnistylesThemes
45
+ this.breakpoints = {} as UnistylesBreakpoints
46
+ this.sortedBreakpointPairs = []
47
+
48
+ this.unistylesBridge.unregister()
49
+ }
39
50
  }
@@ -1,6 +1,6 @@
1
1
  import type { UnistylesBridge } from './types'
2
2
  import type { UnistyleRegistry } from './UnistyleRegistry'
3
- import { UnistylesColorScheme, UnistylesError } from './types'
3
+ import { ScreenOrientation, UnistylesError } from './types'
4
4
  import type { UnistylesThemes } from './global'
5
5
 
6
6
  export class UnistylesRuntime {
@@ -10,49 +10,64 @@ export class UnistylesRuntime {
10
10
  return this.unistylesBridge.colorScheme
11
11
  }
12
12
 
13
- public get breakpoints() {
14
- return this.registry.breakpoints
13
+ public get hasAdaptiveThemes() {
14
+ return this.unistylesBridge.hasAdaptiveThemes
15
15
  }
16
16
 
17
17
  public get sortedBreakpoints() {
18
18
  return this.registry.sortedBreakpointPairs
19
19
  }
20
20
 
21
- public get config() {
22
- return this.registry.config
21
+ public get themeName() {
22
+ return this.unistylesBridge.themeName
23
23
  }
24
24
 
25
- public get theme() {
26
- return this.unistylesBridge.theme
25
+ public get breakpoint() {
26
+ return this.unistylesBridge.breakpoint
27
27
  }
28
28
 
29
- public get currentBreakpoint() {
30
- return this.unistylesBridge.breakpoint
29
+ public get screen() {
30
+ return {
31
+ width: this.unistylesBridge.screenWidth,
32
+ height: this.unistylesBridge.screenHeight
33
+ }
31
34
  }
32
35
 
33
- public setColorScheme = (scheme: UnistylesColorScheme) => {
34
- if (scheme !== this.colorScheme) {
35
- this.unistylesBridge.useColorScheme(scheme)
36
+ public get orientation() {
37
+ const { width, height } = this.screen
38
+
39
+ if (width > height) {
40
+ return ScreenOrientation.Landscape
36
41
  }
42
+
43
+ return ScreenOrientation.Portrait
37
44
  }
38
45
 
39
46
  public setTheme = (name: keyof UnistylesThemes) => {
40
- if (name !== this.theme && this.hasTheme(name)) {
47
+ if (name !== this.themeName && this.hasTheme(name)) {
41
48
  this.unistylesBridge.useTheme(name)
42
49
 
43
50
  return true
44
51
  }
45
52
 
46
- return false
53
+ throw new Error(UnistylesError.ThemeNotRegistered)
47
54
  }
48
55
 
49
- public hasTheme = (name: keyof UnistylesThemes) => name in this.registry.themes
50
-
51
56
  public getTheme = (forName: keyof UnistylesThemes) => {
57
+ if (this.registry.themeNames.length === 0) {
58
+ return {} as UnistylesThemes[keyof UnistylesThemes]
59
+ }
60
+
52
61
  if (!this.hasTheme(forName)) {
53
62
  throw new Error(UnistylesError.ThemeNotFound)
54
63
  }
55
64
 
56
65
  return this.registry.themes[forName]
57
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
58
73
  }
package/src/global.ts CHANGED
@@ -1,2 +1,4 @@
1
1
  export interface UnistylesThemes {}
2
- export interface UnistylesBreakpoints {}
2
+ export interface UnistylesBreakpoints {
3
+ default?: 0
4
+ }
@@ -1,13 +1,16 @@
1
- import { useEffect, useRef, useState } from 'react'
1
+ import { useEffect, useLayoutEffect, useRef, useState } from 'react'
2
2
  import type { ScreenSize } from '../types'
3
- import { isServer } from '../utils'
4
3
 
5
4
  export const useDimensions = (): ScreenSize => {
6
5
  const timerRef = useRef<ReturnType<typeof setTimeout>>()
7
- const [screenSize, setScreenSize] = useState<ScreenSize>({
8
- width: isServer ? 0 : window.innerWidth,
9
- height: isServer ? 0 : window.innerHeight
10
- })
6
+ const [screenSize, setScreenSize] = useState<ScreenSize>({} as ScreenSize)
7
+
8
+ useLayoutEffect(() => {
9
+ setScreenSize({
10
+ width: window.innerWidth,
11
+ height: window.innerHeight
12
+ })
13
+ }, [])
11
14
 
12
15
  useEffect(() => {
13
16
  const handleResize = () => {
package/src/index.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { unistyles } from './Unistyles'
2
2
  import type { UnistylesThemes, UnistylesBreakpoints } from './global'
3
- import { UnistylesColorScheme } from './types'
3
+ import { ScreenOrientation } from './types'
4
4
 
5
5
  export { useInitialTheme } from './useInitialTheme'
6
6
 
@@ -15,13 +15,15 @@ const UnistylesRegistry = {
15
15
  addConfig
16
16
  }
17
17
 
18
+ export const __dangerouslyUnregister = unistyles.registry.dangerouslyUnregister
19
+
18
20
  export {
19
21
  UnistylesRuntime,
20
22
  UnistylesRegistry
21
23
  }
22
24
 
23
25
  export {
24
- UnistylesColorScheme
26
+ ScreenOrientation
25
27
  }
26
28
 
27
29
  export type {
package/src/types/cxx.ts CHANGED
@@ -1,28 +1,35 @@
1
1
  import type { UnistylesThemes, UnistylesBreakpoints } from '../global'
2
2
 
3
3
  export type Nullable<T> = T | null
4
- export enum UnistylesColorScheme {
5
- System = 'system',
6
- Manual = 'manual'
7
- }
4
+ export type ColorSchemeName = 'light' | 'dark' | undefined
8
5
 
9
6
  export type UnistylesConfig = {
10
- colorScheme?: UnistylesColorScheme,
11
- featureFlags?: Array<string>
7
+ adaptiveThemes?: boolean
8
+ }
9
+
10
+ export enum ScreenOrientation {
11
+ Portrait = 1,
12
+ Landscape = 2
12
13
  }
13
14
 
14
15
  export type UnistylesBridge = {
15
16
  // getters
16
- theme: keyof UnistylesThemes
17
+ screenWidth: number,
18
+ screenHeight: number,
19
+ hasAdaptiveThemes: boolean,
20
+ themeName: keyof UnistylesThemes,
17
21
  breakpoint: keyof UnistylesBreakpoints,
18
- colorScheme: UnistylesColorScheme,
19
- sortedBreakpointPairs: Array<[keyof UnistylesBreakpoints, UnistylesBreakpoints[keyof UnistylesBreakpoints]]>
22
+ colorScheme: ColorSchemeName,
23
+ sortedBreakpointPairs: Array<[keyof UnistylesBreakpoints, UnistylesBreakpoints[keyof UnistylesBreakpoints]]>,
20
24
 
21
25
  // setters
26
+ themes: Array<keyof UnistylesThemes>,
22
27
  useBreakpoints(breakpoints: UnistylesBreakpoints): void,
23
28
  useTheme(name: keyof UnistylesThemes): void,
24
- useColorScheme(scheme: UnistylesColorScheme): void,
25
- useFeatureFlags(flags: Array<string>): void
29
+ useAdaptiveThemes(enable: boolean): void,
30
+
31
+ // other
32
+ unregister(): void
26
33
  }
27
34
 
28
35
  export enum CxxUnistylesEventTypes {
@@ -34,7 +41,7 @@ export enum CxxUnistylesEventTypes {
34
41
  export type CxxUnistylesThemeEvent = {
35
42
  type: CxxUnistylesEventTypes.Theme,
36
43
  payload: {
37
- currentTheme: keyof UnistylesThemes
44
+ themeName: keyof UnistylesThemes
38
45
  }
39
46
  }
40
47
 
@@ -49,7 +56,7 @@ export type CxxUnistylesSizeEvent = {
49
56
  export type CxxUnistylesBreakpointEvent = {
50
57
  type: CxxUnistylesEventTypes.Breakpoint,
51
58
  payload: {
52
- currentBreakpoint: keyof UnistylesBreakpoints
59
+ breakpoint: keyof UnistylesBreakpoints
53
60
  }
54
61
  }
55
62
 
@@ -57,5 +64,9 @@ export type UnistylesEvents = CxxUnistylesThemeEvent | CxxUnistylesSizeEvent | C
57
64
 
58
65
  export enum UnistylesError {
59
66
  RuntimeUnavailable = 'UNISTYLES_ERROR_RUNTIME_UNAVAILABLE',
60
- ThemeNotFound = 'UNISTYLES_ERROR_THEME_NOT_FOUND'
67
+ ThemeNotFound = 'UNISTYLES_ERROR_THEME_NOT_FOUND',
68
+ ThemeNotRegistered = 'UNISTYLES_ERROR_THEME_NOT_REGISTERED',
69
+ ThemesCannotBeEmpty = 'UNISTYLES_ERROR_THEMES_CANNOT_BE_EMPTY',
70
+ BreakpointsCannotBeEmpty = 'UNISTYLES_ERROR_BREAKPOINTS_CANNOT_BE_EMPTY',
71
+ BreakpointsMustStartFromZero = 'UNISTYLES_ERROR_BREAKPOINTS_MUST_START_FROM_ZERO',
61
72
  }
@@ -1,7 +1,7 @@
1
- import { useRef } from 'react'
1
+ import { useMemo } from 'react'
2
2
  import { unistyles } from './Unistyles'
3
3
  import type { UnistylesThemes } from './global'
4
4
 
5
5
  export const useInitialTheme = (forName: keyof UnistylesThemes) => {
6
- useRef(unistyles.runtime.setTheme(forName))
6
+ useMemo(() => unistyles.runtime.setTheme(forName), [])
7
7
  }
@@ -12,8 +12,8 @@ import { unistyles } from './Unistyles'
12
12
  const unistylesEvents = new NativeEventEmitter(NativeModules.Unistyles)
13
13
 
14
14
  export const useUnistyles = () => {
15
- const [theme, setTheme] = useState(unistyles.runtime.getTheme(unistyles.runtime.theme))
16
- const [breakpoint, setBreakpoint] = useState(unistyles.runtime.currentBreakpoint)
15
+ const [theme, setTheme] = useState(unistyles.runtime.getTheme(unistyles.runtime.themeName))
16
+ const [breakpoint, setBreakpoint] = useState(unistyles.runtime.breakpoint)
17
17
  const [screenSize, setScreenSize] = useState({
18
18
  width: 0,
19
19
  height: 0
@@ -27,10 +27,11 @@ export const useUnistyles = () => {
27
27
  case CxxUnistylesEventTypes.Theme: {
28
28
  const themeEvent = event as CxxUnistylesThemeEvent
29
29
 
30
- setTheme(unistyles.runtime.getTheme(themeEvent.payload.currentTheme))
30
+ setTheme(unistyles.runtime.getTheme(themeEvent.payload.themeName))
31
31
 
32
32
  return
33
33
  }
34
+ // this event is not available on mobile
34
35
  case CxxUnistylesEventTypes.Size: {
35
36
  const sizeEvent = event as CxxUnistylesSizeEvent
36
37
 
@@ -38,15 +39,13 @@ export const useUnistyles = () => {
38
39
  width: sizeEvent.payload.width,
39
40
  height: sizeEvent.payload.height
40
41
  })
41
- // todo
42
- // setBreakpoint(unistyles.runtime.currentBreakpoint)
43
42
 
44
43
  return
45
44
  }
46
45
  case CxxUnistylesEventTypes.Breakpoint: {
47
46
  const breakpointEvent = event as CxxUnistylesBreakpointEvent
48
47
 
49
- setBreakpoint(breakpointEvent.payload.currentBreakpoint)
48
+ setBreakpoint(breakpointEvent.payload.breakpoint)
50
49
 
51
50
  return
52
51
  }
@@ -66,7 +66,7 @@ export const sortAndValidateBreakpoints = (breakpoints: UnistylesBreakpoints): U
66
66
  export const getBreakpointFromScreenWidth = (width: number, breakpointEntries: Array<[keyof UnistylesBreakpoints, UnistylesBreakpoints[keyof UnistylesBreakpoints]]>): keyof UnistylesBreakpoints & string => {
67
67
  const [key] = breakpointEntries
68
68
  .find(([, value], index, otherBreakpoints) => {
69
- const minVal = value
69
+ const minVal = value as number
70
70
  const maxVal = otherBreakpoints[index + 1]?.[1]
71
71
 
72
72
  if (!maxVal) {
@@ -1,30 +0,0 @@
1
- #pragma once
2
-
3
- #import "UnistylesModule.h"
4
- #import <jsi/jsi.h>
5
- #import <vector>
6
-
7
- using namespace facebook;
8
-
9
- class JSI_EXPORT UnistylesRuntime : public jsi::HostObject {
10
- private:
11
- UnistylesEventHandler eventHandler;
12
- float screenWidth;
13
- float screenHeight;
14
-
15
- public:
16
- UnistylesRuntime(UnistylesEventHandler handler, CGFloat screenWidth, CGFloat screenHeight)
17
- : eventHandler(handler), screenWidth(screenWidth), screenHeight(screenHeight) {}
18
-
19
- std::string theme;
20
- std::string breakpoint;
21
- std::string colorScheme;
22
- std::vector<std::string> featureFlags;
23
- std::vector<std::pair<std::string, double>> sortedBreakpointEntries;
24
-
25
- jsi::Value get(jsi::Runtime&, const jsi::PropNameID& name) override;
26
- std::vector<jsi::PropNameID> getPropertyNames(jsi::Runtime& rt) override;
27
-
28
- void handleScreenSizeChangeWithWidth(CGFloat width, CGFloat height);
29
- std::string getBreakpointFromScreenWidth(double width, const std::vector<std::pair<std::string, double>>& sortedBreakpointEntries);
30
- };
@@ -1,191 +0,0 @@
1
- #import "UnistylesRuntime.h"
2
-
3
- std::vector<jsi::PropNameID> UnistylesRuntime::getPropertyNames(jsi::Runtime& rt) {
4
- std::vector<jsi::PropNameID> properties;
5
-
6
- // getters
7
- properties.push_back(jsi::PropNameID::forUtf8(rt, std::string("theme")));
8
- properties.push_back(jsi::PropNameID::forUtf8(rt, std::string("breakpoint")));
9
- properties.push_back(jsi::PropNameID::forUtf8(rt, std::string("colorScheme")));
10
- properties.push_back(jsi::PropNameID::forUtf8(rt, std::string("sortedBreakpointPairs")));
11
-
12
- // setters
13
- properties.push_back(jsi::PropNameID::forUtf8(rt, std::string("useBreakpoints")));
14
- properties.push_back(jsi::PropNameID::forUtf8(rt, std::string("useTheme")));
15
- properties.push_back(jsi::PropNameID::forUtf8(rt, std::string("useColorScheme")));
16
- properties.push_back(jsi::PropNameID::forUtf8(rt, std::string("useFeatureFlags")));
17
-
18
- return properties;
19
- }
20
-
21
-
22
- jsi::Value UnistylesRuntime::get(jsi::Runtime& runtime, const jsi::PropNameID& propNameId) {
23
- std::string propName = propNameId.utf8(runtime);
24
-
25
- if (propName == "theme") {
26
- return !this->theme.empty()
27
- ? jsi::Value(jsi::String::createFromUtf8(runtime, this->theme))
28
- : jsi::Value::undefined();
29
- }
30
-
31
- if (propName == "breakpoint") {
32
- return !this->breakpoint.empty()
33
- ? jsi::Value(jsi::String::createFromUtf8(runtime, this->breakpoint))
34
- : jsi::Value::undefined();
35
- }
36
-
37
- if (propName == "colorScheme") {
38
- return !this->colorScheme.empty()
39
- ? jsi::Value(jsi::String::createFromUtf8(runtime, this->colorScheme))
40
- : jsi::Value::undefined();
41
- }
42
-
43
- if (propName == "sortedBreakpointPairs") {
44
- std::unique_ptr<jsi::Array> sortedBreakpointEntriesArray = std::make_unique<jsi::Array>(runtime, this->sortedBreakpointEntries.size());
45
-
46
- for (size_t i = 0; i < this->sortedBreakpointEntries.size(); ++i) {
47
- std::unique_ptr<jsi::Array> pairArray = std::make_unique<jsi::Array>(runtime, 2);
48
- jsi::String nameValue = jsi::String::createFromUtf8(runtime, this->sortedBreakpointEntries[i].first);
49
-
50
- pairArray->setValueAtIndex(runtime, 0, nameValue);
51
- pairArray->setValueAtIndex(runtime, 1, jsi::Value(this->sortedBreakpointEntries[i].second));
52
- sortedBreakpointEntriesArray->setValueAtIndex(runtime, i, *pairArray);
53
- }
54
-
55
- return jsi::Value(runtime, *sortedBreakpointEntriesArray);
56
- }
57
-
58
- if (propName == "useBreakpoints") {
59
- return jsi::Function::createFromHostFunction(
60
- runtime,
61
- jsi::PropNameID::forAscii(runtime, "useBreakpoints"),
62
- 1,
63
- [this](jsi::Runtime &runtime, const jsi::Value &thisVal, const jsi::Value *arguments, size_t count) -> jsi::Value {
64
- jsi::Object breakpointsObj = arguments[0].asObject(runtime);
65
- jsi::Array propertyNames = breakpointsObj.getPropertyNames(runtime);
66
- std::vector<std::pair<std::string, double>> sortedBreakpointEntriesVec;
67
-
68
- for (size_t i = 0; i < propertyNames.size(runtime); ++i) {
69
- jsi::Value propNameValue = propertyNames.getValueAtIndex(runtime, i);
70
- std::string name = propNameValue.asString(runtime).utf8(runtime);
71
- jsi::PropNameID propNameID = jsi::PropNameID::forUtf8(runtime, name);
72
- jsi::Value value = breakpointsObj.getProperty(runtime, propNameID);
73
-
74
- if (value.isNumber()) {
75
- double breakpointValue = value.asNumber();
76
- sortedBreakpointEntriesVec.push_back(std::make_pair(name, breakpointValue));
77
- }
78
- }
79
-
80
- std::sort(sortedBreakpointEntriesVec.begin(), sortedBreakpointEntriesVec.end(), [](const std::pair<std::string, double>& a, const std::pair<std::string, double>& b) {
81
- return a.second < b.second;
82
- });
83
-
84
- this->sortedBreakpointEntries = sortedBreakpointEntriesVec;
85
-
86
- std::string breakpoint = this->getBreakpointFromScreenWidth(this->screenWidth, sortedBreakpointEntriesVec);
87
-
88
- this->breakpoint = breakpoint;
89
-
90
- return jsi::Value::undefined();
91
- }
92
- );
93
- }
94
-
95
- if (propName == "useTheme") {
96
- return jsi::Function::createFromHostFunction(runtime,
97
- jsi::PropNameID::forAscii(runtime, "useTheme"),
98
- 1,
99
- [this](jsi::Runtime &runtime, const jsi::Value &thisVal, const jsi::Value *arguments, size_t count) -> jsi::Value {
100
- std::string themeName = arguments[0].asString(runtime).utf8(runtime);
101
- NSString *currentTheme = [NSString stringWithUTF8String:themeName.c_str()];
102
-
103
- this->theme = themeName;
104
-
105
- NSDictionary *body = @{
106
- @"type": @"theme",
107
- @"payload": @{
108
- @"currentTheme": currentTheme
109
- }
110
- };
111
- this->eventHandler(body);
112
-
113
- return jsi::Value::undefined();
114
- }
115
- );
116
- }
117
-
118
- if (propName == "useColorScheme") {
119
- return jsi::Function::createFromHostFunction(runtime,
120
- jsi::PropNameID::forAscii(runtime, "useColorScheme"),
121
- 1,
122
- [this](jsi::Runtime &runtime, const jsi::Value &thisVal, const jsi::Value *arguments, size_t count) -> jsi::Value {
123
- std::string colorScheme = arguments[0].asString(runtime).utf8(runtime);
124
-
125
- this->colorScheme = colorScheme;
126
-
127
- return jsi::Value::undefined();
128
- }
129
- );
130
- }
131
-
132
- if (propName == "useFeatureFlags") {
133
- return jsi::Function::createFromHostFunction(runtime,
134
- jsi::PropNameID::forAscii(runtime, "useFeatureFlags"),
135
- 1,
136
- [this](jsi::Runtime &runtime, const jsi::Value &thisVal, const jsi::Value *arguments, size_t count) -> jsi::Value {
137
- jsi::Array featureFlagsArray = arguments[0].asObject(runtime).asArray(runtime);
138
- size_t length = featureFlagsArray.size(runtime);
139
- std::vector<std::string> featureFlags;
140
- featureFlags.reserve(length);
141
-
142
- for (size_t i = 0; i < length; ++i) {
143
- jsi::Value element = featureFlagsArray.getValueAtIndex(runtime, i);
144
-
145
- if (element.isString()) {
146
- std::string featureFlag = element.asString(runtime).utf8(runtime);
147
- featureFlags.push_back(featureFlag);
148
- }
149
- }
150
-
151
- this->featureFlags = featureFlags;
152
-
153
- return jsi::Value::undefined();
154
- }
155
- );
156
- }
157
-
158
- return jsi::Value::undefined();
159
- }
160
-
161
- std::string UnistylesRuntime::getBreakpointFromScreenWidth(double width, const std::vector<std::pair<std::string, double>>& sortedBreakpointEntries) {
162
- for (size_t i = 0; i < sortedBreakpointEntries.size(); ++i) {
163
- const auto& [key, value] = sortedBreakpointEntries[i];
164
- const double maxVal = (i + 1 < sortedBreakpointEntries.size()) ? sortedBreakpointEntries[i + 1].second : std::numeric_limits<double>::infinity();
165
-
166
- if (width >= value && width < maxVal) {
167
- return key;
168
- }
169
- }
170
-
171
- return sortedBreakpointEntries.empty() ? "" : sortedBreakpointEntries.back().first;
172
- }
173
-
174
- void UnistylesRuntime::handleScreenSizeChangeWithWidth(CGFloat width, CGFloat height) {
175
- std::string currentBreakpoint = this->breakpoint;
176
- std::string nextBreakpoint = this->getBreakpointFromScreenWidth(width, this->sortedBreakpointEntries);
177
-
178
- if (currentBreakpoint != nextBreakpoint) {
179
- this->breakpoint = nextBreakpoint;
180
-
181
- NSString *breakpoint = [NSString stringWithUTF8String:nextBreakpoint.c_str()];
182
- NSDictionary *body = @{
183
- @"type": @"breakpoint",
184
- @"payload": @{
185
- @"currentBreakpoint": breakpoint
186
- }
187
- };
188
-
189
- this->eventHandler(body);
190
- }
191
- }
@@ -1,3 +0,0 @@
1
- import React from 'react';
2
- export declare const App: React.FunctionComponent;
3
- //# sourceMappingURL=App.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"App.d.ts","sourceRoot":"","sources":["../../../../../examples/expo/src/App.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAA;AAgBzB,eAAO,MAAM,GAAG,EAAE,KAAK,CAAC,iBAMvB,CAAA"}
@@ -1,3 +0,0 @@
1
- import React from 'react';
2
- export declare const Cxx: () => React.JSX.Element;
3
- //# sourceMappingURL=Cxx.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"Cxx.d.ts","sourceRoot":"","sources":["../../../../../../examples/expo/src/examples/Cxx.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAA;AAIzB,eAAO,MAAM,GAAG,yBA+Cf,CAAA"}
@@ -1,2 +0,0 @@
1
- export { Cxx } from './Cxx';
2
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../../examples/expo/src/examples/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,OAAO,CAAA"}
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../examples/expo/src/index.ts"],"names":[],"mappings":""}
@@ -1,8 +0,0 @@
1
- export declare const breakpoints: {
2
- xs: number;
3
- sm: number;
4
- md: number;
5
- lg: number;
6
- xl: number;
7
- };
8
- //# sourceMappingURL=breakpoints.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"breakpoints.d.ts","sourceRoot":"","sources":["../../../../../../examples/expo/src/styles/breakpoints.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,WAAW;;;;;;CAMvB,CAAA"}