react-delayload
Advanced tools
Comparing version 0.1.6 to 0.2.0
@@ -8,4 +8,14 @@ 'use strict'; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; }; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
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 _propTypes = require('prop-types'); | ||
var _propTypes2 = _interopRequireDefault(_propTypes); | ||
var _reactDom = require('react-dom'); | ||
var _throttle = require('lodash/function/throttle'); | ||
@@ -15,77 +25,102 @@ | ||
var _react = require('react'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var _react2 = _interopRequireDefault(_react); | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } | ||
var throttle_ms = 100; | ||
var DelayLoad = _react2.default.createClass({ | ||
displayName: 'DelayLoad', | ||
var isBrowser = typeof window !== "undefined" && typeof document !== "undefined"; | ||
propTypes: { | ||
enableDelay: _react2.default.PropTypes.bool, | ||
height: _react2.default.PropTypes.number, | ||
threshold: _react2.default.PropTypes.number | ||
}, | ||
getDefaultProps: function getDefaultProps() { | ||
return { | ||
enableDelay: true, | ||
height: 0, | ||
threshold: 0 | ||
var DelayLoad = function (_Component) { | ||
_inherits(DelayLoad, _Component); | ||
function DelayLoad(props, context) { | ||
_classCallCheck(this, DelayLoad); | ||
var _this = _possibleConstructorReturn(this, (DelayLoad.__proto__ || Object.getPrototypeOf(DelayLoad)).call(this, props, context)); | ||
_this.state = { | ||
visible: !_this.props.enableDelay | ||
}; | ||
}, | ||
getInitialState: function getInitialState() { | ||
return { | ||
visible: !this.props.enableDelay | ||
}; | ||
}, | ||
componentDidMount: function componentDidMount() { | ||
if (!this.state.visible) this.checkVisible(); | ||
if (this.props.enableDelay && window.addEventListener) { | ||
window.addEventListener('scroll', this.checkVisible); | ||
window.addEventListener('resize', this.checkVisible); | ||
} | ||
}, | ||
componentWillUnmount: function componentWillUnmount() { | ||
this.onLoaded(); | ||
}, | ||
componentDidUpdate: function componentDidUpdate() { | ||
if (!this.state.visible) this.checkVisible(); | ||
}, | ||
var func = (0, _throttle2.default)(function () { | ||
if (inviewport((0, _reactDom.findDOMNode)(this), this.props.threshold)) { | ||
this.setState({ visible: true }); | ||
this.onLoaded(); | ||
} | ||
}, throttle_ms); | ||
onLoaded: function onLoaded() { | ||
if (this.props.enableDelay && window.removeEventListener) { | ||
window.removeEventListener('scroll', this.checkVisible); | ||
window.removeEventListener('resize', this.checkVisible); | ||
_this.checkVisible = func.bind(_this); | ||
return _this; | ||
} | ||
_createClass(DelayLoad, [{ | ||
key: 'componentDidMount', | ||
value: function componentDidMount() { | ||
if (!isBrowser) return; | ||
if (!this.state.visible) this.checkVisible(); | ||
if (this.props.enableDelay && window.addEventListener) { | ||
window.addEventListener('scroll', this.checkVisible); | ||
window.addEventListener('resize', this.checkVisible); | ||
} | ||
} | ||
}, | ||
checkVisible: (0, _throttle2.default)(function () { | ||
if (inviewport(this.getDOMNode(), this.props.threshold)) { | ||
this.setState({ visible: true }); | ||
}, { | ||
key: 'componentWillUnmount', | ||
value: function componentWillUnmount() { | ||
this.onLoaded(); | ||
} | ||
}, throttle_ms), | ||
}, { | ||
key: 'componentDidUpdate', | ||
value: function componentDidUpdate() { | ||
if (isBrowser && !this.state.visible) this.checkVisible(); | ||
} | ||
}, { | ||
key: 'onLoaded', | ||
value: function onLoaded() { | ||
if (isBrowser && this.props.enableDelay && window.removeEventListener) { | ||
window.removeEventListener('scroll', this.checkVisible); | ||
window.removeEventListener('resize', this.checkVisible); | ||
} | ||
} | ||
}, { | ||
key: 'render', | ||
value: function render() { | ||
var style = {}; | ||
render: function render() { | ||
var style = {}; | ||
if (this.props.height > 0 && !this.state.visible) { | ||
style.height = this.props.height; | ||
} | ||
if (this.props.style) { | ||
style = extend(style, this.props.style); | ||
} | ||
if (this.props.height > 0 && !this.state.visible) { | ||
style.height = this.props.height; | ||
var cssClass = this.props.className || ''; | ||
return React.createElement( | ||
'div', | ||
{ style: style, className: cssClass }, | ||
this.state.visible ? this.props.children : '' | ||
); | ||
} | ||
if (this.props.style) { | ||
style = extend(style, this.props.style); | ||
} | ||
}]); | ||
var cssClass = this.props.className || ''; | ||
return DelayLoad; | ||
}(_react.Component); | ||
return _react2.default.createElement( | ||
'div', | ||
{ style: style, className: cssClass }, | ||
this.state.visible ? this.props.children : '' | ||
); | ||
} | ||
}); | ||
DelayLoad.propTypes = { | ||
enableDelay: _propTypes2.default.bool, | ||
height: _propTypes2.default.number, | ||
threshold: _propTypes2.default.number | ||
}; | ||
DelayLoad.defaultProps = { | ||
enableDelay: true, | ||
height: 0, | ||
threshold: 0 | ||
}; | ||
@@ -165,8 +200,10 @@ exports.default = DelayLoad; | ||
var setThrottleMilliseconds = exports.setThrottleMilliseconds = function setThrottleMilliseconds(ms) { | ||
if (ms > 30) throttle_ms = ms; | ||
if (typeof ms === 'number' && ms > 30) throttle_ms = ms; | ||
}; | ||
function inviewport(el, threshold) { | ||
if (typeof threshold !== 'number') threshold = 0; | ||
if (!isBrowser) return false; | ||
if (typeof threshold !== 'number' || isNaN(threshold)) threshold = 0; | ||
var winHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight; | ||
@@ -173,0 +210,0 @@ var winWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth; |
@@ -16,3 +16,3 @@ { | ||
"author": "Nickel Jianhui Zhu <nickeljew@hotmail.com>", | ||
"version": "0.1.6", | ||
"version": "0.2.0", | ||
"license": "MIT", | ||
@@ -41,19 +41,21 @@ "private": false, | ||
"peerDependencies": { | ||
"react": "^15.1.0" | ||
"prop-types": "^15.5.8", | ||
"react": "^15.5.4", | ||
"react-dom": "^15.5.4" | ||
}, | ||
"devDependencies": { | ||
"babel-core": "^6.9.0", | ||
"babel-loader": "^6.2.4", | ||
"babel-preset-es2015": "^6.9.0", | ||
"babel-preset-react": "^6.5.0", | ||
"express": "^4.13.4", | ||
"grunt": "^0.4.5", | ||
"babel-core": "^6.24.1", | ||
"babel-loader": "^7.0.0", | ||
"babel-preset-es2015": "^6.24.1", | ||
"babel-preset-react": "^6.24.1", | ||
"es6-docready": "^1.0.0", | ||
"grunt": "^1.0.1", | ||
"grunt-babel": "^6.0.0", | ||
"grunt-contrib-clean": "^0.6.0", | ||
"grunt-contrib-concat": "^0.5.1", | ||
"grunt-contrib-uglify": "^0.9.2", | ||
"grunt-contrib-watch": "^0.6.1", | ||
"grunt-webpack": "^1.0.11", | ||
"swig": "^1.4.2", | ||
"uglify-loader": "^1.3.0" | ||
"grunt-contrib-clean": "^1.1.0", | ||
"grunt-contrib-concat": "^1.0.1", | ||
"grunt-contrib-uglify": "^2.3.0", | ||
"grunt-contrib-watch": "^1.0.0", | ||
"grunt-webpack": "^2.0.1", | ||
"webpack": "^2.4.1", | ||
"webpack-dev-server": "^2.4.4" | ||
}, | ||
@@ -60,0 +62,0 @@ "repository": { |
@@ -11,4 +11,7 @@ # React-DelayLoad | ||
npm install react-delayload --save-dev | ||
yarn add react-picker | ||
or | ||
npm install react-picker --save | ||
## Example | ||
@@ -15,0 +18,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
1
88
0
170430
4
16
193