popper.js
3.18 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/**
* https://github.com/freeze-component/vue-popper
* */
import Vue from 'vue';
const isServer = Vue.prototype.$isServer;
const Popper = isServer ? function() {} : require('popper.js/dist/umd/popper.js'); // eslint-disable-line
export default {
props: {
placement: {
type: String,
default: 'bottom'
},
boundariesPadding: {
type: Number,
default: 5
},
reference: Object,
popper: Object,
offset: {
default: 0
},
value: {
type: Boolean,
default: false
},
transition: String,
options: {
type: Object,
default () {
return {
modifiers: {
computeStyle:{
gpuAcceleration: false,
},
preventOverflow :{
boundariesElement: 'window'
}
}
};
}
},
// visible: {
// type: Boolean,
// default: false
// }
},
data () {
return {
visible: this.value
};
},
watch: {
value: {
immediate: true,
handler(val) {
this.visible = val;
this.$emit('input', val);
}
},
visible(val) {
if (val) {
this.updatePopper();
this.$emit('on-popper-show');
} else {
this.$emit('on-popper-hide');
}
this.$emit('input', val);
}
},
methods: {
createPopper() {
if (isServer) return;
if (!/^(top|bottom|left|right)(-start|-end)?$/g.test(this.placement)) {
return;
}
const options = this.options;
const popper = this.popper || this.$refs.popper;
const reference = this.reference || this.$refs.reference;
if (!popper || !reference) return;
if (this.popperJS && this.popperJS.hasOwnProperty('destroy')) {
this.popperJS.destroy();
}
options.placement = this.placement;
if (!options.modifiers.offset) {
options.modifiers.offset = {};
}
options.modifiers.offset.offset = this.offset;
options.onCreate =()=>{
this.$nextTick(this.updatePopper);
this.$emit('created', this);
};
this.popperJS = new Popper(reference, popper, options);
},
updatePopper() {
if (isServer) return;
this.popperJS ? this.popperJS.update() : this.createPopper();
},
doDestroy() {
if (isServer) return;
if (this.visible) return;
this.popperJS.destroy();
this.popperJS = null;
}
},
updated (){
this.$nextTick(()=>this.updatePopper());
},
beforeDestroy() {
if (isServer) return;
if (this.popperJS) {
this.popperJS.destroy();
}
}
};