@@ -7,18 +7,46 @@ import getSeparatedStyles from './utils/getSeparatedStyles'
77
88import type {
99 JssSheet ,
10+ TagNameOrStyledElementType ,
1011 ComponentStyleType ,
1112 StyledElementPropsType
1213} from './types'
1314
15+ type Comp = Function & Component < * >
16+
1417type StyledArgs = {
15- tagName : string ,
16- elementStyle : ComponentStyleType ,
18+ element : TagNameOrStyledElementType | Comp ,
19+ ownStyle : ComponentStyleType ,
1720 mountSheet : Function ,
1821 jss : Function
1922}
2023
21- const styled = ( { tagName, elementStyle, mountSheet, jss} : StyledArgs ) => {
24+ const getElementName = ( element : Comp ) : string =>
25+ element . displayName || element . name || 'StyledElement'
26+
27+ const getParamsByElement = ( element ) => {
28+ if ( typeof element === 'string' ) return { tagName : element }
29+ if ( element . tagName ) return element
30+
31+ return {
32+ tagName : getElementName ( element ) ,
33+ reactComponent : element
34+ }
35+ }
36+
37+ const styled = ( { element, ownStyle, mountSheet, jss} : StyledArgs ) => {
38+ const {
39+ style,
40+ tagName,
41+ reactComponent = tagName
42+ } : {
43+ style ?: ComponentStyleType ,
44+ tagName : string ,
45+ reactComponent ?: string | typeof element
46+ } = getParamsByElement ( element )
47+
48+ const elementStyle = { ...style , ...ownStyle }
49+
2250 const { dynamicStyle, staticStyle} = getSeparatedStyles ( elementStyle )
2351 const staticTagName = staticStyle && generateTagName ( tagName )
2452
@@ -94,7 +122,7 @@ const styled = ({tagName, elementStyle, mountSheet, jss}: StyledArgs) => {
94122 className
95123 ] )
96124
97- return createElement ( tagName , { ...props , className : tagClass } , children )
125+ return createElement ( reactComponent , { ...props , className : tagClass } , children )
98126 }
99127 }
100128
0 commit comments