1 | import { from as flatten } from 'array-flatten';
|
2 | import globalCache from 'global-cache';
|
3 |
|
4 | import { GLOBAL_CACHE_KEY } from './utils/constants';
|
5 | import getClassName from './utils/getClassName';
|
6 | import separateStyles from './utils/separateStyles';
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | function create(stylesObject) {
|
18 | const stylesToClasses = {};
|
19 | const styleNames = Object.keys(stylesObject);
|
20 | const sharedState = globalCache.get(GLOBAL_CACHE_KEY) || {};
|
21 | const { namespace = '' } = sharedState;
|
22 | styleNames.forEach((styleName) => {
|
23 | const className = getClassName(namespace, styleName);
|
24 | stylesToClasses[styleName] = className;
|
25 | });
|
26 | return stylesToClasses;
|
27 | }
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | function resolve(stylesArray) {
|
38 | const flattenedStyles = flatten(stylesArray);
|
39 | const { classNames, hasInlineStyles, inlineStyles } = separateStyles(flattenedStyles);
|
40 | const specificClassNames = classNames.map((name, index) => `${name} ${name}_${index + 1}`);
|
41 | const className = specificClassNames.join(' ');
|
42 |
|
43 | const result = { className };
|
44 | if (hasInlineStyles) result.style = inlineStyles;
|
45 | return result;
|
46 | }
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | function registerCSSInterfaceNamespace(CSSInterfaceNamespace) {
|
54 | const sharedState = globalCache.get(GLOBAL_CACHE_KEY);
|
55 | if (!sharedState) {
|
56 | globalCache.set(GLOBAL_CACHE_KEY, { namespace: CSSInterfaceNamespace });
|
57 | } else {
|
58 | sharedState.namespace = CSSInterfaceNamespace;
|
59 | }
|
60 | }
|
61 |
|
62 | export default { create, resolve };
|
63 | export { registerCSSInterfaceNamespace };
|