-
Notifications
You must be signed in to change notification settings - Fork 17
/
index.js
94 lines (87 loc) · 2.24 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import throttle from "lodash.throttle"
import ResizeObserver from "resize-observer-polyfill"
export const ResponsiveMixin = {
data() {
return {
el: {
width: 0,
height: 0,
is: {}
}
}
},
mounted() {
if (
typeof process === "undefined" ||
(!process.server && (this.breakpoints || this.$options.breakpoints))
) {
this.$nextTick(() => {
const handleResize = throttle(entries => {
const cr = entries[0].contentRect
;(this.el.width = cr.width), (this.el.height = cr.height)
const conds = Object.assign(
{},
this.breakpoints || {},
this.$options.breakpoints || {}
)
for (const breakpoint in conds) {
this.$set(this.el.is, breakpoint, conds[breakpoint](this.el))
}
}, 200)
const observer = new ResizeObserver(handleResize)
if (this.$el instanceof Element) {
observer.observe(this.$el)
}
})
}
}
}
export const Responsive = {
data() {
return { init: false }
},
props: {
noHide: { type: Boolean, default: false },
breakpoints: { type: Object, default: undefined }
},
mixins: [ResponsiveMixin],
render(h) {
const slot =
(this.$scopedSlots.default && this.$scopedSlots.default(this.el)) ||
this.$slots.default
return !this.noHide && !this.init
? h(
"div",
{
style: { visibility: "hidden" }
},
[slot]
)
: slot
},
mounted() {
this.init = true
}
}
export const ResponsiveDirective = {
inserted(el, conds) {
if (typeof process === "undefined" || !process.server) {
const handleResize = throttle(entries => {
const cr = entries[0].contentRect
for (const breakpoint in conds.value) {
if (conds.value[breakpoint](cr)) {
el.classList.add(breakpoint)
} else {
el.classList.remove(breakpoint)
}
}
}, 200)
const observer = new ResizeObserver(handleResize)
observer.observe(el)
}
}
}
export const VueResponsiveComponents = Vue => {
Vue.component("Responsive", Responsive)
Vue.directive("responsive", ResponsiveDirective)
}