react-native-unistyles 3.0.0-nightly-20250328 → 3.0.0-rc.1

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 (116) hide show
  1. package/README.md +1 -1
  2. package/cxx/core/UnistylesCommitHook.cpp +2 -2
  3. package/cxx/core/UnistylesRegistry.cpp +24 -20
  4. package/cxx/hybridObjects/HybridStyleSheet.cpp +15 -11
  5. package/cxx/parser/Parser.cpp +11 -8
  6. package/cxx/shadowTree/ShadowTrafficController.h +7 -9
  7. package/lib/commonjs/components/native/Image.js +26 -1
  8. package/lib/commonjs/components/native/Image.js.map +1 -1
  9. package/lib/commonjs/components/native/Image.native.js +10 -0
  10. package/lib/commonjs/components/native/Image.native.js.map +1 -0
  11. package/lib/commonjs/components/native/ImageBackground.js +16 -28
  12. package/lib/commonjs/components/native/ImageBackground.js.map +1 -1
  13. package/lib/commonjs/components/native/Pressable.native.js +7 -4
  14. package/lib/commonjs/components/native/Pressable.native.js.map +1 -1
  15. package/lib/commonjs/core/createUnistylesElement.js +3 -19
  16. package/lib/commonjs/core/createUnistylesElement.js.map +1 -1
  17. package/lib/commonjs/core/createUnistylesElement.native.js +6 -4
  18. package/lib/commonjs/core/createUnistylesElement.native.js.map +1 -1
  19. package/lib/commonjs/core/createUnistylesImageBackground.js +7 -5
  20. package/lib/commonjs/core/createUnistylesImageBackground.js.map +1 -1
  21. package/lib/commonjs/core/passForwardRef.js +3 -5
  22. package/lib/commonjs/core/passForwardRef.js.map +1 -1
  23. package/lib/commonjs/server/getServerUnistyles.js +4 -1
  24. package/lib/commonjs/server/getServerUnistyles.js.map +1 -1
  25. package/lib/commonjs/server/serialize.js +24 -0
  26. package/lib/commonjs/server/serialize.js.map +1 -0
  27. package/lib/commonjs/specs/ShadowRegistry/index.js +2 -2
  28. package/lib/commonjs/specs/ShadowRegistry/index.js.map +1 -1
  29. package/lib/commonjs/web/create.js +6 -0
  30. package/lib/commonjs/web/create.js.map +1 -1
  31. package/lib/commonjs/web/css/state.js +5 -2
  32. package/lib/commonjs/web/css/state.js.map +1 -1
  33. package/lib/commonjs/web/registry.js +1 -1
  34. package/lib/commonjs/web/registry.js.map +1 -1
  35. package/lib/commonjs/web/state.js +3 -0
  36. package/lib/commonjs/web/state.js.map +1 -1
  37. package/lib/commonjs/web/utils/createUnistylesRef.js +28 -0
  38. package/lib/commonjs/web/utils/createUnistylesRef.js.map +1 -0
  39. package/lib/commonjs/web/utils/index.js +11 -0
  40. package/lib/commonjs/web/utils/index.js.map +1 -1
  41. package/lib/module/components/native/Image.js +25 -2
  42. package/lib/module/components/native/Image.js.map +1 -1
  43. package/lib/module/components/native/Image.native.js +6 -0
  44. package/lib/module/components/native/Image.native.js.map +1 -0
  45. package/lib/module/components/native/ImageBackground.js +17 -29
  46. package/lib/module/components/native/ImageBackground.js.map +1 -1
  47. package/lib/module/components/native/Pressable.native.js +7 -4
  48. package/lib/module/components/native/Pressable.native.js.map +1 -1
  49. package/lib/module/core/createUnistylesElement.js +4 -20
  50. package/lib/module/core/createUnistylesElement.js.map +1 -1
  51. package/lib/module/core/createUnistylesElement.native.js +6 -4
  52. package/lib/module/core/createUnistylesElement.native.js.map +1 -1
  53. package/lib/module/core/createUnistylesImageBackground.js +7 -5
  54. package/lib/module/core/createUnistylesImageBackground.js.map +1 -1
  55. package/lib/module/core/passForwardRef.js +3 -5
  56. package/lib/module/core/passForwardRef.js.map +1 -1
  57. package/lib/module/server/getServerUnistyles.js +4 -1
  58. package/lib/module/server/getServerUnistyles.js.map +1 -1
  59. package/lib/module/server/serialize.js +19 -0
  60. package/lib/module/server/serialize.js.map +1 -0
  61. package/lib/module/specs/ShadowRegistry/index.js +2 -2
  62. package/lib/module/specs/ShadowRegistry/index.js.map +1 -1
  63. package/lib/module/web/create.js +7 -1
  64. package/lib/module/web/create.js.map +1 -1
  65. package/lib/module/web/css/state.js +5 -2
  66. package/lib/module/web/css/state.js.map +1 -1
  67. package/lib/module/web/registry.js +1 -1
  68. package/lib/module/web/registry.js.map +1 -1
  69. package/lib/module/web/state.js +3 -0
  70. package/lib/module/web/state.js.map +1 -1
  71. package/lib/module/web/utils/createUnistylesRef.js +23 -0
  72. package/lib/module/web/utils/createUnistylesRef.js.map +1 -0
  73. package/lib/module/web/utils/index.js +1 -0
  74. package/lib/module/web/utils/index.js.map +1 -1
  75. package/lib/typescript/src/components/native/Image.d.ts.map +1 -1
  76. package/lib/typescript/src/components/native/Image.native.d.ts +2 -0
  77. package/lib/typescript/src/components/native/Image.native.d.ts.map +1 -0
  78. package/lib/typescript/src/components/native/ImageBackground.d.ts.map +1 -1
  79. package/lib/typescript/src/components/native/Pressable.native.d.ts.map +1 -1
  80. package/lib/typescript/src/core/createUnistylesElement.d.ts.map +1 -1
  81. package/lib/typescript/src/core/createUnistylesElement.native.d.ts.map +1 -1
  82. package/lib/typescript/src/core/createUnistylesImageBackground.d.ts.map +1 -1
  83. package/lib/typescript/src/core/passForwardRef.d.ts +1 -1
  84. package/lib/typescript/src/core/passForwardRef.d.ts.map +1 -1
  85. package/lib/typescript/src/server/getServerUnistyles.d.ts.map +1 -1
  86. package/lib/typescript/src/server/serialize.d.ts +2 -0
  87. package/lib/typescript/src/server/serialize.d.ts.map +1 -0
  88. package/lib/typescript/src/specs/ShadowRegistry/index.d.ts.map +1 -1
  89. package/lib/typescript/src/web/create.d.ts.map +1 -1
  90. package/lib/typescript/src/web/css/state.d.ts +4 -1
  91. package/lib/typescript/src/web/css/state.d.ts.map +1 -1
  92. package/lib/typescript/src/web/registry.d.ts.map +1 -1
  93. package/lib/typescript/src/web/state.d.ts +2 -0
  94. package/lib/typescript/src/web/state.d.ts.map +1 -1
  95. package/lib/typescript/src/web/utils/createUnistylesRef.d.ts +8 -0
  96. package/lib/typescript/src/web/utils/createUnistylesRef.d.ts.map +1 -0
  97. package/lib/typescript/src/web/utils/index.d.ts +1 -0
  98. package/lib/typescript/src/web/utils/index.d.ts.map +1 -1
  99. package/package.json +1 -1
  100. package/src/components/native/Image.native.tsx +4 -0
  101. package/src/components/native/Image.tsx +35 -3
  102. package/src/components/native/ImageBackground.tsx +17 -33
  103. package/src/components/native/Pressable.native.tsx +12 -4
  104. package/src/core/createUnistylesElement.native.tsx +12 -6
  105. package/src/core/createUnistylesElement.tsx +6 -24
  106. package/src/core/createUnistylesImageBackground.tsx +16 -10
  107. package/src/core/passForwardRef.ts +5 -5
  108. package/src/server/getServerUnistyles.tsx +12 -7
  109. package/src/server/serialize.ts +20 -0
  110. package/src/specs/ShadowRegistry/index.ts +1 -2
  111. package/src/web/create.ts +8 -1
  112. package/src/web/css/state.ts +4 -2
  113. package/src/web/registry.ts +4 -2
  114. package/src/web/state.ts +4 -0
  115. package/src/web/utils/createUnistylesRef.ts +29 -0
  116. package/src/web/utils/index.ts +1 -0
@@ -1,4 +1,36 @@
1
- import { Image as NativeImage } from 'react-native'
2
- import { createUnistylesElement } from '../../core'
1
+ import React, { type ComponentProps, forwardRef } from 'react'
2
+ import { type ImageStyle, Image as NativeImage, type StyleProp, type ViewStyle } from 'react-native'
3
+ import { getClassName } from '../../core'
4
+ import { maybeWarnAboutMultipleUnistyles } from '../../core/warn'
5
+ import type { UnistylesValues } from '../../types'
6
+ import { copyComponentProperties } from '../../utils'
7
+ import { createUnistylesRef, keyInObject } from '../../web/utils'
3
8
 
4
- export const Image = createUnistylesElement(NativeImage)
9
+ type Props = ComponentProps<typeof NativeImage> & {
10
+ style?: UnistylesValues
11
+ imageStyle?: UnistylesValues
12
+ }
13
+
14
+ const UnistylesImage = forwardRef<unknown, Props>((props, forwardedRef) => {
15
+ const classNames = getClassName(props.style)
16
+ const ref = createUnistylesRef(classNames, forwardedRef)
17
+ const hasWidthStyle = typeof props.style === 'object' && keyInObject(props.style, 'width')
18
+ const hasHeightStyle = typeof props.style === 'object' && keyInObject(props.style, 'height')
19
+
20
+ maybeWarnAboutMultipleUnistyles(props.style as ViewStyle, 'Image')
21
+
22
+ return (
23
+ <NativeImage
24
+ {...props}
25
+ style={[
26
+ classNames,
27
+ // Clear inline width and height extracted from source
28
+ hasWidthStyle && { width: '' },
29
+ hasHeightStyle && { height: '' }
30
+ ] as StyleProp<ImageStyle>}
31
+ ref={ref}
32
+ />
33
+ )
34
+ })
35
+
36
+ export const Image = copyComponentProperties(NativeImage, UnistylesImage)
@@ -2,10 +2,10 @@ import React from 'react'
2
2
  import { forwardRef } from 'react'
3
3
  import { type ImageStyle, ImageBackground as NativeImageBackground, type StyleProp, type ViewStyle } from 'react-native'
4
4
  import { getClassName } from '../../core'
5
+ import { maybeWarnAboutMultipleUnistyles } from '../../core/warn'
5
6
  import type { UnistylesValues } from '../../types'
6
7
  import { copyComponentProperties } from '../../utils'
7
- import { UnistylesShadowRegistry } from '../../web'
8
- import { isServer } from '../../web/utils'
8
+ import { createUnistylesRef, keyInObject } from '../../web/utils'
9
9
 
10
10
  type Props = {
11
11
  style?: UnistylesValues
@@ -13,44 +13,28 @@ type Props = {
13
13
  }
14
14
 
15
15
  const UnistylesImageBackground = forwardRef<unknown, Props>((props, forwardedRef) => {
16
- let storedRef: NativeImageBackground | null = null
17
- let storedImageRef: NativeImageBackground | null = null
18
16
  const styleClassNames = getClassName(props.style)
19
17
  const imageClassNames = getClassName(props.imageStyle)
18
+ const ref = createUnistylesRef(styleClassNames, forwardedRef)
19
+ const imageRef = createUnistylesRef(imageClassNames)
20
+ const hasWidthStyle = typeof props.imageStyle === 'object' && keyInObject(props.imageStyle, 'width')
21
+ const hasHeightStyle = typeof props.imageStyle === 'object' && keyInObject(props.imageStyle, 'height')
22
+
23
+ maybeWarnAboutMultipleUnistyles(props.style as ViewStyle, 'ImageBackground')
24
+ maybeWarnAboutMultipleUnistyles(props.imageStyle as ViewStyle, 'ImageBackground')
20
25
 
21
26
  return (
22
27
  <NativeImageBackground
23
28
  {...props}
24
29
  style={styleClassNames as StyleProp<ViewStyle>}
25
- imageStyle={imageClassNames as StyleProp<ImageStyle>}
26
- ref={isServer() ? undefined : ref => {
27
- if (!ref) {
28
- // @ts-expect-error hidden from TS
29
- UnistylesShadowRegistry.remove(storedRef, styleClassNames?.hash)
30
- }
31
-
32
- storedRef = ref
33
- // @ts-expect-error hidden from TS
34
- UnistylesShadowRegistry.add(ref, styleClassNames?.hash)
35
-
36
- if (typeof forwardedRef === 'function') {
37
- return forwardedRef(ref)
38
- }
39
-
40
- if (forwardedRef) {
41
- forwardedRef.current = ref
42
- }
43
- }}
44
- imageRef={isServer() ? undefined : ref => {
45
- if (!ref) {
46
- // @ts-expect-error hidden from TS
47
- UnistylesShadowRegistry.remove(storedImageRef, imageClassNames?.hash)
48
- }
49
-
50
- storedImageRef = ref
51
- // @ts-expect-error hidden from TS
52
- UnistylesShadowRegistry.add(ref, imageClassNames?.hash)
53
- }}
30
+ imageStyle={[
31
+ imageClassNames,
32
+ // Clear inline width and height extracted from source
33
+ hasWidthStyle && { width: '' },
34
+ hasHeightStyle && { height: '' }
35
+ ] as StyleProp<ImageStyle>}
36
+ ref={ref}
37
+ imageRef={imageRef}
54
38
  />
55
39
  )
56
40
  })
@@ -50,10 +50,18 @@ export const Pressable = forwardRef<View, PressableProps>(({ variants, style, ..
50
50
  storedRef.current = ref
51
51
  }
52
52
 
53
- // @ts-expect-error - this is hidden from TS
54
- UnistylesShadowRegistry.add(ref, unistyles)
55
-
56
- return passForwardedRef(props, ref, forwardedRef)
53
+ return passForwardedRef(
54
+ ref,
55
+ forwardedRef,
56
+ () => {
57
+ // @ts-expect-error - this is hidden from TS
58
+ UnistylesShadowRegistry.add(ref, unistyles)
59
+ },
60
+ () => {
61
+ // @ts-expect-error - this is hidden from TS
62
+ UnistylesShadowRegistry.remove(ref)
63
+ }
64
+ )
57
65
  }}
58
66
  style={state => {
59
67
  const isPropStyleAFunction = typeof style === 'function'
@@ -38,15 +38,21 @@ export const createUnistylesElement = (Component: any) => {
38
38
  storedRef.current = getNativeRef(Component, ref)
39
39
  }
40
40
 
41
- passForwardedRef(props, ref, forwardedRef)
42
-
43
41
  // @ts-ignore we don't know the type of the component
44
42
  maybeWarnAboutMultipleUnistyles(props.style, Component.displayName)
45
43
 
46
- return () => {
47
- // @ts-ignore
48
- UnistylesShadowRegistry.remove(ref)
49
- }
44
+ return passForwardedRef(
45
+ ref,
46
+ forwardedRef,
47
+ () => {
48
+ // @ts-ignore this is hidden from TS
49
+ UnistylesShadowRegistry.add(ref, props.style)
50
+ },
51
+ () => {
52
+ // @ts-ignore this is hidden from TS
53
+ UnistylesShadowRegistry.remove(ref)
54
+ }
55
+ )
50
56
  }}
51
57
  />
52
58
  )
@@ -1,8 +1,8 @@
1
1
  import React from 'react'
2
+ import type { ViewStyle } from 'react-native'
2
3
  import type { UnistylesValues } from '../types'
3
4
  import { copyComponentProperties } from '../utils'
4
- import { UnistylesShadowRegistry } from '../web'
5
- import { isServer } from '../web/utils'
5
+ import { createUnistylesRef } from '../web/utils'
6
6
  import { getClassName } from './getClassname'
7
7
  import { maybeWarnAboutMultipleUnistyles } from './warn'
8
8
 
@@ -12,34 +12,16 @@ type ComponentProps = {
12
12
 
13
13
  export const createUnistylesElement = (Component: any) => {
14
14
  const UnistylesComponent = React.forwardRef<unknown, ComponentProps>((props, forwardedRef) => {
15
- let storedRef: HTMLElement | null = null
16
15
  const classNames = getClassName(props.style)
16
+ const ref = createUnistylesRef(classNames, forwardedRef)
17
+
18
+ maybeWarnAboutMultipleUnistyles(props.style as ViewStyle, Component.displayName)
17
19
 
18
20
  return (
19
21
  <Component
20
22
  {...props}
21
23
  style={classNames}
22
- ref={isServer() ? undefined : (ref: HTMLElement | null) => {
23
- // @ts-ignore we don't know the type of the component
24
- maybeWarnAboutMultipleUnistyles(props.style, Component.displayName)
25
-
26
- if (!ref) {
27
- // @ts-expect-error hidden from TS
28
- UnistylesShadowRegistry.remove(storedRef, classNames?.hash)
29
- }
30
-
31
- storedRef = ref
32
- // @ts-expect-error hidden from TS
33
- UnistylesShadowRegistry.add(ref, classNames?.hash)
34
-
35
- if (typeof forwardedRef === 'function') {
36
- return forwardedRef(ref)
37
- }
38
-
39
- if (forwardedRef) {
40
- forwardedRef.current = ref
41
- }
42
- }}
24
+ ref={ref}
43
25
  />
44
26
  )
45
27
  })
@@ -27,17 +27,23 @@ export const createUnistylesImageBackground = (Component: typeof ImageBackground
27
27
  <Component
28
28
  {...props}
29
29
  ref={ref => {
30
- passForwardedRef(props, ref, forwardedRef)
31
-
32
- return () => {
33
- // @ts-ignore
34
- UnistylesShadowRegistry.remove(ref)
35
-
36
- if (storedImageRef.current) {
37
- // @ts-ignore
38
- UnistylesShadowRegistry.remove(storedImageRef.current)
30
+ return passForwardedRef(
31
+ ref,
32
+ forwardedRef,
33
+ () => {
34
+ // @ts-expect-error - this is hidden from TS
35
+ UnistylesShadowRegistry.add(ref, props.style)
36
+ },
37
+ () => {
38
+ // @ts-expect-error - this is hidden from TS
39
+ UnistylesShadowRegistry.remove(ref)
40
+
41
+ if (storedImageRef.current) {
42
+ // @ts-expect-error - this is hidden from TS
43
+ UnistylesShadowRegistry.remove(storedImageRef.current)
44
+ }
39
45
  }
40
- }
46
+ )
41
47
  }}
42
48
  imageRef={ref => {
43
49
  if (ref) {
@@ -1,10 +1,10 @@
1
1
  import React from 'react'
2
- import { UnistylesShadowRegistry } from '../specs'
3
2
 
4
3
  export const passForwardedRef = <T>(
5
- props: any,
6
4
  ref: T,
7
- forwardedRef: React.ForwardedRef<T>
5
+ forwardedRef: React.ForwardedRef<T>,
6
+ onMount?: () => void,
7
+ onUnmount?: () => void
8
8
  ) => {
9
9
  const passForwardedRef = () => {
10
10
  if (typeof forwardedRef === 'function') {
@@ -19,10 +19,10 @@ export const passForwardedRef = <T>(
19
19
  }
20
20
  const forwardedRefReturnFn = passForwardedRef()
21
21
 
22
- // @ts-expect-error hidden from TS
23
- UnistylesShadowRegistry.add(ref, props.style)
22
+ onMount?.()
24
23
 
25
24
  return () => {
26
25
  forwardedRefReturnFn?.()
26
+ onUnmount?.()
27
27
  }
28
28
  }
@@ -2,20 +2,25 @@ import React from 'react'
2
2
  import { StyleSheet } from 'react-native'
3
3
  import { UnistylesWeb } from '../web'
4
4
  import { error, isServer } from '../web/utils'
5
+ import { serialize } from './serialize'
5
6
  import { DefaultServerUnistylesSettings, type ServerUnistylesSettings } from './types'
6
7
 
7
8
  export const getServerUnistyles = ({ includeRNWStyles = true }: ServerUnistylesSettings = DefaultServerUnistylesSettings) => {
8
9
  if (!isServer()) {
9
10
  throw error('Server styles should only be read on the server')
10
11
  }
12
+
11
13
  // @ts-ignore
12
14
  const rnwStyle: string | null = includeRNWStyles ? (StyleSheet?.getSheet().textContent ?? '') : null
13
15
  const css = UnistylesWeb.registry.css.getStyles()
14
16
  const state = UnistylesWeb.registry.css.getState()
15
- return <>
16
- {rnwStyle && <style id='rnw-style'>{rnwStyle}</style>}
17
- <style id='unistyles-web'>{css}</style>
18
- {/* biome-ignore lint/security/noDangerouslySetInnerHtml: Needs the json quotes to be unescaped */}
19
- <script id='unistyles-script' dangerouslySetInnerHTML={{ __html: `window.__UNISTYLES_STATE__ = ${JSON.stringify(state)}`}} />
20
- </>
21
- }
17
+
18
+ return (
19
+ <>
20
+ {rnwStyle && <style id='rnw-style'>{rnwStyle}</style>}
21
+ <style id='unistyles-web'>{css}</style>
22
+ {/* biome-ignore lint/security/noDangerouslySetInnerHtml: Needs the json quotes to be unescaped */}
23
+ <script id='unistyles-script' defer dangerouslySetInnerHTML={{ __html: `window.__UNISTYLES_STATE__ = ${serialize(state)}`}} />
24
+ </>
25
+ )
26
+ }
@@ -0,0 +1,20 @@
1
+ export const serialize = (value: any): string => {
2
+ switch (typeof value) {
3
+ case 'function':
4
+ return value.toString()
5
+ case 'object':
6
+ if (Array.isArray(value)) {
7
+ return `[${value.map(serialize).join(',')}]`
8
+ }
9
+
10
+ if (value === null) {
11
+ return 'null'
12
+ }
13
+
14
+ return `{${Object.entries(value)
15
+ .map(([key, value]) => `'${key}':${serialize(value)}`)
16
+ .join(',')}}`
17
+ default:
18
+ return JSON.stringify(value)
19
+ }
20
+ }
@@ -40,9 +40,8 @@ HybridShadowRegistry.add = (handle, styles) => {
40
40
  ? styles.flat()
41
41
  : [styles]
42
42
 
43
- // filter Reanimated styles and styles that are undefined
43
+ // filter styles that are undefined or with no keys
44
44
  const filteredStyles = stylesArray
45
- .filter(style => !style?.initial?.updater)
46
45
  .filter(style => style && Object.keys(style).length > 0)
47
46
  .flat()
48
47
  .filter(Boolean)
package/src/web/create.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type { StyleSheet, StyleSheetWithSuperPowers } from '../types/stylesheet'
2
2
  import { UnistylesWeb } from './index'
3
- import { assignSecrets, error, removeInlineStyles } from './utils'
3
+ import { assignSecrets, error, isServer, removeInlineStyles } from './utils'
4
4
 
5
5
  type Variants = Record<string, string | boolean | undefined>
6
6
 
@@ -9,6 +9,13 @@ export const create = (stylesheet: StyleSheetWithSuperPowers<StyleSheet>, id?: s
9
9
  throw error('Unistyles is not initialized correctly. Please add babel plugin to your babel config.')
10
10
  }
11
11
 
12
+ // For SSR
13
+ if (!UnistylesWeb.state.isInitialized && !isServer()) {
14
+ const config = window?.__UNISTYLES_STATE__?.config
15
+
16
+ config && UnistylesWeb.state.init(config)
17
+ }
18
+
12
19
  const computedStylesheet = UnistylesWeb.registry.getComputedStylesheet(stylesheet)
13
20
  const addSecrets = (value: any, key: string, args = undefined as Array<any> | undefined, variants = {} as Variants) => assignSecrets(value, {
14
21
  __uni__key: key,
@@ -1,5 +1,6 @@
1
1
  import type { UnistylesValues } from '../../types'
2
2
  import { convertUnistyles } from '../convert'
3
+ import type { UnistylesServices } from '../types'
3
4
  import { hyphenate, isServer } from '../utils'
4
5
  import { convertToCSS } from './core'
5
6
 
@@ -33,7 +34,7 @@ export class CSSState {
33
34
  private styleTag: HTMLStyleElement | null = null
34
35
  private CSS = ''
35
36
 
36
- constructor() {
37
+ constructor(private services: UnistylesServices) {
37
38
  if (isServer()) {
38
39
  return
39
40
  }
@@ -159,8 +160,9 @@ export class CSSState {
159
160
 
160
161
  const mainState = getState(this.mainMap)
161
162
  const mqState = getState(this.mqMap)
163
+ const config = this.services.state.getConfig()
162
164
 
163
- return { mainState, mqState }
165
+ return { mainState, mqState, config }
164
166
  }
165
167
 
166
168
  hydrate = ({ mainState, mqState }: ReturnType<typeof this.getState>) => {
@@ -11,9 +11,11 @@ export class UnistylesRegistry {
11
11
  private readonly stylesCounter = new Map<string, Set<HTMLElement>>()
12
12
  private readonly disposeListenersMap = new Map<object, VoidFunction>()
13
13
  private readonly dependenciesMap = new Map<StyleSheetWithSuperPowers<StyleSheet>, Set<UnistyleDependency>>()
14
- readonly css = new CSSState()
14
+ readonly css: CSSState
15
15
 
16
- constructor(private services: UnistylesServices) {}
16
+ constructor(private services: UnistylesServices) {
17
+ this.css = new CSSState(services)
18
+ }
17
19
 
18
20
  getComputedStylesheet = (stylesheet: StyleSheetWithSuperPowers<StyleSheet>, scopedThemeName?: UnistylesTheme) => {
19
21
  if (typeof stylesheet !== 'function') {
package/src/web/state.ts CHANGED
@@ -17,6 +17,7 @@ export class UnistylesState {
17
17
  CSSVars = true
18
18
 
19
19
  private matchingBreakpoints = new Map<string, boolean>()
20
+ private _config: UnistylesConfig = {}
20
21
 
21
22
  get breakpoint() {
22
23
  const [currentBreakpoint] = Array.from(this.matchingBreakpoints)
@@ -37,6 +38,7 @@ export class UnistylesState {
37
38
  return
38
39
  }
39
40
 
41
+ this._config = config
40
42
  this.isInitialized = true
41
43
  this.initThemes(config.themes, config.settings?.CSSVars)
42
44
  this.initBreakpoints(config.breakpoints)
@@ -153,4 +155,6 @@ export class UnistylesState {
153
155
  })
154
156
  })
155
157
  }
158
+
159
+ getConfig = () => this._config
156
160
  }
@@ -0,0 +1,29 @@
1
+ import type React from 'react'
2
+ import type { Nullable } from '../../types'
3
+ import { UnistylesWeb } from '../index'
4
+ import { isServer } from './common'
5
+
6
+ type ClassNames = {
7
+ hash: string
8
+ }
9
+
10
+ export const createUnistylesRef = <T>(classNames?: ClassNames, forwardedRef?: React.ForwardedRef<T>) => {
11
+ const storedRef = { current: null as Nullable<T> }
12
+
13
+ return isServer() ? undefined : (ref: Nullable<T>) => {
14
+ if (!ref) {
15
+ UnistylesWeb.shadowRegistry.remove(storedRef, classNames?.hash)
16
+ }
17
+
18
+ storedRef.current = ref
19
+ UnistylesWeb.shadowRegistry.add(ref, classNames?.hash)
20
+
21
+ if (typeof forwardedRef === 'function') {
22
+ return forwardedRef(ref)
23
+ }
24
+
25
+ if (forwardedRef) {
26
+ forwardedRef.current = ref
27
+ }
28
+ }
29
+ }
@@ -1,2 +1,3 @@
1
1
  export * from './common'
2
2
  export * from './unistyle'
3
+ export * from './createUnistylesRef'