You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

145 rivejä
4.6 KiB

  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.LARGE = exports.MEDIUM = exports.SMALL = undefined;
  6. var _extends2 = require('babel-runtime/helpers/extends');
  7. var _extends3 = _interopRequireDefault(_extends2);
  8. var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');
  9. var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);
  10. var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
  11. var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
  12. var _createClass2 = require('babel-runtime/helpers/createClass');
  13. var _createClass3 = _interopRequireDefault(_createClass2);
  14. var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
  15. var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
  16. var _inherits2 = require('babel-runtime/helpers/inherits');
  17. var _inherits3 = _interopRequireDefault(_inherits2);
  18. exports.default = withWidth;
  19. var _react = require('react');
  20. var _react2 = _interopRequireDefault(_react);
  21. var _reactEventListener = require('react-event-listener');
  22. var _reactEventListener2 = _interopRequireDefault(_reactEventListener);
  23. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  24. var SMALL = exports.SMALL = 1;
  25. var MEDIUM = exports.MEDIUM = 2;
  26. var LARGE = exports.LARGE = 3;
  27. function withWidth() {
  28. var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  29. var _options$largeWidth = options.largeWidth,
  30. largeWidth = _options$largeWidth === undefined ? 992 : _options$largeWidth,
  31. _options$mediumWidth = options.mediumWidth,
  32. mediumWidth = _options$mediumWidth === undefined ? 768 : _options$mediumWidth,
  33. _options$resizeInterv = options.resizeInterval,
  34. resizeInterval = _options$resizeInterv === undefined ? 166 : _options$resizeInterv;
  35. return function (MyComponent) {
  36. return function (_Component) {
  37. (0, _inherits3.default)(WithWidth, _Component);
  38. function WithWidth() {
  39. var _ref;
  40. var _temp, _this, _ret;
  41. (0, _classCallCheck3.default)(this, WithWidth);
  42. for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
  43. args[_key] = arguments[_key];
  44. }
  45. return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, (_ref = WithWidth.__proto__ || (0, _getPrototypeOf2.default)(WithWidth)).call.apply(_ref, [this].concat(args))), _this), _this.state = {
  46. width: null
  47. }, _this.handleResize = function () {
  48. clearTimeout(_this.deferTimer);
  49. _this.deferTimer = setTimeout(function () {
  50. _this.updateWidth();
  51. }, resizeInterval);
  52. }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
  53. }
  54. (0, _createClass3.default)(WithWidth, [{
  55. key: 'componentDidMount',
  56. value: function componentDidMount() {
  57. this.updateWidth();
  58. }
  59. }, {
  60. key: 'componentWillUnmount',
  61. value: function componentWillUnmount() {
  62. clearTimeout(this.deferTimer);
  63. }
  64. }, {
  65. key: 'updateWidth',
  66. value: function updateWidth() {
  67. var innerWidth = window.innerWidth;
  68. var width = void 0;
  69. if (innerWidth >= largeWidth) {
  70. width = LARGE;
  71. } else if (innerWidth >= mediumWidth) {
  72. width = MEDIUM;
  73. } else {
  74. // innerWidth < 768
  75. width = SMALL;
  76. }
  77. if (width !== this.state.width) {
  78. this.setState({
  79. width: width
  80. });
  81. }
  82. }
  83. }, {
  84. key: 'render',
  85. value: function render() {
  86. var width = this.state.width;
  87. /**
  88. * When rendering the component on the server,
  89. * we have no idea about the screen width.
  90. * In order to prevent blinks and help the reconciliation
  91. * we are not rendering the component.
  92. *
  93. * A better alternative would be to send client hints.
  94. * But the browser support of this API is low:
  95. * http://caniuse.com/#search=client%20hint
  96. */
  97. if (width === null) {
  98. return null;
  99. }
  100. return _react2.default.createElement(
  101. _reactEventListener2.default,
  102. { target: 'window', onResize: this.handleResize },
  103. _react2.default.createElement(MyComponent, (0, _extends3.default)({
  104. width: width
  105. }, this.props))
  106. );
  107. }
  108. }]);
  109. return WithWidth;
  110. }(_react.Component);
  111. };
  112. }