forked from kasinskas/react-native-media-query
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinject.js
More file actions
38 lines (32 loc) · 975 Bytes
/
inject.js
File metadata and controls
38 lines (32 loc) · 975 Bytes
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
import React from 'react';
const rules = {};
let styleSheet;
if (typeof window !== 'undefined') {
styleSheet = (() => {
const style = document.createElement('style');
style.id = 'rnwsCSS';
style.appendChild(document.createTextNode(''));
document.head.appendChild(style);
return style.sheet;
})();
}
export const setRule = (id, text) => {
if (!hasRule(id, text)) {
rules[id] = rules?.[id] || {};
rules[id].text = (rules[id]?.text || '') + text;
if (styleSheet) {
styleSheet.insertRule(text);
}
}
};
export const hasRule = (id, text) => !!rules[id] && !!rules[id].text?.includes?.(text);
export const collectWebStyles = () =>
React.createElement('style', {
id: 'rnws',
key: 'rnws',
dangerouslySetInnerHTML: {
__html: Object.keys(rules)
.map((key) => rules[key].text)
.join('\n'),
},
});