-
-
Notifications
You must be signed in to change notification settings - Fork 389
Expand file tree
/
Copy pathStyleSheet.js
More file actions
197 lines (164 loc) · 4.25 KB
/
StyleSheet.js
File metadata and controls
197 lines (164 loc) · 4.25 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
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
194
195
196
197
/* @flow */
import linkRule from './utils/linkRule'
import RuleList from './RuleList'
import type {
InternalStyleSheetOptions,
Rule,
ToCssOptions,
RuleOptions,
StyleSheetOptions,
JssStyle
} from './types'
export default class StyleSheet {
options: InternalStyleSheetOptions
linked: boolean
deployed: boolean
attached: boolean
rules: RuleList
renderer: Object
classes: Object
queue: ?Array<Rule>
constructor(styles: Object, options: StyleSheetOptions) {
this.attached = false
this.deployed = false
this.linked = false
this.classes = {}
this.options = {
...options,
sheet: this,
parent: this,
classes: this.classes
}
this.renderer = new options.Renderer(this)
this.rules = new RuleList(this.options)
for (const name in styles) {
this.rules.add(name, styles[name])
}
this.rules.process()
}
/**
* Attach renderable to the render tree.
*/
attach(): this {
if (this.attached) return this
if (!this.deployed) this.deploy()
this.renderer.attach()
if (!this.linked && this.options.link) this.link()
this.attached = true
return this
}
/**
* Remove renderable from render tree.
*/
detach(): this {
if (!this.attached) return this
this.renderer.detach()
this.attached = false
return this
}
/**
* Add a rule to the current stylesheet.
* Will insert a rule also after the stylesheet has been rendered first time.
*/
addRule(name: string, decl: JssStyle, options?: RuleOptions): Rule {
const {queue} = this
// Plugins can create rules.
// In order to preserve the right order, we need to queue all `.addRule` calls,
// which happen after the first `rules.add()` call.
if (this.attached && !queue) this.queue = []
const rule = this.rules.add(name, decl, options)
this.options.jss.plugins.onProcessRule(rule)
if (this.attached) {
if (!this.deployed) return rule
// Don't insert rule directly if there is no stringified version yet.
// It will be inserted all together when .attach is called.
if (queue) queue.unshift(rule)
else {
this.insertRule(rule)
if (this.queue) {
this.queue.forEach(this.insertRule, this)
this.queue = undefined
}
}
return rule
}
// We can't add rules to a detached style node.
// We will redeploy the sheet once user will attach it.
this.deployed = false
return rule
}
/**
* Insert rule into the StyleSheet
*/
insertRule(rule: Rule) {
const renderable = this.renderer.insertRule(rule)
if (renderable && this.options.link) linkRule(rule, renderable)
}
/**
* Create and add rules.
* Will render also after Style Sheet was rendered the first time.
*/
addRules(styles: Object, options?: RuleOptions): Array<Rule> {
const added = []
for (const name in styles) {
added.push(this.addRule(name, styles[name], options))
}
return added
}
/**
* Get a rule by name.
*/
getRule(name: string): Rule {
return this.rules.get(name)
}
/**
* Delete a rule by name.
* Returns `true`: if rule has been deleted from the DOM.
*/
deleteRule(name: string): boolean {
const rule = this.rules.get(name)
if (!rule) return false
this.rules.remove(rule)
if (this.attached && rule.renderable) {
return this.renderer.deleteRule(rule.renderable)
}
return true
}
/**
* Get index of a rule.
*/
indexOf(rule: Rule): number {
return this.rules.indexOf(rule)
}
/**
* Deploy pure CSS string to a renderable.
*/
deploy(): this {
this.renderer.deploy()
this.deployed = true
return this
}
/**
* Link renderable CSS rules from sheet with their corresponding models.
*/
link(): this {
const cssRules = this.renderer.getRules()
// Is undefined when VirtualRenderer is used.
if (cssRules) this.rules.link(cssRules)
this.linked = true
return this
}
/**
* Update the function values with a new data.
*/
update(name?: string, data?: Object): this {
this.rules.update(name, data)
return this
}
/**
* Convert rules to a CSS string.
*/
toString(options?: ToCssOptions): string {
return this.rules.toString(options)
}
}