UNPKG

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