-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
75 lines (57 loc) · 2.04 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
let stylesheet;
let cache = {
css: '',
classesNames: {}
};
export function c(rules) {
return parse(rules);
}
export function styles() {
return cache.css;
}
function insert(rule) {
if (typeof document !== 'undefined') {
if (!stylesheet) {
stylesheet = document.createElement('style');
document.body.appendChild(stylesheet);
}
// @see: https://stackoverflow.com/a/28930990
stylesheet.appendChild(document.createTextNode(rule));
}
if (typeof document === 'undefined' && cache.css.indexOf(rule) === -1) {
cache.css = cache.css + rule;
}
}
// @see https://github.com/jxnblk/object-style/blob/v1.0.3/src/index.js
// @see https://github.com/cxs-css/cxs/blob/master/src/index.js
function parse(obj, children = '', media = '') {
return Object.keys(obj)
.map((key) => {
const value = obj[key];
if (value === null) return '';
if (typeof value === 'object') {
const isMedia = /^@m/.test(key);
return parse(
value,
isMedia ? children : (children + key).replace(' ', ''),
isMedia ? key : media
);
}
const id = key + value + children + (media || '');
if (cache.classesNames[id]) return cache.classesNames[id];
// @see https://github.com/cristianbote/goober/blob/v1/src/core/to-hash.js#L10
const className =
'c' + id.split('').reduce((out, i) => (101 * out + i.charCodeAt(0)) >>> 0, 11);
insert(createRule(className, key, value, children, media));
cache.classesNames[id] = className;
return className;
})
.join(' ');
}
function createRule(className, key, value, children, media) {
const selector = '.' + className + children;
const rule =
selector + '{' + key.replace(/[A-Z]|^ms/g, '-$&').toLowerCase() + ':' + value + '}';
if (!media) return rule;
return media + '{' + rule + '}';
}