-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathindex.js
More file actions
111 lines (87 loc) · 2.92 KB
/
index.js
File metadata and controls
111 lines (87 loc) · 2.92 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
import {PureComponent, createElement} from 'react'
import {create as createJss, getDynamicStyles} from 'jss'
import preset from 'jss-preset-default'
import filterProps from './utils/filter-props'
const jssDefault = createJss(preset())
type StyledElementAttrsType = { tag: string, styles: Object }
type StyledElementType = Function & StyledElementAttrsType
type tagOrStyledElementTypeype = string | StyledElementType
type StyledElementPropsType = {
classes: Object,
children: ?any,
className: ?string,
}
const createStyled = (jss?: Function = jssDefault) => (baseStyles: Object = {}) => {
let sheet
let dynamicSheet
let counter = 0
const styled = (
tagOrStyledElement: tagOrStyledElementTypeype,
ownStyles: Object
): StyledElementType => {
const {tag, styles}: StyledElementAttrsType = typeof tagOrStyledElement === 'string'
? {tag: tagOrStyledElement, styles: {}}
: tagOrStyledElement
const elementStyles = {...styles, ...ownStyles}
const dynamicStyles = getDynamicStyles(elementStyles)
const staticTag = `${tag}-${++counter}`
return class StyledElement extends PureComponent {
static tag = tag
static styles = elementStyles
props: StyledElementPropsType
tagScoped = ''
constructor(props) {
super(props)
this.tagScoped = `${tag}-${++counter}`
}
componentWillMount() {
if (!sheet) {
sheet = jss.createStyleSheet(baseStyles, {
link: true,
meta: 'StaticBaseSheet',
}).attach()
dynamicSheet = jss.createStyleSheet({}, {
link: true,
meta: 'DynamicComponentSheet',
}).attach()
}
if (!sheet.getRule(staticTag)) {
sheet.addRule(staticTag, elementStyles)
}
if (dynamicStyles && !dynamicSheet.getRule(this.tagScoped)) {
dynamicSheet.detach()
dynamicSheet.addRule(this.tagScoped, dynamicStyles)
dynamicSheet.update(this.tagScoped, this.props)
dynamicSheet.attach()
}
}
componentWillReceiveProps(nextProps: StyledElementPropsType) {
if (dynamicStyles) dynamicSheet.update(this.tagScoped, nextProps)
}
componentWillUnmount() {
dynamicSheet.deleteRule(this.tagScoped)
}
render() {
if (!sheet) return null
const {children, className, ...attrs} = this.props
const props = filterProps(attrs)
const tagClass = [
sheet.classes[staticTag],
dynamicSheet.classes[this.tagScoped],
className,
]
.filter(Boolean)
.join(' ')
return createElement(tag, {...props, className: tagClass}, children)
}
}
}
return Object.assign(styled, {styles: baseStyles})
}
const defaultStyledCreator = createStyled()
const defaultStyled = defaultStyledCreator()
export {
createStyled,
defaultStyled as styled,
}
export default defaultStyledCreator