react-native-unistyles 3.0.0-beta.7 → 3.0.0-beta.8

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 (79) hide show
  1. package/README.md +16 -0
  2. package/android/CMakeLists.txt +2 -1
  3. package/android/src/main/java/com/unistyles/NativePlatform+insets.kt +9 -1
  4. package/cxx/common/Helpers.h +85 -0
  5. package/cxx/core/UnistylesCommitHook.cpp +20 -17
  6. package/cxx/core/UnistylesMountHook.cpp +2 -2
  7. package/cxx/core/UnistylesMountHook.h +1 -4
  8. package/cxx/core/UnistylesRegistry.cpp +8 -6
  9. package/cxx/hybridObjects/HybridShadowRegistry.cpp +2 -5
  10. package/cxx/hybridObjects/HybridStyleSheet.cpp +1 -1
  11. package/cxx/hybridObjects/HybridUnistylesRuntime.cpp +5 -1
  12. package/cxx/parser/Parser.cpp +74 -9
  13. package/cxx/shadowTree/ShadowTrafficController.h +10 -4
  14. package/cxx/shadowTree/ShadowTreeManager.cpp +38 -35
  15. package/lib/commonjs/components/native/ImageBackground.js +26 -30
  16. package/lib/commonjs/components/native/ImageBackground.js.map +1 -1
  17. package/lib/commonjs/components/native/Pressable.native.js +12 -1
  18. package/lib/commonjs/components/native/Pressable.native.js.map +1 -1
  19. package/lib/commonjs/core/createUnistylesElement.native.js +4 -0
  20. package/lib/commonjs/core/createUnistylesElement.native.js.map +1 -1
  21. package/lib/commonjs/core/createUnistylesImageBackground.js +4 -0
  22. package/lib/commonjs/core/createUnistylesImageBackground.js.map +1 -1
  23. package/lib/commonjs/core/useProxifiedUnistyles/useProxifiedUnistyles.js +1 -1
  24. package/lib/commonjs/core/useProxifiedUnistyles/useProxifiedUnistyles.js.map +1 -1
  25. package/lib/commonjs/core/withUnistyles/withUnistyles.native.js +14 -7
  26. package/lib/commonjs/core/withUnistyles/withUnistyles.native.js.map +1 -1
  27. package/lib/commonjs/specs/ShadowRegistry/index.js +1 -1
  28. package/lib/commonjs/specs/ShadowRegistry/index.js.map +1 -1
  29. package/lib/module/components/native/ImageBackground.js +26 -30
  30. package/lib/module/components/native/ImageBackground.js.map +1 -1
  31. package/lib/module/components/native/Pressable.native.js +12 -1
  32. package/lib/module/components/native/Pressable.native.js.map +1 -1
  33. package/lib/module/core/createUnistylesElement.native.js +4 -0
  34. package/lib/module/core/createUnistylesElement.native.js.map +1 -1
  35. package/lib/module/core/createUnistylesImageBackground.js +4 -0
  36. package/lib/module/core/createUnistylesImageBackground.js.map +1 -1
  37. package/lib/module/core/useProxifiedUnistyles/useProxifiedUnistyles.js +1 -1
  38. package/lib/module/core/useProxifiedUnistyles/useProxifiedUnistyles.js.map +1 -1
  39. package/lib/module/core/withUnistyles/withUnistyles.native.js +14 -7
  40. package/lib/module/core/withUnistyles/withUnistyles.native.js.map +1 -1
  41. package/lib/module/specs/ShadowRegistry/index.js +1 -1
  42. package/lib/module/specs/ShadowRegistry/index.js.map +1 -1
  43. package/lib/typescript/src/components/native/ImageBackground.d.ts.map +1 -1
  44. package/lib/typescript/src/components/native/Pressable.native.d.ts.map +1 -1
  45. package/lib/typescript/src/core/createUnistylesElement.native.d.ts.map +1 -1
  46. package/lib/typescript/src/core/createUnistylesImageBackground.d.ts.map +1 -1
  47. package/lib/typescript/src/core/withUnistyles/withUnistyles.native.d.ts.map +1 -1
  48. package/lib/typescript/src/specs/ShadowRegistry/index.d.ts.map +1 -1
  49. package/lib/typescript/src/types/stylesheet.d.ts +1 -1
  50. package/lib/typescript/src/types/stylesheet.d.ts.map +1 -1
  51. package/nitrogen/generated/android/c++/JFunc_void_UnistylesNativeMiniRuntime.hpp +2 -2
  52. package/nitrogen/generated/android/c++/JFunc_void_std__vector_UnistyleDependency__UnistylesNativeMiniRuntime.hpp +2 -2
  53. package/nitrogen/generated/android/c++/JHybridNativePlatformSpec.cpp +19 -19
  54. package/nitrogen/generated/android/kotlin/com/margelo/nitro/unistyles/Func_void_UnistylesNativeMiniRuntime.kt +7 -2
  55. package/nitrogen/generated/android/kotlin/com/margelo/nitro/unistyles/Func_void_std__vector_UnistyleDependency__UnistylesNativeMiniRuntime.kt +7 -2
  56. package/nitrogen/generated/android/kotlin/com/margelo/nitro/unistyles/HybridNativePlatformSpec.kt +22 -22
  57. package/nitrogen/generated/android/unistyles+autolinking.cmake +16 -0
  58. package/nitrogen/generated/ios/Unistyles-Swift-Cxx-Umbrella.hpp +0 -1
  59. package/nitrogen/generated/shared/c++/Dimensions.hpp +1 -0
  60. package/nitrogen/generated/shared/c++/Insets.hpp +1 -0
  61. package/nitrogen/generated/shared/c++/UnistylesCxxMiniRuntime.hpp +1 -0
  62. package/nitrogen/generated/shared/c++/UnistylesNativeMiniRuntime.hpp +1 -0
  63. package/package.json +28 -18
  64. package/plugin/index.d.ts +51 -18
  65. package/plugin/index.js +791 -178
  66. package/src/components/native/ImageBackground.tsx +3 -4
  67. package/src/components/native/Pressable.native.tsx +16 -1
  68. package/src/core/createUnistylesElement.native.tsx +5 -0
  69. package/src/core/createUnistylesImageBackground.tsx +5 -0
  70. package/src/core/useProxifiedUnistyles/useProxifiedUnistyles.ts +1 -1
  71. package/src/core/withUnistyles/withUnistyles.native.tsx +40 -14
  72. package/src/specs/ShadowRegistry/index.ts +1 -0
  73. package/src/types/stylesheet.ts +1 -1
  74. package/plugin/consts.js +0 -63
  75. package/plugin/exotic.js +0 -54
  76. package/plugin/import.js +0 -51
  77. package/plugin/ref.js +0 -11
  78. package/plugin/stylesheet.js +0 -565
  79. package/plugin/variants.js +0 -66
@@ -1,6 +1,6 @@
1
1
  import React from 'react'
2
2
  import { forwardRef } from 'react'
3
- import { ImageBackground as NativeImageBackground } from 'react-native'
3
+ import { type ImageStyle, ImageBackground as NativeImageBackground, type StyleProp, type ViewStyle } from 'react-native'
4
4
  import { getClassName } from '../../core'
5
5
  import type { UnistylesValues } from '../../types'
6
6
  import { copyComponentProperties } from '../../utils'
@@ -19,11 +19,10 @@ const UnistylesImageBackground = forwardRef<unknown, Props>((props, forwardedRef
19
19
  const imageClassNames = getClassName(props.imageStyle)
20
20
 
21
21
  return (
22
- // @ts-expect-error - RN types are not compatible with RNW styles
23
22
  <NativeImageBackground
24
23
  {...props}
25
- style={styleClassNames}
26
- imageStyle={imageClassNames}
24
+ style={styleClassNames as StyleProp<ViewStyle>}
25
+ imageStyle={imageClassNames as StyleProp<ImageStyle>}
27
26
  ref={isServer() ? undefined : ref => {
28
27
  if (!ref) {
29
28
  // @ts-expect-error hidden from TS
@@ -25,7 +25,8 @@ const getStyles = (styleProps: Record<string, any> = {}) => {
25
25
  }
26
26
 
27
27
  export const Pressable = forwardRef<View, PressableProps>(({ variants, style, ...props }, forwardedRef) => {
28
- const storedRef = useRef<View | null>()
28
+ const storedRef = useRef<View | null>(null)
29
+ const scopedTheme = UnistylesShadowRegistry.getScopedTheme()
29
30
 
30
31
  useLayoutEffect(() => {
31
32
  return () => {
@@ -40,14 +41,26 @@ export const Pressable = forwardRef<View, PressableProps>(({ variants, style, ..
40
41
  <NativePressableReactNative
41
42
  {...props}
42
43
  ref={ref => {
44
+ const isPropStyleAFunction = typeof style === 'function'
45
+ const unistyles = isPropStyleAFunction
46
+ ? style.call(style, { pressed: false })
47
+ : getStyles(style as unknown as Record<string, any>)
48
+
43
49
  if (ref) {
44
50
  storedRef.current = ref
45
51
  }
46
52
 
53
+ // @ts-expect-error - this is hidden from TS
54
+ UnistylesShadowRegistry.add(storedRef.current, unistyles)
55
+
47
56
  return passForwardedRef(props, ref, forwardedRef)
48
57
  }}
49
58
  style={state => {
50
59
  const isPropStyleAFunction = typeof style === 'function'
60
+ const previousScopedTheme = UnistylesShadowRegistry.getScopedTheme()
61
+
62
+ UnistylesShadowRegistry.setScopedTheme(scopedTheme)
63
+
51
64
  const unistyles = isPropStyleAFunction
52
65
  ? style.call(style, state)
53
66
  : getStyles(style as unknown as Record<string, any>)
@@ -62,6 +75,8 @@ export const Pressable = forwardRef<View, PressableProps>(({ variants, style, ..
62
75
  // @ts-expect-error - this is hidden from TS
63
76
  UnistylesShadowRegistry.add(storedRef.current, unistyles)
64
77
 
78
+ UnistylesShadowRegistry.setScopedTheme(previousScopedTheme)
79
+
65
80
  return unistyles
66
81
  }}
67
82
  />
@@ -42,6 +42,11 @@ export const createUnistylesElement = (Component: any) => {
42
42
 
43
43
  // @ts-ignore we don't know the type of the component
44
44
  maybeWarnAboutMultipleUnistyles(props.style, Component.displayName)
45
+
46
+ return () => {
47
+ // @ts-ignore
48
+ UnistylesShadowRegistry.remove(ref)
49
+ }
45
50
  }}
46
51
  />
47
52
  )
@@ -32,6 +32,11 @@ export const createUnistylesImageBackground = (Component: typeof ImageBackground
32
32
  return () => {
33
33
  // @ts-ignore
34
34
  UnistylesShadowRegistry.remove(ref)
35
+
36
+ if (storedImageRef.current) {
37
+ // @ts-ignore
38
+ UnistylesShadowRegistry.remove(storedImageRef.current)
39
+ }
35
40
  }
36
41
  }}
37
42
  imageRef={ref => {
@@ -31,7 +31,7 @@ export const useProxifiedUnistyles = (forcedTheme?: UnistylesTheme) => {
31
31
  const [dependencies] = useState(() => new Set<number>())
32
32
  const [theme, setTheme] = useState(UnistylesRuntime.getTheme(scopedTheme))
33
33
  const [_, runtimeChanged] = useReducer(() => ({}), {})
34
- const disposeRef = useRef<VoidFunction>()
34
+ const disposeRef = useRef<VoidFunction>(undefined)
35
35
 
36
36
  const reinitListener = () => {
37
37
  disposeRef.current?.()
@@ -12,22 +12,48 @@ type GenericComponentProps<P> = ComponentProps<P>
12
12
  // @ts-expect-error
13
13
  type GenericComponentRef<T> = ComponentRef<T>
14
14
 
15
+ type UnistylesSecrets = {
16
+ uni__getStyles: () => Record<string, any>,
17
+ uni__dependencies: Array<UnistyleDependency>
18
+ }
19
+
20
+ type MappedSecrets = {
21
+ styles: Record<string, any>,
22
+ dependencies: Array<UnistyleDependency>
23
+ }
24
+
15
25
  export const withUnistyles = <TComponent, TMappings extends GenericComponentProps<TComponent>>(Component: TComponent, mappings?: Mappings<TMappings>) => {
16
26
  type TProps = GenericComponentProps<TComponent>
17
27
  type PropsWithUnistyles = PartialBy<TProps, keyof TMappings | SupportedStyleProps> & {
18
28
  uniProps?: Mappings<TProps>
19
29
  }
20
- const getSecrets = (styleProps: Record<string, any> = {}): { uni__getStyles(): any, uni__dependencies: Array<UnistyleDependency> } => {
21
- const unistyleKey = Object
22
- .keys(styleProps)
23
- .find(key => key.startsWith('unistyles_'))
24
-
25
- return unistyleKey
26
- ? styleProps[unistyleKey]
27
- : {
28
- uni__getStyles: () => styleProps,
29
- uni__dependencies: [],
30
- }
30
+ const getSecrets = (styleProps: Record<string, any> = {}): MappedSecrets => {
31
+ const styles = Array.isArray(styleProps)
32
+ ? styleProps.flat()
33
+ : [styleProps]
34
+
35
+ const secrets: Array<UnistylesSecrets> = styles
36
+ .filter(Boolean)
37
+ .reduce((acc, style) => {
38
+ const unistyleKey = Object
39
+ .keys(style)
40
+ .find(key => key.startsWith('unistyles_'))
41
+
42
+ return acc.concat([
43
+ unistyleKey
44
+ ? style[unistyleKey]
45
+ : {
46
+ uni__getStyles: () => styleProps,
47
+ uni__dependencies: [],
48
+ }
49
+ ])
50
+ }, [])
51
+
52
+ return {
53
+ styles: secrets.reduce((acc, secret) => Object
54
+ .assign(acc, secret.uni__getStyles()), {} as Record<string, any>),
55
+ dependencies: secrets.flatMap(secret => secret.uni__dependencies),
56
+ }
31
57
  }
32
58
 
33
59
  return forwardRef<GenericComponentRef<TComponent>, PropsWithUnistyles>((props, ref) => {
@@ -46,7 +72,7 @@ export const withUnistyles = <TComponent, TMappings extends GenericComponentProp
46
72
  const styleSecrets = getSecrets(narrowedProps.style)
47
73
  const contentContainerStyleSecrets = getSecrets(narrowedProps.contentContainerStyle)
48
74
 
49
- addDependencies(Array.from(new Set([...styleSecrets.uni__dependencies, ...contentContainerStyleSecrets.uni__dependencies])))
75
+ addDependencies(Array.from(new Set([...styleSecrets.dependencies, ...contentContainerStyleSecrets.dependencies])))
50
76
  }, [narrowedProps.style, narrowedProps.contentContainerStyle])
51
77
 
52
78
  const mappingsProps = mappings ? mappings(proxifiedTheme, proxifiedRuntime) : {}
@@ -58,10 +84,10 @@ export const withUnistyles = <TComponent, TMappings extends GenericComponentProp
58
84
  const finalProps = {
59
85
  ...deepMergeObjects(mappingsProps, unistyleProps, props),
60
86
  ...narrowedProps.style ? {
61
- style: styleSecrets.uni__getStyles(),
87
+ style: styleSecrets.styles,
62
88
  } : {},
63
89
  ...narrowedProps.contentContainerStyle ? {
64
- contentContainerStyle: contentContainerStyleSecrets.uni__getStyles(),
90
+ contentContainerStyle: contentContainerStyleSecrets.styles,
65
91
  } : {},
66
92
  } as any
67
93
 
@@ -45,6 +45,7 @@ HybridShadowRegistry.add = (handle, styles) => {
45
45
  .filter(style => !style?.initial?.updater)
46
46
  .filter(style => style && Object.keys(style).length > 0)
47
47
  .flat()
48
+ .filter(Boolean)
48
49
 
49
50
  if (filteredStyles.length > 0) {
50
51
  HybridShadowRegistry.link(findShadowNodeForHandle(handle), filteredStyles)
@@ -62,7 +62,7 @@ export type UnistylesValues = FlatUnistylesValues & {
62
62
  } & VariantsAndCompoundVariants & {
63
63
  [propName in NestedKeys]?: UnistyleNestedStyles[propName]
64
64
  } & {
65
- _web?: CSSProperties & CustomClassName & {
65
+ _web?: ToDeepUnistyles<CSSProperties> & CustomClassName & {
66
66
  [propName in Pseudo]?: ToDeepUnistyles<CSSProperties>
67
67
  }
68
68
  }
package/plugin/consts.js DELETED
@@ -1,63 +0,0 @@
1
- const REACT_NATIVE_COMPONENT_NAMES = [
2
- 'ActivityIndicator',
3
- 'View',
4
- 'Text',
5
- 'Image',
6
- 'ImageBackground',
7
- 'KeyboardAvoidingView',
8
- 'Pressable',
9
- 'ScrollView',
10
- 'FlatList',
11
- 'SectionList',
12
- 'Switch',
13
- 'TextInput',
14
- 'RefreshControl',
15
- 'TouchableHighlight',
16
- 'TouchableOpacity',
17
- 'VirtualizedList',
18
- 'Animated'
19
- // Modal - there is no exposed native handle
20
- // TouchableWithoutFeedback - can't accept a ref
21
- ]
22
-
23
- // auto replace RN imports to Unistyles imports under these paths
24
- // our implementation simply borrows 'ref' to register it in ShadowRegistry
25
- // so we won't affect anyone's implementation
26
- const REPLACE_WITH_UNISTYLES_PATHS = [
27
- 'react-native-reanimated/src/component',
28
- 'react-native-gesture-handler/src/components'
29
- ]
30
-
31
- // this is more powerful API as it allows to convert unmatched imports to Unistyles
32
- // { path: string, imports: Array<{ name: string, isDefault: boolean, path: string, mapTo: string }> }
33
- // path => node_modules path
34
- // imports:
35
- // name? <- target import name if isDefault is false
36
- // isDefault <- is the import default?
37
- // path <- path to the target import
38
- // mapTo <- name of the Unistyles component
39
- const REPLACE_WITH_UNISTYLES_EXOTIC_PATHS = []
40
-
41
- // this list will additionally detect React Native direct imports
42
- const NATIVE_COMPONENTS_PATHS = {
43
- imports: [
44
- {
45
- name: 'NativeText',
46
- isDefault: false,
47
- path: 'react-native/Libraries/Text/TextNativeComponent',
48
- mapTo: 'NativeText'
49
- },
50
- {
51
- isDefault: true,
52
- path: 'react-native/Libraries/Components/View/ViewNativeComponent',
53
- mapTo: 'NativeView'
54
- }
55
- ]
56
- }
57
-
58
- module.exports = {
59
- NATIVE_COMPONENTS_PATHS,
60
- REACT_NATIVE_COMPONENT_NAMES,
61
- REPLACE_WITH_UNISTYLES_PATHS,
62
- REPLACE_WITH_UNISTYLES_EXOTIC_PATHS
63
- }
package/plugin/exotic.js DELETED
@@ -1,54 +0,0 @@
1
- function handleExoticImport(t, path, state, exoticImport) {
2
- const specifiers = path.node.specifiers
3
- const source = path.node.source
4
-
5
- if (path.node.importKind !== 'value') {
6
- return
7
- }
8
-
9
- specifiers.forEach(specifier => {
10
- for (const rule of exoticImport.imports) {
11
- const hasMatchingImportType = (!rule.isDefault && t.isImportSpecifier(specifier)) || (rule.isDefault && t.isImportDefaultSpecifier(specifier))
12
- const hasMatchingImportName = rule.isDefault || (!rule.isDefault && rule.name === specifier.local.name)
13
- const hasMatchingPath = rule.path === source.value
14
-
15
- if (!hasMatchingImportType || !hasMatchingImportName || !hasMatchingPath) {
16
- continue
17
- }
18
-
19
- if (t.isImportDefaultSpecifier(specifier)) {
20
- const newImport = t.importDeclaration(
21
- [t.importDefaultSpecifier(t.identifier(specifier.local.name))],
22
- t.stringLiteral(state.opts.isLocal
23
- ? state.file.opts.filename.split('react-native-unistyles').at(0).concat(`react-native-unistyles/components/native/${rule.mapTo}`)
24
- : `react-native-unistyles/components/native/${rule.mapTo}`
25
- )
26
- )
27
-
28
- path.replaceWith(newImport)
29
- } else {
30
- const newImport = t.importDeclaration(
31
- [t.importSpecifier(t.identifier(rule.mapTo), t.identifier(rule.mapTo))],
32
- t.stringLiteral(state.opts.isLocal
33
- ? state.file.opts.filename.split('react-native-unistyles').at(0).concat(`react-native-unistyles/components/native/${rule.mapTo}`)
34
- : `react-native-unistyles/components/native/${rule.mapTo}`
35
- )
36
- )
37
-
38
- path.node.specifiers = specifiers.filter(s => s !== specifier)
39
-
40
- if (path.node.specifiers.length === 0) {
41
- path.replaceWith(newImport)
42
- } else {
43
- path.insertBefore(newImport)
44
- }
45
- }
46
-
47
- return
48
- }
49
- })
50
- }
51
-
52
- module.exports = {
53
- handleExoticImport
54
- }
package/plugin/import.js DELETED
@@ -1,51 +0,0 @@
1
- /** @param {import('./index').UnistylesPluginPass} state */
2
- function addUnistylesImport(t, path, state) {
3
- const localNames = Object.keys(state.reactNativeImports)
4
- const names = Object.values(state.reactNativeImports)
5
- const pairs = Object.entries(state.reactNativeImports)
6
- const nodesToRemove = []
7
-
8
- // remove rn-imports
9
- path.node.body.forEach(node => {
10
- // user might have multiple imports like import type, import
11
- if (t.isImportDeclaration(node) && node.source.value === 'react-native') {
12
- node.specifiers = node.specifiers.filter(specifier => !localNames.some(name => name === specifier.local.name))
13
-
14
- if (node.specifiers.length === 0) {
15
- nodesToRemove.push(node)
16
- }
17
- }
18
- })
19
-
20
- // remove RNWeb imports
21
- names.forEach(name => {
22
- const rnWebImport = path.node.body.find(node => t.isImportDeclaration(node) && node.source.value === `react-native-web/dist/exports/${name}`)
23
-
24
- if (rnWebImport) {
25
- rnWebImport.specifiers = []
26
- }
27
- })
28
-
29
- // import components from react-native-unistyles
30
- pairs.forEach(([localName, name]) => {
31
- const newImport = t.importDeclaration(
32
- [t.importSpecifier(t.identifier(localName), t.identifier(name))],
33
- t.stringLiteral(state.opts.isLocal
34
- ? state.file.opts.filename.split('react-native-unistyles').at(0).concat(`react-native-unistyles/src/components/native/${name}`)
35
- : `react-native-unistyles/components/native/${name}`
36
- )
37
- )
38
-
39
- path.node.body.unshift(newImport)
40
- })
41
-
42
- // cleanup
43
- nodesToRemove.forEach(node => path.node.body.splice(path.node.body.indexOf(node), 1))
44
- }
45
-
46
- const isInsideNodeModules = state => state.file.opts.filename.includes('node_modules')
47
-
48
- module.exports = {
49
- isInsideNodeModules,
50
- addUnistylesImport
51
- }
package/plugin/ref.js DELETED
@@ -1,11 +0,0 @@
1
- function hasStringRef(t, path) {
2
- return path.node.openingElement.attributes.find(attr =>
3
- t.isJSXAttribute(attr) &&
4
- t.isJSXIdentifier(attr.name, { name: 'ref' }) &&
5
- t.isStringLiteral(attr.value)
6
- )
7
- }
8
-
9
- module.exports = {
10
- hasStringRef
11
- }