|
| 1 | +// @flow |
| 2 | + |
| 3 | +import { createElement, type ComponentType, type Node } from 'react' |
| 4 | + |
| 5 | +import { getChildrenArray } from '../element' |
| 6 | +import { readContextValue } from '../internals' |
| 7 | +import { mount as mountFunctionComponent } from './functionComponent' |
| 8 | + |
| 9 | +import type { |
| 10 | + DefaultProps, |
| 11 | + ForwardRefElement, |
| 12 | + Frame, |
| 13 | + Visitor, |
| 14 | + ComponentStatics |
| 15 | +} from '../types' |
| 16 | + |
| 17 | +let styledComponents: any |
| 18 | +try { |
| 19 | + styledComponents = require('styled-components') |
| 20 | +} catch (_error) {} |
| 21 | + |
| 22 | +type AttrsFn = (context: mixed) => DefaultProps |
| 23 | +type Attr = void | AttrsFn | { [propName: string]: ?AttrsFn } |
| 24 | + |
| 25 | +type StyledComponentStatics = { |
| 26 | + styledComponentId: string, |
| 27 | + attrs: Attr | Attr[], |
| 28 | + target: ComponentType<DefaultProps> & ComponentStatics |
| 29 | +} |
| 30 | + |
| 31 | +/** Computes a StyledComponent's props with attributes */ |
| 32 | +const computeAttrsProps = ( |
| 33 | + input: Attr[], |
| 34 | + props: DefaultProps, |
| 35 | + theme: mixed |
| 36 | +): any => { |
| 37 | + const executionContext = { ...props, theme } |
| 38 | + |
| 39 | + const attrs = input.reduce((acc, attr) => { |
| 40 | + if (typeof attr === 'function') { |
| 41 | + return Object.assign(acc, attr(executionContext)) |
| 42 | + } else if (typeof attr !== 'object' || attr === null) { |
| 43 | + return acc |
| 44 | + } |
| 45 | + |
| 46 | + for (const key in attr) { |
| 47 | + const attrProp = attr[key] |
| 48 | + if (typeof attrProp === 'function') { |
| 49 | + acc[key] = attrProp(executionContext) |
| 50 | + } else if (attr.hasOwnProperty(key)) { |
| 51 | + acc[key] = attrProp |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + return acc |
| 56 | + }, {}) |
| 57 | + |
| 58 | + return Object.assign(attrs, props) |
| 59 | +} |
| 60 | + |
| 61 | +/** Checks whether a ForwardRefElement is a StyledComponent element */ |
| 62 | +export const isStyledElement = (element: ForwardRefElement): boolean %checks => |
| 63 | + typeof element.type.styledComponentId === 'string' |
| 64 | + |
| 65 | +/** This is an optimised faux mounting strategy for StyledComponents. |
| 66 | + It is only enabled when styled-components is installed and the component |
| 67 | + can safely be skipped */ |
| 68 | +export const mount = ( |
| 69 | + element: ForwardRefElement, |
| 70 | + queue: Frame[], |
| 71 | + visitor: Visitor |
| 72 | +): Node => { |
| 73 | + if ( |
| 74 | + styledComponents === undefined || |
| 75 | + styledComponents.ThemeContext === undefined |
| 76 | + ) { |
| 77 | + // styled-components is not installed or incompatible, so the component will have to be |
| 78 | + // mounted normally |
| 79 | + const { render } = element.type |
| 80 | + 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 |
| 84 | + } |
| 85 | + |
| 86 | + const type = ((element.type: any): StyledComponentStatics) |
| 87 | + |
| 88 | + // Imitate styled-components' attrs props without computing styles |
| 89 | + const theme = readContextValue(styledComponents.ThemeContext) || {} |
| 90 | + const attrs: Attr[] = Array.isArray(type.attrs) ? type.attrs : [type.attrs] |
| 91 | + const props = computeAttrsProps(attrs, element.props, theme) |
| 92 | + |
| 93 | + return createElement((type.target: any), props) |
| 94 | +} |
0 commit comments