{"version":3,"file":"jss-plugin-rule-value-function.js","sources":["../../../node_modules/tiny-warning/dist/tiny-warning.esm.js","../src/index.js"],"sourcesContent":["var isProduction = process.env.NODE_ENV === 'production';\nfunction warning(condition, message) {\n if (!isProduction) {\n if (condition) {\n return;\n }\n\n var text = \"Warning: \" + message;\n\n if (typeof console !== 'undefined') {\n console.warn(text);\n }\n\n try {\n throw Error(text);\n } catch (x) {}\n }\n}\n\nexport default warning;\n","// @flow\nimport warning from 'tiny-warning'\nimport {\n createRule,\n type Rule,\n type JssStyle,\n type RuleOptions,\n type UpdateOptions,\n type StyleRule,\n type StyleSheet,\n type Plugin\n} from 'jss'\n\n// A symbol replacement.\nlet now = Date.now()\nconst fnValuesNs = `fnValues${now}`\nconst fnRuleNs = `fnStyle${++now}`\n\ntype StyleRuleWithRuleFunction = StyleRule & {[key: string]: Function}\n\ntype FunctionPlugin = () => Plugin\n\nconst functionPlugin: FunctionPlugin = () => ({\n onCreateRule(name?: string, decl: JssStyle, options: RuleOptions): Rule | null {\n if (typeof decl !== 'function') return null\n const rule: StyleRuleWithRuleFunction = (createRule(name, {}, options): any)\n rule[fnRuleNs] = decl\n return rule\n },\n\n onProcessStyle(style: JssStyle, rule: Rule): JssStyle {\n // We need to extract function values from the declaration, so that we can keep core unaware of them.\n // We need to do that only once.\n // We don't need to extract functions on each style update, since this can happen only once.\n // We don't support function values inside of function rules.\n if (fnValuesNs in rule || fnRuleNs in rule) return style\n\n const fnValues = {}\n for (const prop in style) {\n const value = style[prop]\n if (typeof value !== 'function') continue\n delete style[prop]\n fnValues[prop] = value\n }\n // $FlowFixMe[prop-missing]\n rule[fnValuesNs] = fnValues\n return style\n },\n\n onUpdate(data: Object, rule: Rule, sheet?: StyleSheet, options: UpdateOptions) {\n const styleRule: StyleRule = (rule: any)\n\n // $FlowFixMe[prop-missing]\n const fnRule = styleRule[fnRuleNs]\n\n // If we have a style function, the entire rule is dynamic and style object\n // will be returned from that function.\n if (fnRule) {\n // Empty object will remove all currently defined props\n // in case function rule returns a falsy value.\n styleRule.style = fnRule(data) || {}\n\n if (process.env.NODE_ENV === 'development') {\n for (const prop in styleRule.style) {\n if (typeof styleRule.style[prop] === 'function') {\n warning(false, '[JSS] Function values inside function rules are not supported.')\n break\n }\n }\n }\n }\n\n // $FlowFixMe[prop-missing]\n const fnValues = styleRule[fnValuesNs]\n\n // If we have a fn values map, it is a rule with function values.\n if (fnValues) {\n for (const prop in fnValues) {\n styleRule.prop(prop, fnValues[prop](data), options)\n }\n }\n }\n})\n\nexport default functionPlugin\n"],"names":["now","Date","fnValuesNs","fnRuleNs","functionPlugin","onCreateRule","name","decl","options","rule","createRule","onProcessStyle","style","fnValues","prop","value","onUpdate","data","sheet","styleRule","fnRule","warning"],"mappings":";;;;;;EACA,SAAS,OAAO,CAAC,SAAS,EAAE,OAAO,EAAE;EACrC,EAAqB;EACrB,IAAI,IAAI,SAAS,EAAE;EACnB,MAAM,OAAO;EACb,KAAK;AACL;EACA,IAAI,IAAI,IAAI,GAAG,WAAW,GAAG,OAAO,CAAC;AACrC;EACA,IAAI,IAAI,OAAO,OAAO,KAAK,WAAW,EAAE;EACxC,MAAM,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;EACzB,KAAK;AACL;EACA,IAAI,IAAI;EACR,MAAM,MAAM,KAAK,CAAC,IAAI,CAAC,CAAC;EACxB,KAAK,CAAC,OAAO,CAAC,EAAE,EAAE;EAClB,GAAG;EACH;;ECHA,IAAIA,GAAG,GAAGC,IAAI,CAACD,GAAL,EAAV;EACA,IAAME,UAAU,gBAAcF,GAA9B;EACA,IAAMG,QAAQ,eAAa,EAAEH,GAA7B;;MAMMI,cAA8B,GAAG,SAAjCA,cAAiC;EAAA,SAAO;EAC5CC,IAAAA,YAD4C,wBAC/BC,IAD+B,EAChBC,IADgB,EACAC,OADA,EACmC;EAC7E,UAAI,OAAOD,IAAP,KAAgB,UAApB,EAAgC,OAAO,IAAP;EAChC,UAAME,IAA+B,GAAIC,cAAU,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