UNPKG

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