react-media
Version:
A CSS media query component for React
157 lines (112 loc) • 5.55 kB
JavaScript
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory(require("react"));
else if(typeof define === 'function' && define.amd)
define(["react"], factory);
else if(typeof exports === 'object')
exports["ReactMedia"] = factory(require("react"));
else
root["ReactMedia"] = factory(root["React"]);
})(this, function(__WEBPACK_EXTERNAL_MODULE_2__) {
return /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ exports: {},
/******/ id: moduleId,
/******/ loaded: false
/******/ };
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/ // Flag the module as loaded
/******/ module.loaded = true;
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/ // Load entry module and return exports
/******/ return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
var _Media = __webpack_require__(1);
var _Media2 = _interopRequireDefault(_Media);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
// TODO: Remove in the next major release.
_Media2.default.Media = _Media2.default; /* eslint-env node */
module.exports = _Media2.default;
/***/ },
/* 1 */
/***/ function(module, exports, __webpack_require__) {
'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 = __webpack_require__(2);
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;
/***/ },
/* 2 */
/***/ function(module, exports) {
module.exports = __WEBPACK_EXTERNAL_MODULE_2__;
/***/ }
/******/ ])
});
;