UNPKG

tonami

Version:

Minimal CSS-in-JS library that promotes CSS best-practices and strongly-typed design systems.

3 lines (2 loc) 7.47 kB
var e=require("react");function t(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var r=t(e);function n(e){var t=[];for(var r in e)t.push("className"===r?"."+e.className.split(" ").join("."):!0===e[r]?"["+r+"]":"["+r+"="+e[r]+"]");return t.join("")}function i(e,t,r){return void 0===r&&(r="& {}"),r.replace(/\&/gi,e).replace(/\{\}/gi,"{ "+function(e){var t=[];for(var r in e)"--"===r.slice(0,2)?t.push(r+": "+e[r]+";"):t.push(r.replace(/([a-z0-9]|(?=[A-Z]))([A-Z])/g,"$1-$2").toLowerCase()+": "+e[r]+";");return t.join(" ")}(t)+" }")}function a(){return(a=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e}).apply(this,arguments)}function o(e,t){if(null==e)return{};var r,n,i={},a=Object.keys(e);for(n=0;n<a.length;n++)t.indexOf(r=a[n])>=0||(i[r]=e[r]);return i}function s(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=new Array(t);r<t;r++)n[r]=e[r];return n}function u(e,t){var r="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(r)return(r=r.call(e)).next.bind(r);if(Array.isArray(e)||(r=function(e,t){if(e){if("string"==typeof e)return s(e,t);var r=Object.prototype.toString.call(e).slice(8,-1);return"Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r?Array.from(e):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?s(e,t):void 0}}(e))||t&&e&&"number"==typeof e.length){r&&(e=r);var n=0;return function(){return n>=e.length?{done:!0}:{done:!1,value:e[n++]}}}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 l="undefined"!=typeof window&&"HTMLElement"in window,c={shouldForwardProp:function(e,t){return"$"!==e[0]}},f=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(l){var e,t=document.getElementById("tonami");if(t)return t;var r=document.createElement("style");return r.setAttribute("id","tonami"),r.setAttribute("data-testid","tonami"),document.head.appendChild(r),null==(e=document.getElementById("tonami-ssr"))||e.remove(),r}return null},t.getSheet=function(){return this.tag?function(e){if(e.sheet)return e.sheet;for(var t=document.styleSheets,r=0,n=t.length;r<n;r++){var i=t[r];if(i.ownerNode===e)return i}throw new Error("Unable to get CSSStyleSheet")}(this.tag):null},t.insertStaticRules=function(e){for(var t,r,n=u(e);!(r=n()).done;){var i=r.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,r,n,i=[],a=u(e);!(n=a()).done;)r=this.dynamicIndexes.length,this.rules[t=this.rules.length]=n.value,this.sheet&&this.sheet.insertRule(this.rules[t],t),this.dynamicIndexes[r]=t,i.push(r);return i.sort(function(e,t){return t-e})},t.removeRules=function(e){for(var t,r,n=u(e);!(r=n()).done;){var i=r.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 d(t){var r,n=e.useRef([]),i=e.useRef([]);r=n.current,JSON.stringify(t.sort())!==JSON.stringify(r.sort())&&(i.current.length&&f.removeRules(i.current),i.current=f.insertDynamicRules(t),n.current=t),e.useEffect(function(){return function(){i.current.length&&f.removeRules(i.current)}},[])}function h(e,t){var r=Object.assign({},e);for(var n in e)"function"==typeof e[n]&&(r[n]=e[n](t));return r}function m(e){return function(t){var r=[];for(var n in e)r.push(i(n,h(e[n],t)));return d(r),null}}function p(e){return"TA"+v(e)}var v=function(e){for(var t=0,r=37;t<e.length;)r=97*r+e.charCodeAt(t++)>>>0;return r.toString(16)},y=["apply","condition","selectors"];function g(){for(var e=[].slice.call(arguments),t=p(JSON.stringify(e)),r=[],s=new Map,u=new Map,l=0;l<e.length;l++){var c,d,h=e[l],m=h.apply,v=h.condition,g=h.selectors,j=o(h,y),w=O(j,u,t);v=null==(c=v)||c,m=null!=(d=m)?d:{className:p(JSON.stringify(w))},s.set(m,v);var x="."+t+n(m);if(r.push(i(x,w)),g)for(var N in g)r.push(i(x,O(g[N],u,t),N))}function I(e){var r=b(e),n=S(s,u,t,e),i=r.style?a({},n.style,r.style):n.style;return a({},n,r,{style:i,className:r.className?n.className+" "+r.className:n.className})}return f.insertStaticRules(r),I}function b(e){var t={};for(var r in e)c.shouldForwardProp(r,e[r])&&(t[r]=e[r]);return t}function S(e,t,r,n){for(var i,o={style:{},className:[r]},s=u(e);!(i=s()).done;){var l=i.value,c=l[0],f=l[1];("function"==typeof f?f(n):f)&&("className"in c?o.className.push(c.className):o=a({},o,c))}o.className=o.className.join(" ");for(var d,h=u(t);!(d=h()).done;){var m=d.value;o.style[m[0]]=(0,m[1])(n)}return o}function O(e,t,r){var n={};if(!e)return n;for(var i in e){var a=e[i];if("function"==typeof a){var o="--"+r+"-"+t.size;t.set(o,a),n[i]="var("+o+")"}else n[i]=a}return n}function j(t){return function(){var n=g.apply(void 0,[].slice.call(arguments)),i=t.isStyledComponent?t.getComponentProps.concat(n):[n],a=e.forwardRef(function(e,n){var a,o=null!=(a=e.as)?a:t,s=i.reduce(function(e,t){return t(e)},e);return r.default.createElement(o,Object.assign({},s,{ref:n}))}),o=e.memo(a);return Object.assign(o,{isStyledComponent:!0,getComponentProps:i}),o}}var w=j;["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){w[e]=j(e)}),exports.ServerStylesheet=function(){return r.default.createElement("style",{id:"tonami-ssr",dangerouslySetInnerHTML:{__html:f.getStyleString()}})},exports.createGlobalStyle=m,exports.createTokens=function e(t){var r={},n={};return Object.setPrototypeOf(n,{toStyleObject:function(){return r},Tokens:m({html:r}),createDynamicTokens:function(t){return function(r){return d([i("html",e(t(r)).toStyleObject())]),null}}}),function e(t,n,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("-");r[u]=o,n[a]="var("+u+")"}else"object"==typeof o&&(n[a]={},e(o,n[a],s))}}(t,n),n},exports.options=c,exports.styled=w; //# sourceMappingURL=tonami.js.map