UNPKG

81.3 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', { value: true });
4
5function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
6
7var Stylis = _interopDefault(require('stylis/stylis.min'));
8var _insertRulePlugin = _interopDefault(require('stylis-rule-sheet'));
9var React = require('react');
10var React__default = _interopDefault(React);
11var unitless = _interopDefault(require('@emotion/unitless'));
12var reactIs = require('react-is');
13var memoize = _interopDefault(require('memoize-one'));
14var stream = _interopDefault(require('stream'));
15var PropTypes = _interopDefault(require('prop-types'));
16var ReactDOM = _interopDefault(require('react-dom'));
17var validAttr = _interopDefault(require('@emotion/is-prop-valid'));
18var babelPluginMacros = require('babel-plugin-macros');
19var babelPlugin = _interopDefault(require('babel-plugin-styled-components'));
20
21//
22
23var interleave = (function (strings, interpolations) {
24 var result = [strings[0]];
25
26 for (var i = 0, len = interpolations.length; i < len; i += 1) {
27 result.push(interpolations[i], strings[i + 1]);
28 }
29
30 return result;
31});
32
33var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {
34 return typeof obj;
35} : function (obj) {
36 return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
37};
38
39var classCallCheck = function (instance, Constructor) {
40 if (!(instance instanceof Constructor)) {
41 throw new TypeError("Cannot call a class as a function");
42 }
43};
44
45var createClass = function () {
46 function defineProperties(target, props) {
47 for (var i = 0; i < props.length; i++) {
48 var descriptor = props[i];
49 descriptor.enumerable = descriptor.enumerable || false;
50 descriptor.configurable = true;
51 if ("value" in descriptor) descriptor.writable = true;
52 Object.defineProperty(target, descriptor.key, descriptor);
53 }
54 }
55
56 return function (Constructor, protoProps, staticProps) {
57 if (protoProps) defineProperties(Constructor.prototype, protoProps);
58 if (staticProps) defineProperties(Constructor, staticProps);
59 return Constructor;
60 };
61}();
62
63var _extends = Object.assign || function (target) {
64 for (var i = 1; i < arguments.length; i++) {
65 var source = arguments[i];
66
67 for (var key in source) {
68 if (Object.prototype.hasOwnProperty.call(source, key)) {
69 target[key] = source[key];
70 }
71 }
72 }
73
74 return target;
75};
76
77var inherits = function (subClass, superClass) {
78 if (typeof superClass !== "function" && superClass !== null) {
79 throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
80 }
81
82 subClass.prototype = Object.create(superClass && superClass.prototype, {
83 constructor: {
84 value: subClass,
85 enumerable: false,
86 writable: true,
87 configurable: true
88 }
89 });
90 if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
91};
92
93var objectWithoutProperties = function (obj, keys) {
94 var target = {};
95
96 for (var i in obj) {
97 if (keys.indexOf(i) >= 0) continue;
98 if (!Object.prototype.hasOwnProperty.call(obj, i)) continue;
99 target[i] = obj[i];
100 }
101
102 return target;
103};
104
105var possibleConstructorReturn = function (self, call) {
106 if (!self) {
107 throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
108 }
109
110 return call && (typeof call === "object" || typeof call === "function") ? call : self;
111};
112
113//
114var isPlainObject = (function (x) {
115 return (typeof x === 'undefined' ? 'undefined' : _typeof(x)) === 'object' && x.constructor === Object;
116});
117
118//
119var EMPTY_ARRAY = Object.freeze([]);
120var EMPTY_OBJECT = Object.freeze({});
121
122//
123function isFunction(test) {
124 return typeof test === 'function';
125}
126
127//
128
129function getComponentName(target) {
130 return target.displayName || target.name || 'Component';
131}
132
133//
134function isStyledComponent(target) {
135 return target && typeof target.styledComponentId === 'string';
136}
137
138//
139
140var SC_ATTR = typeof process !== 'undefined' && process.env.SC_ATTR || 'data-styled';
141
142var SC_VERSION_ATTR = 'data-styled-version';
143
144var SC_STREAM_ATTR = 'data-styled-streamed';
145
146var IS_BROWSER = typeof window !== 'undefined' && 'HTMLElement' in window;
147
148var DISABLE_SPEEDY = typeof SC_DISABLE_SPEEDY === 'boolean' && SC_DISABLE_SPEEDY || process.env.NODE_ENV !== 'production';
149
150// Shared empty execution context when generating static styles
151var STATIC_EXECUTION_CONTEXT = {};
152
153//
154
155
156/**
157 * Parse errors.md and turn it into a simple hash of code: message
158 */
159var ERRORS = process.env.NODE_ENV !== 'production' ? {
160 "1": "Cannot create styled-component for component: %s.\n\n",
161 "2": "Can't collect styles once you've consumed a `ServerStyleSheet`'s styles! `ServerStyleSheet` is a one off instance for each server-side render cycle.\n\n- Are you trying to reuse it across renders?\n- Are you accidentally calling collectStyles twice?\n\n",
162 "3": "Streaming SSR is only supported in a Node.js environment; Please do not try to call this method in the browser.\n\n",
163 "4": "The `StyleSheetManager` expects a valid target or sheet prop!\n\n- Does this error occur on the client and is your target falsy?\n- Does this error occur on the server and is the sheet falsy?\n\n",
164 "5": "The clone method cannot be used on the client!\n\n- Are you running in a client-like environment on the server?\n- Are you trying to run SSR on the client?\n\n",
165 "6": "Trying to insert a new style tag, but the given Node is unmounted!\n\n- Are you using a custom target that isn't mounted?\n- Does your document not have a valid head element?\n- Have you accidentally removed a style tag manually?\n\n",
166 "7": "ThemeProvider: Please return an object from your \"theme\" prop function, e.g.\n\n```js\ntheme={() => ({})}\n```\n\n",
167 "8": "ThemeProvider: Please make your \"theme\" prop an object.\n\n",
168 "9": "Missing document `<head>`\n\n",
169 "10": "Cannot find a StyleSheet instance. Usually this happens if there are multiple copies of styled-components loaded at once. Check out this issue for how to troubleshoot and fix the common cases where this situation can happen: https://github.com/styled-components/styled-components/issues/1941#issuecomment-417862021\n\n",
170 "11": "_This error was replaced with a dev-time warning, it will be deleted for v4 final._ [createGlobalStyle] received children which will not be rendered. Please use the component without passing children elements.\n\n",
171 "12": "It seems you are interpolating a keyframe declaration (%s) into an untagged string. This was supported in styled-components v3, but is not longer supported in v4 as keyframes are now injected on-demand. Please wrap your string in the css\\`\\` helper (see https://www.styled-components.com/docs/api#css), which ensures the styles are injected correctly.\n\n",
172 "13": "%s is not a styled component and cannot be referred to via component selector. See https://www.styled-components.com/docs/advanced#referring-to-other-components for more details.\n"
173} : {};
174
175/**
176 * super basic version of sprintf
177 */
178function format() {
179 var a = arguments.length <= 0 ? undefined : arguments[0];
180 var b = [];
181
182 for (var c = 1, len = arguments.length; c < len; c += 1) {
183 b.push(arguments.length <= c ? undefined : arguments[c]);
184 }
185
186 b.forEach(function (d) {
187 a = a.replace(/%[a-z]/, d);
188 });
189
190 return a;
191}
192
193/**
194 * Create an error file out of errors.md for development and a simple web link to the full errors
195 * in production mode.
196 */
197
198var StyledComponentsError = function (_Error) {
199 inherits(StyledComponentsError, _Error);
200
201 function StyledComponentsError(code) {
202 classCallCheck(this, StyledComponentsError);
203
204 for (var _len = arguments.length, interpolations = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
205 interpolations[_key - 1] = arguments[_key];
206 }
207
208 if (process.env.NODE_ENV === 'production') {
209 var _this = possibleConstructorReturn(this, _Error.call(this, 'An error occurred. See https://github.com/styled-components/styled-components/blob/master/src/utils/errors.md#' + code + ' for more information. ' + (interpolations ? 'Additional arguments: ' + interpolations.join(', ') : '')));
210 } else {
211 var _this = possibleConstructorReturn(this, _Error.call(this, format.apply(undefined, [ERRORS[code]].concat(interpolations)).trim()));
212 }
213 return possibleConstructorReturn(_this);
214 }
215
216 return StyledComponentsError;
217}(Error);
218
219//
220var SC_COMPONENT_ID = /^[^\S\n]*?\/\* sc-component-id:\s*(\S+)\s+\*\//gm;
221
222var extractComps = (function (maybeCSS) {
223 var css = '' + (maybeCSS || ''); // Definitely a string, and a clone
224 var existingComponents = [];
225 css.replace(SC_COMPONENT_ID, function (match, componentId, matchIndex) {
226 existingComponents.push({ componentId: componentId, matchIndex: matchIndex });
227 return match;
228 });
229 return existingComponents.map(function (_ref, i) {
230 var componentId = _ref.componentId,
231 matchIndex = _ref.matchIndex;
232
233 var nextComp = existingComponents[i + 1];
234 var cssFromDOM = nextComp ? css.slice(matchIndex, nextComp.matchIndex) : css.slice(matchIndex);
235 return { componentId: componentId, cssFromDOM: cssFromDOM };
236 });
237});
238
239//
240
241var COMMENT_REGEX = /^\s*\/\/.*$/gm;
242
243// NOTE: This stylis instance is only used to split rules from SSR'd style tags
244var stylisSplitter = new Stylis({
245 global: false,
246 cascade: true,
247 keyframe: false,
248 prefix: false,
249 compress: false,
250 semicolon: true
251});
252
253var stylis = new Stylis({
254 global: false,
255 cascade: true,
256 keyframe: false,
257 prefix: true,
258 compress: false,
259 semicolon: false // NOTE: This means "autocomplete missing semicolons"
260});
261
262// Wrap `insertRulePlugin to build a list of rules,
263// and then make our own plugin to return the rules. This
264// makes it easier to hook into the existing SSR architecture
265
266var parsingRules = [];
267
268// eslint-disable-next-line consistent-return
269var returnRulesPlugin = function returnRulesPlugin(context) {
270 if (context === -2) {
271 var parsedRules = parsingRules;
272 parsingRules = [];
273 return parsedRules;
274 }
275};
276
277var parseRulesPlugin = _insertRulePlugin(function (rule) {
278 parsingRules.push(rule);
279});
280
281var _componentId = void 0;
282var _selector = void 0;
283var _selectorRegexp = void 0;
284
285var selfReferenceReplacer = function selfReferenceReplacer(match, offset, string) {
286 if (
287 // the first self-ref is always untouched
288 offset > 0 &&
289 // there should be at least two self-refs to do a replacement (.b > .b)
290 string.slice(0, offset).indexOf(_selector) !== -1 &&
291 // no consecutive self refs (.b.b); that is a precedence boost and treated differently
292 string.slice(offset - _selector.length, offset) !== _selector) {
293 return '.' + _componentId;
294 }
295
296 return match;
297};
298
299/**
300 * When writing a style like
301 *
302 * & + & {
303 * color: red;
304 * }
305 *
306 * The second ampersand should be a reference to the static component class. stylis
307 * has no knowledge of static class so we have to intelligently replace the base selector.
308 */
309var selfReferenceReplacementPlugin = function selfReferenceReplacementPlugin(context, _, selectors) {
310 if (context === 2 && selectors.length && selectors[0].lastIndexOf(_selector) > 0) {
311 // eslint-disable-next-line no-param-reassign
312 selectors[0] = selectors[0].replace(_selectorRegexp, selfReferenceReplacer);
313 }
314};
315
316stylis.use([selfReferenceReplacementPlugin, parseRulesPlugin, returnRulesPlugin]);
317stylisSplitter.use([parseRulesPlugin, returnRulesPlugin]);
318
319var splitByRules = function splitByRules(css) {
320 return stylisSplitter('', css);
321};
322
323function stringifyRules(rules, selector, prefix) {
324 var componentId = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : '&';
325
326 var flatCSS = rules.join('').replace(COMMENT_REGEX, ''); // replace JS comments
327
328 var cssStr = selector && prefix ? prefix + ' ' + selector + ' { ' + flatCSS + ' }' : flatCSS;
329
330 // stylis has no concept of state to be passed to plugins
331 // but since JS is single=threaded, we can rely on that to ensure
332 // these properties stay in sync with the current stylis run
333 _componentId = componentId;
334 _selector = selector;
335 _selectorRegexp = new RegExp('\\' + _selector + '\\b', 'g');
336
337 return stylis(prefix || !selector ? '' : selector, cssStr);
338}
339
340//
341/* eslint-disable camelcase, no-undef */
342
343var getNonce = (function () {
344 return typeof __webpack_nonce__ !== 'undefined' ? __webpack_nonce__ : null;
345});
346
347//
348/* These are helpers for the StyleTags to keep track of the injected
349 * rule names for each (component) ID that they're keeping track of.
350 * They're crucial for detecting whether a name has already been
351 * injected.
352 * (This excludes rehydrated names) */
353
354/* adds a new ID:name pairing to a names dictionary */
355var addNameForId = function addNameForId(names, id, name) {
356 if (name) {
357 // eslint-disable-next-line no-param-reassign
358 var namesForId = names[id] || (names[id] = Object.create(null));
359 namesForId[name] = true;
360 }
361};
362
363/* resets an ID entirely by overwriting it in the dictionary */
364var resetIdNames = function resetIdNames(names, id) {
365 // eslint-disable-next-line no-param-reassign
366 names[id] = Object.create(null);
367};
368
369/* factory for a names dictionary checking the existance of an ID:name pairing */
370var hasNameForId = function hasNameForId(names) {
371 return function (id, name) {
372 return names[id] !== undefined && names[id][name];
373 };
374};
375
376/* stringifies names for the html/element output */
377var stringifyNames = function stringifyNames(names) {
378 var str = '';
379 // eslint-disable-next-line guard-for-in
380 for (var id in names) {
381 str += Object.keys(names[id]).join(' ') + ' ';
382 }
383 return str.trim();
384};
385
386/* clones the nested names dictionary */
387var cloneNames = function cloneNames(names) {
388 var clone = Object.create(null);
389 // eslint-disable-next-line guard-for-in
390 for (var id in names) {
391 clone[id] = _extends({}, names[id]);
392 }
393 return clone;
394};
395
396//
397
398/* These are helpers that deal with the insertRule (aka speedy) API
399 * They are used in the StyleTags and specifically the speedy tag
400 */
401
402/* retrieve a sheet for a given style tag */
403var sheetForTag = function sheetForTag(tag) {
404 // $FlowFixMe
405 if (tag.sheet) return tag.sheet;
406
407 /* Firefox quirk requires us to step through all stylesheets to find one owned by the given tag */
408 var size = document.styleSheets.length;
409 for (var i = 0; i < size; i += 1) {
410 var sheet = document.styleSheets[i];
411 // $FlowFixMe
412 if (sheet.ownerNode === tag) return sheet;
413 }
414
415 /* we should always be able to find a tag */
416 throw new StyledComponentsError(10);
417};
418
419/* insert a rule safely and return whether it was actually injected */
420var safeInsertRule = function safeInsertRule(sheet, cssRule, index) {
421 /* abort early if cssRule string is falsy */
422 if (!cssRule) return false;
423
424 var maxIndex = sheet.cssRules.length;
425
426 try {
427 /* use insertRule and cap passed index with maxIndex (no of cssRules) */
428 sheet.insertRule(cssRule, index <= maxIndex ? index : maxIndex);
429 } catch (err) {
430 /* any error indicates an invalid rule */
431 return false;
432 }
433
434 return true;
435};
436
437/* deletes `size` rules starting from `removalIndex` */
438var deleteRules = function deleteRules(sheet, removalIndex, size) {
439 var lowerBound = removalIndex - size;
440 for (var i = removalIndex; i > lowerBound; i -= 1) {
441 sheet.deleteRule(i);
442 }
443};
444
445//
446
447/* this marker separates component styles and is important for rehydration */
448var makeTextMarker = function makeTextMarker(id) {
449 return '\n/* sc-component-id: ' + id + ' */\n';
450};
451
452/* add up all numbers in array up until and including the index */
453var addUpUntilIndex = function addUpUntilIndex(sizes, index) {
454 var totalUpToIndex = 0;
455 for (var i = 0; i <= index; i += 1) {
456 totalUpToIndex += sizes[i];
457 }
458
459 return totalUpToIndex;
460};
461
462/* create a new style tag after lastEl */
463var makeStyleTag = function makeStyleTag(target, tagEl, insertBefore) {
464 var el = document.createElement('style');
465 el.setAttribute(SC_ATTR, '');
466 el.setAttribute(SC_VERSION_ATTR, "4.1.0");
467
468 var nonce = getNonce();
469 if (nonce) {
470 el.setAttribute('nonce', nonce);
471 }
472
473 /* Work around insertRule quirk in EdgeHTML */
474 el.appendChild(document.createTextNode(''));
475
476 if (target && !tagEl) {
477 /* Append to target when no previous element was passed */
478 target.appendChild(el);
479 } else {
480 if (!tagEl || !target || !tagEl.parentNode) {
481 throw new StyledComponentsError(6);
482 }
483
484 /* Insert new style tag after the previous one */
485 tagEl.parentNode.insertBefore(el, insertBefore ? tagEl : tagEl.nextSibling);
486 }
487
488 return el;
489};
490
491/* takes a css factory function and outputs an html styled tag factory */
492var wrapAsHtmlTag = function wrapAsHtmlTag(css, names) {
493 return function (additionalAttrs) {
494 var nonce = getNonce();
495 var attrs = [nonce && 'nonce="' + nonce + '"', SC_ATTR + '="' + stringifyNames(names) + '"', SC_VERSION_ATTR + '="' + "4.1.0" + '"', additionalAttrs];
496
497 var htmlAttr = attrs.filter(Boolean).join(' ');
498 return '<style ' + htmlAttr + '>' + css() + '</style>';
499 };
500};
501
502/* takes a css factory function and outputs an element factory */
503var wrapAsElement = function wrapAsElement(css, names) {
504 return function () {
505 var _props;
506
507 var props = (_props = {}, _props[SC_ATTR] = stringifyNames(names), _props[SC_VERSION_ATTR] = "4.1.0", _props);
508
509 var nonce = getNonce();
510 if (nonce) {
511 // $FlowFixMe
512 props.nonce = nonce;
513 }
514
515 // eslint-disable-next-line react/no-danger
516 return React__default.createElement('style', _extends({}, props, { dangerouslySetInnerHTML: { __html: css() } }));
517 };
518};
519
520var getIdsFromMarkersFactory = function getIdsFromMarkersFactory(markers) {
521 return function () {
522 return Object.keys(markers);
523 };
524};
525
526/* speedy tags utilise insertRule */
527var makeSpeedyTag = function makeSpeedyTag(el, getImportRuleTag) {
528 var names = Object.create(null);
529 var markers = Object.create(null);
530 var sizes = [];
531
532 var extractImport = getImportRuleTag !== undefined;
533 /* indicates whther getImportRuleTag was called */
534 var usedImportRuleTag = false;
535
536 var insertMarker = function insertMarker(id) {
537 var prev = markers[id];
538 if (prev !== undefined) {
539 return prev;
540 }
541
542 markers[id] = sizes.length;
543 sizes.push(0);
544 resetIdNames(names, id);
545
546 return markers[id];
547 };
548
549 var insertRules = function insertRules(id, cssRules, name) {
550 var marker = insertMarker(id);
551 var sheet = sheetForTag(el);
552 var insertIndex = addUpUntilIndex(sizes, marker);
553
554 var injectedRules = 0;
555 var importRules = [];
556 var cssRulesSize = cssRules.length;
557
558 for (var i = 0; i < cssRulesSize; i += 1) {
559 var cssRule = cssRules[i];
560 var mayHaveImport = extractImport; /* @import rules are reordered to appear first */
561 if (mayHaveImport && cssRule.indexOf('@import') !== -1) {
562 importRules.push(cssRule);
563 } else if (safeInsertRule(sheet, cssRule, insertIndex + injectedRules)) {
564 mayHaveImport = false;
565 injectedRules += 1;
566 }
567 }
568
569 if (extractImport && importRules.length > 0) {
570 usedImportRuleTag = true;
571 // $FlowFixMe
572 getImportRuleTag().insertRules(id + '-import', importRules);
573 }
574
575 sizes[marker] += injectedRules; /* add up no of injected rules */
576 addNameForId(names, id, name);
577 };
578
579 var removeRules = function removeRules(id) {
580 var marker = markers[id];
581 if (marker === undefined) return;
582
583 var size = sizes[marker];
584 var sheet = sheetForTag(el);
585 var removalIndex = addUpUntilIndex(sizes, marker) - 1;
586 deleteRules(sheet, removalIndex, size);
587 sizes[marker] = 0;
588 resetIdNames(names, id);
589
590 if (extractImport && usedImportRuleTag) {
591 // $FlowFixMe
592 getImportRuleTag().removeRules(id + '-import');
593 }
594 };
595
596 var css = function css() {
597 var _sheetForTag = sheetForTag(el),
598 cssRules = _sheetForTag.cssRules;
599
600 var str = '';
601
602 // eslint-disable-next-line guard-for-in
603 for (var id in markers) {
604 str += makeTextMarker(id);
605 var marker = markers[id];
606 var end = addUpUntilIndex(sizes, marker);
607 var size = sizes[marker];
608 for (var i = end - size; i < end; i += 1) {
609 var rule = cssRules[i];
610 if (rule !== undefined) {
611 str += rule.cssText;
612 }
613 }
614 }
615
616 return str;
617 };
618
619 return {
620 clone: function clone() {
621 throw new StyledComponentsError(5);
622 },
623
624 css: css,
625 getIds: getIdsFromMarkersFactory(markers),
626 hasNameForId: hasNameForId(names),
627 insertMarker: insertMarker,
628 insertRules: insertRules,
629 removeRules: removeRules,
630 sealed: false,
631 styleTag: el,
632 toElement: wrapAsElement(css, names),
633 toHTML: wrapAsHtmlTag(css, names)
634 };
635};
636
637var makeTextNode = function makeTextNode(id) {
638 return document.createTextNode(makeTextMarker(id));
639};
640
641var makeBrowserTag = function makeBrowserTag(el, getImportRuleTag) {
642 var names = Object.create(null);
643 var markers = Object.create(null);
644
645 var extractImport = getImportRuleTag !== undefined;
646
647 /* indicates whther getImportRuleTag was called */
648 var usedImportRuleTag = false;
649
650 var insertMarker = function insertMarker(id) {
651 var prev = markers[id];
652 if (prev !== undefined) {
653 return prev;
654 }
655
656 markers[id] = makeTextNode(id);
657 el.appendChild(markers[id]);
658 names[id] = Object.create(null);
659
660 return markers[id];
661 };
662
663 var insertRules = function insertRules(id, cssRules, name) {
664 var marker = insertMarker(id);
665 var importRules = [];
666 var cssRulesSize = cssRules.length;
667
668 for (var i = 0; i < cssRulesSize; i += 1) {
669 var rule = cssRules[i];
670 var mayHaveImport = extractImport;
671 if (mayHaveImport && rule.indexOf('@import') !== -1) {
672 importRules.push(rule);
673 } else {
674 mayHaveImport = false;
675 var separator = i === cssRulesSize - 1 ? '' : ' ';
676 marker.appendData('' + rule + separator);
677 }
678 }
679
680 addNameForId(names, id, name);
681
682 if (extractImport && importRules.length > 0) {
683 usedImportRuleTag = true;
684 // $FlowFixMe
685 getImportRuleTag().insertRules(id + '-import', importRules);
686 }
687 };
688
689 var removeRules = function removeRules(id) {
690 var marker = markers[id];
691 if (marker === undefined) return;
692
693 /* create new empty text node and replace the current one */
694 var newMarker = makeTextNode(id);
695 el.replaceChild(newMarker, marker);
696 markers[id] = newMarker;
697 resetIdNames(names, id);
698
699 if (extractImport && usedImportRuleTag) {
700 // $FlowFixMe
701 getImportRuleTag().removeRules(id + '-import');
702 }
703 };
704
705 var css = function css() {
706 var str = '';
707
708 // eslint-disable-next-line guard-for-in
709 for (var id in markers) {
710 str += markers[id].data;
711 }
712
713 return str;
714 };
715
716 return {
717 clone: function clone() {
718 throw new StyledComponentsError(5);
719 },
720
721 css: css,
722 getIds: getIdsFromMarkersFactory(markers),
723 hasNameForId: hasNameForId(names),
724 insertMarker: insertMarker,
725 insertRules: insertRules,
726 removeRules: removeRules,
727 sealed: false,
728 styleTag: el,
729 toElement: wrapAsElement(css, names),
730 toHTML: wrapAsHtmlTag(css, names)
731 };
732};
733
734var makeServerTag = function makeServerTag(namesArg, markersArg) {
735 var names = namesArg === undefined ? Object.create(null) : namesArg;
736 var markers = markersArg === undefined ? Object.create(null) : markersArg;
737
738 var insertMarker = function insertMarker(id) {
739 var prev = markers[id];
740 if (prev !== undefined) {
741 return prev;
742 }
743
744 return markers[id] = [''];
745 };
746
747 var insertRules = function insertRules(id, cssRules, name) {
748 var marker = insertMarker(id);
749 marker[0] += cssRules.join(' ');
750 addNameForId(names, id, name);
751 };
752
753 var removeRules = function removeRules(id) {
754 var marker = markers[id];
755 if (marker === undefined) return;
756 marker[0] = '';
757 resetIdNames(names, id);
758 };
759
760 var css = function css() {
761 var str = '';
762 // eslint-disable-next-line guard-for-in
763 for (var id in markers) {
764 var cssForId = markers[id][0];
765 if (cssForId) {
766 str += makeTextMarker(id) + cssForId;
767 }
768 }
769 return str;
770 };
771
772 var clone = function clone() {
773 var namesClone = cloneNames(names);
774 var markersClone = Object.create(null);
775
776 // eslint-disable-next-line guard-for-in
777 for (var id in markers) {
778 markersClone[id] = [markers[id][0]];
779 }
780
781 return makeServerTag(namesClone, markersClone);
782 };
783
784 var tag = {
785 clone: clone,
786 css: css,
787 getIds: getIdsFromMarkersFactory(markers),
788 hasNameForId: hasNameForId(names),
789 insertMarker: insertMarker,
790 insertRules: insertRules,
791 removeRules: removeRules,
792 sealed: false,
793 styleTag: null,
794 toElement: wrapAsElement(css, names),
795 toHTML: wrapAsHtmlTag(css, names)
796 };
797
798 return tag;
799};
800
801var makeTag = function makeTag(target, tagEl, forceServer, insertBefore, getImportRuleTag) {
802 if (IS_BROWSER && !forceServer) {
803 var el = makeStyleTag(target, tagEl, insertBefore);
804
805 if (DISABLE_SPEEDY) {
806 return makeBrowserTag(el, getImportRuleTag);
807 } else {
808 return makeSpeedyTag(el, getImportRuleTag);
809 }
810 }
811
812 return makeServerTag();
813};
814
815var rehydrate = function rehydrate(tag, els, extracted) {
816 /* add all extracted components to the new tag */
817 for (var i = 0, len = extracted.length; i < len; i += 1) {
818 var _extracted$i = extracted[i],
819 componentId = _extracted$i.componentId,
820 cssFromDOM = _extracted$i.cssFromDOM;
821
822 var cssRules = splitByRules(cssFromDOM);
823 tag.insertRules(componentId, cssRules);
824 }
825
826 /* remove old HTMLStyleElements, since they have been rehydrated */
827 for (var _i = 0, _len = els.length; _i < _len; _i += 1) {
828 var el = els[_i];
829 if (el.parentNode) {
830 el.parentNode.removeChild(el);
831 }
832 }
833};
834
835//
836
837var SPLIT_REGEX = /\s+/;
838
839/* determine the maximum number of components before tags are sharded */
840var MAX_SIZE = void 0;
841if (IS_BROWSER) {
842 /* in speedy mode we can keep a lot more rules in a sheet before a slowdown can be expected */
843 MAX_SIZE = DISABLE_SPEEDY ? 40 : 1000;
844} else {
845 /* for servers we do not need to shard at all */
846 MAX_SIZE = -1;
847}
848
849var sheetRunningId = 0;
850var master = void 0;
851
852var StyleSheet = function () {
853
854 /* a map from ids to tags */
855
856 /* deferred rules for a given id */
857
858 /* this is used for not reinjecting rules via hasNameForId() */
859
860 /* when rules for an id are removed using remove() we have to ignore rehydratedNames for it */
861
862 /* a list of tags belonging to this StyleSheet */
863
864 /* a tag for import rules */
865
866 /* current capacity until a new tag must be created */
867
868 /* children (aka clones) of this StyleSheet inheriting all and future injections */
869
870 function StyleSheet() {
871 var _this = this;
872
873 var target = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : IS_BROWSER ? document.head : null;
874 var forceServer = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
875 classCallCheck(this, StyleSheet);
876
877 this.getImportRuleTag = function () {
878 var importRuleTag = _this.importRuleTag;
879
880 if (importRuleTag !== undefined) {
881 return importRuleTag;
882 }
883
884 var firstTag = _this.tags[0];
885 var insertBefore = true;
886
887 return _this.importRuleTag = makeTag(_this.target, firstTag ? firstTag.styleTag : null, _this.forceServer, insertBefore);
888 };
889
890 sheetRunningId += 1;
891 this.id = sheetRunningId;
892 this.forceServer = forceServer;
893 this.target = forceServer ? null : target;
894 this.tagMap = {};
895 this.deferred = {};
896 this.rehydratedNames = {};
897 this.ignoreRehydratedNames = {};
898 this.tags = [];
899 this.capacity = 1;
900 this.clones = [];
901 }
902
903 /* rehydrate all SSR'd style tags */
904
905
906 StyleSheet.prototype.rehydrate = function rehydrate$$1() {
907 if (!IS_BROWSER || this.forceServer) return this;
908
909 var els = [];
910 var extracted = [];
911 var isStreamed = false;
912
913 /* retrieve all of our SSR style elements from the DOM */
914 var nodes = document.querySelectorAll('style[' + SC_ATTR + '][' + SC_VERSION_ATTR + '="' + "4.1.0" + '"]');
915
916 var nodesSize = nodes.length;
917
918 /* abort rehydration if no previous style tags were found */
919 if (!nodesSize) return this;
920
921 for (var i = 0; i < nodesSize; i += 1) {
922 var el = nodes[i];
923
924 /* check if style tag is a streamed tag */
925 if (!isStreamed) isStreamed = !!el.getAttribute(SC_STREAM_ATTR);
926
927 /* retrieve all component names */
928 var elNames = (el.getAttribute(SC_ATTR) || '').trim().split(SPLIT_REGEX);
929 var elNamesSize = elNames.length;
930 for (var j = 0, name; j < elNamesSize; j += 1) {
931 name = elNames[j];
932 /* add rehydrated name to sheet to avoid re-adding styles */
933 this.rehydratedNames[name] = true;
934 }
935
936 /* extract all components and their CSS */
937 extracted.push.apply(extracted, extractComps(el.textContent));
938
939 /* store original HTMLStyleElement */
940 els.push(el);
941 }
942
943 /* abort rehydration if nothing was extracted */
944 var extractedSize = extracted.length;
945 if (!extractedSize) return this;
946
947 /* create a tag to be used for rehydration */
948 var tag = this.makeTag(null);
949
950 rehydrate(tag, els, extracted);
951
952 /* reset capacity and adjust MAX_SIZE by the initial size of the rehydration */
953 this.capacity = Math.max(1, MAX_SIZE - extractedSize);
954 this.tags.push(tag);
955
956 /* retrieve all component ids */
957 for (var _j = 0; _j < extractedSize; _j += 1) {
958 this.tagMap[extracted[_j].componentId] = tag;
959 }
960
961 return this;
962 };
963
964 /* retrieve a "master" instance of StyleSheet which is typically used when no other is available
965 * The master StyleSheet is targeted by createGlobalStyle, keyframes, and components outside of any
966 * StyleSheetManager's context */
967
968
969 /* reset the internal "master" instance */
970 StyleSheet.reset = function reset() {
971 var forceServer = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
972
973 master = new StyleSheet(undefined, forceServer).rehydrate();
974 };
975
976 /* adds "children" to the StyleSheet that inherit all of the parents' rules
977 * while their own rules do not affect the parent */
978
979
980 StyleSheet.prototype.clone = function clone() {
981 var sheet = new StyleSheet(this.target, this.forceServer);
982
983 /* add to clone array */
984 this.clones.push(sheet);
985
986 /* clone all tags */
987 sheet.tags = this.tags.map(function (tag) {
988 var ids = tag.getIds();
989 var newTag = tag.clone();
990
991 /* reconstruct tagMap */
992 for (var i = 0; i < ids.length; i += 1) {
993 sheet.tagMap[ids[i]] = newTag;
994 }
995
996 return newTag;
997 });
998
999 /* clone other maps */
1000 sheet.rehydratedNames = _extends({}, this.rehydratedNames);
1001 sheet.deferred = _extends({}, this.deferred);
1002
1003 return sheet;
1004 };
1005
1006 /* force StyleSheet to create a new tag on the next injection */
1007
1008
1009 StyleSheet.prototype.sealAllTags = function sealAllTags() {
1010 this.capacity = 1;
1011
1012 this.tags.forEach(function (tag) {
1013 // eslint-disable-next-line no-param-reassign
1014 tag.sealed = true;
1015 });
1016 };
1017
1018 StyleSheet.prototype.makeTag = function makeTag$$1(tag) {
1019 var lastEl = tag ? tag.styleTag : null;
1020 var insertBefore = false;
1021
1022 return makeTag(this.target, lastEl, this.forceServer, insertBefore, this.getImportRuleTag);
1023 };
1024
1025 /* get a tag for a given componentId, assign the componentId to one, or shard */
1026 StyleSheet.prototype.getTagForId = function getTagForId(id) {
1027 /* simply return a tag, when the componentId was already assigned one */
1028 var prev = this.tagMap[id];
1029 if (prev !== undefined && !prev.sealed) {
1030 return prev;
1031 }
1032
1033 var tag = this.tags[this.tags.length - 1];
1034
1035 /* shard (create a new tag) if the tag is exhausted (See MAX_SIZE) */
1036 this.capacity -= 1;
1037
1038 if (this.capacity === 0) {
1039 this.capacity = MAX_SIZE;
1040 tag = this.makeTag(tag);
1041 this.tags.push(tag);
1042 }
1043
1044 return this.tagMap[id] = tag;
1045 };
1046
1047 /* mainly for createGlobalStyle to check for its id */
1048
1049
1050 StyleSheet.prototype.hasId = function hasId(id) {
1051 return this.tagMap[id] !== undefined;
1052 };
1053
1054 /* caching layer checking id+name to already have a corresponding tag and injected rules */
1055
1056
1057 StyleSheet.prototype.hasNameForId = function hasNameForId(id, name) {
1058 /* exception for rehydrated names which are checked separately */
1059 if (this.ignoreRehydratedNames[id] === undefined && this.rehydratedNames[name]) {
1060 return true;
1061 }
1062
1063 var tag = this.tagMap[id];
1064 return tag !== undefined && tag.hasNameForId(id, name);
1065 };
1066
1067 /* registers a componentId and registers it on its tag */
1068
1069
1070 StyleSheet.prototype.deferredInject = function deferredInject(id, cssRules) {
1071 /* don't inject when the id is already registered */
1072 if (this.tagMap[id] !== undefined) return;
1073
1074 var clones = this.clones;
1075
1076 for (var i = 0; i < clones.length; i += 1) {
1077 clones[i].deferredInject(id, cssRules);
1078 }
1079
1080 this.getTagForId(id).insertMarker(id);
1081 this.deferred[id] = cssRules;
1082 };
1083
1084 /* injects rules for a given id with a name that will need to be cached */
1085
1086
1087 StyleSheet.prototype.inject = function inject(id, cssRules, name) {
1088 var clones = this.clones;
1089
1090
1091 for (var i = 0; i < clones.length; i += 1) {
1092 clones[i].inject(id, cssRules, name);
1093 }
1094
1095 var tag = this.getTagForId(id);
1096
1097 /* add deferred rules for component */
1098 if (this.deferred[id] !== undefined) {
1099 // Combine passed cssRules with previously deferred CSS rules
1100 // NOTE: We cannot mutate the deferred array itself as all clones
1101 // do the same (see clones[i].inject)
1102 var rules = this.deferred[id].concat(cssRules);
1103 tag.insertRules(id, rules, name);
1104
1105 this.deferred[id] = undefined;
1106 } else {
1107 tag.insertRules(id, cssRules, name);
1108 }
1109 };
1110
1111 /* removes all rules for a given id, which doesn't remove its marker but resets it */
1112
1113
1114 StyleSheet.prototype.remove = function remove(id) {
1115 var tag = this.tagMap[id];
1116 if (tag === undefined) return;
1117
1118 var clones = this.clones;
1119
1120 for (var i = 0; i < clones.length; i += 1) {
1121 clones[i].remove(id);
1122 }
1123
1124 /* remove all rules from the tag */
1125 tag.removeRules(id);
1126
1127 /* ignore possible rehydrated names */
1128 this.ignoreRehydratedNames[id] = true;
1129
1130 /* delete possible deferred rules */
1131 this.deferred[id] = undefined;
1132 };
1133
1134 StyleSheet.prototype.toHTML = function toHTML() {
1135 return this.tags.map(function (tag) {
1136 return tag.toHTML();
1137 }).join('');
1138 };
1139
1140 StyleSheet.prototype.toReactElements = function toReactElements() {
1141 var id = this.id;
1142
1143
1144 return this.tags.map(function (tag, i) {
1145 var key = 'sc-' + id + '-' + i;
1146 return React.cloneElement(tag.toElement(), { key: key });
1147 });
1148 };
1149
1150 createClass(StyleSheet, null, [{
1151 key: 'master',
1152 get: function get$$1() {
1153 return master || (master = new StyleSheet().rehydrate());
1154 }
1155
1156 /* NOTE: This is just for backwards-compatibility with jest-styled-components */
1157
1158 }, {
1159 key: 'instance',
1160 get: function get$$1() {
1161 return StyleSheet.master;
1162 }
1163 }]);
1164 return StyleSheet;
1165}();
1166
1167//
1168
1169var Keyframes = function () {
1170 function Keyframes(name, rules) {
1171 var _this = this;
1172
1173 classCallCheck(this, Keyframes);
1174
1175 this.inject = function (styleSheet) {
1176 if (!styleSheet.hasNameForId(_this.id, _this.name)) {
1177 styleSheet.inject(_this.id, _this.rules, _this.name);
1178 }
1179 };
1180
1181 this.toString = function () {
1182 throw new StyledComponentsError(12, String(_this.name));
1183 };
1184
1185 this.name = name;
1186 this.rules = rules;
1187
1188 this.id = 'sc-keyframes-' + name;
1189 }
1190
1191 Keyframes.prototype.getName = function getName() {
1192 return this.name;
1193 };
1194
1195 return Keyframes;
1196}();
1197
1198//
1199
1200/**
1201 * inlined version of
1202 * https://github.com/facebook/fbjs/blob/master/packages/fbjs/src/core/hyphenateStyleName.js
1203 */
1204
1205var uppercasePattern = /([A-Z])/g;
1206var msPattern = /^ms-/;
1207
1208/**
1209 * Hyphenates a camelcased CSS property name, for example:
1210 *
1211 * > hyphenateStyleName('backgroundColor')
1212 * < "background-color"
1213 * > hyphenateStyleName('MozTransition')
1214 * < "-moz-transition"
1215 * > hyphenateStyleName('msTransition')
1216 * < "-ms-transition"
1217 *
1218 * As Modernizr suggests (http://modernizr.com/docs/#prefixed), an `ms` prefix
1219 * is converted to `-ms-`.
1220 *
1221 * @param {string} string
1222 * @return {string}
1223 */
1224function hyphenateStyleName(string) {
1225 return string.replace(uppercasePattern, '-$1').toLowerCase().replace(msPattern, '-ms-');
1226}
1227
1228//
1229
1230// Taken from https://github.com/facebook/react/blob/b87aabdfe1b7461e7331abb3601d9e6bb27544bc/packages/react-dom/src/shared/dangerousStyleValue.js
1231function addUnitIfNeeded(name, value) {
1232 // https://github.com/amilajack/eslint-plugin-flowtype-errors/issues/133
1233 // $FlowFixMe
1234 if (value == null || typeof value === 'boolean' || value === '') {
1235 return '';
1236 }
1237
1238 if (typeof value === 'number' && value !== 0 && !(name in unitless)) {
1239 return value + 'px'; // Presumes implicit 'px' suffix for unitless numbers
1240 }
1241
1242 return String(value).trim();
1243}
1244
1245//
1246
1247/**
1248 * It's falsish not falsy because 0 is allowed.
1249 */
1250var isFalsish = function isFalsish(chunk) {
1251 return chunk === undefined || chunk === null || chunk === false || chunk === '';
1252};
1253
1254var objToCss = function objToCss(obj, prevKey) {
1255 var css = Object.keys(obj).filter(function (key) {
1256 return !isFalsish(obj[key]);
1257 }).map(function (key) {
1258 if (isPlainObject(obj[key])) return objToCss(obj[key], key);
1259 return hyphenateStyleName(key) + ': ' + addUnitIfNeeded(key, obj[key]) + ';';
1260 }).join(' ');
1261 return prevKey ? prevKey + ' {\n ' + css + '\n}' : css;
1262};
1263
1264function flatten(chunk, executionContext, styleSheet) {
1265 if (Array.isArray(chunk)) {
1266 var ruleSet = [];
1267
1268 for (var i = 0, len = chunk.length, result; i < len; i += 1) {
1269 result = flatten(chunk[i], executionContext, styleSheet);
1270
1271 if (result === null) continue;else if (Array.isArray(result)) ruleSet.push.apply(ruleSet, result);else ruleSet.push(result);
1272 }
1273
1274 return ruleSet;
1275 }
1276
1277 if (isFalsish(chunk)) {
1278 return null;
1279 }
1280
1281 /* Handle other components */
1282 if (isStyledComponent(chunk)) {
1283 return '.' + chunk.styledComponentId;
1284 }
1285
1286 /* Either execute or defer the function */
1287 if (isFunction(chunk)) {
1288 if (executionContext) {
1289 // eslint-disable-next-line new-cap
1290 if (reactIs.isElement(new chunk(executionContext))) {
1291 throw new StyledComponentsError(13, getComponentName(chunk));
1292 }
1293
1294 return flatten(chunk(executionContext), executionContext, styleSheet);
1295 } else return chunk;
1296 }
1297
1298 if (chunk instanceof Keyframes) {
1299 if (styleSheet) {
1300 chunk.inject(styleSheet);
1301 return chunk.getName();
1302 } else return chunk;
1303 }
1304
1305 /* Handle objects */
1306 return isPlainObject(chunk) ? objToCss(chunk) : chunk.toString();
1307}
1308
1309//
1310
1311function css(styles) {
1312 for (var _len = arguments.length, interpolations = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
1313 interpolations[_key - 1] = arguments[_key];
1314 }
1315
1316 if (isFunction(styles) || isPlainObject(styles)) {
1317 // $FlowFixMe
1318 return flatten(interleave(EMPTY_ARRAY, [styles].concat(interpolations)));
1319 }
1320
1321 // $FlowFixMe
1322 return flatten(interleave(styles, interpolations));
1323}
1324
1325//
1326
1327function constructWithOptions(componentConstructor, tag) {
1328 var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : EMPTY_OBJECT;
1329
1330 if (!reactIs.isValidElementType(tag)) {
1331 throw new StyledComponentsError(1, String(tag));
1332 }
1333
1334 /* This is callable directly as a template function */
1335 // $FlowFixMe: Not typed to avoid destructuring arguments
1336 var templateFunction = function templateFunction() {
1337 return componentConstructor(tag, options, css.apply(undefined, arguments));
1338 };
1339
1340 /* If config methods are called, wrap up a new template function and merge options */
1341 templateFunction.withConfig = function (config) {
1342 return constructWithOptions(componentConstructor, tag, _extends({}, options, config));
1343 };
1344
1345 /* Modify/inject new props at runtime */
1346 templateFunction.attrs = function (attrs) {
1347 return constructWithOptions(componentConstructor, tag, _extends({}, options, {
1348 attrs: Array.prototype.concat(options.attrs, attrs).filter(Boolean)
1349 }));
1350 };
1351
1352 return templateFunction;
1353}
1354
1355//
1356// Source: https://github.com/garycourt/murmurhash-js/blob/master/murmurhash2_gc.js
1357function murmurhash(c) {
1358 for (var e = c.length | 0, a = e | 0, d = 0, b; e >= 4;) {
1359 b = c.charCodeAt(d) & 255 | (c.charCodeAt(++d) & 255) << 8 | (c.charCodeAt(++d) & 255) << 16 | (c.charCodeAt(++d) & 255) << 24, b = 1540483477 * (b & 65535) + ((1540483477 * (b >>> 16) & 65535) << 16), b ^= b >>> 24, b = 1540483477 * (b & 65535) + ((1540483477 * (b >>> 16) & 65535) << 16), a = 1540483477 * (a & 65535) + ((1540483477 * (a >>> 16) & 65535) << 16) ^ b, e -= 4, ++d;
1360 }
1361 switch (e) {
1362 case 3:
1363 a ^= (c.charCodeAt(d + 2) & 255) << 16;
1364 case 2:
1365 a ^= (c.charCodeAt(d + 1) & 255) << 8;
1366 case 1:
1367 a ^= c.charCodeAt(d) & 255, a = 1540483477 * (a & 65535) + ((1540483477 * (a >>> 16) & 65535) << 16);
1368 }
1369 a ^= a >>> 13;
1370 a = 1540483477 * (a & 65535) + ((1540483477 * (a >>> 16) & 65535) << 16);
1371 return (a ^ a >>> 15) >>> 0;
1372}
1373
1374//
1375/* eslint-disable no-bitwise */
1376
1377/* This is the "capacity" of our alphabet i.e. 2x26 for all letters plus their capitalised
1378 * counterparts */
1379var charsLength = 52;
1380
1381/* start at 75 for 'a' until 'z' (25) and then start at 65 for capitalised letters */
1382var getAlphabeticChar = function getAlphabeticChar(code) {
1383 return String.fromCharCode(code + (code > 25 ? 39 : 97));
1384};
1385
1386/* input a number, usually a hash and convert it to base-52 */
1387function generateAlphabeticName(code) {
1388 var name = '';
1389 var x = void 0;
1390
1391 /* get a char and divide by alphabet-length */
1392 for (x = code; x > charsLength; x = Math.floor(x / charsLength)) {
1393 name = getAlphabeticChar(x % charsLength) + name;
1394 }
1395
1396 return getAlphabeticChar(x % charsLength) + name;
1397}
1398
1399//
1400
1401function hasFunctionObjectKey(obj) {
1402 // eslint-disable-next-line guard-for-in, no-restricted-syntax
1403 for (var key in obj) {
1404 if (isFunction(obj[key])) {
1405 return true;
1406 }
1407 }
1408
1409 return false;
1410}
1411
1412function isStaticRules(rules, attrs) {
1413 for (var i = 0; i < rules.length; i += 1) {
1414 var rule = rules[i];
1415
1416 // recursive case
1417 if (Array.isArray(rule) && !isStaticRules(rule, attrs)) {
1418 return false;
1419 } else if (isFunction(rule) && !isStyledComponent(rule)) {
1420 // functions are allowed to be static if they're just being
1421 // used to get the classname of a nested styled component
1422 return false;
1423 }
1424 }
1425
1426 if (attrs.some(function (x) {
1427 return isFunction(x) || hasFunctionObjectKey(x);
1428 })) return false;
1429
1430 return true;
1431}
1432
1433//
1434
1435var isHMREnabled = process.env.NODE_ENV !== 'production' && typeof module !== 'undefined' && module.hot;
1436
1437/* combines hashStr (murmurhash) and nameGenerator for convenience */
1438var hasher = function hasher(str) {
1439 return generateAlphabeticName(murmurhash(str));
1440};
1441
1442/*
1443 ComponentStyle is all the CSS-specific stuff, not
1444 the React-specific stuff.
1445 */
1446
1447var ComponentStyle = function () {
1448 function ComponentStyle(rules, attrs, componentId) {
1449 classCallCheck(this, ComponentStyle);
1450
1451 this.rules = rules;
1452 this.isStatic = !isHMREnabled && isStaticRules(rules, attrs);
1453 this.componentId = componentId;
1454
1455 if (!StyleSheet.master.hasId(componentId)) {
1456 StyleSheet.master.deferredInject(componentId, []);
1457 }
1458 }
1459
1460 /*
1461 * Flattens a rule set into valid CSS
1462 * Hashes it, wraps the whole chunk in a .hash1234 {}
1463 * Returns the hash to be injected on render()
1464 * */
1465
1466
1467 ComponentStyle.prototype.generateAndInjectStyles = function generateAndInjectStyles(executionContext, styleSheet) {
1468 var isStatic = this.isStatic,
1469 componentId = this.componentId,
1470 lastClassName = this.lastClassName;
1471
1472 if (IS_BROWSER && isStatic && typeof lastClassName === 'string' && styleSheet.hasNameForId(componentId, lastClassName)) {
1473 return lastClassName;
1474 }
1475
1476 var flatCSS = flatten(this.rules, executionContext, styleSheet);
1477 var name = hasher(this.componentId + flatCSS.join(''));
1478 if (!styleSheet.hasNameForId(componentId, name)) {
1479 styleSheet.inject(this.componentId, stringifyRules(flatCSS, '.' + name, undefined, componentId), name);
1480 }
1481
1482 this.lastClassName = name;
1483 return name;
1484 };
1485
1486 ComponentStyle.generateName = function generateName(str) {
1487 return hasher(str);
1488 };
1489
1490 return ComponentStyle;
1491}();
1492
1493//
1494
1495var LIMIT = 200;
1496
1497var createWarnTooManyClasses = (function (displayName) {
1498 var generatedClasses = {};
1499 var warningSeen = false;
1500
1501 return function (className) {
1502 if (!warningSeen) {
1503 generatedClasses[className] = true;
1504 if (Object.keys(generatedClasses).length >= LIMIT) {
1505 // Unable to find latestRule in test environment.
1506 /* eslint-disable no-console, prefer-template */
1507 console.warn('Over ' + LIMIT + ' classes were generated for component ' + displayName + '. \n' + 'Consider using the attrs method, together with a style object for frequently changed styles.\n' + 'Example:\n' + ' const Component = styled.div.attrs({\n' + ' style: ({ background }) => ({\n' + ' background,\n' + ' }),\n' + ' })`width: 100%;`\n\n' + ' <Component />');
1508 warningSeen = true;
1509 generatedClasses = {};
1510 }
1511 }
1512 };
1513});
1514
1515//
1516
1517var determineTheme = (function (props, fallbackTheme) {
1518 var defaultProps = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : EMPTY_OBJECT;
1519
1520 // Props should take precedence over ThemeProvider, which should take precedence over
1521 // defaultProps, but React automatically puts defaultProps on props.
1522
1523 /* eslint-disable react/prop-types, flowtype-errors/show-errors */
1524 var isDefaultTheme = defaultProps ? props.theme === defaultProps.theme : false;
1525 var theme = props.theme && !isDefaultTheme ? props.theme : fallbackTheme || defaultProps.theme;
1526 /* eslint-enable */
1527
1528 return theme;
1529});
1530
1531//
1532var escapeRegex = /[[\].#*$><+~=|^:(),"'`-]+/g;
1533var dashesAtEnds = /(^-|-$)/g;
1534
1535/**
1536 * TODO: Explore using CSS.escape when it becomes more available
1537 * in evergreen browsers.
1538 */
1539function escape(str) {
1540 return str
1541 // Replace all possible CSS selectors
1542 .replace(escapeRegex, '-')
1543
1544 // Remove extraneous hyphens at the start and end
1545 .replace(dashesAtEnds, '');
1546}
1547
1548//
1549
1550function isTag(target) /* : %checks */{
1551 return typeof target === 'string';
1552}
1553
1554//
1555
1556function generateDisplayName(target) {
1557 return isTag(target) ? 'styled.' + target : 'Styled(' + getComponentName(target) + ')';
1558}
1559
1560var _TYPE_STATICS;
1561
1562var REACT_STATICS = {
1563 childContextTypes: true,
1564 contextTypes: true,
1565 defaultProps: true,
1566 displayName: true,
1567 getDerivedStateFromProps: true,
1568 propTypes: true,
1569 type: true
1570};
1571
1572var KNOWN_STATICS = {
1573 name: true,
1574 length: true,
1575 prototype: true,
1576 caller: true,
1577 callee: true,
1578 arguments: true,
1579 arity: true
1580};
1581
1582var TYPE_STATICS = (_TYPE_STATICS = {}, _TYPE_STATICS[reactIs.ForwardRef] = {
1583 $$typeof: true,
1584 render: true
1585}, _TYPE_STATICS);
1586
1587var defineProperty$1 = Object.defineProperty,
1588 getOwnPropertyNames = Object.getOwnPropertyNames,
1589 _Object$getOwnPropert = Object.getOwnPropertySymbols,
1590 getOwnPropertySymbols = _Object$getOwnPropert === undefined ? function () {
1591 return [];
1592} : _Object$getOwnPropert,
1593 getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor,
1594 getPrototypeOf = Object.getPrototypeOf,
1595 objectPrototype = Object.prototype;
1596var arrayPrototype = Array.prototype;
1597
1598
1599function hoistNonReactStatics(targetComponent, sourceComponent, blacklist) {
1600 if (typeof sourceComponent !== 'string') {
1601 // don't hoist over string (html) components
1602
1603 var inheritedComponent = getPrototypeOf(sourceComponent);
1604
1605 if (inheritedComponent && inheritedComponent !== objectPrototype) {
1606 hoistNonReactStatics(targetComponent, inheritedComponent, blacklist);
1607 }
1608
1609 var keys = arrayPrototype.concat(getOwnPropertyNames(sourceComponent),
1610 // $FlowFixMe
1611 getOwnPropertySymbols(sourceComponent));
1612
1613 var targetStatics = TYPE_STATICS[targetComponent.$$typeof] || REACT_STATICS;
1614
1615 var sourceStatics = TYPE_STATICS[sourceComponent.$$typeof] || REACT_STATICS;
1616
1617 var i = keys.length;
1618 var descriptor = void 0;
1619 var key = void 0;
1620
1621 // eslint-disable-next-line no-plusplus
1622 while (i--) {
1623 key = keys[i];
1624
1625 if (
1626 // $FlowFixMe
1627 !KNOWN_STATICS[key] && !(blacklist && blacklist[key]) && !(sourceStatics && sourceStatics[key]) &&
1628 // $FlowFixMe
1629 !(targetStatics && targetStatics[key])) {
1630 descriptor = getOwnPropertyDescriptor(sourceComponent, key);
1631
1632 if (descriptor) {
1633 try {
1634 // Avoid failures from read-only properties
1635 defineProperty$1(targetComponent, key, descriptor);
1636 } catch (e) {
1637 /* fail silently */
1638 }
1639 }
1640 }
1641 }
1642
1643 return targetComponent;
1644 }
1645
1646 return targetComponent;
1647}
1648
1649//
1650function isDerivedReactComponent(fn) {
1651 return !!(fn && fn.prototype && fn.prototype.isReactComponent);
1652}
1653
1654//
1655// Helper to call a given function, only once
1656var once = (function (cb) {
1657 var called = false;
1658
1659 return function () {
1660 if (!called) {
1661 called = true;
1662 cb.apply(undefined, arguments);
1663 }
1664 };
1665});
1666
1667//
1668
1669var ThemeContext = React.createContext();
1670
1671var ThemeConsumer = ThemeContext.Consumer;
1672
1673/**
1674 * Provide a theme to an entire react component tree via context
1675 */
1676
1677var ThemeProvider = function (_Component) {
1678 inherits(ThemeProvider, _Component);
1679
1680 function ThemeProvider(props) {
1681 classCallCheck(this, ThemeProvider);
1682
1683 var _this = possibleConstructorReturn(this, _Component.call(this, props));
1684
1685 _this.getContext = memoize(_this.getContext.bind(_this));
1686 _this.renderInner = _this.renderInner.bind(_this);
1687 return _this;
1688 }
1689
1690 ThemeProvider.prototype.render = function render() {
1691 if (!this.props.children) return null;
1692
1693 return React__default.createElement(
1694 ThemeContext.Consumer,
1695 null,
1696 this.renderInner
1697 );
1698 };
1699
1700 ThemeProvider.prototype.renderInner = function renderInner(outerTheme) {
1701 var context = this.getContext(this.props.theme, outerTheme);
1702
1703 return React__default.createElement(
1704 ThemeContext.Provider,
1705 { value: context },
1706 React__default.Children.only(this.props.children)
1707 );
1708 };
1709
1710 /**
1711 * Get the theme from the props, supporting both (outerTheme) => {}
1712 * as well as object notation
1713 */
1714
1715
1716 ThemeProvider.prototype.getTheme = function getTheme(theme, outerTheme) {
1717 if (isFunction(theme)) {
1718 var mergedTheme = theme(outerTheme);
1719
1720 if (process.env.NODE_ENV !== 'production' && (mergedTheme === null || Array.isArray(mergedTheme) || (typeof mergedTheme === 'undefined' ? 'undefined' : _typeof(mergedTheme)) !== 'object')) {
1721 throw new StyledComponentsError(7);
1722 }
1723
1724 return mergedTheme;
1725 }
1726
1727 if (theme === null || Array.isArray(theme) || (typeof theme === 'undefined' ? 'undefined' : _typeof(theme)) !== 'object') {
1728 throw new StyledComponentsError(8);
1729 }
1730
1731 return _extends({}, outerTheme, theme);
1732 };
1733
1734 ThemeProvider.prototype.getContext = function getContext(theme, outerTheme) {
1735 return this.getTheme(theme, outerTheme);
1736 };
1737
1738 return ThemeProvider;
1739}(React.Component);
1740
1741//
1742
1743var ServerStyleSheet = function () {
1744 function ServerStyleSheet() {
1745 classCallCheck(this, ServerStyleSheet);
1746
1747 /* The master sheet might be reset, so keep a reference here */
1748 this.masterSheet = StyleSheet.master;
1749 this.instance = this.masterSheet.clone();
1750 this.sealed = false;
1751 }
1752
1753 /**
1754 * Mark the ServerStyleSheet as being fully emitted and manually GC it from the
1755 * StyleSheet singleton.
1756 */
1757
1758
1759 ServerStyleSheet.prototype.seal = function seal() {
1760 if (!this.sealed) {
1761 /* Remove sealed StyleSheets from the master sheet */
1762 var index = this.masterSheet.clones.indexOf(this.instance);
1763 this.masterSheet.clones.splice(index, 1);
1764 this.sealed = true;
1765 }
1766 };
1767
1768 ServerStyleSheet.prototype.collectStyles = function collectStyles(children) {
1769 if (this.sealed) {
1770 throw new StyledComponentsError(2);
1771 }
1772
1773 return React__default.createElement(
1774 StyleSheetManager,
1775 { sheet: this.instance },
1776 children
1777 );
1778 };
1779
1780 ServerStyleSheet.prototype.getStyleTags = function getStyleTags() {
1781 this.seal();
1782 return this.instance.toHTML();
1783 };
1784
1785 ServerStyleSheet.prototype.getStyleElement = function getStyleElement() {
1786 this.seal();
1787 return this.instance.toReactElements();
1788 };
1789
1790 ServerStyleSheet.prototype.interleaveWithNodeStream = function interleaveWithNodeStream(readableStream) {
1791 var _this = this;
1792
1793 if (!__SERVER__ || IS_BROWSER) {
1794 throw new StyledComponentsError(3);
1795 }
1796
1797 /* the tag index keeps track of which tags have already been emitted */
1798 var instance = this.instance;
1799
1800 var instanceTagIndex = 0;
1801
1802 var streamAttr = SC_STREAM_ATTR + '="true"';
1803
1804 var transformer = new stream.Transform({
1805 transform: function appendStyleChunks(chunk, /* encoding */_, callback) {
1806 var tags = instance.tags;
1807
1808 var html = '';
1809
1810 /* retrieve html for each new style tag */
1811 for (; instanceTagIndex < tags.length; instanceTagIndex += 1) {
1812 var tag = tags[instanceTagIndex];
1813 html += tag.toHTML(streamAttr);
1814 }
1815
1816 /* force our StyleSheets to emit entirely new tags */
1817 instance.sealAllTags();
1818
1819 /* prepend style html to chunk */
1820 this.push(html + chunk);
1821 callback();
1822 }
1823 });
1824
1825 readableStream.on('end', function () {
1826 return _this.seal();
1827 });
1828 readableStream.on('error', function (err) {
1829 _this.seal();
1830
1831 // forward the error to the transform stream
1832 transformer.emit('error', err);
1833 });
1834
1835 return readableStream.pipe(transformer);
1836 };
1837
1838 return ServerStyleSheet;
1839}();
1840
1841//
1842
1843var StyleSheetContext = React.createContext();
1844
1845var StyleSheetConsumer = StyleSheetContext.Consumer;
1846
1847var StyleSheetManager = function (_Component) {
1848 inherits(StyleSheetManager, _Component);
1849
1850 function StyleSheetManager(props) {
1851 classCallCheck(this, StyleSheetManager);
1852
1853 var _this = possibleConstructorReturn(this, _Component.call(this, props));
1854
1855 _this.getContext = memoize(_this.getContext);
1856 return _this;
1857 }
1858
1859 StyleSheetManager.prototype.getContext = function getContext(sheet, target) {
1860 if (sheet) {
1861 return sheet;
1862 } else if (target) {
1863 return new StyleSheet(target);
1864 } else {
1865 throw new StyledComponentsError(4);
1866 }
1867 };
1868
1869 StyleSheetManager.prototype.render = function render() {
1870 var _props = this.props,
1871 children = _props.children,
1872 sheet = _props.sheet,
1873 target = _props.target;
1874
1875
1876 return React__default.createElement(
1877 StyleSheetContext.Provider,
1878 { value: this.getContext(sheet, target) },
1879 process.env.NODE_ENV !== 'production' ? React__default.Children.only(children) : children
1880 );
1881 };
1882
1883 return StyleSheetManager;
1884}(React.Component);
1885process.env.NODE_ENV !== "production" ? StyleSheetManager.propTypes = {
1886 sheet: PropTypes.oneOfType([PropTypes.instanceOf(StyleSheet), PropTypes.instanceOf(ServerStyleSheet)]),
1887
1888 target: PropTypes.shape({
1889 appendChild: PropTypes.func.isRequired
1890 })
1891} : void 0;
1892
1893//
1894
1895var didWarnAboutClassNameUsage = new Set();
1896
1897var classNameUsageCheckInjector = (function (target) {
1898 var elementClassName = '';
1899
1900 var targetCDM = target.componentDidMount;
1901
1902 // eslint-disable-next-line no-param-reassign
1903 target.componentDidMount = function componentDidMount() {
1904 if (typeof targetCDM === 'function') {
1905 targetCDM.call(this);
1906 }
1907
1908 var forwardTarget = this.props.forwardedClass.target;
1909
1910 if (target.props && target.props.suppressClassNameWarning || target.attrs && target.attrs.suppressClassNameWarning || didWarnAboutClassNameUsage.has(forwardTarget)) {
1911 return;
1912 }
1913
1914 didWarnAboutClassNameUsage.add(forwardTarget);
1915
1916 var classNames = elementClassName.replace(/ +/g, ' ').trim().split(' ');
1917 // eslint-disable-next-line react/no-find-dom-node
1918 var node = ReactDOM.findDOMNode(this);
1919 var selector = classNames.map(function (s) {
1920 return '.' + s;
1921 }).join('');
1922
1923 if (node && node.nodeType === 1 && !classNames.every(function (className) {
1924 return node.classList && node.classList.contains(className);
1925 }) && !node.querySelector(selector)) {
1926 console.warn('It looks like you\'ve wrapped styled() around your React component (' + getComponentName(forwardTarget) + '), but the className prop is not being passed down to a child. No styles will be rendered unless className is composed within your React component.');
1927 }
1928 };
1929
1930 var prevRenderInner = target.renderInner;
1931
1932 // eslint-disable-next-line no-param-reassign
1933 target.renderInner = function renderInner() {
1934 for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
1935 args[_key] = arguments[_key];
1936 }
1937
1938 var element = prevRenderInner.apply(this, args);
1939
1940 elementClassName = element.props.className;
1941
1942 return element;
1943 };
1944});
1945
1946//
1947
1948var identifiers = {};
1949
1950/* We depend on components having unique IDs */
1951function generateId(_ComponentStyle, _displayName, parentComponentId) {
1952 var displayName = typeof _displayName !== 'string' ? 'sc' : escape(_displayName);
1953
1954 /**
1955 * This ensures uniqueness if two components happen to share
1956 * the same displayName.
1957 */
1958 var nr = (identifiers[displayName] || 0) + 1;
1959 identifiers[displayName] = nr;
1960
1961 var componentId = displayName + '-' + _ComponentStyle.generateName(displayName + nr);
1962
1963 return parentComponentId ? parentComponentId + '-' + componentId : componentId;
1964}
1965
1966var warnInnerRef = once(function () {
1967 return (
1968 // eslint-disable-next-line no-console
1969 console.warn('The "innerRef" API has been removed in styled-components v4 in favor of React 16 ref forwarding, use "ref" instead like a typical component.')
1970 );
1971});
1972
1973var warnAttrsFnObjectKeyDeprecated = once(function (key, displayName) {
1974 return (
1975 // eslint-disable-next-line no-console
1976 console.warn('Functions as object-form attrs({}) keys are now deprecated and will be removed in a future version of styled-components. Switch to the new attrs(props => ({})) syntax instead for easier and more powerful composition. The attrs key in question is "' + key + '" on component "' + displayName + '".')
1977 );
1978});
1979
1980var warnNonStyledComponentAttrsObjectKey = once(function (key, displayName) {
1981 return (
1982 // eslint-disable-next-line no-console
1983 console.warn('It looks like you\'ve used a non styled-component as the value for the "' + key + '" prop in an object-form attrs constructor of "' + displayName + '".\n' + 'You should use the new function-form attrs constructor which avoids this issue: attrs(props => ({ yourStuff }))\n' + "To continue using the deprecated object syntax, you'll need to wrap your component prop in a function to make it available inside the styled component (you'll still get the deprecation warning though.)\n" + ('For example, { ' + key + ': () => InnerComponent } instead of { ' + key + ': InnerComponent }'))
1984 );
1985});
1986
1987// $FlowFixMe
1988
1989var StyledComponent = function (_Component) {
1990 inherits(StyledComponent, _Component);
1991
1992 function StyledComponent() {
1993 classCallCheck(this, StyledComponent);
1994
1995 var _this = possibleConstructorReturn(this, _Component.call(this));
1996
1997 _this.attrs = {};
1998
1999 _this.renderOuter = _this.renderOuter.bind(_this);
2000 _this.renderInner = _this.renderInner.bind(_this);
2001
2002 if (process.env.NODE_ENV !== 'production' && IS_BROWSER) {
2003 classNameUsageCheckInjector(_this);
2004 }
2005 return _this;
2006 }
2007
2008 StyledComponent.prototype.render = function render() {
2009 return React__default.createElement(
2010 StyleSheetConsumer,
2011 null,
2012 this.renderOuter
2013 );
2014 };
2015
2016 StyledComponent.prototype.renderOuter = function renderOuter(styleSheet) {
2017 this.styleSheet = styleSheet;
2018 var componentStyle = this.props.forwardedClass.componentStyle;
2019
2020 // No need to subscribe a static component to theme changes, it won't change anything
2021
2022 if (componentStyle.isStatic) return this.renderInner();
2023
2024 return React__default.createElement(
2025 ThemeConsumer,
2026 null,
2027 this.renderInner
2028 );
2029 };
2030
2031 StyledComponent.prototype.renderInner = function renderInner(theme) {
2032 var _props$forwardedClass = this.props.forwardedClass,
2033 componentStyle = _props$forwardedClass.componentStyle,
2034 defaultProps = _props$forwardedClass.defaultProps,
2035 styledComponentId = _props$forwardedClass.styledComponentId,
2036 target = _props$forwardedClass.target;
2037
2038
2039 var generatedClassName = void 0;
2040 if (componentStyle.isStatic) {
2041 generatedClassName = this.generateAndInjectStyles(EMPTY_OBJECT, this.props, this.styleSheet);
2042 } else if (theme !== undefined) {
2043 generatedClassName = this.generateAndInjectStyles(determineTheme(this.props, theme, defaultProps), this.props, this.styleSheet);
2044 } else {
2045 generatedClassName = this.generateAndInjectStyles(this.props.theme || EMPTY_OBJECT, this.props, this.styleSheet);
2046 }
2047 var elementToBeCreated = this.props.as || this.attrs.as || target;
2048 var isTargetTag = isTag(elementToBeCreated);
2049
2050 var propsForElement = {};
2051 var computedProps = _extends({}, this.attrs, this.props);
2052
2053 var key = void 0;
2054 // eslint-disable-next-line guard-for-in
2055 for (key in computedProps) {
2056 if (process.env.NODE_ENV !== 'production' && key === 'innerRef') {
2057 warnInnerRef();
2058 }
2059
2060 if (key === 'forwardedClass' || key === 'as') continue;else if (key === 'forwardedRef') propsForElement.ref = computedProps[key];else if (!isTargetTag || validAttr(key)) {
2061 // Don't pass through non HTML tags through to HTML elements
2062 propsForElement[key] = computedProps[key];
2063 }
2064 }
2065
2066 if (this.props.style && this.attrs.style) {
2067 propsForElement.style = _extends({}, this.attrs.style, this.props.style);
2068 }
2069
2070 propsForElement.className = [this.props.className, styledComponentId, this.attrs.className, generatedClassName].filter(Boolean).join(' ');
2071
2072 return React.createElement(elementToBeCreated, propsForElement);
2073 };
2074
2075 StyledComponent.prototype.buildExecutionContext = function buildExecutionContext(theme, props, attrs) {
2076 var _this2 = this;
2077
2078 var context = _extends({}, props, { theme: theme });
2079
2080 if (!attrs.length) return context;
2081
2082 this.attrs = {};
2083
2084 attrs.forEach(function (attrDef) {
2085 var resolvedAttrDef = attrDef;
2086 var attrDefWasFn = false;
2087 var attr = void 0;
2088 var key = void 0;
2089
2090 if (isFunction(resolvedAttrDef)) {
2091 // $FlowFixMe
2092 resolvedAttrDef = resolvedAttrDef(props);
2093 attrDefWasFn = true;
2094 }
2095
2096 /* eslint-disable guard-for-in */
2097 // $FlowFixMe
2098 for (key in resolvedAttrDef) {
2099 attr = resolvedAttrDef[key];
2100
2101 if (!attrDefWasFn) {
2102 if (isFunction(attr) && !isDerivedReactComponent(attr) && !isStyledComponent(attr)) {
2103 if (process.env.NODE_ENV !== 'production' && warnAttrsFnObjectKeyDeprecated) {
2104 warnAttrsFnObjectKeyDeprecated(key, props.forwardedClass.displayName);
2105 }
2106
2107 attr = attr(context);
2108
2109 if (process.env.NODE_ENV !== 'production' && React__default.isValidElement(attr) && warnNonStyledComponentAttrsObjectKey) {
2110 warnNonStyledComponentAttrsObjectKey(key, props.forwardedClass.displayName);
2111 }
2112 }
2113 }
2114
2115 _this2.attrs[key] = attr;
2116 context[key] = attr;
2117 }
2118 /* eslint-enable */
2119 });
2120
2121 return context;
2122 };
2123
2124 StyledComponent.prototype.generateAndInjectStyles = function generateAndInjectStyles(theme, props) {
2125 var styleSheet = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : StyleSheet.master;
2126 var _props$forwardedClass2 = props.forwardedClass,
2127 attrs = _props$forwardedClass2.attrs,
2128 componentStyle = _props$forwardedClass2.componentStyle,
2129 warnTooManyClasses = _props$forwardedClass2.warnTooManyClasses;
2130
2131 // statically styled-components don't need to build an execution context object,
2132 // and shouldn't be increasing the number of class names
2133
2134 if (componentStyle.isStatic && !attrs.length) {
2135 return componentStyle.generateAndInjectStyles(EMPTY_OBJECT, styleSheet);
2136 }
2137
2138 var className = componentStyle.generateAndInjectStyles(this.buildExecutionContext(theme, props, attrs), styleSheet);
2139
2140 if (process.env.NODE_ENV !== 'production' && warnTooManyClasses) warnTooManyClasses(className);
2141
2142 return className;
2143 };
2144
2145 return StyledComponent;
2146}(React.Component);
2147
2148function createStyledComponent(target, options, rules) {
2149 var isTargetStyledComp = isStyledComponent(target);
2150 var isClass = !isTag(target);
2151
2152 var _options$displayName = options.displayName,
2153 displayName = _options$displayName === undefined ? generateDisplayName(target) : _options$displayName,
2154 _options$componentId = options.componentId,
2155 componentId = _options$componentId === undefined ? generateId(ComponentStyle, options.displayName, options.parentComponentId) : _options$componentId,
2156 _options$ParentCompon = options.ParentComponent,
2157 ParentComponent = _options$ParentCompon === undefined ? StyledComponent : _options$ParentCompon,
2158 _options$attrs = options.attrs,
2159 attrs = _options$attrs === undefined ? EMPTY_ARRAY : _options$attrs;
2160
2161
2162 var styledComponentId = options.displayName && options.componentId ? escape(options.displayName) + '-' + options.componentId : options.componentId || componentId;
2163
2164 // fold the underlying StyledComponent attrs up (implicit extend)
2165 var finalAttrs =
2166 // $FlowFixMe
2167 isTargetStyledComp && target.attrs ? Array.prototype.concat(target.attrs, attrs).filter(Boolean) : attrs;
2168
2169 var componentStyle = new ComponentStyle(isTargetStyledComp ? // fold the underlying StyledComponent rules up (implicit extend)
2170 // $FlowFixMe
2171 target.componentStyle.rules.concat(rules) : rules, finalAttrs, styledComponentId);
2172
2173 /**
2174 * forwardRef creates a new interim component, which we'll take advantage of
2175 * instead of extending ParentComponent to create _another_ interim class
2176 */
2177 var WrappedStyledComponent = React__default.forwardRef(function (props, ref) {
2178 return React__default.createElement(ParentComponent, _extends({}, props, { forwardedClass: WrappedStyledComponent, forwardedRef: ref }));
2179 });
2180
2181 // $FlowFixMe
2182 WrappedStyledComponent.attrs = finalAttrs;
2183 // $FlowFixMe
2184 WrappedStyledComponent.componentStyle = componentStyle;
2185 WrappedStyledComponent.displayName = displayName;
2186 // $FlowFixMe
2187 WrappedStyledComponent.styledComponentId = styledComponentId;
2188
2189 // fold the underlying StyledComponent target up since we folded the styles
2190 // $FlowFixMe
2191 WrappedStyledComponent.target = isTargetStyledComp ? target.target : target;
2192
2193 // $FlowFixMe
2194 WrappedStyledComponent.withComponent = function withComponent(tag) {
2195 var previousComponentId = options.componentId,
2196 optionsToCopy = objectWithoutProperties(options, ['componentId']);
2197
2198
2199 var newComponentId = previousComponentId && previousComponentId + '-' + (isTag(tag) ? tag : escape(getComponentName(tag)));
2200
2201 var newOptions = _extends({}, optionsToCopy, {
2202 attrs: finalAttrs,
2203 componentId: newComponentId,
2204 ParentComponent: ParentComponent
2205 });
2206
2207 return createStyledComponent(tag, newOptions, rules);
2208 };
2209
2210 if (process.env.NODE_ENV !== 'production') {
2211 // $FlowFixMe
2212 WrappedStyledComponent.warnTooManyClasses = createWarnTooManyClasses(displayName);
2213 }
2214
2215 // $FlowFixMe
2216 WrappedStyledComponent.toString = function () {
2217 return '.' + WrappedStyledComponent.styledComponentId;
2218 };
2219
2220 if (isClass) {
2221 hoistNonReactStatics(WrappedStyledComponent, target, {
2222 // all SC-specific things should not be hoisted
2223 attrs: true,
2224 componentStyle: true,
2225 displayName: true,
2226 styledComponentId: true,
2227 target: true,
2228 withComponent: true
2229 });
2230 }
2231
2232 return WrappedStyledComponent;
2233}
2234
2235//
2236// Thanks to ReactDOMFactories for this handy list!
2237
2238var domElements = ['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', 'marquee', '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',
2239
2240// SVG
2241'circle', 'clipPath', 'defs', 'ellipse', 'foreignObject', 'g', 'image', 'line', 'linearGradient', 'mask', 'path', 'pattern', 'polygon', 'polyline', 'radialGradient', 'rect', 'stop', 'svg', 'text', 'tspan'];
2242
2243//
2244
2245var styled = function styled(tag) {
2246 return constructWithOptions(createStyledComponent, tag);
2247};
2248
2249// Shorthands for all valid HTML Elements
2250domElements.forEach(function (domElement) {
2251 styled[domElement] = styled(domElement);
2252});
2253
2254//
2255
2256var GlobalStyle = function () {
2257 function GlobalStyle(rules, componentId) {
2258 classCallCheck(this, GlobalStyle);
2259
2260 this.rules = rules;
2261 this.componentId = componentId;
2262 this.isStatic = isStaticRules(rules, EMPTY_ARRAY);
2263
2264 if (!StyleSheet.master.hasId(componentId)) {
2265 StyleSheet.master.deferredInject(componentId, []);
2266 }
2267 }
2268
2269 GlobalStyle.prototype.createStyles = function createStyles(executionContext, styleSheet) {
2270 var flatCSS = flatten(this.rules, executionContext, styleSheet);
2271 var css = stringifyRules(flatCSS, '');
2272
2273 styleSheet.inject(this.componentId, css);
2274 };
2275
2276 GlobalStyle.prototype.removeStyles = function removeStyles(styleSheet) {
2277 var componentId = this.componentId;
2278
2279 if (styleSheet.hasId(componentId)) {
2280 styleSheet.remove(componentId);
2281 }
2282 };
2283
2284 // TODO: overwrite in-place instead of remove+create?
2285
2286
2287 GlobalStyle.prototype.renderStyles = function renderStyles(executionContext, styleSheet) {
2288 this.removeStyles(styleSheet);
2289 this.createStyles(executionContext, styleSheet);
2290 };
2291
2292 return GlobalStyle;
2293}();
2294
2295//
2296
2297// place our cache into shared context so it'll persist between HMRs
2298if (IS_BROWSER) {
2299 window.scCGSHMRCache = {};
2300}
2301
2302function createGlobalStyle(strings) {
2303 for (var _len = arguments.length, interpolations = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
2304 interpolations[_key - 1] = arguments[_key];
2305 }
2306
2307 var rules = css.apply(undefined, [strings].concat(interpolations));
2308 var id = 'sc-global-' + murmurhash(JSON.stringify(rules));
2309 var style = new GlobalStyle(rules, id);
2310
2311 var GlobalStyleComponent = function (_React$Component) {
2312 inherits(GlobalStyleComponent, _React$Component);
2313
2314 function GlobalStyleComponent() {
2315 classCallCheck(this, GlobalStyleComponent);
2316
2317 var _this = possibleConstructorReturn(this, _React$Component.call(this));
2318
2319 var _this$constructor = _this.constructor,
2320 globalStyle = _this$constructor.globalStyle,
2321 styledComponentId = _this$constructor.styledComponentId;
2322
2323
2324 if (IS_BROWSER) {
2325 window.scCGSHMRCache[styledComponentId] = (window.scCGSHMRCache[styledComponentId] || 0) + 1;
2326 }
2327
2328 /**
2329 * This fixes HMR compatiblility. Don't ask me why, but this combination of
2330 * caching the closure variables via statics and then persisting the statics in
2331 * state works across HMR where no other combination did. ¯\_()_
2332 */
2333 _this.state = {
2334 globalStyle: globalStyle,
2335 styledComponentId: styledComponentId
2336 };
2337 return _this;
2338 }
2339
2340 GlobalStyleComponent.prototype.componentDidMount = function componentDidMount() {
2341 if (process.env.NODE_ENV !== 'production' && IS_BROWSER && window.scCGSHMRCache[this.state.styledComponentId] > 1 && !this.props.suppressMultiMountWarning) {
2342 console.warn('The global style component ' + this.state.styledComponentId + ' was composed and rendered multiple times in your React component tree. Only the last-rendered copy will have its styles remain in <head> (or your StyleSheetManager target.)');
2343 }
2344 };
2345
2346 GlobalStyleComponent.prototype.componentWillUnmount = function componentWillUnmount() {
2347 if (window.scCGSHMRCache[this.state.styledComponentId]) {
2348 window.scCGSHMRCache[this.state.styledComponentId] -= 1;
2349 }
2350 /**
2351 * Depending on the order "render" is called this can cause the styles to be lost
2352 * until the next render pass of the remaining instance, which may
2353 * not be immediate.
2354 */
2355 if (window.scCGSHMRCache[this.state.styledComponentId] === 0) {
2356 this.state.globalStyle.removeStyles(this.styleSheet);
2357 }
2358 };
2359
2360 GlobalStyleComponent.prototype.render = function render() {
2361 var _this2 = this;
2362
2363 if (process.env.NODE_ENV !== 'production' && React__default.Children.count(this.props.children)) {
2364 console.warn('The global style component ' + this.state.styledComponentId + ' was given child JSX. createGlobalStyle does not render children.');
2365 }
2366
2367 return React__default.createElement(
2368 StyleSheetConsumer,
2369 null,
2370 function (styleSheet) {
2371 _this2.styleSheet = styleSheet || StyleSheet.master;
2372
2373 var globalStyle = _this2.state.globalStyle;
2374
2375
2376 if (globalStyle.isStatic) {
2377 globalStyle.renderStyles(STATIC_EXECUTION_CONTEXT, _this2.styleSheet);
2378
2379 return null;
2380 } else {
2381 return React__default.createElement(
2382 ThemeConsumer,
2383 null,
2384 function (theme) {
2385 var defaultProps = _this2.constructor.defaultProps;
2386
2387
2388 var context = _extends({}, _this2.props);
2389
2390 if (typeof theme !== 'undefined') {
2391 context.theme = determineTheme(_this2.props, theme, defaultProps);
2392 }
2393
2394 globalStyle.renderStyles(context, _this2.styleSheet);
2395
2396 return null;
2397 }
2398 );
2399 }
2400 }
2401 );
2402 };
2403
2404 return GlobalStyleComponent;
2405 }(React__default.Component);
2406
2407 GlobalStyleComponent.defaultProps = {
2408 suppressMultiMountWarning: false
2409 };
2410 GlobalStyleComponent.globalStyle = style;
2411 GlobalStyleComponent.styledComponentId = id;
2412 process.env.NODE_ENV !== "production" ? GlobalStyleComponent.propTypes = {
2413 suppressMultiMountWarning: PropTypes.bool
2414 } : void 0;
2415
2416
2417 return GlobalStyleComponent;
2418}
2419
2420//
2421
2422var replaceWhitespace = function replaceWhitespace(str) {
2423 return str.replace(/\s|\\n/g, '');
2424};
2425
2426function keyframes(strings) {
2427 /* Warning if you've used keyframes on React Native */
2428 if (process.env.NODE_ENV !== 'production' && typeof navigator !== 'undefined' && navigator.product === 'ReactNative') {
2429 console.warn('`keyframes` cannot be used on ReactNative, only on the web. To do animation in ReactNative please use Animated.');
2430 }
2431
2432 for (var _len = arguments.length, interpolations = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
2433 interpolations[_key - 1] = arguments[_key];
2434 }
2435
2436 var rules = css.apply(undefined, [strings].concat(interpolations));
2437
2438 var name = generateAlphabeticName(murmurhash(replaceWhitespace(JSON.stringify(rules))));
2439
2440 return new Keyframes(name, stringifyRules(rules, name, '@keyframes'));
2441}
2442
2443//
2444
2445var withTheme = (function (Component) {
2446 var WithTheme = React__default.forwardRef(function (props, ref) {
2447 return React__default.createElement(
2448 ThemeConsumer,
2449 null,
2450 function (theme) {
2451 // $FlowFixMe
2452 var defaultProps = Component.defaultProps;
2453
2454 var themeProp = determineTheme(props, theme, defaultProps);
2455
2456 if (process.env.NODE_ENV !== 'production' && themeProp === undefined) {
2457 // eslint-disable-next-line no-console
2458 console.warn('[withTheme] You are not using a ThemeProvider nor passing a theme prop or a theme in defaultProps in component class "' + getComponentName(Component) + '"');
2459 }
2460
2461 return React__default.createElement(Component, _extends({}, props, { theme: themeProp, ref: ref }));
2462 }
2463 );
2464 });
2465
2466 hoistNonReactStatics(WithTheme, Component);
2467
2468 WithTheme.displayName = 'WithTheme(' + getComponentName(Component) + ')';
2469
2470 return WithTheme;
2471});
2472
2473//
2474
2475/* eslint-disable */
2476var __DO_NOT_USE_OR_YOU_WILL_BE_HAUNTED_BY_SPOOKY_GHOSTS = {
2477 StyleSheet: StyleSheet
2478};
2479
2480//
2481
2482/* Warning if you've imported this file on React Native */
2483if (process.env.NODE_ENV !== 'production' && typeof navigator !== 'undefined' && navigator.product === 'ReactNative') {
2484 // eslint-disable-next-line no-console
2485 console.warn("It looks like you've imported 'styled-components' on React Native.\n" + "Perhaps you're looking to import 'styled-components/native'?\n" + 'Read more about this at https://www.styled-components.com/docs/basics#react-native');
2486}
2487
2488/* Warning if there are several instances of styled-components */
2489if (process.env.NODE_ENV !== 'production' && process.env.NODE_ENV !== 'test' && typeof window !== 'undefined' && typeof navigator !== 'undefined' && typeof navigator.userAgent === 'string' && navigator.userAgent.indexOf('Node.js') === -1 && navigator.userAgent.indexOf('jsdom') === -1) {
2490 window['__styled-components-init__'] = window['__styled-components-init__'] || 0;
2491
2492 if (window['__styled-components-init__'] === 1) {
2493 // eslint-disable-next-line no-console
2494 console.warn("It looks like there are several instances of 'styled-components' initialized in this application. " + 'This may cause dynamic styles not rendering properly, errors happening during rehydration process ' + 'and makes your application bigger without a good reason.\n\n' + 'See https://s-c.sh/2BAXzed for more info.');
2495 }
2496
2497 window['__styled-components-init__'] += 1;
2498}
2499
2500//
2501
2502var styled$1 = /*#__PURE__*/Object.freeze({
2503 default: styled,
2504 css: css,
2505 keyframes: keyframes,
2506 createGlobalStyle: createGlobalStyle,
2507 isStyledComponent: isStyledComponent,
2508 ThemeConsumer: ThemeConsumer,
2509 ThemeContext: ThemeContext,
2510 ThemeProvider: ThemeProvider,
2511 withTheme: withTheme,
2512 ServerStyleSheet: ServerStyleSheet,
2513 StyleSheetManager: StyleSheetManager,
2514 __DO_NOT_USE_OR_YOU_WILL_BE_HAUNTED_BY_SPOOKY_GHOSTS: __DO_NOT_USE_OR_YOU_WILL_BE_HAUNTED_BY_SPOOKY_GHOSTS
2515});
2516
2517//
2518
2519var allowedImports = Object.keys(styled$1).filter(function (helper) {
2520 return helper !== '__esModule';
2521});
2522
2523function styledComponentsMacro(_ref) {
2524 var references = _ref.references,
2525 state = _ref.state,
2526 t = _ref.babel.types,
2527 _ref$config = _ref.config,
2528 config = _ref$config === undefined ? {} : _ref$config;
2529
2530 var program = state.file.path;
2531
2532 // replace `styled-components/macro` by `styled-components`
2533 // create a node for styled-components's imports
2534 var imports = t.importDeclaration([], t.stringLiteral('styled-components'));
2535 // and add it to top of the document
2536 program.node.body.unshift(imports);
2537
2538 // references looks like this
2539 // { default: [path, path], css: [path], ... }
2540 Object.keys(references).forEach(function (refName) {
2541 if (!allowedImports.includes(refName)) {
2542 throw new babelPluginMacros.MacroError('Invalid import: ' + refName + '. You can only import ' + allowedImports.join(', ') + ' from \'styled-components/macro\'.');
2543 }
2544
2545 // generate new identifier and add to imports
2546 var id = void 0;
2547 if (refName === 'default') {
2548 id = program.scope.generateUidIdentifier('styled');
2549 imports.specifiers.push(t.importDefaultSpecifier(id));
2550 } else {
2551 id = program.scope.generateUidIdentifier(refName);
2552 imports.specifiers.push(t.importSpecifier(id, t.identifier(refName)));
2553 }
2554
2555 // update references with the new identifiers
2556 references[refName].forEach(function (referencePath) {
2557 // eslint-disable-next-line no-param-reassign
2558 referencePath.node.name = id.name;
2559 });
2560 });
2561
2562 // apply babel-plugin-styled-components to the file
2563 var stateWithOpts = _extends({}, state, { opts: config });
2564 program.traverse(babelPlugin({ types: t }).visitor, stateWithOpts);
2565}
2566
2567var configName = 'styledComponents';
2568
2569var index = babelPluginMacros.createMacro(styledComponentsMacro, { configName: configName });
2570
2571exports.default = index;
2572//# sourceMappingURL=styled-components-macro.cjs.js.map
2573
\No newline at end of file