UNPKG

7.26 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
7const React = require('react'); // eslint-disable-line import/no-extraneous-dependencies
8// eslint-disable-next-line no-control-regex
9
10
11const 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
13const camelCase = str => (str.match(reWords) || []).reduce((acc, next) => acc + (!acc ? next : next[0].toUpperCase() + next.slice(1)), '');
14
15function varsToStyles(props, vars) {
16 if (!vars || !vars.length) return props.style;
17
18 const style = _extends({}, props.style);
19
20 vars.forEach((_ref) => {
21 let id = _ref[0],
22 value = _ref[1],
23 _ref$ = _ref[2],
24 unit = _ref$ === void 0 ? '' : _ref$;
25 const result = typeof value === 'function' ? value(props) : value;
26 style[`--${id}`] = `${result}${unit}`;
27 });
28 return style;
29}
30
31function propsToStyles(props, styles, hasModifiers) {
32 const componentClassName = styles.cls2 || styles.cls1;
33 let className = props.className ? `${props.className} ${componentClassName}` : componentClassName;
34
35 if (hasModifiers) {
36 Object.keys(props).forEach(propName => {
37 const propValue = props[propName];
38 const typeOf = typeof propValue;
39
40 if (typeOf === 'boolean' || propValue == null) {
41 if (styles[propName]) {
42 if (propValue) {
43 className += ` ${styles[propName]}`;
44 }
45
46 delete props[propName];
47 } else {
48 const camelPropName = camelCase(propName);
49
50 if (styles[camelPropName]) {
51 if (propValue) {
52 className += ` ${styles[camelPropName]}`;
53 }
54
55 delete props[propName];
56 }
57 }
58 } else if (typeOf === 'string' || typeOf === 'number') {
59 const propKey = `${propName}-${propValue}`;
60
61 if (styles[propKey]) {
62 className += ` ${styles[propKey]}`;
63 delete props[propName];
64 } else {
65 const camelPropKey = camelCase(propKey);
66
67 if (styles[camelPropKey]) {
68 className += ` ${styles[camelPropKey]}`;
69 delete props[propName];
70 }
71 }
72 }
73 });
74 }
75
76 return className;
77}
78
79function styled(type, options, settings) {
80 if (process.env.NODE_ENV !== "production") {
81 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');
82 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. ');
83 }
84
85 const displayName = settings.displayName,
86 attrs = settings.attrs,
87 vars = settings.vars,
88 styles = settings.styles;
89 options = options || {
90 allowAs: typeof type === 'string'
91 }; // always passthrough if the type is a styled component
92
93 const allowAs = type.isAstroturf ? false : options.allowAs;
94 const hasModifiers = Object.keys(styles).some(className => className !== (styles.cls2 || styles.cls1));
95
96 function Styled(rawProps, ref) {
97 const props = attrs ? attrs(rawProps) : rawProps;
98
99 const childProps = _extends({}, props, {
100 ref
101 });
102
103 if (allowAs) delete childProps.as;
104 childProps.style = varsToStyles(childProps, vars);
105 childProps.className = propsToStyles(childProps, styles, hasModifiers);
106 return React.createElement(allowAs && props.as ? props.as : type, childProps);
107 }
108
109 const decorated = React.forwardRef ? React.forwardRef(Styled) : props => Styled(props, null);
110 decorated.displayName = displayName;
111
112 decorated.withComponent = nextType => styled(nextType, options, settings);
113
114 decorated.isAstroturf = true;
115 return decorated;
116}
117
118function jsx(type, props) {
119 if (props && props.css) {
120 const _props = props,
121 css = _props.css,
122 childProps = _objectWithoutPropertiesLoose(_props, ["css"]);
123
124 childProps.style = varsToStyles(childProps, css[1]);
125 childProps.className = propsToStyles(childProps, css[0] || css, true);
126 props = childProps;
127 }
128
129 for (var _len = arguments.length, children = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
130 children[_key - 2] = arguments[_key];
131 }
132
133 return React.createElement(type, props, ...children);
134}
135
136module.exports = styled;
137module.exports.styled = styled;
138module.exports.jsx = jsx;
139module.exports.F = React.Fragment;
140
141if (process.env.NODE_ENV !== "production") {
142 module.exports.css = () => {
143 throw new Error('css template literal evaluated at runtime. ' + 'Make sure astroturf is properly configured to compile this file');
144 };
145}
\No newline at end of file