Skip to content
This repository has been archived by the owner on Aug 19, 2022. It is now read-only.

Commit

Permalink
Use exenv to bail on browser-specifics
Browse files Browse the repository at this point in the history
While rendering on the server, we cannot access browser specific
features. Use `exenv` to check if the browser environment is available.
  • Loading branch information
ianobermiller committed May 14, 2015
1 parent f28d28e commit e6a4cf4
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
5 changes: 3 additions & 2 deletions modules/keyframes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

var prefix = require('./prefix');

var ExecutionEnvironment = require('exenv');
var kebabCase = require('lodash/string/kebabCase');

var msPrefix = /^ms-/;
var animationIndex = 1;
var animationStyleSheet = null;
var keyframesPrefixed = null;

if (document) {
if (ExecutionEnvironment.canUseDOM) {
animationStyleSheet = document.createElement('style');
document.head.appendChild(animationStyleSheet);

Expand All @@ -34,7 +35,7 @@ var keyframes = function (keyframeRules) {
var name = 'Animation' + animationIndex;
animationIndex += 1;

if (!document) {
if (!ExecutionEnvironment.canUseDOM) {
return name;
}

Expand Down
21 changes: 15 additions & 6 deletions modules/prefix.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

'use strict';

var ExecutionEnvironment = require('exenv');
var kebabCase = require('lodash/string/kebabCase');

var jsCssMap = {
Expand All @@ -16,17 +17,21 @@ var jsCssMap = {
};
var testProp = 'Transform';

var domStyle = document.createElement('p').style;
var domStyle;
var prefixedPropertyCache = {};
var prefixedValueCache = {};
var jsVendorPrefix = '';
var cssVendorPrefix = '';

for (var js in jsCssMap) {
if ((js + testProp) in domStyle) {
jsVendorPrefix = js;
cssVendorPrefix = jsCssMap[js];
break;
if (ExecutionEnvironment.canUseDOM) {
domStyle = document.createElement('p').style;

for (var js in jsCssMap) {
if ((js + testProp) in domStyle) {
jsVendorPrefix = js;
cssVendorPrefix = jsCssMap[js];
break;
}
}
}

Expand Down Expand Up @@ -104,6 +109,10 @@ var _getPrefixedValue = function (property, value) {
// and values.
/*eslint-disable no-console */
var prefix = function (style, mode /* 'css' or 'js' */) {
if (!ExecutionEnvironment.canUseDOM) {
return style;
}

mode = mode || 'js';
var newStyle = {};
Object.keys(style).forEach(function (property) {
Expand Down
11 changes: 10 additions & 1 deletion modules/resolve-styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var MouseUpListener = require('./mouse-up-listener');
var getState = require('./get-state');
var prefix = require('./prefix');

var ExecutionEnvironment = require('exenv');
var React = require('react/addons');
var clone = require('lodash/lang/clone');
var isArray = require('lodash/lang/isArray');
Expand Down Expand Up @@ -56,6 +57,10 @@ var _onMediaQueryChange = function (component, query, mediaQueryList) {
};

var _resolveMediaQueryStyles = function (component, style) {
if (!ExecutionEnvironment.canUseDOM) {
return style;
}

Object.keys(style)
.filter(function (name) { return name[0] === '@'; })
.map(function (query) {
Expand Down Expand Up @@ -235,7 +240,11 @@ var resolveStyles = function (component, renderedElement, existingKeyMap) {
}
});

if (style[':active'] && !component._radiumMouseUpListener) {
if (
style[':active'] &&
!component._radiumMouseUpListener &&
ExecutionEnvironment.canUseEventListeners
) {
component._radiumMouseUpListener = MouseUpListener.subscribe(
_mouseUp.bind(null, component)
);
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"react": ">=0.12.0 <0.14.0"
},
"dependencies": {
"exenv": "^1.1.0",
"lodash": "^3.2.0"
},
"devDependencies": {
Expand All @@ -50,6 +51,7 @@
"modules/wrap.js": true
},
"unmockedModulePathPatterns": [
"exenv",
"lodash",
"react"
]
Expand Down

0 comments on commit e6a4cf4

Please sign in to comment.