-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathstyled.js
More file actions
146 lines (112 loc) · 3.81 KB
/
styled.js
File metadata and controls
146 lines (112 loc) · 3.81 KB
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
import {Component, createElement} from 'react'
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
}
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 = tagName
}: {
style?: ComponentStyleType,
tagName: string,
reactComponent?: string | typeof element
} = getParamsByElement(element)
const elementStyle = {...style, ...ownStyle}
const {dynamicStyle, staticStyle} = getSeparatedStyles(elementStyle)
const staticTagName = staticStyle && generateTagName(tagName)
const availableDynamicTagNames = []
const classMap = {}
let staticClassName
class StyledElement extends Component<StyledElementPropsType> {
static tagName: string = tagName
static style: ComponentStyleType = elementStyle
constructor(props: StyledElementPropsType) {
super(props)
if (!this.dynamicTagName && dynamicStyle) {
this.dynamicTagName = availableDynamicTagNames.pop() || generateTagName(tagName)
}
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)
}
componentWillReceiveProps(nextProps: StyledElementPropsType) {
if (dynamicStyle) this.updateSheet(nextProps)
}
componentWillUnmount() {
availableDynamicTagNames.push(this.dynamicTagName)
}
dynamicTagName = ''
sheet: JssSheet
staticClassName = ''
updateSheet(props: StyledElementPropsType) {
let rule
let ruleIndex = 0
// 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]
this.sheet.update(rule.key, props)
}
}
render() {
const {children, className, ...attrs} = this.props
const props = filterProps(tagName, attrs)
const tagClass = composeClasses([
this.staticClassName,
staticTagName && this.sheet.classes[staticTagName],
this.dynamicTagName && this.sheet.classes[this.dynamicTagName],
className
])
return createElement(reactComponent, {...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