UNPKG

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