-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
51 lines (42 loc) · 1.07 KB
/
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
'use strict';
var staticMatch = require('css-mediaquery').match;
var dynamicMatch = typeof window !== 'undefined' ? window.matchMedia : null;
// our fake MediaQueryList
function Mql(query, values){
var self = this;
if(dynamicMatch){
var mql = dynamicMatch.call(window, query);
this.matches = mql.matches;
this.media = mql.media;
// TODO: is there a time it makes sense to remove this listener?
mql.addListener(update);
} else {
this.matches = staticMatch(query, values);
this.media = query;
}
this.addListener = addListener;
this.removeListener = removeListener;
this.dispose = dispose;
function addListener(listener){
if(mql){
mql.addListener(listener);
}
}
function removeListener(listener){
if(mql){
mql.removeListener(listener);
}
}
// update ourselves!
function update(evt){
self.matches = evt.matches;
self.media = evt.media;
}
function dispose(){
mql.removeListener(update);
}
}
function matchMedia(query, values){
return new Mql(query, values);
}
module.exports = matchMedia;