UNPKG

7.39 kBJavaScriptView Raw
1"use strict";
2
3function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
4
5function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
6
7var React = require('react'); // eslint-disable-line import/no-extraneous-dependencies
8// eslint-disable-next-line no-control-regex
9
10
11var reWords = /[A-Z\xc0-\xd6\xd8-\xde]?[a-z\xdf-\xf6\xf8-\xff]+(?:['’](?:d|ll|m|re|s|t|ve))?(?=[\xac\xb1\xd7\xf7\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\xbf\u2000-\u206f \t\x0b\f\xa0\ufeff\n\r\u2028\u2029\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000]|[A-Z\xc0-\xd6\xd8-\xde]|$)|(?:[A-Z\xc0-\xd6\xd8-\xde]|[^\ud800-\udfff\xac\xb1\xd7\xf7\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\xbf\u2000-\u206f \t\x0b\f\xa0\ufeff\n\r\u2028\u2029\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000\d+\u2700-\u27bfa-z\xdf-\xf6\xf8-\xffA-Z\xc0-\xd6\xd8-\xde])+(?:['’](?:D|LL|M|RE|S|T|VE))?(?=[\xac\xb1\xd7\xf7\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\xbf\u2000-\u206f \t\x0b\f\xa0\ufeff\n\r\u2028\u2029\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000]|[A-Z\xc0-\xd6\xd8-\xde](?:[a-z\xdf-\xf6\xf8-\xff]|[^\ud800-\udfff\xac\xb1\xd7\xf7\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\xbf\u2000-\u206f \t\x0b\f\xa0\ufeff\n\r\u2028\u2029\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000\d+\u2700-\u27bfa-z\xdf-\xf6\xf8-\xffA-Z\xc0-\xd6\xd8-\xde])|$)|[A-Z\xc0-\xd6\xd8-\xde]?(?:[a-z\xdf-\xf6\xf8-\xff]|[^\ud800-\udfff\xac\xb1\xd7\xf7\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\xbf\u2000-\u206f \t\x0b\f\xa0\ufeff\n\r\u2028\u2029\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000\d+\u2700-\u27bfa-z\xdf-\xf6\xf8-\xffA-Z\xc0-\xd6\xd8-\xde])+(?:['’](?:d|ll|m|re|s|t|ve))?|[A-Z\xc0-\xd6\xd8-\xde]+(?:['’](?:D|LL|M|RE|S|T|VE))?|\d*(?:1ST|2ND|3RD|(?![123])\dTH)(?=\b|[a-z_])|\d*(?:1st|2nd|3rd|(?![123])\dth)(?=\b|[A-Z_])|\d+|(?:[\u2700-\u27bf]|(?:\ud83c[\udde6-\uddff]){2}|[\ud800-\udbff][\udc00-\udfff])[\ufe0e\ufe0f]?(?:[\u0300-\u036f\ufe20-\ufe2f\u20d0-\u20ff]|\ud83c[\udffb-\udfff])?(?:\u200d(?:[^\ud800-\udfff]|(?:\ud83c[\udde6-\uddff]){2}|[\ud800-\udbff][\udc00-\udfff])[\ufe0e\ufe0f]?(?:[\u0300-\u036f\ufe20-\ufe2f\u20d0-\u20ff]|\ud83c[\udffb-\udfff])?)*/g;
12
13var camelCase = function camelCase(str) {
14 return (str.match(reWords) || []).reduce(function (acc, next) {
15 return acc + (!acc ? next : next[0].toUpperCase() + next.slice(1));
16 }, '');
17};
18
19function varsToStyles(props, vars) {
20 if (!vars || !vars.length) return props.style;
21
22 var style = _extends({}, props.style);
23
24 vars.forEach(function (_ref) {
25 var id = _ref[0],
26 value = _ref[1],
27 _ref$ = _ref[2],
28 unit = _ref$ === void 0 ? '' : _ref$;
29 var result = typeof value === 'function' ? value(props) : value;
30 style["--" + id] = "" + result + unit;
31 });
32 return style;
33}
34
35function propsToStyles(props, styles, hasModifiers) {
36 var componentClassName = styles.cls2 || styles.cls1;
37 var className = props.className ? props.className + " " + componentClassName : componentClassName;
38
39 if (hasModifiers) {
40 Object.keys(props).forEach(function (propName) {
41 var propValue = props[propName];
42 var typeOf = typeof propValue;
43
44 if (typeOf === 'boolean' || propValue == null) {
45 if (styles[propName]) {
46 if (propValue) {
47 className += " " + styles[propName];
48 }
49
50 delete props[propName];
51 } else {
52 var camelPropName = camelCase(propName);
53
54 if (styles[camelPropName]) {
55 if (propValue) {
56 className += " " + styles[camelPropName];
57 }
58
59 delete props[propName];
60 }
61 }
62 } else if (typeOf === 'string' || typeOf === 'number') {
63 var propKey = propName + "-" + propValue;
64
65 if (styles[propKey]) {
66 className += " " + styles[propKey];
67 delete props[propName];
68 } else {
69 var camelPropKey = camelCase(propKey);
70
71 if (styles[camelPropKey]) {
72 className += " " + styles[camelPropKey];
73 delete props[propName];
74 }
75 }
76 }
77 });
78 }
79
80 return className;
81}
82
83function styled(type, options, settings) {
84 if (process.env.NODE_ENV !== "production") {
85 if (Array.isArray(type)) throw new Error('This styled() template tag was mistakenly evaluated at runtime. ' + 'Make sure astroturf is properly configured to compile this file');
86 if (typeof settings === 'string') throw new Error('It looks like you have incompatible astroturf versions in your app. ' + 'This runtime expects styles compiled with a newer version of astroturf, ' + 'ensure that your versions are properly deduped and upgraded. ');
87 }
88
89 var displayName = settings.displayName,
90 attrs = settings.attrs,
91 vars = settings.vars,
92 styles = settings.styles;
93 options = options || {
94 allowAs: typeof type === 'string'
95 }; // always passthrough if the type is a styled component
96
97 var allowAs = type.isAstroturf ? false : options.allowAs;
98 var hasModifiers = Object.keys(styles).some(function (className) {
99 return className !== (styles.cls2 || styles.cls1);
100 });
101
102 function Styled(rawProps, ref) {
103 var props = attrs ? attrs(rawProps) : rawProps;
104
105 var childProps = _extends({}, props, {
106 ref: ref
107 });
108
109 if (allowAs) delete childProps.as;
110 childProps.style = varsToStyles(childProps, vars);
111 childProps.className = propsToStyles(childProps, styles, hasModifiers);
112 return React.createElement(allowAs && props.as ? props.as : type, childProps);
113 }
114
115 var decorated = React.forwardRef ? React.forwardRef(Styled) : function (props) {
116 return Styled(props, null);
117 };
118 decorated.displayName = displayName;
119
120 decorated.withComponent = function (nextType) {
121 return styled(nextType, options, settings);
122 };
123
124 decorated.isAstroturf = true;
125 return decorated;
126}
127
128function jsx(type, props) {
129 if (props && props.css) {
130 var _props = props,
131 css = _props.css,
132 childProps = _objectWithoutPropertiesLoose(_props, ["css"]);
133
134 childProps.style = varsToStyles(childProps, css[1]);
135 childProps.className = propsToStyles(childProps, css[0] || css, true);
136 props = childProps;
137 }
138
139 for (var _len = arguments.length, children = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
140 children[_key - 2] = arguments[_key];
141 }
142
143 return React.createElement.apply(React, [type, props].concat(children));
144}
145
146module.exports = styled;
147module.exports.styled = styled;
148module.exports.jsx = jsx;
149module.exports.F = React.Fragment;
150
151if (process.env.NODE_ENV !== "production") {
152 module.exports.css = function () {
153 throw new Error('css template literal evaluated at runtime. ' + 'Make sure astroturf is properly configured to compile this file');
154 };
155}
\No newline at end of file