Skip to content

Commit f3abe08

Browse files
committed
Resolve #23 with new jss version
1 parent 2ce6b09 commit f3abe08

1 file changed

Lines changed: 12 additions & 32 deletions

File tree

src/styled.js

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ const styled = ({tagName, elementStyle, mountSheet}: StyledArgs) => {
3636
dynamicTagName = ''
3737

3838
sheet: JssSheet
39-
cssRules: CSSStyleRule[]
4039
rulesIndex: Rule[]
4140

4241
constructor(props: StyledElementPropsType) {
@@ -49,10 +48,8 @@ const styled = ({tagName, elementStyle, mountSheet}: StyledArgs) => {
4948
componentWillMount() {
5049
this.sheet = this.sheet || mountSheet()
5150
this.rulesIndex = this.sheet.rules.index
52-
this.cssRules = this.cssRules || this.sheet.renderer.getRules() || []
5351

5452
const rulesTotal = this.rulesIndex.length
55-
const cssRulesTotal = this.cssRules.length
5653

5754
if (!this.sheet.getRule(staticTagName)) {
5855
this.sheet.addRule(staticTagName, elementStyle)
@@ -65,44 +62,27 @@ const styled = ({tagName, elementStyle, mountSheet}: StyledArgs) => {
6562
}
6663

6764
classMap[this.dynamicTagName] = this.rulesIndex.slice(rulesTotal)
68-
69-
let cssRule
70-
let rule
71-
let cssRuleIndex = 0
72-
let ruleIndex = 0
73-
// nested styles become to flatten rules, so we need to update each nested rule
74-
for (ruleIndex; ruleIndex < classMap[this.dynamicTagName].length; ruleIndex++) {
75-
rule = classMap[this.dynamicTagName][ruleIndex]
76-
cssRule = this.cssRules[cssRulesTotal + cssRuleIndex]
77-
if (cssRule && cssRule.selectorText === rule.selectorText) {
78-
/**
79-
* we need to set cssRule in rule.renderable
80-
* @see {@link https://github.com/cssinjs/jss/issues/500}
81-
* and we don't want to use link(), because there is no need to iterate over all rules
82-
*/
83-
rule.renderable = cssRule
84-
cssRuleIndex++
85-
}
86-
this.sheet.update(rule.name, this.props)
87-
}
65+
this.updateSheet(this.props)
8866
}
8967

9068
componentWillReceiveProps(nextProps: StyledElementPropsType) {
91-
if (dynamicStyle) {
92-
let rule
93-
let ruleIndex = 0
94-
// the same rules update as in constructor
95-
for (ruleIndex; ruleIndex < classMap[this.dynamicTagName].length; ruleIndex++) {
96-
rule = classMap[this.dynamicTagName][ruleIndex]
97-
this.sheet.update(rule.name, nextProps)
98-
}
99-
}
69+
if (dynamicStyle) this.updateSheet(nextProps)
10070
}
10171

10272
componentWillUnmount() {
10373
availableDynamicTagNames.push(this.dynamicTagName)
10474
}
10575

76+
updateSheet(props: StyledElementPropsType) {
77+
let rule
78+
let ruleIndex = 0
79+
// nested styles become to flatten rules, so we need to update each nested rule
80+
for (ruleIndex; ruleIndex < classMap[this.dynamicTagName].length; ruleIndex++) {
81+
rule = classMap[this.dynamicTagName][ruleIndex]
82+
this.sheet.update(rule.name, props)
83+
}
84+
}
85+
10686
render() {
10787
const {children, className, ...attrs} = this.props
10888

0 commit comments

Comments
 (0)