UNPKG

react-media

Version:
72 lines (50 loc) 3.11 kB
'use strict'; exports.__esModule = true; 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 _react = require('react'); var _react2 = _interopRequireDefault(_react); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 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; } var Media = function (_React$Component) { _inherits(Media, _React$Component); function Media() { var _temp, _this, _ret; _classCallCheck(this, Media); for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return _ret = (_temp = (_this = _possibleConstructorReturn(this, _React$Component.call.apply(_React$Component, [this].concat(args))), _this), _this.state = { matches: true }, _this.updateMatches = function () { return _this.setState({ matches: _this.mediaQueryList.matches }); }, _temp), _possibleConstructorReturn(_this, _ret); } Media.prototype.componentWillMount = function componentWillMount() { if ((typeof window === 'undefined' ? 'undefined' : _typeof(window)) === 'object') { this.mediaQueryList = window.matchMedia(this.props.query); this.mediaQueryList.addListener(this.updateMatches); this.updateMatches(); } }; Media.prototype.componentWillUnmount = function componentWillUnmount() { this.mediaQueryList.removeListener(this.updateMatches); }; Media.prototype.render = function render() { var _props = this.props; var children = _props.children; var render = _props.render; var matches = this.state.matches; if (matches && render) return render(); if (typeof children === 'function') return children(matches); return matches ? _react2.default.Children.only(children) : null; }; return Media; }(_react2.default.Component); Media.propTypes = { query: _react.PropTypes.string.isRequired, render: _react.PropTypes.func, children: _react.PropTypes.oneOfType([_react.PropTypes.node, _react.PropTypes.func]) }; exports.default = Media;