Skip to content

Commit 1994fb6

Browse files
committed
Wrap matchMedia (rather than referencing) and add mq()
1 parent 10c1c69 commit 1994fb6

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

src/index.js

+23-8
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,16 @@
6868
, crossover: namespaceIt('crossover') // fires on window each time dynamic breakpoint bands is crossed
6969
}
7070

71-
// Response.media (normalized matchMedia)
72-
// @example Response.media("(orientation:landscape)").matches
73-
// If both versions are undefined, .matches will equal undefined
74-
// Also see: band / wave / device.band / device.wave / dpr
71+
// normalized matchMedia
7572
, matchMedia = win.matchMedia || win.msMatchMedia
76-
, media = matchMedia || function() { return {}; }
73+
, media = matchMedia ? bind(matchMedia, win) : function() {
74+
return {};
75+
}
76+
, mq = matchMedia ? function(q) {
77+
return !!matchMedia.call(win, q);
78+
} : function() {
79+
return false;
80+
}
7781

7882
// http://ryanve.com/lab/dimensions
7983
// http://github.com/ryanve/verge/issues/13
@@ -85,11 +89,22 @@
8589
var a = docElem['clientHeight'], b = win['innerHeight'];
8690
return a < b ? b : a;
8791
};
88-
92+
8993
function isNumber(item) {
9094
return item === +item;
9195
}
9296

97+
/**
98+
* @param {Function} fn
99+
* @param {*=} scope
100+
* @return {Function}
101+
*/
102+
function bind(fn, scope) {
103+
return function() {
104+
return fn.apply(scope, arguments);
105+
};
106+
}
107+
93108
/**
94109
* @param {{length:number}} stack
95110
* @param {Function} fn
@@ -231,8 +246,8 @@
231246
// The generic min-device-pixel-ratio is expected to be added to the W3 spec.
232247
// Return false if neither method is available.
233248
decimal = 'only all and (min--moz-device-pixel-ratio:' + decimal + ')';
234-
if (media(decimal).matches) return true;
235-
return !!media(decimal.replace('-moz-', '')).matches;
249+
if (mq(decimal)) return true;
250+
return mq(decimal.replace('-moz-', ''));
236251
}
237252

238253
/**

0 commit comments

Comments
 (0)