zent
Version:
一套前端设计语言和基于React的实现
54 lines (53 loc) • 1.9 kB
JavaScript
import { __extends } from "tslib";
import { Component } from 'react';
import isBrowser from '../../utils/isBrowser';
import { createMediaQueryDispatcher } from '../../utils/enquire';
var supportMediaQuery = isBrowser && window.matchMedia;
var enquire = supportMediaQuery ? createMediaQueryDispatcher() : null;
var BreakPointHub = (function (_super) {
__extends(BreakPointHub, _super);
function BreakPointHub() {
return _super !== null && _super.apply(this, arguments) || this;
}
BreakPointHub.prototype.render = function () {
return null;
};
BreakPointHub.prototype.componentDidMount = function () {
this.registerBreakpoints(this.props);
};
BreakPointHub.prototype.componentDidUpdate = function (prevProps) {
if (prevProps.breakpoints !== this.props.breakpoints ||
prevProps.onChange !== this.props.onChange) {
this.unregisterBreakpoints(prevProps);
this.registerBreakpoints(this.props);
}
};
BreakPointHub.prototype.registerBreakpoints = function (props) {
if (!supportMediaQuery) {
return;
}
var breakpoints = props.breakpoints, onChange = props.onChange;
breakpoints.forEach(function (brk) {
enquire.register(brk, {
match: function () {
onChange(brk, true);
},
unmatch: function () {
onChange(brk, false);
},
});
});
};
BreakPointHub.prototype.unregisterBreakpoints = function (props) {
if (!supportMediaQuery) {
return;
}
var breakpoints = props.breakpoints;
breakpoints.forEach(function (brk) {
enquire.unregister(brk);
});
};
return BreakPointHub;
}(Component));
export { BreakPointHub };
export default BreakPointHub;