-
Notifications
You must be signed in to change notification settings - Fork 1
/
Cress.min.js
1 lines (1 loc) · 1018 Bytes
/
Cress.min.js
1
function Cress(e,t){this.elems=Array.from(document.querySelectorAll(e)),this.this=t.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=(e=>{if(!t.props)return null;let s={};for(let r in t.props){let i=e.getAttribute(`data-cress-${r}`);s[r]=i||t.props[r]}return s}),this.styleUp=(e=>{let s=this.getProps(e),r=s?this.sharedId+"-"+Object.values(s).join(""):this.sharedId;e.dataset.cress=r;let i=(t.reset?this.reset+t.css(s):t.css(s)).replace(this.pattern,`[data-cress="${r}"]`);document.getElementById(r)||(document.head.innerHTML+=`\n <style id="${r}">${i.replace(/\s\s+/g," ").trim()}</style>\n `)}),this.elems.forEach(e=>{t.props&&new MutationObserver(()=>{this.styleUp(e)}).observe(e,{attributes:!0,attributeFilter:Object.keys(t.props).map(e=>`data-${e}`)}),"ResizeObserver"in window&&t.resize&&new ResizeObserver(e=>{t.resize(e[0])}).observe(e),this.styleUp(e)})}export{Cress};