Skip to content

Commit 03f0cbd

Browse files
committed
Fix defaultProps and props/attrs.as for styled-components
1 parent bed10b1 commit 03f0cbd

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

.flowconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
[untyped]
66
.*/node_modules/react-is/.*
7+
.*/node_modules/styled-components/.*
78

89
[libs]
910
types/

src/render/styledComponent.js

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import { createElement, type ComponentType, type Node } from 'react'
44

5-
import { getChildrenArray } from '../element'
5+
import { getChildrenArray, computeProps } from '../element'
66
import { readContextValue } from '../internals'
77
import { mount as mountFunctionComponent } from './functionComponent'
88

@@ -17,6 +17,14 @@ import type {
1717
let styledComponents: any
1818
try {
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

2230
type 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

Comments
 (0)