65 lines
2.3 KiB
JavaScript
65 lines
2.3 KiB
JavaScript
(function (global, factory) {
|
|
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('jss')) :
|
|
typeof define === 'function' && define.amd ? define(['exports', 'jss'], factory) :
|
|
(global = global || self, factory(global.jssPluginRuleValueFunction = {}, global.jss));
|
|
}(this, function (exports, jss) { 'use strict';
|
|
|
|
var now = Date.now();
|
|
var fnValuesNs = "fnValues" + now;
|
|
var fnRuleNs = "fnStyle" + ++now;
|
|
function functionPlugin() {
|
|
return {
|
|
onCreateRule: function onCreateRule(name, decl, options) {
|
|
if (typeof decl !== 'function') return null;
|
|
var rule = jss.createRule(name, {}, options);
|
|
rule[fnRuleNs] = decl;
|
|
return rule;
|
|
},
|
|
onProcessStyle: function onProcessStyle(style, rule) {
|
|
// We need to extract function values from the declaration, so that we can keep core unaware of them.
|
|
// We need to do that only once.
|
|
// We don't need to extract functions on each style update, since this can happen only once.
|
|
// We don't support function values inside of function rules.
|
|
if (fnValuesNs in rule || fnRuleNs in rule) return style;
|
|
var fnValues = {};
|
|
|
|
for (var prop in style) {
|
|
var value = style[prop];
|
|
if (typeof value !== 'function') continue;
|
|
delete style[prop];
|
|
fnValues[prop] = value;
|
|
} // $FlowFixMe
|
|
|
|
|
|
rule[fnValuesNs] = fnValues;
|
|
return style;
|
|
},
|
|
onUpdate: function onUpdate(data, rule, sheet, options) {
|
|
var styleRule = rule;
|
|
var fnRule = styleRule[fnRuleNs]; // If we have a style function, the entire rule is dynamic and style object
|
|
// will be returned from that function.
|
|
|
|
if (fnRule) {
|
|
// Empty object will remove all currently defined props
|
|
// in case function rule returns a falsy value.
|
|
styleRule.style = fnRule(data) || {};
|
|
}
|
|
|
|
var fnValues = styleRule[fnValuesNs]; // If we have a fn values map, it is a rule with function values.
|
|
|
|
if (fnValues) {
|
|
for (var prop in fnValues) {
|
|
styleRule.prop(prop, fnValues[prop](data), options);
|
|
}
|
|
}
|
|
}
|
|
};
|
|
}
|
|
|
|
exports.default = functionPlugin;
|
|
|
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
|
}));
|
|
//# sourceMappingURL=jss-plugin-rule-value-function.js.map
|