react-native-unistyles 3.0.0-nightly-20250329 → 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 (82) 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/serialize.js +1 -1
  24. package/lib/commonjs/server/serialize.js.map +1 -1
  25. package/lib/commonjs/specs/ShadowRegistry/index.js +2 -2
  26. package/lib/commonjs/specs/ShadowRegistry/index.js.map +1 -1
  27. package/lib/commonjs/web/utils/createUnistylesRef.js +28 -0
  28. package/lib/commonjs/web/utils/createUnistylesRef.js.map +1 -0
  29. package/lib/commonjs/web/utils/index.js +11 -0
  30. package/lib/commonjs/web/utils/index.js.map +1 -1
  31. package/lib/module/components/native/Image.js +25 -2
  32. package/lib/module/components/native/Image.js.map +1 -1
  33. package/lib/module/components/native/Image.native.js +6 -0
  34. package/lib/module/components/native/Image.native.js.map +1 -0
  35. package/lib/module/components/native/ImageBackground.js +17 -29
  36. package/lib/module/components/native/ImageBackground.js.map +1 -1
  37. package/lib/module/components/native/Pressable.native.js +7 -4
  38. package/lib/module/components/native/Pressable.native.js.map +1 -1
  39. package/lib/module/core/createUnistylesElement.js +4 -20
  40. package/lib/module/core/createUnistylesElement.js.map +1 -1
  41. package/lib/module/core/createUnistylesElement.native.js +6 -4
  42. package/lib/module/core/createUnistylesElement.native.js.map +1 -1
  43. package/lib/module/core/createUnistylesImageBackground.js +7 -5
  44. package/lib/module/core/createUnistylesImageBackground.js.map +1 -1
  45. package/lib/module/core/passForwardRef.js +3 -5
  46. package/lib/module/core/passForwardRef.js.map +1 -1
  47. package/lib/module/server/serialize.js +1 -1
  48. package/lib/module/server/serialize.js.map +1 -1
  49. package/lib/module/specs/ShadowRegistry/index.js +2 -2
  50. package/lib/module/specs/ShadowRegistry/index.js.map +1 -1
  51. package/lib/module/web/utils/createUnistylesRef.js +23 -0
  52. package/lib/module/web/utils/createUnistylesRef.js.map +1 -0
  53. package/lib/module/web/utils/index.js +1 -0
  54. package/lib/module/web/utils/index.js.map +1 -1
  55. package/lib/typescript/src/components/native/Image.d.ts.map +1 -1
  56. package/lib/typescript/src/components/native/Image.native.d.ts +2 -0
  57. package/lib/typescript/src/components/native/Image.native.d.ts.map +1 -0
  58. package/lib/typescript/src/components/native/ImageBackground.d.ts.map +1 -1
  59. package/lib/typescript/src/components/native/Pressable.native.d.ts.map +1 -1
  60. package/lib/typescript/src/core/createUnistylesElement.d.ts.map +1 -1
  61. package/lib/typescript/src/core/createUnistylesElement.native.d.ts.map +1 -1
  62. package/lib/typescript/src/core/createUnistylesImageBackground.d.ts.map +1 -1
  63. package/lib/typescript/src/core/passForwardRef.d.ts +1 -1
  64. package/lib/typescript/src/core/passForwardRef.d.ts.map +1 -1
  65. package/lib/typescript/src/specs/ShadowRegistry/index.d.ts.map +1 -1
  66. package/lib/typescript/src/web/utils/createUnistylesRef.d.ts +8 -0
  67. package/lib/typescript/src/web/utils/createUnistylesRef.d.ts.map +1 -0
  68. package/lib/typescript/src/web/utils/index.d.ts +1 -0
  69. package/lib/typescript/src/web/utils/index.d.ts.map +1 -1
  70. package/package.json +1 -1
  71. package/src/components/native/Image.native.tsx +4 -0
  72. package/src/components/native/Image.tsx +35 -3
  73. package/src/components/native/ImageBackground.tsx +17 -33
  74. package/src/components/native/Pressable.native.tsx +12 -4
  75. package/src/core/createUnistylesElement.native.tsx +12 -6
  76. package/src/core/createUnistylesElement.tsx +6 -24
  77. package/src/core/createUnistylesImageBackground.tsx +16 -10
  78. package/src/core/passForwardRef.ts +5 -5
  79. package/src/server/serialize.ts +1 -1
  80. package/src/specs/ShadowRegistry/index.ts +1 -2
  81. package/src/web/utils/createUnistylesRef.ts +29 -0
  82. package/src/web/utils/index.ts +1 -0
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../src/web/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAA;AACxB,cAAc,YAAY,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../src/web/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAA;AACxB,cAAc,YAAY,CAAA;AAC1B,cAAc,sBAAsB,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-unistyles",
3
- "version": "3.0.0-nightly-20250329",
3
+ "version": "3.0.0-rc.1",
4
4
  "description": "Level up your React Native StyleSheet",
5
5
  "scripts": {
6
6
  "test": "jest",
@@ -0,0 +1,4 @@
1
+ import { Image as NativeImage } from 'react-native'
2
+ import { createUnistylesElement } from '../../core'
3
+
4
+ export const Image = createUnistylesElement(NativeImage)
@@ -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
  }
@@ -12,7 +12,7 @@ export const serialize = (value: any): string => {
12
12
  }
13
13
 
14
14
  return `{${Object.entries(value)
15
- .map(([key, value]) => `${key}:${serialize(value)}`)
15
+ .map(([key, value]) => `'${key}':${serialize(value)}`)
16
16
  .join(',')}}`
17
17
  default:
18
18
  return JSON.stringify(value)
@@ -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)
@@ -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'