-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathindex.js
201 lines (188 loc) Β· 5.94 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// @flow
import type { Emotion, Interpolations } from 'create-emotion'
import { channel, contextTypes } from '../../emotion-theming/src/utils'
import type { ElementType } from 'react'
import typeof ReactType from 'react'
import type { CreateStyled, StyledOptions } from './utils'
import {
testPickPropsOnComponent,
testAlwaysTrue,
testPickPropsOnStringTag,
pickAssign,
setTheme
} from './utils'
function createEmotionStyled(emotion: Emotion, view: ReactType) {
let createStyled: CreateStyled = (tag, options) => {
if (process.env.NODE_ENV !== 'production') {
if (tag === undefined) {
throw new Error(
'You are trying to create a styled element with an undefined component.\nYou may have forgotten to import it.'
)
}
}
let staticClassName
let identifierName
let stableClassName
let shouldForwardProp
if (options !== undefined) {
staticClassName = options.e
identifierName = options.label
stableClassName = options.target
shouldForwardProp = options.shouldForwardProp
}
const isReal = tag.__emotion_real === tag
const baseTag =
staticClassName === undefined
? (isReal && tag.__emotion_base) || tag
: tag
if (typeof shouldForwardProp !== 'function') {
shouldForwardProp =
typeof baseTag === 'string' &&
baseTag.charAt(0) === baseTag.charAt(0).toLowerCase()
? testPickPropsOnStringTag
: testPickPropsOnComponent
}
return function() {
let args = arguments
let styles =
isReal && tag.__emotion_styles !== undefined
? tag.__emotion_styles.slice(0)
: []
if (identifierName !== undefined) {
styles.push(`label:${identifierName};`)
}
if (staticClassName === undefined) {
if (args[0] == null || args[0].raw === undefined) {
styles.push.apply(styles, args)
} else {
styles.push(args[0][0])
let len = args.length
let i = 1
for (; i < len; i++) {
styles.push(args[i], args[0][i])
}
}
}
class Styled extends view.Component<*, { theme: Object }> {
unsubscribe: number
mergedProps: Object
static toString: () => string
static __emotion_real: any
static __emotion_styles: Interpolations
static __emotion_base: Styled
static __emotion_target: string
static withComponent: (ElementType, options?: StyledOptions) => any
componentWillMount() {
if (this.context[channel] !== undefined) {
this.unsubscribe = this.context[channel].subscribe(
setTheme.bind(this)
)
}
}
componentWillUnmount() {
if (this.unsubscribe !== undefined) {
this.context[channel].unsubscribe(this.unsubscribe)
}
}
render() {
const { props, state } = this
this.mergedProps = pickAssign(testAlwaysTrue, {}, props, {
theme: (state !== null && state.theme) || props.theme || {}
})
let className = ''
let classInterpolations = []
if (props.className) {
if (staticClassName === undefined) {
className += emotion.getRegisteredStyles(
classInterpolations,
props.className
)
} else {
className += `${props.className} `
}
}
if (staticClassName === undefined) {
className += emotion.css.apply(
this,
styles.concat(classInterpolations)
)
} else {
className += staticClassName
}
if (stableClassName !== undefined) {
className += ` ${stableClassName}`
}
return view.createElement(
baseTag,
// $FlowFixMe
pickAssign(shouldForwardProp, {}, props, {
className,
ref: props.innerRef
})
)
}
}
Styled.displayName =
identifierName !== undefined
? identifierName
: `Styled(${
typeof baseTag === 'string'
? baseTag
: baseTag.displayName || baseTag.name || 'Component'
})`
Styled.contextTypes = contextTypes
Styled.__emotion_styles = styles
Styled.__emotion_base = baseTag
Styled.__emotion_real = Styled
Object.defineProperty(Styled, 'toString', {
enumerable: false,
value() {
if (
process.env.NODE_ENV !== 'production' &&
stableClassName === undefined
) {
return 'NO_COMPONENT_SELECTOR'
}
// $FlowFixMe
return `.${stableClassName}`
}
})
Styled.withComponent = (
nextTag: ElementType,
nextOptions?: StyledOptions
) => {
return createStyled(
nextTag,
nextOptions !== undefined
? // $FlowFixMe
pickAssign(testAlwaysTrue, {}, options, nextOptions)
: options
)(...styles)
}
return Styled
}
}
if (process.env.NODE_ENV !== 'production' && typeof Proxy !== 'undefined') {
createStyled = new Proxy(createStyled, {
get(target, property) {
switch (property) {
// react-hot-loader tries to access this stuff
case '__proto__':
case 'name':
case 'prototype':
case 'displayName': {
return target[property]
}
default: {
throw new Error(
`You're trying to use the styled shorthand without babel-plugin-emotion.` +
`\nPlease install and setup babel-plugin-emotion or use the function call syntax(\`styled('${property}')\` instead of \`styled.${property}\`)`
)
}
}
}
})
}
return createStyled
}
export default createEmotionStyled