react-native-unistyles 2.0.0-beta.1 → 2.0.0-beta.3

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