style-it
Version:
Component for writing plaintext CSS in React apps -- isomorphic, scoped, FOUC-free, fully featured, CSS-in-JS
2 lines (1 loc) • 6.74 kB
JavaScript
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("react")):"function"==typeof define&&define.amd?define(["react"],t):"object"==typeof exports?exports.Style=t(require("react")):e.Style=t(e.React)}(this,function(e){return function(e){function t(n){if(r[n])return r[n].exports;var o=r[n]={exports:{},id:n,loaded:!1};return e[n].call(o.exports,o,o.exports,t),o.loaded=!0,o.exports}var r={};return t.m=e,t.c=r,t.p="",t(0)}([function(e,t,r){"use strict";function n(e){return e&&e.__esModule?e:{default:e}}function o(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function s(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t}function i(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)}Object.defineProperty(t,"__esModule",{value:!0});var c=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},a="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},l=function(){function e(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}return function(t,r,n){return r&&e(t.prototype,r),n&&e(t,n),t}}(),p=r(2),u=n(p),f=r(1),d=n(f),h=!1,m=function(e){function t(e){o(this,t);var r=s(this,(t.__proto__||Object.getPrototypeOf(t)).call(this,e));return r.getStyleString=function(){if(r.props.children instanceof Array){var e=r.props.children.filter(function(e){return!(0,p.isValidElement)(e)&&"string"==typeof e});if(e.length>1)throw new Error("Multiple style objects as direct descedents of a Style component are not supported ("+e.length+" style objects detected): \n\n"+e[0]);return e[0]}return"string"!=typeof r.props.children||(0,p.isValidElement)(r.props.children)?null:r.props.children},r.getRootElement=function(){if(r.props.children instanceof Array){var e=r.props.children.filter(function(e){return(0,p.isValidElement)(e)});if(h){if(e.length>1)throw console.log(e),new Error("Adjacent JSX elements must be wrapped in an enclosing tag ("+e.length+" root elements detected).");if("undefined"!=typeof e[0]&&r.isVoidElement(e[0].type))throw new Error("Self-closing void elements like "+e.type+" must be wrapped in an enclosing tag. Reactive Style must be able to nest a style element inside of the root element and void element content models never allowit to have contents under any circumstances.")}return e[0]}return(0,p.isValidElement)(r.props.children)?r.props.children:null},r.getRootSelectors=function(e){var t=[];return e.props.id&&t.push("#"+e.props.id),e.props.className&&e.props.className.trim().split(/\s+/g).forEach(function(e){return t.push(e)}),t.length||"function"==typeof e.type||t.push(e.type),t},r.processCSSText=function(e,t,n){return e.replace(/\s*\/\/(?![^(]*\)).*|\s*\/\*.*\*\//g,"").replace(/\s\s+/g," ").split("}").map(function(e){var o=/.*:.*;/g,s=/.*:.*(;|$|\s+)/g,i=/\s*@/g,c=/\s*(([0-9][0-9]?|100)\s*%)|\s*(to|from)\s*$/g;return e.split("{").map(function(e,a,l){if(!e.trim().length)return"";var p=l.length-1===a&&e.match(s);if(e.match(o)||p)return r.escapeTextContentForBrowser(e);var u=e;return t&&!/:target/gi.test(u)?u.match(i)||u.match(c)?u:r.scopeSelector(t,u,n):u}).join("{\n")}).join("}\n")},r.escaper=function(e){var t={">":">","<":"<"};return t[e]},r.escapeTextContentForBrowser=function(e){var t=/[><]/g;return(""+e).replace(t,r.escaper)},r.scopeSelector=function(e,t,r){for(var n=[],o=/,(?![^(|[]*\)|\])/g,s=t.split(o),i=0;i<s.length;i++){var c=void 0,a=void 0;if(r.length&&r.some(function(e){return t.match(e)})){a=s[i];var l=r.map(function(e){return e.replace(/[-\/\\^$*+?.()|[\]{}]/g,"\\$&")});a=a.replace(new RegExp("("+l.join("|")+")"),"$1"+e),c=e+" "+s[i],n.push(a,c)}else c=e+" "+s[i],n.push(c)}return n.join(", ")},r.getScopeClassName=function(e,t){var n=e;return t&&(r.pepper="",r.traverseObjectToGeneratePepper(t),n+=r.pepper),(h?"scope-":"s")+(0,d.default)(n)},r.traverseObjectToGeneratePepper=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0;if(!(t>32||r.pepper.length>1e4))for(var n in e){var o=/^[_$]|type|ref|^value$/.test(n);e[n]&&"object"===a(e[n])&&!o?r.traverseObjectToGeneratePepper(e[n],t+1):e[n]&&!o&&"function"!=typeof e[n]&&(r.pepper+=e[n])}},r.isVoidElement=function(e){return["area","base","br","col","command","embed","hr","img","input","keygen","link","meta","param","source","track","wbr"].some(function(t){return e===t})},r.addCSSTextToHead=function(e){if(e.length){var t=(0,d.default)(e);window._reactiveStyle.cssTextHashesAddedToHead.some(function(e){return e===t})||(window._reactiveStyle.el.innerHTML+=e,window._reactiveStyle.cssTextHashesAddedToHead.push(t))}},r.createStyleElement=function(e,t){return u.default.createElement("style",{type:"text/css",key:t,ref:function(e){return r._style=e},dangerouslySetInnerHTML:{__html:e||""}})},r.getNewChildrenForCloneElement=function(e,t,n){return[r.createStyleElement(e,n)].concat(t.props.children)},r.scopeClassNameCache={},r.scopedCSSTextCache={},r}return i(t,e),l(t,[{key:"render",value:function(){if(!this.props.children)return this.createStyleElement();var e=this.getStyleString(),t=this.getRootElement();if(!e&&t)return t.props.children;if(e&&!t)return this.createStyleElement(this.processCSSText(e),this.getScopeClassName(e,t));var r=t.props.className?t.props.className+" ":"",n=t.props.id?t.props.id:"",o=void 0,s=void 0,i=r+n+e;return this.scopeClassNameCache[i]?(o=this.scopeClassNameCache[i],s=this.scopedCSSTextCache[o]):(o=this.getScopeClassName(e,t),s=this.processCSSText(e,"."+o,this.getRootSelectors(t)),this.scopeClassNameCache[i]=o,this.scopedCSSTextCache[o]=s),(0,p.cloneElement)(t,c({},t.props,{className:""+r+o}),this.getNewChildrenForCloneElement(s,t,o))}}]),t}(p.Component);m.it=function(e,t){return u.default.createElement(m,null,e,t)},e.exports=m},function(e,t){"use strict";function r(e){for(var t=1,r=0,o=0,s=e.length,i=s&-4;o<i;){for(var c=Math.min(o+4096,i);o<c;o+=4)r+=(t+=e.charCodeAt(o))+(t+=e.charCodeAt(o+1))+(t+=e.charCodeAt(o+2))+(t+=e.charCodeAt(o+3));t%=n,r%=n}for(;o<s;o++)r+=t+=e.charCodeAt(o);return t%=n,r%=n,t|r<<16}var n=65521;e.exports=r},function(t,r){t.exports=e}])});