/
styled.js
193 lines (147 loc) · 5.14 KB
/
styled.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
import {Component, createElement} from 'react'
import {object} from 'prop-types'
import {themeListener, channel} from 'theming'
import filterProps from './utils/filterProps'
import composeClasses from './utils/composeClasses'
import generateTagName from './utils/generateTagName'
import getSeparatedStyles from './utils/getSeparatedStyles'
import type {
JssSheet,
TagNameOrStyledElementType,
ComponentStyleType,
StyledElementPropsType
} from './types'
type Comp = Function & Component<*>
type StyledArgs = {
element: TagNameOrStyledElementType | Comp,
ownStyle: ComponentStyleType[],
mountSheet: Function,
jss: Function
}
type StateType = {
theme?: Object
}
const getElementName = (element: Comp): string =>
element.displayName || element.name || 'StyledElement'
const getParamsByElement = (element) => {
if (typeof element === 'string') return {tagName: element}
if (element.tagName) return element
return {
tagName: getElementName(element),
reactComponent: element
}
}
const styled = ({element, ownStyle, mountSheet, jss}: StyledArgs) => {
const {
style = [],
tagName,
reactComponent,
}: {
style?: ComponentStyleType[],
tagName: string,
reactComponent?: typeof element
} = getParamsByElement(element)
const elementStyle = style.concat(ownStyle)
const {dynamicStyle, staticStyle} = getSeparatedStyles(...elementStyle)
const staticTagName = staticStyle && generateTagName(tagName)
const isFunctionStyle = typeof dynamicStyle === 'function'
const availableDynamicTagNames = []
const classMap = {}
let staticClassName
class StyledElement extends Component<StyledElementPropsType, StateType> {
static tagName: string = tagName
static style: ComponentStyleType[] = elementStyle
// If the base component is a React component (and thus neither an intrinsic tag or a
// styled element), make sure to keep a reference to the component around. Otherwise deeply
// nested styled elements won't render the base component correctly.
static reactComponent = reactComponent
static contextTypes = {
[channel]: object
}
constructor(props: StyledElementPropsType, context: any) {
super(props, context)
if (!this.dynamicTagName && dynamicStyle) {
this.dynamicTagName = availableDynamicTagNames.pop() || generateTagName(tagName)
}
this.state = {}
if (context[channel]) {
this.state.theme = themeListener.initial(context)
}
this.staticClassName = staticClassName
}
componentWillMount() {
this.sheet = this.sheet || mountSheet()
const rulesIndex = this.sheet.rules.index
const rulesTotal = rulesIndex.length
if (staticStyle && !this.sheet.getRule(staticTagName)) {
this.sheet.addRule(staticTagName, staticStyle)
}
if (!dynamicStyle) return
if (!this.sheet.getRule(this.dynamicTagName)) {
this.sheet.addRule(this.dynamicTagName, dynamicStyle)
}
classMap[this.dynamicTagName] = classMap[this.dynamicTagName] || rulesIndex.slice(rulesTotal)
this.updateSheet(this.props, this.state)
}
componentDidMount() {
if (this.state.theme) {
this.subscriptionId = themeListener.subscribe(this.context, this.setTheme)
}
}
componentWillUpdate(nextProps: StyledElementPropsType, nextState: StateType) {
if (dynamicStyle) this.updateSheet(nextProps, nextState)
}
componentWillUnmount() {
availableDynamicTagNames.push(this.dynamicTagName)
if (this.subscriptionId) {
themeListener.unsubscribe(this.context, this.subscriptionId)
}
}
setTheme = (theme: Object) => this.setState({theme})
dynamicTagName = ''
sheet: JssSheet
staticClassName = ''
subscriptionId: ?number
updateSheet(props: StyledElementPropsType, state: StateType) {
let rule
let ruleIndex = 0
const styleProps = state.theme
? Object.assign({}, state, props)
: props
// nested styles become to flatten rules, so we need to update each nested rule
for (ruleIndex; ruleIndex < classMap[this.dynamicTagName].length; ruleIndex++) {
rule = classMap[this.dynamicTagName][ruleIndex]
if (isFunctionStyle) {
this.sheet.update(rule.key, styleProps)
}
else {
this.sheet.update(rule.key, styleProps)
}
}
}
render() {
const {children, className, ...attrs} = this.props
const props = reactComponent ? attrs : filterProps(tagName, attrs)
const tagClass = composeClasses([
this.staticClassName,
staticTagName && this.sheet.classes[staticTagName],
this.dynamicTagName && this.sheet.classes[this.dynamicTagName],
className
])
return createElement(reactComponent || tagName, {...props, className: tagClass}, children)
}
}
// $FlowIgnore
StyledElement.valueOf = () => {
if (!staticClassName) {
staticClassName = `${jss.generateClassName({
key: generateTagName('static')
})}`
}
return `.${staticClassName}`
}
// $FlowIgnore
StyledElement.toString = StyledElement.valueOf
return StyledElement
}
export default styled