UNPKG

react-matches

Version:

Simple helper components to make responsive design easier in React.

158 lines (120 loc) 6.81 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.Container = undefined; var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; 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 _react2 = _interopRequireDefault(_react); var _propTypes = require('prop-types'); var _propTypes2 = _interopRequireDefault(_propTypes); var _resizeObserverPolyfill = require('resize-observer-polyfill'); var _resizeObserverPolyfill2 = _interopRequireDefault(_resizeObserverPolyfill); var _resolveProps = require('./resolve-props'); var _resolveProps2 = _interopRequireDefault(_resolveProps); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } 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; } function matchQueries(queries, _ref) { var width = _ref.width, height = _ref.height; var matchedQueries = {}; Object.keys(queries).forEach(function (key) { var _queries$key = queries[key], _queries$key$minWidth = _queries$key.minWidth, minWidth = _queries$key$minWidth === undefined ? 0 : _queries$key$minWidth, _queries$key$maxWidth = _queries$key.maxWidth, maxWidth = _queries$key$maxWidth === undefined ? Infinity : _queries$key$maxWidth, _queries$key$minHeigh = _queries$key.minHeight, minHeight = _queries$key$minHeigh === undefined ? 0 : _queries$key$minHeigh, _queries$key$maxHeigh = _queries$key.maxHeight, maxHeight = _queries$key$maxHeigh === undefined ? Infinity : _queries$key$maxHeigh; matchedQueries[key] = minWidth <= width && width <= maxWidth && minHeight <= height && height <= maxHeight; }); return matchedQueries; } var Container = exports.Container = function (_Component) { _inherits(Container, _Component); function Container() { var _ref2; var _temp, _this, _ret; _classCallCheck(this, Container); for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref2 = Container.__proto__ || Object.getPrototypeOf(Container)).call.apply(_ref2, [this].concat(args))), _this), _this.state = { matches: {} }, _this._updateMatches = function (entries) { var rect = entries[0].contentRect; var matches = matchQueries(_this.props.queries, rect); if (_this._isMounted && JSON.stringify(matches) !== JSON.stringify(_this.state.matches)) { _this.setState({ matches: matches }); _this.props.onUpdate(rect); } }, _this._setNode = function (component) { _this._node = component; }, _this._resolve = function (propQueries) { return (0, _resolveProps2.default)(_this.state.matches, propQueries); }, _temp), _possibleConstructorReturn(_this, _ret); } _createClass(Container, [{ key: 'componentDidMount', value: function componentDidMount() { if ((typeof window === 'undefined' ? 'undefined' : _typeof(window)) !== 'object') return; if (this._node) { this._resizeObserver = new _resizeObserverPolyfill2.default(this._updateMatches); this._resizeObserver.observe(this._node); } else { console.error('No ref found, attach the `containerRef` prop passed back in the child function to the component you want to measure.'); } this._isMounted = true; } }, { key: 'componentWillUnmount', value: function componentWillUnmount() { if (this.resizeObserver && this._node) { this.resizeObserver.disconnect(this._node); } this._isMounted = false; } }, { key: 'render', value: function render() { var _props = this.props, tag = _props.tag, queries = _props.queries, children = _props.children, onUpdate = _props.onUpdate, props = _objectWithoutProperties(_props, ['tag', 'queries', 'children', 'onUpdate']); if (tag) { return (0, _react.createElement)(tag, _extends({ ref: this._setNode }, props), children({ matches: this.state.matches, resolve: this._resolve })); } return children({ containerRef: this._setNode, matches: this.state.matches, resolve: this._resolve }); } }]); return Container; }(_react.Component); Container.propTypes = { tag: _propTypes2.default.oneOfType([_propTypes2.default.string, _propTypes2.default.bool]), queries: _propTypes2.default.object, onUpdate: _propTypes2.default.func }; Container.defaultProps = { tag: 'div', queries: {}, onUpdate: function onUpdate() { return null; } }; exports.default = Container;