-
-
Notifications
You must be signed in to change notification settings - Fork 390
Expand file tree
/
Copy pathlayerRule.js
More file actions
89 lines (75 loc) · 2.06 KB
/
layerRule.js
File metadata and controls
89 lines (75 loc) · 2.06 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
import RuleList from '../RuleList'
import getWhitespaceSymbols from '../utils/getWhitespaceSymbols'
const defaultToStringOptions = {
indent: 1,
children: true
}
const atRegExp = /@([\w-]+)/
/**
* Rule for @layer
*/
export class LayerRule {
type = 'layer'
isProcessed = false
constructor(key, styles, options) {
this.key = key
const atMatch = key.match(atRegExp)
this.at = atMatch ? atMatch[1] : 'unknown'
// Key might contain a unique suffix in case the `name` passed by user was duplicate.
this.query = options.name || `@${this.at}`
this.options = options
this.rules = new RuleList({...options, parent: this})
for (const name in styles) {
this.rules.add(name, styles[name])
}
this.rules.process()
}
/**
* Get a rule.
*/
getRule(name) {
return this.rules.get(name)
}
/**
* Get index of a rule.
*/
indexOf(rule) {
return this.rules.indexOf(rule)
}
/**
* Create and register rule, run plugins.
*/
addRule(name, style, options) {
const rule = this.rules.add(name, style, options)
if (!rule) return null
this.options.jss.plugins.onProcessRule(rule)
return rule
}
/**
* Replace rule, run plugins.
*/
replaceRule(name, style, options) {
const newRule = this.rules.replace(name, style, options)
if (newRule) this.options.jss.plugins.onProcessRule(newRule)
return newRule
}
/**
* Generates a CSS string.
*/
toString(options = defaultToStringOptions) {
const {linebreak} = getWhitespaceSymbols(options)
if (options.indent == null) options.indent = defaultToStringOptions.indent
if (options.children == null) options.children = defaultToStringOptions.children
if (options.children === false) {
return `${this.query} {}`
}
const children = this.rules.toString(options)
return children ? `${this.query} {${linebreak}${children}${linebreak}}` : ''
}
}
const keyRegExp = /@layer\s+/
export default {
onCreateRule(key, styles, options) {
return keyRegExp.test(key) ? new LayerRule(key, styles, options) : null
}
}