forked from callstack/linaria
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsheet.js
More file actions
49 lines (39 loc) · 1.17 KB
/
Copy pathsheet.js
File metadata and controls
49 lines (39 loc) · 1.17 KB
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
/* @flow */
import stylis from 'stylis';
function sheet() {
let cache: { [selector: string]: string } = {};
const isBrowser =
typeof window === 'object' && window != null && window.document != null;
// eslint-disable-next-line global-require
const document = isBrowser ? window.document : require('./document').default;
const style = document.createElement('style');
const node = document.createTextNode('');
style.appendChild(node);
style.setAttribute('type', 'text/css');
document.head.appendChild(style);
if (process.env.NODE_ENV !== 'test' && isBrowser) {
console.warn(
'Babel preset for Linaria is not configured. See https://github.com/callstack/linaria/blob/master/docs/BABEL_PRESET.md for instructions.'
);
}
return {
insert(selector: string, css: string) {
if (selector in cache) {
return;
}
const text = stylis(selector, css);
cache[selector] = css;
node.appendData(`\n${text}`);
},
styles() {
return cache;
},
dump() {
const result = node.textContent;
cache = {};
node.textContent = '';
return result.trim();
},
};
}
export default sheet();