UNPKG

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