11import type { AnyObject } from '@react-universal/utils' ;
2- import { isArray , isFunction , isString , noop } from '@react-universal/utils' ;
3- import { useRef } from 'react' ;
4- import { UnistylesShadowRegistry } from 'react-native-unistyles' ;
2+ import { isFunction , isObject , isString } from '@react-universal/utils' ;
3+ import {
4+ Image as RNImage ,
5+ Pressable as RNPressable ,
6+ ScrollView as RNScrollView ,
7+ Text as RNText ,
8+ TextInput as RNTextInput ,
9+ View as RNView ,
10+ } from 'react-native' ;
11+ import { Image as UnistylesImage } from 'react-native-unistyles/components/native/Image' ;
12+ import { Pressable as UnistylesPressable } from 'react-native-unistyles/components/native/Pressable' ;
13+ import { ScrollView as UnistylesScrollView } from 'react-native-unistyles/components/native/ScrollView' ;
14+ import { Text as UnistylesText } from 'react-native-unistyles/components/native/Text' ;
15+ import { TextInput as UnistylesTextInput } from 'react-native-unistyles/components/native/TextInput' ;
16+ import { View as UnistylesView } from 'react-native-unistyles/components/native/View' ;
517import { createElement } from './createElement' ;
618import { useStyles } from './hooks/useStyles' ;
719import type { CreateStyledComponent , StyledOptions } from './styled.types' ;
820import type { StyleInterpolation , StyleProp } from './types' ;
9- import { passRef } from './utils/passRef' ;
1021
1122export function defaultShouldForwardProp ( prop : string ) {
1223 return prop !== 'ownerState' && prop !== 'theme' && prop !== 'sx' && prop !== 'as' ;
1324}
1425
15- function maybeWarnAboutMultipleUnistyles ( style : AnyObject , displayName = 'Unknown' ) {
16- if ( process . env . NODE_ENV !== 'production' && isArray ( style ) ) {
17- const unistylesKeys = Object . keys ( style ) . filter ( ( key ) => key . startsWith ( 'unistyles_' ) ) ;
18-
19- if ( unistylesKeys . length > 1 ) {
20- console . warn (
21- `React Universal: We detected style object with ${ unistylesKeys . length } Unistyles styles. This might cause no updates or unpredictable behavior. Please check style prop for "${ displayName } " and use array syntax instead of object syntax.` ,
22- ) ;
23- }
24- }
25- }
26+ const componentsMap = new Map < React . ComponentType < any > , React . ComponentType < any > > ( [
27+ [ RNImage , UnistylesImage ] ,
28+ [ RNPressable , UnistylesPressable ] ,
29+ [ RNScrollView , UnistylesScrollView ] ,
30+ [ RNText , UnistylesText ] ,
31+ [ RNTextInput , UnistylesTextInput ] ,
32+ [ RNView , UnistylesView ] ,
33+ ] ) ;
2634
2735export function styled < T extends React . ComponentClass < React . ComponentProps < T > > > (
2836 component : T ,
@@ -58,7 +66,11 @@ export function styled<T extends React.ComponentType<React.ComponentProps<T>>>(
5866 style ?: StyleProp < AnyObject > ;
5967 }
6068 > = ( { ref, style, ...props } ) => {
61- const Component = shouldUseAs ? ( props . as ?? component ) : component ;
69+ const _component = shouldUseAs ? ( props . as ?? component ) : component ;
70+
71+ const Component = isObject ( _component )
72+ ? ( componentsMap . get ( _component ) ?? _component )
73+ : _component ;
6274
6375 const _style = useStyles ( styles , { ...props , id, skipSx } ) ;
6476
@@ -73,38 +85,7 @@ export function styled<T extends React.ComponentType<React.ComponentProps<T>>>(
7385 }
7486 }
7587
76- const scrollViewRef = useRef < any > ( null ) ;
77-
78- // newProps.ref = ref;
79- newProps . ref = ( node : T ) => {
80- maybeWarnAboutMultipleUnistyles ( _style , component . displayName ) ;
81-
82- // https://github.com/facebook/react-native/issues/51878
83- // Tested with ScrollView, Animated ScrollView and Reanimated ScrollView
84- if ( component . displayName === 'ScrollView' ) {
85- if ( node != null ) {
86- scrollViewRef . current = node as any ;
87- } else {
88- // @ts -expect-error: `remove` is hidden from types
89- UnistylesShadowRegistry . remove ( scrollViewRef . current ) ;
90- scrollViewRef . current = null ;
91- return noop ;
92- }
93- }
94-
95- return passRef (
96- node ,
97- ref as React . Ref < T > ,
98- ( ) => {
99- // @ts -expect-error: `add` is hidden from types
100- UnistylesShadowRegistry . add ( node , _style ) ;
101- } ,
102- ( ) => {
103- // @ts -expect-error: `remove` is hidden from types
104- UnistylesShadowRegistry . remove ( node ) ;
105- } ,
106- ) ;
107- } ;
88+ newProps . ref = ref ;
10889 newProps . style = isFunction ( style ) ? ( state : any ) => [ _style , style ( state ) ] : [ _style , style ] ;
10990
11091 return createElement ( Component , newProps ) ;
0 commit comments