react-style-tag
Advanced tools
Comparing version 1.1.0 to 1.2.0
@@ -16,2 +16,4 @@ 'use strict'; | ||
var _is = require('./is'); | ||
var _transform = require('./transform'); | ||
@@ -62,9 +64,14 @@ | ||
/** | ||
* determine if object is a string | ||
* | ||
* @param {*} object | ||
* return the propsValue if it exists, else return the defaultValue | ||
* | ||
* @param {boolean} propsValue | ||
* @param {boolean} defaultValue | ||
* @returns {boolean} | ||
*/ | ||
var isString = function isString(object) { | ||
return Object.prototype.toString.call(object) === '[object String]'; | ||
var getCoalescedPropsValue = function getCoalescedPropsValue(propsValue, defaultValue) { | ||
if ((0, _is.isUndefined)(propsValue)) { | ||
return defaultValue; | ||
} | ||
return propsValue; | ||
}; | ||
@@ -78,3 +85,3 @@ | ||
var throwErrorIfIsNotText = function throwErrorIfIsNotText(children) { | ||
if (!isString(children)) { | ||
if (!(0, _is.isString)(children)) { | ||
throw new Error(ONLY_TEXT_ERROR); | ||
@@ -84,2 +91,8 @@ } | ||
var REACT_STYLE_TAG_GLOBAL_PROPERTIES = { | ||
doNotPrefix: false, | ||
hasSourceMap: false, | ||
isMinified: false | ||
}; | ||
var Style = function (_Component) { | ||
@@ -105,5 +118,8 @@ _inherits(Style, _Component); | ||
var hasSourceMap = _this.props.hasSourceMap; | ||
var hasSourceMapGlobal = REACT_STYLE_TAG_GLOBAL_PROPERTIES.hasSourceMap; | ||
if (hasSourceMap) { | ||
var hasSourceMapFinal = getCoalescedPropsValue(hasSourceMap, hasSourceMapGlobal); | ||
if (hasSourceMapFinal) { | ||
_this.setLinkTag(); | ||
@@ -125,4 +141,10 @@ } else { | ||
if (HAS_BLOB_SUPPORT) { | ||
var doNotPrefixGlobal = REACT_STYLE_TAG_GLOBAL_PROPERTIES.doNotPrefix; | ||
var isMinifiedGlobal = REACT_STYLE_TAG_GLOBAL_PROPERTIES.isMinified; | ||
var doNotPrefixFinal = getCoalescedPropsValue(doNotPrefix, doNotPrefixGlobal); | ||
var isMinifiedFinal = getCoalescedPropsValue(isMinified, isMinifiedGlobal); | ||
var link = _this.refs.link; | ||
var transformedCss = getTransformedCss(children, doNotPrefix, isMinified); | ||
var transformedCss = getTransformedCss(children, doNotPrefixFinal, isMinifiedFinal); | ||
var blob = new window.Blob([transformedCss], { | ||
@@ -143,2 +165,4 @@ type: 'text/css' | ||
var isMinified = _this$props2.isMinified; | ||
var doNotPrefixGlobal = REACT_STYLE_TAG_GLOBAL_PROPERTIES.doNotPrefix; | ||
var isMinifiedGlobal = REACT_STYLE_TAG_GLOBAL_PROPERTIES.isMinified; | ||
@@ -150,5 +174,7 @@ | ||
var doNotPrefixFinal = getCoalescedPropsValue(doNotPrefix, doNotPrefixGlobal); | ||
var isMinifiedFinal = getCoalescedPropsValue(isMinified, isMinifiedGlobal); | ||
var style = _this.refs.style; | ||
style.textContent = getTransformedCss(children, doNotPrefix, isMinified); | ||
style.textContent = getTransformedCss(children, doNotPrefixFinal, isMinifiedFinal); | ||
@@ -181,2 +207,24 @@ document.head.appendChild(style); | ||
/** | ||
* set the global options for all instances of Style | ||
* | ||
* @param {object} options | ||
* @param {boolean} [options.doNotPrefix] | ||
* @param {boolean} [options.hasSourceMap] | ||
* @param {boolean} [options.isMinified] | ||
*/ | ||
Style.setGlobalOptions = function setGlobalOptions(options) { | ||
Object.keys(options).forEach(function (option) { | ||
if (REACT_STYLE_TAG_GLOBAL_PROPERTIES.hasOwnProperty(option)) { | ||
if (!(0, _is.isBoolean)(options[option])) { | ||
throw new Error(option + ' must be a boolean value.'); | ||
} | ||
REACT_STYLE_TAG_GLOBAL_PROPERTIES[option] = options[option]; | ||
} | ||
}); | ||
}; | ||
/** | ||
* remove the tagType from the document head if it exists | ||
@@ -212,3 +260,8 @@ * | ||
if (hasSourceMap && HAS_BLOB_SUPPORT) { | ||
var hasSourceMapGlobal = REACT_STYLE_TAG_GLOBAL_PROPERTIES.hasSourceMap; | ||
var hasSourceMapFinal = getCoalescedPropsValue(hasSourceMap, hasSourceMapGlobal); | ||
if (hasSourceMapFinal && HAS_BLOB_SUPPORT) { | ||
return _react2.default.createElement('link', _extends({ | ||
@@ -238,8 +291,3 @@ rel: 'stylesheet', | ||
}; | ||
Style.defaultProps = { | ||
doNotPrefix: false, | ||
hasSourceMap: false, | ||
isMinified: false | ||
}; | ||
exports.hashKeys = _hash2.default; | ||
exports.default = Style; |
@@ -57,3 +57,3 @@ { | ||
}, | ||
"version": "1.1.0" | ||
"version": "1.2.0" | ||
} |
@@ -10,4 +10,5 @@ # react-style-tag | ||
* [Summary](#summary) | ||
* [Scoped styles](#scoped-styles) | ||
* [Scoped Styles](#scoped-styles) | ||
* [Props](#props) | ||
* [Global Options](#global-options) | ||
* [Development](#development) | ||
@@ -66,3 +67,3 @@ * [Todo](#todo) | ||
#### Scoped styles | ||
#### Scoped Styles | ||
@@ -159,2 +160,33 @@ There is an additional utility provided that can help to scope your styles in the vein of [CSS Modules](https://github.com/css-modules/css-modules), and this is `hashKeys`. This function accepts an array of keys to hash, and returns a map of the keys to their hashed values. | ||
### Global Options | ||
All of the props available are also available as global options for all instances that can be set with the `setGlobalOptions` method: | ||
```javascript | ||
import Style from 'react-style-tag'; | ||
Style.setGlobalOptions({ | ||
doNotPrefix: true, | ||
hasSourceMap: true, | ||
isMinified: true | ||
}); | ||
``` | ||
All default values are the same as those available for props. A common use case would be something like: | ||
```javascript | ||
const IS_PRODUCTION = process.env.NODE_ENV === 'production'; | ||
Style.setGlobalOptions({ | ||
hasSourceMap: !IS_PRODUCTION, | ||
isMinified: IS_PRODUCTION | ||
}); | ||
<Style> | ||
.test { | ||
display: block; | ||
} | ||
</Style> | ||
``` | ||
### Development | ||
@@ -161,0 +193,0 @@ |
20027
7
309
200