Skip to content

Commit

Permalink
Merge pull request #3 from breeny/release-0.1.1
Browse files Browse the repository at this point in the history
Release 0.1.1
  • Loading branch information
breeny authored Jul 28, 2018
2 parents f857576 + 1e76d04 commit a0460c1
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 2 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
node_modules/
es/
coverage/
84 changes: 84 additions & 0 deletions es/Breakpoint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
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; }

import React from 'react';
import breakpoints from './breakpoints';

var Breakpoint = function (_React$Component) {
_inherits(Breakpoint, _React$Component);

function Breakpoint() {
var _temp, _this, _ret;

_classCallCheck(this, Breakpoint);

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 = {
breakpoint: breakpoints.getBreakpoints()[0]
}, _temp), _possibleConstructorReturn(_this, _ret);
}

Breakpoint.prototype.componentDidMount = function componentDidMount() {
var _this2 = this;

this.queries = [];
breakpoints.getBreakpoints().forEach(function (breakpoint) {
var mq = window.matchMedia(breakpoint.query);

if (mq.matches) {
_this2.setState({
breakpoint: breakpoint
});
}

var listener = function listener(mql) {
if (mql.matches) {
_this2.setState({
breakpoint: breakpoint
});
}
};

mq.addListener(listener);

_this2.queries.push({
query: mq,
listener: listener
});
});
};

Breakpoint.prototype.componentWillUnmount = function componentWillUnmount() {
this.queries.forEach(function (bp) {
bp.query.removeListener(bp.listener);
});
};

Breakpoint.prototype.render = function render() {
var breakpoint = this.state.breakpoint;

if (this.props[breakpoint.name]) {
return this.props[breakpoint.name]();
}

if (this.props.render) {
return this.props.render(breakpoint.name);
}

if (this.props.children) {
return this.props.children(breakpoint.name);
}

return null;
};

return Breakpoint;
}(React.Component);

export default Breakpoint;
49 changes: 49 additions & 0 deletions es/breakpoints.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

import defaultBreakpoints from './default_breakpoints.json';

var Breakpoints = function () {
function Breakpoints() {
_classCallCheck(this, Breakpoints);

this.generateMediaQueries(defaultBreakpoints);
}

Breakpoints.prototype.generateMediaQueries = function generateMediaQueries(breakpoints) {
var queries = breakpoints.map(function (breakpoint) {

var query = '';
if (breakpoint.minWidth) {
query += '(min-width: ' + breakpoint.minWidth + 'px)';
}

if (query && breakpoint.maxWidth) {
query += ' and ';
}

if (breakpoint.maxWidth) {
query += '(max-width: ' + breakpoint.maxWidth + 'px)';
}

return {
name: breakpoint.name,
query: query
};
});

this.breakpoints = queries;
};

Breakpoints.prototype.configureBreakpoints = function configureBreakpoints(breakpoints) {
this.generateMediaQueries(breakpoints);
};

Breakpoints.prototype.getBreakpoints = function getBreakpoints() {
return this.breakpoints;
};

return Breakpoints;
}();

var breakpoints = new Breakpoints();
export default breakpoints;
File renamed without changes.
9 changes: 9 additions & 0 deletions es/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import breakpoints from './breakpoints';

var configure = function configure(newBreakpoints) {
breakpoints.configureBreakpoints(newBreakpoints);
};

export { default } from './Breakpoint';

export { configure };
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-media-breakpoints",
"version": "0.1.0",
"version": "0.1.3",
"description": "Simple and configurable breakpoint matcher using matchMedia and render props",
"main": "index.js",
"module": "es/index.js",
Expand Down

0 comments on commit a0460c1

Please sign in to comment.