-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
match-media.js
58 lines (50 loc) · 1.55 KB
/
match-media.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
52
53
54
55
56
57
58
(function() {
'use strict';
if (! ('MediaQueryListEvent' in globalThis)) {
/**
* @see https://developer.mozilla.org/en-US/docs/Web/API/MediaQueryListEvent
*/
globalThis.MediaQueryListEvent = class MediaQueryListEvent extends Event {
constructor(type, { media, matches } = {}) {
super(type);
Object.defineProperties(this, {
'media': {
enumerable: true,
configurable: false,
writable: false,
value: media,
},
'matches': {
enumerable: true,
configurable: false,
writable: false,
value: matches,
},
});
}
};
}
if ('MediaQueryList' in globalThis && ! (globalThis.MediaQueryList.prototype.addEventListener instanceof Function)) {
const targets = new WeakMap();
const listener = function listener() {
const { media, matches } = this;
this.dispatchEvent(new MediaQueryListEvent('change', { media, matches }));
};
const init = function init(mql) {
if (! targets.has(mql)) {
targets.set(mql, new EventTarget());
mql.addListener(listener.bind(mql));
}
return targets.get(mql);
};
globalThis.MediaQueryList.prototype.addEventListener = function addEventListener(type, callback, opts) {
init(this).addEventListener(type, callback, opts);
};
globalThis.MediaQueryList.prototype.removeEventListener = function removeEventListner(type, callback, opts) {
init(this).removeEventListener(type, callback, opts);
};
globalThis.MediaQueryList.prototype.dispatchEvent = function dispatchEvent(event) {
init(this).dispatchEvent(event);
};
}
})();