react-ionicons
Version:
A React SVG ionicon component
196 lines (142 loc) • 6.93 kB
JavaScript
;
exports.__esModule = true;
exports.clones = exports.CONTEXT_KEY = exports.LOCAL_ATTR = exports.SC_ATTR = undefined;
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _BrowserStyleSheet = require('./BrowserStyleSheet');
var _BrowserStyleSheet2 = _interopRequireDefault(_BrowserStyleSheet);
var _ServerStyleSheet = require('./ServerStyleSheet');
var _ServerStyleSheet2 = _interopRequireDefault(_ServerStyleSheet);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var SC_ATTR = exports.SC_ATTR = 'data-styled-components';
var LOCAL_ATTR = exports.LOCAL_ATTR = 'data-styled-components-is-local';
var CONTEXT_KEY = exports.CONTEXT_KEY = '__styled-components-stylesheet__';
var instance = null;
// eslint-disable-next-line no-use-before-define
var clones = exports.clones = [];
var StyleSheet = function () {
function StyleSheet(tagConstructor) {
var tags = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
var names = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
_classCallCheck(this, StyleSheet);
this.hashes = {};
this.deferredInjections = {};
this.stylesCacheable = typeof document !== 'undefined';
this.tagConstructor = tagConstructor;
this.tags = tags;
this.names = names;
this.constructComponentTagMap();
}
// helper for `ComponentStyle` to know when it cache static styles.
// staticly styled-component can not safely cache styles on the server
// without all `ComponentStyle` instances saving a reference to the
// the styleSheet instance they last rendered with,
// or listening to creation / reset events. otherwise you might create
// a component with one stylesheet and render it another api response
// with another, losing styles on from your server-side render.
StyleSheet.prototype.constructComponentTagMap = function constructComponentTagMap() {
var _this = this;
this.componentTags = {};
this.tags.forEach(function (tag) {
Object.keys(tag.components).forEach(function (componentId) {
_this.componentTags[componentId] = tag;
});
});
};
/* Best level of caching—get the name from the hash straight away. */
StyleSheet.prototype.getName = function getName(hash) {
return this.hashes[hash.toString()];
};
/* Second level of caching—if the name is already in the dom, don't
* inject anything and record the hash for getName next time. */
StyleSheet.prototype.alreadyInjected = function alreadyInjected(hash, name) {
if (!this.names[name]) return false;
this.hashes[hash.toString()] = name;
return true;
};
/* Third type of caching—don't inject components' componentId twice. */
StyleSheet.prototype.hasInjectedComponent = function hasInjectedComponent(componentId) {
return !!this.componentTags[componentId];
};
StyleSheet.prototype.deferredInject = function deferredInject(componentId, isLocal, css) {
if (this === instance) {
clones.forEach(function (clone) {
clone.deferredInject(componentId, isLocal, css);
});
}
this.getOrCreateTag(componentId, isLocal);
this.deferredInjections[componentId] = css;
};
StyleSheet.prototype.inject = function inject(componentId, isLocal, css, hash, name) {
if (this === instance) {
clones.forEach(function (clone) {
clone.inject(componentId, isLocal, css);
});
}
var tag = this.getOrCreateTag(componentId, isLocal);
var deferredInjection = this.deferredInjections[componentId];
if (deferredInjection) {
tag.inject(componentId, deferredInjection);
delete this.deferredInjections[componentId];
}
tag.inject(componentId, css, name);
if (hash && name) {
this.hashes[hash.toString()] = name;
}
};
StyleSheet.prototype.toHTML = function toHTML() {
return this.tags.map(function (tag) {
return tag.toHTML();
}).join('');
};
StyleSheet.prototype.toReactElements = function toReactElements() {
return this.tags.map(function (tag, i) {
return tag.toReactElement('sc-' + i);
});
};
StyleSheet.prototype.getOrCreateTag = function getOrCreateTag(componentId, isLocal) {
var existingTag = this.componentTags[componentId];
if (existingTag) {
return existingTag;
}
var lastTag = this.tags[this.tags.length - 1];
var componentTag = !lastTag || lastTag.isFull() || lastTag.isLocal !== isLocal ? this.createNewTag(isLocal) : lastTag;
this.componentTags[componentId] = componentTag;
componentTag.addComponent(componentId);
return componentTag;
};
StyleSheet.prototype.createNewTag = function createNewTag(isLocal) {
var newTag = this.tagConstructor(isLocal);
this.tags.push(newTag);
return newTag;
};
StyleSheet.reset = function reset(isServer) {
instance = StyleSheet.create(isServer);
};
/* We can make isServer totally implicit once Jest 20 drops and we
* can change environment on a per-test basis. */
StyleSheet.create = function create() {
var isServer = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : typeof document === 'undefined';
return (isServer ? _ServerStyleSheet2.default : _BrowserStyleSheet2.default).create();
};
StyleSheet.clone = function clone(oldSheet) {
var newSheet = new StyleSheet(oldSheet.tagConstructor, oldSheet.tags.map(function (tag) {
return tag.clone();
}), _extends({}, oldSheet.names));
newSheet.hashes = _extends({}, oldSheet.hashes);
newSheet.deferredInjections = _extends({}, oldSheet.deferredInjections);
clones.push(newSheet);
return newSheet;
};
_createClass(StyleSheet, null, [{
key: 'instance',
get: function get() {
return instance || (instance = StyleSheet.create());
}
}]);
return StyleSheet;
}();
exports.default = StyleSheet;