forked from laravel/laravel
-
Notifications
You must be signed in to change notification settings - Fork 3
/
viewport.js
51 lines (37 loc) · 1.07 KB
/
viewport.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
import debounce from 'lodash/debounce';
import variables from '../../variables.json';
const breakpoints = {};
// TODO: convert to Object.fromEntries
Object.keys(variables.breakpoints).forEach((name) => {
let value = variables.breakpoints[name];
if (variables['em-media-queries']) {
value /= variables['browser-default-font-size'] || 16;
}
breakpoints[name] = value;
});
export default {
created() {
this.onDebouncedResize = debounce(this.onResize, 300);
},
mounted() {
this.onResize();
window.addEventListener('resize', this.onDebouncedResize);
},
methods: {
onResize() {},
mq(name, extremum = 'min', property = 'width') {
if (!window.matchMedia) {
return false;
}
let value = breakpoints[name];
if (!value) {
throw new Error(`Unkown breakpoint: ${name} is not defined`);
}
if (extremum === 'max') {
value -= variables['em-media-queries'] ? 0.01 : 1;
}
const unit = variables['em-media-queries'] ? 'em' : 'px';
return window.matchMedia(`only screen and (${extremum}-${property}: ${value}${unit})`).matches;
},
},
};