tonami
Version:
Minimal CSS-in-JS library that promotes CSS best-practices and strongly-typed design systems.
3 lines (2 loc) • 7.63 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],t):t((e||self).tonami={},e.react)}(this,function(e,t){function n(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var r=n(t);function i(e){var t=[];for(var n in e)t.push("className"===n?"."+e.className.split(" ").join("."):!0===e[n]?"["+n+"]":"["+n+"="+e[n]+"]");return t.join("")}function a(e,t,n){return void 0===n&&(n="& {}"),n.replace(/\&/gi,e).replace(/\{\}/gi,"{ "+function(e){var t=[];for(var n in e)"--"===n.slice(0,2)?t.push(n+": "+e[n]+";"):t.push(n.replace(/([a-z0-9]|(?=[A-Z]))([A-Z])/g,"$1-$2").toLowerCase()+": "+e[n]+";");return t.join(" ")}(t)+" }")}function o(){return(o=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e}).apply(this,arguments)}function s(e,t){if(null==e)return{};var n,r,i={},a=Object.keys(e);for(r=0;r<a.length;r++)t.indexOf(n=a[r])>=0||(i[n]=e[n]);return i}function u(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,r=new Array(t);n<t;n++)r[n]=e[n];return r}function l(e,t){var n="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(n)return(n=n.call(e)).next.bind(n);if(Array.isArray(e)||(n=function(e,t){if(e){if("string"==typeof e)return u(e,t);var n=Object.prototype.toString.call(e).slice(8,-1);return"Object"===n&&e.constructor&&(n=e.constructor.name),"Map"===n||"Set"===n?Array.from(e):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?u(e,t):void 0}}(e))||t&&e&&"number"==typeof e.length){n&&(e=n);var r=0;return function(){return r>=e.length?{done:!0}:{done:!1,value:e[r++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var c="undefined"!=typeof window&&"HTMLElement"in window,f="tonami",d="tonami-ssr",h={shouldForwardProp:function(e,t){return"$"!==e[0]}},m=new(function(){function e(){this.rules=[],this.tag=this.findOrCreateTag(),this.sheet=this.getSheet(),this.dynamicIndexes=[]}var t=e.prototype;return t.findOrCreateTag=function(){if(this.tag)return this.tag;if(c){var e,t=document.getElementById(f);if(t)return t;var n=document.createElement("style");return n.setAttribute("id",f),n.setAttribute("data-testid",f),document.head.appendChild(n),null==(e=document.getElementById(d))||e.remove(),n}return null},t.getSheet=function(){return this.tag?function(e){if(e.sheet)return e.sheet;for(var t=document.styleSheets,n=0,r=t.length;n<r;n++){var i=t[n];if(i.ownerNode===e)return i}throw new Error("Unable to get CSSStyleSheet")}(this.tag):null},t.insertStaticRules=function(e){for(var t,n,r=l(e);!(n=r()).done;){var i=n.value;this.rules.indexOf(i)>-1||(this.rules[t=this.rules.length]=i,this.sheet&&this.sheet.insertRule(i,t))}},t.insertDynamicRules=function(e){for(var t,n,r,i=[],a=l(e);!(r=a()).done;)n=this.dynamicIndexes.length,this.rules[t=this.rules.length]=r.value,this.sheet&&this.sheet.insertRule(this.rules[t],t),this.dynamicIndexes[n]=t,i.push(n);return i.sort(function(e,t){return t-e})},t.removeRules=function(e){for(var t,n,r=l(e);!(n=r()).done;){var i=n.value;this.rules.splice(t=this.dynamicIndexes[i],1);try{this.sheet.deleteRule(t),this.dynamicIndexes[i]=-1;for(var a=0;a<this.dynamicIndexes.length;a++){var o=this.dynamicIndexes[a];o>t&&(this.dynamicIndexes[a]=o-1)}}catch(e){console.log(e.message)}}},t.getStyleString=function(){return this.rules.join("\n")},t.reset=function(){this.rules=[],this.dynamicIndexes=[],this.tag&&this.tag.remove(),this.tag=null,this.tag=this.findOrCreateTag(),this.sheet=this.getSheet()},e}());function p(e){var n,r=t.useRef([]),i=t.useRef([]);n=r.current,JSON.stringify(e.sort())!==JSON.stringify(n.sort())&&(i.current.length&&m.removeRules(i.current),i.current=m.insertDynamicRules(e),r.current=e),t.useEffect(function(){return function(){i.current.length&&m.removeRules(i.current)}},[])}function y(e,t){var n=Object.assign({},e);for(var r in e)"function"==typeof e[r]&&(n[r]=e[r](t));return n}function v(e){return function(t){var n=[];for(var r in e)n.push(a(r,y(e[r],t)));return p(n),null}}function g(e){return"TA"+b(e)}var b=function(e){for(var t=0,n=37;t<e.length;)n=97*n+e.charCodeAt(t++)>>>0;return n.toString(16)},S=["apply","condition","selectors"];function j(){for(var e=[].slice.call(arguments),t=g(JSON.stringify(e)),n=[],r=new Map,u=new Map,l=0;l<e.length;l++){var c,f,d=e[l],h=d.apply,p=d.condition,y=d.selectors,v=s(d,S),b=N(v,u,t);p=null==(c=p)||c,h=null!=(f=h)?f:{className:g(JSON.stringify(b))},r.set(h,p);var j="."+t+i(h);if(n.push(a(j,b)),y)for(var x in y)n.push(a(j,N(y[x],u,t),x))}function I(e){var n=O(e),i=w(r,u,t,e),a=n.style?o({},i.style,n.style):i.style;return o({},i,n,{style:a,className:n.className?i.className+" "+n.className:i.className})}return m.insertStaticRules(n),I}function O(e){var t={};for(var n in e)h.shouldForwardProp(n,e[n])&&(t[n]=e[n]);return t}function w(e,t,n,r){for(var i,a={style:{},className:[n]},s=l(e);!(i=s()).done;){var u=i.value,c=u[0],f=u[1];("function"==typeof f?f(r):f)&&("className"in c?a.className.push(c.className):a=o({},a,c))}a.className=a.className.join(" ");for(var d,h=l(t);!(d=h()).done;){var m=d.value;a.style[m[0]]=(0,m[1])(r)}return a}function N(e,t,n){var r={};if(!e)return r;for(var i in e){var a=e[i];if("function"==typeof a){var o="--"+n+"-"+t.size;t.set(o,a),r[i]="var("+o+")"}else r[i]=a}return r}function x(e){return function(){var n=j.apply(void 0,[].slice.call(arguments)),i=e.isStyledComponent?e.getComponentProps.concat(n):[n],a=t.forwardRef(function(t,n){var a,o=null!=(a=t.as)?a:e,s=i.reduce(function(e,t){return t(e)},t);return r.default.createElement(o,Object.assign({},s,{ref:n}))}),o=t.memo(a);return Object.assign(o,{isStyledComponent:!0,getComponentProps:i}),o}}var I=x;["a","abbr","address","area","article","aside","audio","b","base","bdi","bdo","big","blockquote","body","br","button","canvas","caption","cite","code","col","colgroup","data","datalist","dd","del","details","dfn","dialog","div","dl","dt","em","embed","fieldset","figcaption","figure","footer","form","h1","h2","h3","h4","h5","h6","head","header","hgroup","hr","html","i","iframe","img","input","ins","kbd","keygen","label","legend","li","link","main","map","mark","menu","menuitem","meta","meter","nav","noscript","object","ol","optgroup","option","output","p","param","picture","pre","progress","q","rp","rt","ruby","s","samp","script","section","select","small","source","span","strong","style","sub","summary","sup","table","tbody","td","textarea","tfoot","th","thead","time","title","tr","track","u","ul","var","video","wbr","circle","clipPath","defs","ellipse","foreignObject","g","image","line","linearGradient","marker","mask","path","pattern","polygon","polyline","radialGradient","rect","stop","svg","text","tspan"].forEach(function(e){I[e]=x(e)}),e.ServerStylesheet=function(){return r.default.createElement("style",{id:d,dangerouslySetInnerHTML:{__html:m.getStyleString()}})},e.createGlobalStyle=v,e.createTokens=function e(t){var n={},r={};return Object.setPrototypeOf(r,{toStyleObject:function(){return n},Tokens:v({html:n}),createDynamicTokens:function(t){return function(n){return p([a("html",e(t(n)).toStyleObject())]),null}}}),function e(t,r,i){for(var a in void 0===i&&(i=[]),t){var o=t[a],s=i.concat(a);if(["string","number"].includes(typeof o)){var u="--"+s.join("-");n[u]=o,r[a]="var("+u+")"}else"object"==typeof o&&(r[a]={},e(o,r[a],s))}}(t,r),r},e.options=h,e.styled=I});
//# sourceMappingURL=tonami.umd.js.map