-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
30 lines (27 loc) · 853 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
var canUseDOM = require('can-use-dom');
var enquire = canUseDOM && require('enquire.js');
var json2mq = require('json2mq');
var ResponsiveMixin = {
media: function (query, handler) {
query = json2mq(query);
if (typeof handler === 'function') {
handler = {
match: handler
};
}
canUseDOM && enquire.register(query, handler);
// Queue the handlers to unregister them at unmount
if (! this._responsiveMediaHandlers) {
this._responsiveMediaHandlers = [];
}
this._responsiveMediaHandlers.push({query: query, handler: handler});
},
componentWillUnmount: function () {
if (this._responsiveMediaHandlers) {
this._responsiveMediaHandlers.forEach(function(obj) {
canUseDOM && enquire.unregister(obj.query, obj.handler);
});
}
}
};
module.exports = ResponsiveMixin;