|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- 'use strict';
-
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.LARGE = exports.MEDIUM = exports.SMALL = undefined;
-
- var _extends2 = require('babel-runtime/helpers/extends');
-
- var _extends3 = _interopRequireDefault(_extends2);
-
- var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');
-
- var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);
-
- var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
-
- var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
-
- var _createClass2 = require('babel-runtime/helpers/createClass');
-
- var _createClass3 = _interopRequireDefault(_createClass2);
-
- var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
-
- var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
-
- var _inherits2 = require('babel-runtime/helpers/inherits');
-
- var _inherits3 = _interopRequireDefault(_inherits2);
-
- exports.default = withWidth;
-
- var _react = require('react');
-
- var _react2 = _interopRequireDefault(_react);
-
- var _reactEventListener = require('react-event-listener');
-
- var _reactEventListener2 = _interopRequireDefault(_reactEventListener);
-
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
-
- var SMALL = exports.SMALL = 1;
- var MEDIUM = exports.MEDIUM = 2;
- var LARGE = exports.LARGE = 3;
-
- function withWidth() {
- var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
- var _options$largeWidth = options.largeWidth,
- largeWidth = _options$largeWidth === undefined ? 992 : _options$largeWidth,
- _options$mediumWidth = options.mediumWidth,
- mediumWidth = _options$mediumWidth === undefined ? 768 : _options$mediumWidth,
- _options$resizeInterv = options.resizeInterval,
- resizeInterval = _options$resizeInterv === undefined ? 166 : _options$resizeInterv;
-
-
- return function (MyComponent) {
- return function (_Component) {
- (0, _inherits3.default)(WithWidth, _Component);
-
- function WithWidth() {
- var _ref;
-
- var _temp, _this, _ret;
-
- (0, _classCallCheck3.default)(this, WithWidth);
-
- for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
- args[_key] = arguments[_key];
- }
-
- return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, (_ref = WithWidth.__proto__ || (0, _getPrototypeOf2.default)(WithWidth)).call.apply(_ref, [this].concat(args))), _this), _this.state = {
- width: null
- }, _this.handleResize = function () {
- clearTimeout(_this.deferTimer);
- _this.deferTimer = setTimeout(function () {
- _this.updateWidth();
- }, resizeInterval);
- }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
- }
-
- (0, _createClass3.default)(WithWidth, [{
- key: 'componentDidMount',
- value: function componentDidMount() {
- this.updateWidth();
- }
- }, {
- key: 'componentWillUnmount',
- value: function componentWillUnmount() {
- clearTimeout(this.deferTimer);
- }
- }, {
- key: 'updateWidth',
- value: function updateWidth() {
- var innerWidth = window.innerWidth;
- var width = void 0;
-
- if (innerWidth >= largeWidth) {
- width = LARGE;
- } else if (innerWidth >= mediumWidth) {
- width = MEDIUM;
- } else {
- // innerWidth < 768
- width = SMALL;
- }
-
- if (width !== this.state.width) {
- this.setState({
- width: width
- });
- }
- }
- }, {
- key: 'render',
- value: function render() {
- var width = this.state.width;
-
- /**
- * When rendering the component on the server,
- * we have no idea about the screen width.
- * In order to prevent blinks and help the reconciliation
- * we are not rendering the component.
- *
- * A better alternative would be to send client hints.
- * But the browser support of this API is low:
- * http://caniuse.com/#search=client%20hint
- */
- if (width === null) {
- return null;
- }
-
- return _react2.default.createElement(
- _reactEventListener2.default,
- { target: 'window', onResize: this.handleResize },
- _react2.default.createElement(MyComponent, (0, _extends3.default)({
- width: width
- }, this.props))
- );
- }
- }]);
- return WithWidth;
- }(_react.Component);
- };
- }
|