Skip to content

Commit 682aee9

Browse files
committed
Use common dynamic stylesheet
1 parent f8a7e59 commit 682aee9

1 file changed

Lines changed: 36 additions & 17 deletions

File tree

src/index.jsx

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type StyledElementPropsT = {
2020

2121
export const createStyled = (jss?: Function = JSS) => (baseStyles: Object = {}) => {
2222
let sheet
23+
let dynamicSheet
2324

2425
let currentId = 0
2526

@@ -31,7 +32,21 @@ export const createStyled = (jss?: Function = JSS) => (baseStyles: Object = {})
3132
const elementStyles = {...styles, ...ownStyles}
3233
const dynamicStyles = getDynamicStyles(elementStyles)
3334

34-
const staticTag = `${Tag}-${++currentId}`
35+
const updateRule = (className, data) => {
36+
const componentRule = dynamicSheet.rules.map[className]
37+
38+
if (componentRule.type === 'regular') {
39+
for (const prop in componentRule.style) {
40+
const value = componentRule.style[prop]
41+
if (typeof value === 'function') {
42+
const computedValue = value(data)
43+
componentRule.prop(prop, computedValue)
44+
}
45+
}
46+
}
47+
}
48+
49+
const StaticTag = `${Tag}-${++currentId}`
3550

3651
return class StyledElement extends PureComponent {
3752
props: StyledElementPropsT
@@ -40,7 +55,6 @@ export const createStyled = (jss?: Function = JSS) => (baseStyles: Object = {})
4055
static styles = elementStyles
4156

4257
tagScoped = ''
43-
dynamicSheet = null
4458

4559
constructor(props) {
4660
super(props)
@@ -54,29 +68,31 @@ export const createStyled = (jss?: Function = JSS) => (baseStyles: Object = {})
5468
link: true,
5569
meta: 'StaticBaseSheet',
5670
}).attach()
71+
72+
dynamicSheet = jss.createStyleSheet({}, {
73+
link: true,
74+
meta: 'DynamicComponentSheet',
75+
}).attach()
5776
}
5877

59-
if (!sheet.getRule(staticTag)) {
60-
sheet.addRule(staticTag, elementStyles)
78+
if (!sheet.getRule(StaticTag)) {
79+
sheet.addRule(StaticTag, elementStyles)
6180
}
6281

63-
if (dynamicStyles && !this.dynamicSheet) {
64-
this.dynamicSheet = jss.createStyleSheet({[this.tagScoped]: dynamicStyles}, {
65-
link: true,
66-
meta: `DynamicComponentSheet-${this.tagScoped}`,
67-
}).update(this.props).attach()
82+
if (dynamicStyles && !dynamicSheet.getRule(this.tagScoped)) {
83+
dynamicSheet.detach()
84+
dynamicSheet.addRule(this.tagScoped, dynamicStyles)
85+
updateRule(this.tagScoped, this.props)
86+
dynamicSheet.attach()
6887
}
6988
}
7089

7190
componentWillReceiveProps(nextProps: StyledElementPropsT) {
72-
if (this.dynamicSheet) this.dynamicSheet.update(nextProps)
91+
if (dynamicStyles) updateRule(this.tagScoped, nextProps)
7392
}
7493

7594
componentWillUnmount() {
76-
if (this.dynamicSheet) {
77-
jss.removeStyleSheet(this.dynamicSheet)
78-
this.dynamicSheet = null
79-
}
95+
dynamicSheet.deleteRule(this.tagScoped)
8096
}
8197

8298
render() {
@@ -86,15 +102,18 @@ export const createStyled = (jss?: Function = JSS) => (baseStyles: Object = {})
86102

87103
const props = filterProps(attrs)
88104
const tagClass = [
89-
sheet.classes[staticTag],
90-
this.dynamicSheet && this.dynamicSheet.classes[this.tagScoped],
105+
sheet.classes[StaticTag],
106+
dynamicSheet.classes[this.tagScoped],
91107
className,
92108
]
93109
.filter(Boolean)
94110
.join(' ')
95111

96112
return (
97-
<Tag className={tagClass} {...props}>
113+
<Tag
114+
className={tagClass}
115+
{...props}
116+
>
98117
{children}
99118
</Tag>
100119
)

0 commit comments

Comments
 (0)