22
33import { createElement , type ComponentType , type Node } from 'react'
44
5- import { getChildrenArray } from '../element'
5+ import { getChildrenArray , computeProps } from '../element'
66import { readContextValue } from '../internals'
77import { mount as mountFunctionComponent } from './functionComponent'
88
@@ -17,6 +17,14 @@ import type {
1717let styledComponents : any
1818try {
1919 styledComponents = require ( 'styled-components' )
20+
21+ if (
22+ styledComponents . __DO_NOT_USE_OR_YOU_WILL_BE_HAUNTED_BY_SPOOKY_GHOSTS ===
23+ undefined ||
24+ styledComponents . ThemeContext === undefined
25+ ) {
26+ styledComponents = undefined
27+ }
2028} catch ( _error ) { }
2129
2230type AttrsFn = ( context : mixed ) => DefaultProps
@@ -70,25 +78,25 @@ export const mount = (
7078 queue : Frame [ ] ,
7179 visitor : Visitor
7280) : Node => {
73- if (
74- styledComponents === undefined ||
75- styledComponents . ThemeContext === undefined
76- ) {
81+ if ( styledComponents === undefined ) {
7782 // styled-components is not installed or incompatible, so the component will have to be
7883 // mounted normally
7984 const { render } = element . type
8085 return mountFunctionComponent ( render , element . props , queue , visitor )
81- } else if ( typeof element . type . target !== 'function' ) {
82- // StyledComponents rendering DOM elements can safely be skipped like normal DOM elements
83- return element . props . children || null
8486 }
8587
86- const type = ( ( element . type : any ) : StyledComponentStatics )
87-
8888 // Imitate styled-components' attrs props without computing styles
89+ const type = ( ( element . type : any ) : StyledComponentStatics )
8990 const theme = readContextValue ( styledComponents . ThemeContext ) || { }
9091 const attrs : Attr [ ] = Array . isArray ( type . attrs ) ? type . attrs : [ type . attrs ]
91- const props = computeAttrsProps ( attrs , element . props , theme )
92+ const computedProps = computeProps ( element . props , ( type : any ) . defaultProps )
93+ const props = computeAttrsProps ( attrs , computedProps , theme )
94+ const as = props . as || type . target
9295
93- return createElement ( ( type . target : any ) , props )
96+ if ( typeof as !== 'function' ) {
97+ // StyledComponents rendering DOM elements can safely be skipped like normal DOM elements
98+ return element . props . children || null
99+ } else {
100+ return createElement ( ( as : any ) , props )
101+ }
94102}
0 commit comments