MCPcopy
hub / github.com/cssinjs/jss / functionPlugin

Function functionPlugin

packages/jss-plugin-rule-value-function/src/index.js:9–66  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

7const fnRuleNs = `fnStyle${++now}`
8
9const functionPlugin = () => ({
10 onCreateRule(name, decl, options) {
11 if (typeof decl !== 'function') return null
12 const rule = createRule(name, {}, options)
13 rule[fnRuleNs] = decl
14 return rule
15 },
16
17 onProcessStyle(style, rule) {
18 // We need to extract function values from the declaration, so that we can keep core unaware of them.
19 // We need to do that only once.
20 // We don't need to extract functions on each style update, since this can happen only once.
21 // We don't support function values inside of function rules.
22 if (fnValuesNs in rule || fnRuleNs in rule) return style
23
24 const fnValues = {}
25 for (const prop in style) {
26 const value = style[prop]
27 if (typeof value !== 'function') continue
28 delete style[prop]
29 fnValues[prop] = value
30 }
31 rule[fnValuesNs] = fnValues
32 return style
33 },
34
35 onUpdate(data, rule, sheet, options) {
36 const styleRule = rule
37
38 const fnRule = styleRule[fnRuleNs]
39
40 // If we have a style function, the entire rule is dynamic and style object
41 // will be returned from that function.
42 if (fnRule) {
43 // Empty object will remove all currently defined props
44 // in case function rule returns a falsy value.
45 styleRule.style = fnRule(data) || {}
46
47 if (process.env.NODE_ENV === 'development') {
48 for (const prop in styleRule.style) {
49 if (typeof styleRule.style[prop] === 'function') {
50 warning(false, '[JSS] Function values inside function rules are not supported.')
51 break
52 }
53 }
54 }
55 }
56
57 const fnValues = styleRule[fnValuesNs]
58
59 // If we have a fn values map, it is a rule with function values.
60 if (fnValues) {
61 for (const prop in fnValues) {
62 styleRule.prop(prop, fnValues[prop](data), options)
63 }
64 }
65 }
66})

Callers 7

index.test.jsFile · 0.85
index.test.jsFile · 0.85
index.test.jsFile · 0.85
index.test.jsFile · 0.85
index.test.jsFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…