-
Notifications
You must be signed in to change notification settings - Fork 1
/
Cress.js
59 lines (51 loc) · 1.8 KB
/
Cress.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
/**
* Constructor for creating Cress stylesheet components
* @param {String} selector The selector to match nodes that will become instances of the Cress component
* @param {Object} config The configuration object. See [the README on Github]{@link https://github.com/Heydon/cress/blob/master/README.md}
* @class
*/
function Cress(selector, config) {
this.elems = Array.from(document.querySelectorAll(selector));
this.this = config.this || 'this';
this.pattern = new RegExp(this.this, 'g');
this.sharedId = Math.random().toString(36).substr(2, 9);
this.reset = `${this.this}, ${this.this} * { all: initial }`;
this.getProps = elem => {
if (!config.props) {
return null;
}
let props = {};
for (let prop in config.props) {
let attr = elem.getAttribute(`data-cress-${prop}`);
props[prop] = attr ? attr : config.props[prop];
}
return props;
}
this.styleUp = elem => {
let props = this.getProps(elem);
let i = !props ? this.sharedId : this.sharedId + '-' + Object.values(props).join('');
elem.dataset.cress = i;
let styles = config.reset ? this.reset + config.css(props) : config.css(props);
let css = styles.replace(this.pattern, `[data-cress="${i}"]`);
if (!document.getElementById(i)) {
document.head.innerHTML += `
<style id="${i}">${css.replace(/\s\s+/g, ' ').trim()}</style>
`;
}
}
this.elems.forEach(elem => {
config.props && new MutationObserver(() => {
this.styleUp(elem);
}).observe(elem, {
attributes: true,
attributeFilter: Object.keys(config.props).map(k => `data-${k}`)
});
if ('ResizeObserver' in window && config.resize) {
new ResizeObserver(entries => {
config.resize(entries[0]);
}).observe(elem);
}
this.styleUp(elem);
});
}
export { Cress };