-
-
Notifications
You must be signed in to change notification settings - Fork 389
Expand file tree
/
Copy pathsheets.js
More file actions
120 lines (97 loc) · 3.23 KB
/
sheets.js
File metadata and controls
120 lines (97 loc) · 3.23 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
import warning from 'tiny-warning'
import {getDynamicStyles} from 'jss'
import {getManager} from './managers'
import defaultJss from '../jss'
import {addMeta, getMeta} from './sheetsMeta'
const getStyles = (options) => {
const {styles} = options
if (typeof styles !== 'function') {
return styles
}
warning(
styles.length !== 0,
`[JSS] <${
options.name || 'Hook'
} />'s styles function doesn't rely on the "theme" argument. We recommend declaring styles as an object instead.`
)
return styles(options.theme)
}
function getSheetOptions(options, link) {
let minify
if (options.context.id && options.context.id.minify != null) {
minify = options.context.id.minify
}
let classNamePrefix = options.context.classNamePrefix || ''
if (options.name && !minify) {
classNamePrefix += `${options.name.replace(/\s/g, '-')}-`
}
let meta = ''
if (options.name) meta = `${options.name}, `
meta += typeof options.styles === 'function' ? 'Themed' : 'Unthemed'
return {
...options.sheetOptions,
index: options.index,
meta,
classNamePrefix,
link,
generateId:
options.sheetOptions && options.sheetOptions.generateId
? options.sheetOptions.generateId
: options.context.generateId
}
}
export const createStyleSheet = (options) => {
if (options.context.disableStylesGeneration) {
return undefined
}
const manager = getManager(options.context, options.index)
const existingSheet = manager.get(options.theme)
if (existingSheet) {
return existingSheet
}
const jss = options.context.jss || defaultJss
const styles = getStyles(options)
const dynamicStyles = getDynamicStyles(styles)
const sheet = jss.createStyleSheet(styles, getSheetOptions(options, dynamicStyles !== null))
addMeta(sheet, {
dynamicStyles,
styles
})
manager.add(options.theme, sheet)
return sheet
}
export const removeDynamicRules = (sheet, rules) => {
// Loop over each dynamic rule and remove the dynamic rule
// We can't just remove the whole sheet as this has all of the rules for every component instance
for (const key in rules) {
sheet.deleteRule(rules[key])
}
}
export const updateDynamicRules = (data, sheet, rules) => {
// Loop over each dynamic rule and update it
// We can't just update the whole sheet as this has all of the rules for every component instance
for (const key in rules) {
sheet.updateOne(rules[key], data)
}
}
export const addDynamicRules = (sheet, data) => {
const meta = getMeta(sheet)
if (!meta) {
return undefined
}
const rules = {}
// Loop over each dynamic rule and add it to the stylesheet
for (const key in meta.dynamicStyles) {
const initialRuleCount = sheet.rules.index.length
const originalRule = sheet.addRule(key, meta.dynamicStyles[key])
// Loop through all created rules, fixes updating dynamic rules
for (let i = initialRuleCount; i < sheet.rules.index.length; i++) {
const rule = sheet.rules.index[i]
sheet.updateOne(rule, data)
// If it's the original rule, we need to add it by the correct key so the hook and hoc
// can correctly concat the dynamic class with the static one
rules[originalRule === rule ? key : rule.key] = rule
}
}
return rules
}