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