UNPKG

3.09 kBJavaScriptView Raw
1Object.defineProperty(exports, "__esModule", {
2 value: true
3});
4exports.registerCSSInterfaceNamespace = undefined;
5
6var _arrayFlatten = require('array-flatten');
7
8var _globalCache = require('global-cache');
9
10var _globalCache2 = _interopRequireDefault(_globalCache);
11
12var _constants = require('./utils/constants');
13
14var _getClassName = require('./utils/getClassName');
15
16var _getClassName2 = _interopRequireDefault(_getClassName);
17
18var _separateStyles2 = require('./utils/separateStyles');
19
20var _separateStyles3 = _interopRequireDefault(_separateStyles2);
21
22function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
23
24/**
25 * Function required as part of the react-with-styles interface. Parses the styles provided by
26 * react-with-styles to produce class names based on the style name and optionally the namespace if
27 * available.
28 *
29 * stylesObject {Object} The styles object passed to withStyles.
30 *
31 * Return an object mapping style names to class names.
32 */
33function create(stylesObject) {
34 var stylesToClasses = {};
35 var styleNames = Object.keys(stylesObject);
36 var sharedState = _globalCache2['default'].get(_constants.GLOBAL_CACHE_KEY) || {};
37 var _sharedState$namespac = sharedState.namespace,
38 namespace = _sharedState$namespac === undefined ? '' : _sharedState$namespac;
39
40 styleNames.forEach(function (styleName) {
41 var className = (0, _getClassName2['default'])(namespace, styleName);
42 stylesToClasses[styleName] = className;
43 });
44 return stylesToClasses;
45}
46
47/**
48 * Process styles to be consumed by a component.
49 *
50 * stylesArray {Array} Array of the following: values returned by create, plain JavaScript objects
51 * representing inline styles, or arrays thereof.
52 *
53 * Return an object with optional className and style properties to be spread on a component.
54 */
55function resolve(stylesArray) {
56 var flattenedStyles = (0, _arrayFlatten.from)(stylesArray);
57
58 var _separateStyles = (0, _separateStyles3['default'])(flattenedStyles),
59 classNames = _separateStyles.classNames,
60 hasInlineStyles = _separateStyles.hasInlineStyles,
61 inlineStyles = _separateStyles.inlineStyles;
62
63 var specificClassNames = classNames.map(function (name, index) {
64 return String(name) + ' ' + String(name) + '_' + String(index + 1);
65 });
66 var className = specificClassNames.join(' ');
67
68 var result = { className: className };
69 if (hasInlineStyles) result.style = inlineStyles;
70 return result;
71}
72
73/**
74 * Register a namespace to use for constructing unique class names.
75 *
76 * CSSInterfaceNamespace {String} The namespace to be used. e.g. Name of the project
77 */
78function registerCSSInterfaceNamespace(CSSInterfaceNamespace) {
79 var sharedState = _globalCache2['default'].get(_constants.GLOBAL_CACHE_KEY);
80 if (!sharedState) {
81 _globalCache2['default'].set(_constants.GLOBAL_CACHE_KEY, { namespace: CSSInterfaceNamespace });
82 } else {
83 sharedState.namespace = CSSInterfaceNamespace;
84 }
85}
86
87exports['default'] = { create: create, resolve: resolve };
88exports.registerCSSInterfaceNamespace = registerCSSInterfaceNamespace;
\No newline at end of file