forked from gaearon/react-document-title
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathindex.js
48 lines (39 loc) · 1.19 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
'use strict';
var React = require('react');
var withSideEffect = require('react-side-effect');
var PropTypes = require('prop-types');
function splitClassName(className) {
return className.split(/\s+/);
}
function reducePropsToState(propsList) {
return propsList.map(function(props) {
return props.className;
}).filter(function (value, index, self) {
return self.indexOf(value) === index;
}).join(' ');
}
function handleStateChangeOnClient(stringClassNames) {
var currentClassNames = splitClassName(document.body.className).filter(
function (className) {
return BodyClassName.cache.indexOf(className) === -1;
});
var newClassNames = splitClassName(stringClassNames);
BodyClassName.cache = newClassNames;
document.body.className = currentClassNames.concat(newClassNames).join(' ').trim();
}
function BodyClassName(props){
if (props.children) {
return React.Children.only(props.children);
} else {
return null;
}
}
BodyClassName.displayName = 'BodyClassName';
BodyClassName.cache = [];
BodyClassName.propTypes = {
className: PropTypes.string.isRequired
};
module.exports = withSideEffect(
reducePropsToState,
handleStateChangeOnClient
)(BodyClassName);