|
| 1 | +// @flow |
| 2 | + |
| 3 | +import { createElement, type ComponentType, type Node } from 'react' |
| 4 | + |
| 5 | +import { getChildrenArray, computeProps } from '../element' |
| 6 | +import { readContextValue } from '../internals' |
| 7 | +import { mount as mountFunctionComponent } from './functionComponent' |
| 8 | + |
| 9 | +import type { |
| 10 | + AbstractElement, |
| 11 | + DefaultProps, |
| 12 | + ForwardRefElement, |
| 13 | + Frame, |
| 14 | + ComponentStatics |
| 15 | +} from '../types' |
| 16 | + |
| 17 | +let styledComponents: any |
| 18 | +try { |
| 19 | + 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 | + } |
| 28 | +} catch (_error) {} |
| 29 | + |
| 30 | +type AttrsFn = (context: mixed) => DefaultProps |
| 31 | +type Attr = void | AttrsFn | { [propName: string]: ?AttrsFn } |
| 32 | + |
| 33 | +type StyledComponentStatics = { |
| 34 | + styledComponentId: string, |
| 35 | + attrs: Attr | Attr[], |
| 36 | + target: ComponentType<DefaultProps> & ComponentStatics, |
| 37 | + defaultProps?: Object |
| 38 | +} |
| 39 | + |
| 40 | +/** Determines a StyledComponent's theme taking defaults into account */ |
| 41 | +const computeTheme = (props: Object, defaultProps: Object): Object => { |
| 42 | + const defaultTheme = defaultProps ? defaultProps.theme : undefined |
| 43 | + const isDefaultTheme = defaultTheme ? props.theme === defaultTheme : false |
| 44 | + |
| 45 | + if (props.theme && !isDefaultTheme) { |
| 46 | + return props.theme |
| 47 | + } else { |
| 48 | + const contextTheme = readContextValue(styledComponents.ThemeContext) |
| 49 | + return contextTheme || defaultTheme |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +/** Computes a StyledComponent's props with attributes */ |
| 54 | +const computeAttrsProps = (input: Attr[], props: any, theme: any): any => { |
| 55 | + const executionContext = { ...props, theme } |
| 56 | + |
| 57 | + const attrs = input.reduce((acc, attr) => { |
| 58 | + if (typeof attr === 'function') { |
| 59 | + return Object.assign(acc, attr(executionContext)) |
| 60 | + } else if (typeof attr !== 'object' || attr === null) { |
| 61 | + return acc |
| 62 | + } |
| 63 | + |
| 64 | + for (const key in attr) { |
| 65 | + const attrProp = attr[key] |
| 66 | + if (typeof attrProp === 'function') { |
| 67 | + acc[key] = attrProp(executionContext) |
| 68 | + } else if (attr.hasOwnProperty(key)) { |
| 69 | + acc[key] = attrProp |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + return acc |
| 74 | + }, {}) |
| 75 | + |
| 76 | + const newProps = (Object.assign(attrs, props): any) |
| 77 | + newProps.className = props.className || '' |
| 78 | + newProps.style = props.style |
| 79 | + ? Object.assign({}, attrs.style, props.style) |
| 80 | + : attrs.style |
| 81 | + return newProps |
| 82 | +} |
| 83 | + |
| 84 | +/** Checks whether a ForwardRefElement is a StyledComponent element */ |
| 85 | +export const isStyledElement = (element: ForwardRefElement): boolean %checks => |
| 86 | + styledComponents !== undefined && |
| 87 | + typeof element.type.target !== undefined && |
| 88 | + typeof element.type.styledComponentId === 'string' |
| 89 | + |
| 90 | +/** This is an optimised faux mounting strategy for StyledComponents. |
| 91 | + It is only enabled when styled-components is installed and the component |
| 92 | + can safely be skipped */ |
| 93 | +export const mount = (element: ForwardRefElement): AbstractElement[] => { |
| 94 | + // Imitate styled-components' attrs props without computing styles |
| 95 | + const type = ((element.type: any): StyledComponentStatics) |
| 96 | + const attrs: Attr[] = Array.isArray(type.attrs) ? type.attrs : [type.attrs] |
| 97 | + const computedProps = computeProps(element.props, type.defaultProps) |
| 98 | + const theme = computeTheme(element.props, type) |
| 99 | + const props = computeAttrsProps(attrs, computedProps, theme) |
| 100 | + const as = props.as || type.target |
| 101 | + const children = computedProps.children || null |
| 102 | + |
| 103 | + // StyledComponents rendering DOM elements can safely be skipped like normal DOM elements |
| 104 | + if (typeof as === 'string') { |
| 105 | + return getChildrenArray(children) |
| 106 | + } else { |
| 107 | + delete props.as |
| 108 | + return [(createElement((as: any), props, children): any)] |
| 109 | + } |
| 110 | +} |
0 commit comments